system: Initialize console keyboard layout in the initrd.
[jackhill/guix/guix.git] / gnu / system.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
4 ;;; Copyright © 2015, 2016 Alex Kost <alezost@gmail.com>
5 ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
6 ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
7 ;;; Copyright © 2019 Meiyo Peng <meiyo.peng@gmail.com>
8 ;;;
9 ;;; This file is part of GNU Guix.
10 ;;;
11 ;;; GNU Guix is free software; you can redistribute it and/or modify it
12 ;;; under the terms of the GNU General Public License as published by
13 ;;; the Free Software Foundation; either version 3 of the License, or (at
14 ;;; your option) any later version.
15 ;;;
16 ;;; GNU Guix is distributed in the hope that it will be useful, but
17 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;;; GNU General Public License for more details.
20 ;;;
21 ;;; You should have received a copy of the GNU General Public License
22 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23
24 (define-module (gnu system)
25 #:use-module (guix inferior)
26 #:use-module (guix store)
27 #:use-module (guix monads)
28 #:use-module (guix gexp)
29 #:use-module (guix records)
30 #:use-module (guix packages)
31 #:use-module (guix derivations)
32 #:use-module (guix profiles)
33 #:use-module (guix ui)
34 #:use-module (gnu packages base)
35 #:use-module (gnu packages bash)
36 #:use-module (gnu packages guile)
37 #:use-module (gnu packages admin)
38 #:use-module (gnu packages linux)
39 #:use-module (gnu packages pciutils)
40 #:use-module (gnu packages package-management)
41 #:use-module (gnu packages less)
42 #:use-module (gnu packages zile)
43 #:use-module (gnu packages nano)
44 #:use-module (gnu packages gawk)
45 #:use-module (gnu packages man)
46 #:use-module (gnu packages texinfo)
47 #:use-module (gnu packages compression)
48 #:use-module (gnu packages firmware)
49 #:use-module (gnu services)
50 #:use-module (gnu services shepherd)
51 #:use-module (gnu services base)
52 #:use-module (gnu bootloader)
53 #:use-module (gnu system shadow)
54 #:use-module (gnu system nss)
55 #:use-module (gnu system locale)
56 #:use-module (gnu system pam)
57 #:use-module (gnu system linux-initrd)
58 #:use-module (gnu system uuid)
59 #:use-module (gnu system file-systems)
60 #:use-module (gnu system mapped-devices)
61 #:use-module (ice-9 match)
62 #:use-module (srfi srfi-1)
63 #:use-module (srfi srfi-26)
64 #:use-module (srfi srfi-34)
65 #:use-module (srfi srfi-35)
66 #:use-module (rnrs bytevectors)
67 #:export (operating-system
68 operating-system?
69
70 operating-system-bootloader
71 operating-system-services
72 operating-system-user-services
73 operating-system-packages
74 operating-system-host-name
75 operating-system-hosts-file
76 operating-system-kernel
77 operating-system-kernel-file
78 operating-system-kernel-arguments
79 operating-system-initrd-modules
80 operating-system-initrd
81 operating-system-users
82 operating-system-groups
83 operating-system-issue
84 operating-system-timezone
85 operating-system-locale
86 operating-system-locale-definitions
87 operating-system-locale-libcs
88 operating-system-mapped-devices
89 operating-system-file-systems
90 operating-system-store-file-system
91 operating-system-user-mapped-devices
92 operating-system-boot-mapped-devices
93 operating-system-activation-script
94 operating-system-user-accounts
95 operating-system-shepherd-service-names
96 operating-system-user-kernel-arguments
97
98 operating-system-derivation
99 operating-system-profile
100 operating-system-bootcfg
101 operating-system-etc-directory
102 operating-system-locale-directory
103 operating-system-boot-script
104
105 system-linux-image-file-name
106
107 boot-parameters
108 boot-parameters?
109 boot-parameters-label
110 boot-parameters-root-device
111 boot-parameters-bootloader-name
112 boot-parameters-store-device
113 boot-parameters-store-mount-point
114 boot-parameters-kernel
115 boot-parameters-kernel-arguments
116 boot-parameters-initrd
117 read-boot-parameters
118 read-boot-parameters-file
119 boot-parameters->menu-entry
120
121 local-host-aliases
122 %root-account
123 %setuid-programs
124 %base-packages
125 %base-firmware))
126
127 ;;; Commentary:
128 ;;;
129 ;;; This module supports whole-system configuration.
130 ;;;
131 ;;; Code:
132
133 (define (bootable-kernel-arguments system root-device)
134 "Return a list of kernel arguments (gexps) to boot SYSTEM from ROOT-DEVICE."
135 (list (string-append "--root="
136 (cond ((uuid? root-device)
137
138 ;; Note: Always use the DCE format because that's
139 ;; what (gnu build linux-boot) expects for the
140 ;; '--root' kernel command-line option.
141 (uuid->string (uuid-bytevector root-device)
142 'dce))
143 ((file-system-label? root-device)
144 (file-system-label->string root-device))
145 (else root-device)))
146 #~(string-append "--system=" #$system)
147 #~(string-append "--load=" #$system "/boot")))
148
149 ;; System-wide configuration.
150 ;; TODO: Add per-field docstrings/stexi.
151 (define-record-type* <operating-system> operating-system
152 make-operating-system
153 operating-system?
154 (kernel operating-system-kernel ; package
155 (default linux-libre))
156 (kernel-arguments operating-system-user-kernel-arguments
157 (default '())) ; list of gexps/strings
158 (bootloader operating-system-bootloader) ; <bootloader-configuration>
159
160 (keyboard-layout operating-system-keyboard-layout ;#f | <keyboard-layout>
161 (default #f))
162 (initrd operating-system-initrd ; (list fs) -> file-like
163 (default base-initrd))
164 (initrd-modules operating-system-initrd-modules ; list of strings
165 (thunked) ; it's system-dependent
166 (default %base-initrd-modules))
167
168 (firmware operating-system-firmware ; list of packages
169 (default %base-firmware))
170
171 (host-name operating-system-host-name) ; string
172 (hosts-file operating-system-hosts-file ; file-like | #f
173 (default #f))
174
175 (mapped-devices operating-system-mapped-devices ; list of <mapped-device>
176 (default '()))
177 (file-systems operating-system-file-systems) ; list of fs
178 (swap-devices operating-system-swap-devices ; list of strings
179 (default '()))
180
181 (users operating-system-users ; list of user accounts
182 (default %base-user-accounts))
183 (groups operating-system-groups ; list of user groups
184 (default %base-groups))
185
186 (skeletons operating-system-skeletons ; list of name/file-like value
187 (default (default-skeletons)))
188 (issue operating-system-issue ; string
189 (default %default-issue))
190
191 (packages operating-system-packages ; list of (PACKAGE OUTPUT...)
192 (default %base-packages)) ; or just PACKAGE
193
194 (timezone operating-system-timezone) ; string
195 (locale operating-system-locale ; string
196 (default "en_US.utf8"))
197 (locale-definitions operating-system-locale-definitions ; list of <locale-definition>
198 (default %default-locale-definitions))
199 (locale-libcs operating-system-locale-libcs ; list of <packages>
200 (default %default-locale-libcs))
201 (name-service-switch operating-system-name-service-switch ; <name-service-switch>
202 (default %default-nss))
203
204 (services operating-system-user-services ; list of services
205 (default %base-services))
206
207 (pam-services operating-system-pam-services ; list of PAM services
208 (default (base-pam-services)))
209 (setuid-programs operating-system-setuid-programs
210 (default %setuid-programs)) ; list of string-valued gexps
211
212 (sudoers-file operating-system-sudoers-file ; file-like
213 (default %sudoers-specification)))
214
215 (define (operating-system-kernel-arguments os root-device)
216 "Return all the kernel arguments, including the ones not specified
217 directly by the user."
218 (append (bootable-kernel-arguments os root-device)
219 (operating-system-user-kernel-arguments os)))
220
221 \f
222 ;;;
223 ;;; Boot parameters
224 ;;;
225
226 (define-record-type* <boot-parameters>
227 boot-parameters make-boot-parameters boot-parameters?
228 (label boot-parameters-label)
229 ;; Because we will use the 'store-device' to create the GRUB search command,
230 ;; the 'store-device' has slightly different semantics than 'root-device'.
231 ;; The 'store-device' can be a file system uuid, a file system label, or #f,
232 ;; but it cannot be a device path such as "/dev/sda3", since GRUB would not
233 ;; understand that. The 'root-device', on the other hand, corresponds
234 ;; exactly to the device field of the <file-system> object representing the
235 ;; OS's root file system, so it might be a device path like "/dev/sda3".
236 (root-device boot-parameters-root-device)
237 (bootloader-name boot-parameters-bootloader-name)
238 (store-device boot-parameters-store-device)
239 (store-mount-point boot-parameters-store-mount-point)
240 (kernel boot-parameters-kernel)
241 (kernel-arguments boot-parameters-kernel-arguments)
242 (initrd boot-parameters-initrd))
243
244 (define (ensure-not-/dev device)
245 "If DEVICE starts with a slash, return #f. This is meant to filter out
246 Linux device names such as /dev/sda, and to preserve GRUB device names and
247 file system labels."
248 (if (and (string? device) (string-prefix? "/" device))
249 #f
250 device))
251
252 (define (read-boot-parameters port)
253 "Read boot parameters from PORT and return the corresponding
254 <boot-parameters> object or #f if the format is unrecognized."
255 (define device-sexp->device
256 (match-lambda
257 (('uuid (? symbol? type) (? bytevector? bv))
258 (bytevector->uuid bv type))
259 (('file-system-label (? string? label))
260 (file-system-label label))
261 ((? bytevector? bv) ;old format
262 (bytevector->uuid bv 'dce))
263 ((? string? device)
264 ;; It used to be that we would not distinguish between labels and
265 ;; device names. Try to infer the right thing here.
266 (if (string-prefix? "/dev/" device)
267 device
268 (file-system-label device)))))
269
270 (match (read port)
271 (('boot-parameters ('version 0)
272 ('label label) ('root-device root)
273 ('kernel linux)
274 rest ...)
275 (boot-parameters
276 (label label)
277 (root-device (device-sexp->device root))
278
279 (bootloader-name
280 (match (assq 'bootloader-name rest)
281 ((_ args) args)
282 (#f 'grub))) ; for compatibility reasons.
283
284 ;; In the past, we would store the directory name of the kernel instead
285 ;; of the absolute file name of its image. Detect that and correct it.
286 (kernel (if (string=? linux (direct-store-path linux))
287 (string-append linux "/"
288 (system-linux-image-file-name))
289 linux))
290
291 (kernel-arguments
292 (match (assq 'kernel-arguments rest)
293 ((_ args) args)
294 (#f '()))) ;the old format
295
296 (initrd
297 (match (assq 'initrd rest)
298 (('initrd ('string-append directory file)) ;the old format
299 (string-append directory file))
300 (('initrd (? string? file))
301 file)))
302
303 (store-device
304 ;; Linux device names like "/dev/sda1" are not suitable GRUB device
305 ;; identifiers, so we just filter them out.
306 (ensure-not-/dev
307 (match (assq 'store rest)
308 (('store ('device #f) _ ...)
309 root-device)
310 (('store ('device device) _ ...)
311 (device-sexp->device device))
312 (_ ;the old format
313 root-device))))
314
315 (store-mount-point
316 (match (assq 'store rest)
317 (('store ('device _) ('mount-point mount-point) _ ...)
318 mount-point)
319 (_ ;the old format
320 "/")))))
321 (x ;unsupported format
322 (warning (G_ "unrecognized boot parameters at '~a'~%")
323 (port-filename port))
324 #f)))
325
326 (define (read-boot-parameters-file system)
327 "Read boot parameters from SYSTEM's (system or generation) \"parameters\"
328 file and returns the corresponding <boot-parameters> object or #f if the
329 format is unrecognized.
330 The object has its kernel-arguments extended in order to make it bootable."
331 (let* ((file (string-append system "/parameters"))
332 (params (call-with-input-file file read-boot-parameters))
333 (root (boot-parameters-root-device params)))
334 (boot-parameters
335 (inherit params)
336 (kernel-arguments (append (bootable-kernel-arguments system root)
337 (boot-parameters-kernel-arguments params))))))
338
339 (define (boot-parameters->menu-entry conf)
340 (menu-entry
341 (label (boot-parameters-label conf))
342 (device (boot-parameters-store-device conf))
343 (device-mount-point (boot-parameters-store-mount-point conf))
344 (linux (boot-parameters-kernel conf))
345 (linux-arguments (boot-parameters-kernel-arguments conf))
346 (initrd (boot-parameters-initrd conf))))
347
348
349 \f
350 ;;;
351 ;;; Services.
352 ;;;
353
354 (define (non-boot-file-system-service os)
355 "Return the file system service for the file systems of OS that are not
356 marked as 'needed-for-boot'."
357 (define file-systems
358 (remove file-system-needed-for-boot?
359 (operating-system-file-systems os)))
360
361 (define mapped-devices-for-boot
362 (operating-system-boot-mapped-devices os))
363
364 (define (device-mappings fs)
365 (let ((device (file-system-device fs)))
366 (if (string? device) ;title is 'device
367 (filter (lambda (md)
368 (string=? (string-append "/dev/mapper/"
369 (mapped-device-target md))
370 device))
371 (operating-system-mapped-devices os))
372 '())))
373
374 (define (add-dependencies fs)
375 ;; Add the dependencies due to device mappings to FS.
376 (file-system
377 (inherit fs)
378 (dependencies
379 (delete-duplicates
380 (remove (cut member <> mapped-devices-for-boot)
381 (append (device-mappings fs)
382 (file-system-dependencies fs)))
383 eq?))))
384
385 (service file-system-service-type
386 (map add-dependencies file-systems)))
387
388 (define (mapped-device-users device file-systems)
389 "Return the subset of FILE-SYSTEMS that use DEVICE."
390 (let ((target (string-append "/dev/mapper/" (mapped-device-target device))))
391 (filter (lambda (fs)
392 (or (member device (file-system-dependencies fs))
393 (and (string? (file-system-device fs))
394 (string=? (file-system-device fs) target))))
395 file-systems)))
396
397 (define (operating-system-user-mapped-devices os)
398 "Return the subset of mapped devices that can be installed in
399 user-land--i.e., those not needed during boot."
400 (let ((devices (operating-system-mapped-devices os))
401 (file-systems (operating-system-file-systems os)))
402 (filter (lambda (md)
403 (let ((users (mapped-device-users md file-systems)))
404 (not (any file-system-needed-for-boot? users))))
405 devices)))
406
407 (define (operating-system-boot-mapped-devices os)
408 "Return the subset of mapped devices that must be installed during boot,
409 from the initrd."
410 (let ((devices (operating-system-mapped-devices os))
411 (file-systems (operating-system-file-systems os)))
412 (filter (lambda (md)
413 (let ((users (mapped-device-users md file-systems)))
414 (any file-system-needed-for-boot? users)))
415 devices)))
416
417 (define (device-mapping-services os)
418 "Return the list of device-mapping services for OS as a list."
419 (map device-mapping-service
420 (operating-system-user-mapped-devices os)))
421
422 (define (swap-services os)
423 "Return the list of swap services for OS."
424 (map swap-service (operating-system-swap-devices os)))
425
426 (define* (system-linux-image-file-name #:optional (system (%current-system)))
427 "Return the basename of the kernel image file for SYSTEM."
428 ;; FIXME: Evaluate the conditional based on the actual current system.
429 (cond
430 ((string-prefix? "arm" (%current-system)) "zImage")
431 ((string-prefix? "mips" (%current-system)) "vmlinuz")
432 ((string-prefix? "aarch64" (%current-system)) "Image")
433 (else "bzImage")))
434
435 (define (operating-system-kernel-file os)
436 "Return an object representing the absolute file name of the kernel image of
437 OS."
438 (file-append (operating-system-kernel os)
439 "/" (system-linux-image-file-name os)))
440
441 (define* (operating-system-directory-base-entries os #:key container?)
442 "Return the basic entries of the 'system' directory of OS for use as the
443 value of the SYSTEM-SERVICE-TYPE service."
444 (let ((locale (operating-system-locale-directory os)))
445 (with-monad %store-monad
446 (if container?
447 (return `(("locale" ,locale)))
448 (mlet %store-monad
449 ((kernel -> (operating-system-kernel os))
450 (initrd -> (operating-system-initrd-file os))
451 (params (operating-system-boot-parameters-file os)))
452 (return `(("kernel" ,kernel)
453 ("parameters" ,params)
454 ("initrd" ,initrd)
455 ("locale" ,locale)))))))) ;used by libc
456
457 (define* (essential-services os #:key container?)
458 "Return the list of essential services for OS. These are special services
459 that implement part of what's declared in OS are responsible for low-level
460 bookkeeping. CONTAINER? determines whether to return the list of services for
461 a container or that of a \"bare metal\" system."
462 (define known-fs
463 (map file-system-mount-point (operating-system-file-systems os)))
464
465 (let* ((mappings (device-mapping-services os))
466 (root-fs (root-file-system-service))
467 (other-fs (non-boot-file-system-service os))
468 (swaps (swap-services os))
469 (procs (service user-processes-service-type))
470 (host-name (host-name-service (operating-system-host-name os)))
471 (entries (operating-system-directory-base-entries
472 os #:container? container?)))
473 (cons* (service system-service-type entries)
474 %boot-service
475
476 ;; %SHEPHERD-ROOT-SERVICE must come last so that the gexp that
477 ;; execs shepherd comes last in the boot script (XXX). Likewise,
478 ;; the cleanup service must come first so that its gexp runs before
479 ;; activation code.
480 (service cleanup-service-type #f)
481 %activation-service
482 %shepherd-root-service
483
484 (pam-root-service (operating-system-pam-services os))
485 (account-service (append (operating-system-accounts os)
486 (operating-system-groups os))
487 (operating-system-skeletons os))
488 (operating-system-etc-service os)
489 (service fstab-service-type '())
490 (session-environment-service
491 (operating-system-environment-variables os))
492 host-name procs root-fs
493 (service setuid-program-service-type
494 (operating-system-setuid-programs os))
495 (service profile-service-type
496 (operating-system-packages os))
497 other-fs
498 (append mappings swaps
499
500 ;; Add the firmware service, unless we are building for a
501 ;; container.
502 (if container?
503 (list %containerized-shepherd-service)
504 (list %linux-bare-metal-service
505 (service firmware-service-type
506 (operating-system-firmware os))))))))
507
508 (define* (operating-system-services os #:key container?)
509 "Return all the services of OS, including \"internal\" services that do not
510 explicitly appear in OS."
511 (instantiate-missing-services
512 (append (operating-system-user-services os)
513 (essential-services os #:container? container?))))
514
515 \f
516 ;;;
517 ;;; /etc.
518 ;;;
519
520 (define %base-firmware
521 ;; Firmware usable by default.
522 (list ath9k-htc-firmware
523 openfwwf-firmware))
524
525 (define %base-packages
526 ;; Default set of packages globally visible. It should include anything
527 ;; required for basic administrator tasks.
528 (cons* procps psmisc which less zile nano
529 pciutils usbutils
530 util-linux
531 inetutils isc-dhcp
532 (@ (gnu packages admin) shadow) ;for 'passwd'
533
534 ;; wireless-tools is deprecated in favor of iw, but it's still what
535 ;; many people are familiar with, so keep it around.
536 iw wireless-tools
537
538 iproute
539 net-tools ; XXX: remove when Inetutils suffices
540 man-db
541 info-reader ;the standalone Info reader (no Perl)
542
543 ;; The 'sudo' command is already in %SETUID-PROGRAMS, but we also
544 ;; want the other commands and the man pages (notably because
545 ;; auto-completion in Emacs shell relies on man pages.)
546 sudo
547
548 ;; Get 'insmod' & co. from kmod, not module-init-tools, since udev
549 ;; already depends on it anyway.
550 kmod eudev
551
552 e2fsprogs kbd
553
554 bash-completion
555
556 ;; XXX: We don't use (canonical-package guile-2.2) here because that
557 ;; would create a collision in the global profile between the GMP
558 ;; variant propagated by 'guile-final' and the GMP variant propagated
559 ;; by 'gnutls', itself propagated by 'guix'.
560 guile-2.2
561
562 ;; The packages below are also in %FINAL-INPUTS, so take them from
563 ;; there to avoid duplication.
564 (map canonical-package
565 (list bash coreutils findutils grep sed
566 diffutils patch gawk tar gzip bzip2 xz lzip))))
567
568 (define %default-issue
569 ;; Default contents for /etc/issue.
570 "
571 This is the GNU system. Welcome.\n")
572
573 (define (local-host-aliases host-name)
574 "Return aliases for HOST-NAME, to be used in /etc/hosts."
575 (string-append "127.0.0.1 localhost " host-name "\n"
576 "::1 localhost " host-name "\n"))
577
578 (define (default-/etc/hosts host-name)
579 "Return the default /etc/hosts file."
580 (plain-file "hosts" (local-host-aliases host-name)))
581
582 (define* (operating-system-etc-service os)
583 "Return a <service> that builds containing the static part of the /etc
584 directory."
585 (let ((login.defs
586 (plain-file "login.defs"
587 (string-append
588 "# Default paths for non-login shells started by su(1).\n"
589 "ENV_PATH /run/setuid-programs:"
590 "/run/current-system/profile/bin:"
591 "/run/current-system/profile/sbin\n"
592 "ENV_SUPATH /run/setuid-programs:"
593 "/run/current-system/profile/bin:"
594 "/run/current-system/profile/sbin\n")))
595
596 (issue (plain-file "issue" (operating-system-issue os)))
597 (nsswitch (plain-file "nsswitch.conf"
598 (name-service-switch->string
599 (operating-system-name-service-switch os))))
600
601 ;; Startup file for POSIX-compliant login shells, which set system-wide
602 ;; environment variables.
603 (profile (mixed-text-file "profile" "\
604 # Crucial variables that could be missing in the profiles' 'etc/profile'
605 # because they would require combining both profiles.
606 # FIXME: See <http://bugs.gnu.org/20255>.
607 export MANPATH=$HOME/.guix-profile/share/man:/run/current-system/profile/share/man
608 export INFOPATH=$HOME/.guix-profile/share/info:/run/current-system/profile/share/info
609 export XDG_DATA_DIRS=$HOME/.guix-profile/share:/run/current-system/profile/share
610 export XDG_CONFIG_DIRS=$HOME/.guix-profile/etc/xdg:/run/current-system/profile/etc/xdg
611
612 # Make sure libXcursor finds cursors installed into user or system profiles. See <http://bugs.gnu.org/24445>
613 export XCURSOR_PATH=$HOME/.icons:$HOME/.guix-profile/share/icons:/run/current-system/profile/share/icons
614
615 # Ignore the default value of 'PATH'.
616 unset PATH
617
618 # Load the system profile's settings.
619 GUIX_PROFILE=/run/current-system/profile ; \\
620 . /run/current-system/profile/etc/profile
621
622 # Since 'lshd' does not use pam_env, /etc/environment must be explicitly
623 # loaded when someone logs in via SSH. See <http://bugs.gnu.org/22175>.
624 # We need 'PATH' to be defined here, for 'cat' and 'cut'. Do this before
625 # reading the user's 'etc/profile' to allow variables to be overridden.
626 if [ -f /etc/environment -a -n \"$SSH_CLIENT\" \\
627 -a -z \"$LINUX_MODULE_DIRECTORY\" ]
628 then
629 . /etc/environment
630 export `cat /etc/environment | cut -d= -f1`
631 fi
632
633 # Arrange so that ~/.config/guix/current comes first.
634 for profile in \"$HOME/.guix-profile\" \"$HOME/.config/guix/current\"
635 do
636 if [ -f \"$profile/etc/profile\" ]
637 then
638 # Load the user profile's settings.
639 GUIX_PROFILE=\"$profile\" ; \\
640 . \"$profile/etc/profile\"
641 else
642 # At least define this one so that basic things just work
643 # when the user installs their first package.
644 export PATH=\"$profile/bin:$PATH\"
645 fi
646 done
647
648 # Prepend setuid programs.
649 export PATH=/run/setuid-programs:$PATH
650
651 # Arrange so that ~/.config/guix/current/share/info comes first.
652 export INFOPATH=\"$HOME/.config/guix/current/share/info:$INFOPATH\"
653
654 # Set the umask, notably for users logging in via 'lsh'.
655 # See <http://bugs.gnu.org/22650>.
656 umask 022
657
658 # Allow Hunspell-based applications (IceCat, LibreOffice, etc.) to
659 # find dictionaries.
660 export DICPATH=\"$HOME/.guix-profile/share/hunspell:/run/current-system/profile/share/hunspell\"
661
662 # Allow GStreamer-based applications to find plugins.
663 export GST_PLUGIN_PATH=\"$HOME/.guix-profile/lib/gstreamer-1.0\"
664
665 if [ -n \"$BASH_VERSION\" -a -f /etc/bashrc ]
666 then
667 # Load Bash-specific initialization code.
668 . /etc/bashrc
669 fi
670 "))
671
672 (bashrc (plain-file "bashrc" "\
673 # Bash-specific initialization.
674
675 # The 'bash-completion' package.
676 if [ -f /run/current-system/profile/etc/profile.d/bash_completion.sh ]
677 then
678 # Bash-completion sources ~/.bash_completion. It installs a dynamic
679 # completion loader that searches its own completion files as well
680 # as those in ~/.guix-profile and /run/current-system/profile.
681 source /run/current-system/profile/etc/profile.d/bash_completion.sh
682 fi\n")))
683 (etc-service
684 `(("services" ,(file-append net-base "/etc/services"))
685 ("protocols" ,(file-append net-base "/etc/protocols"))
686 ("rpc" ,(file-append net-base "/etc/rpc"))
687 ("login.defs" ,#~#$login.defs)
688 ("issue" ,#~#$issue)
689 ("nsswitch.conf" ,#~#$nsswitch)
690 ("profile" ,#~#$profile)
691 ("bashrc" ,#~#$bashrc)
692 ("hosts" ,#~#$(or (operating-system-hosts-file os)
693 (default-/etc/hosts (operating-system-host-name os))))
694 ;; Write the operating-system-host-name to /etc/hostname to prevent
695 ;; NetworkManager from changing the system's hostname when connecting
696 ;; to certain networks. Some discussion at
697 ;; https://lists.gnu.org/archive/html/help-guix/2017-09/msg00037.html
698 ("hostname" ,(plain-file "hostname" (operating-system-host-name os)))
699 ("localtime" ,(file-append tzdata "/share/zoneinfo/"
700 (operating-system-timezone os)))
701 ("sudoers" ,(operating-system-sudoers-file os))))))
702
703 (define %root-account
704 ;; Default root account.
705 (user-account
706 (name "root")
707 (password "")
708 (uid 0) (group "root")
709 (comment "System administrator")
710 (home-directory "/root")))
711
712 (define (operating-system-accounts os)
713 "Return the user accounts for OS, including an obligatory 'root' account,
714 and excluding accounts requested by services."
715 ;; Make sure there's a root account.
716 (if (find (lambda (user)
717 (and=> (user-account-uid user) zero?))
718 (operating-system-users os))
719 (operating-system-users os)
720 (cons %root-account (operating-system-users os))))
721
722 (define (maybe-string->file file-name thing)
723 "If THING is a string, return a <plain-file> with THING as its content.
724 Otherwise just return THING.
725
726 This is for backward-compatibility of fields that used to be strings and are
727 now file-like objects.."
728 (match thing
729 ((? string?)
730 (warning (G_ "using a string for file '~a' is deprecated; \
731 use 'plain-file' instead~%")
732 file-name)
733 (plain-file file-name thing))
734 (x
735 x)))
736
737 (define (maybe-file->monadic file-name thing)
738 "If THING is a value in %STORE-MONAD, return it as is; otherwise return
739 THING in the %STORE-MONAD.
740
741 This is for backward-compatibility of fields that used to be monadic values
742 and are now file-like objects."
743 (with-monad %store-monad
744 (match thing
745 ((? procedure?)
746 (warning (G_ "using a monadic value for '~a' is deprecated; \
747 use 'plain-file' instead~%")
748 file-name)
749 thing)
750 (x
751 (return x)))))
752
753 (define (operating-system-etc-directory os)
754 "Return that static part of the /etc directory of OS."
755 (etc-directory
756 (fold-services (operating-system-services os)
757 #:target-type etc-service-type)))
758
759 (define (operating-system-environment-variables os)
760 "Return the environment variables of OS for
761 @var{session-environment-service-type}, to be used in @file{/etc/environment}."
762 `(("LANG" . ,(operating-system-locale os))
763 ;; Note: No need to set 'TZ' since (1) we provide /etc/localtime, and (2)
764 ;; it doesn't work for setuid binaries. See <https://bugs.gnu.org/29212>.
765 ("TZDIR" . ,(file-append tzdata "/share/zoneinfo"))
766 ;; Tell 'modprobe' & co. where to look for modules.
767 ("LINUX_MODULE_DIRECTORY" . "/run/booted-system/kernel/lib/modules")
768 ;; These variables are honored by OpenSSL (libssl) and Git.
769 ("SSL_CERT_DIR" . "/etc/ssl/certs")
770 ("SSL_CERT_FILE" . "/etc/ssl/certs/ca-certificates.crt")
771 ("GIT_SSL_CAINFO" . "/etc/ssl/certs/ca-certificates.crt")
772
773 ;; 'GTK_DATA_PREFIX' must name one directory where GTK+ themes are
774 ;; searched for.
775 ("GTK_DATA_PREFIX" . "/run/current-system/profile")
776
777 ;; By default, applications that use D-Bus, such as Emacs, abort at startup
778 ;; when /etc/machine-id is missing. Make sure these warnings are non-fatal.
779 ("DBUS_FATAL_WARNINGS" . "0")
780
781 ;; XXX: Normally we wouldn't need to do this, but our glibc@2.23 package
782 ;; used to look things up in 'PREFIX/lib/locale' instead of
783 ;; '/run/current-system/locale' as was intended. Keep this hack around so
784 ;; that people who still have glibc@2.23-using packages in their profiles
785 ;; can use them correctly.
786 ;; TODO: Remove when glibc@2.23 is long gone.
787 ("GUIX_LOCPATH" . "/run/current-system/locale")))
788
789 (define %setuid-programs
790 ;; Default set of setuid-root programs.
791 (let ((shadow (@ (gnu packages admin) shadow)))
792 (list (file-append shadow "/bin/passwd")
793 (file-append shadow "/bin/su")
794 (file-append shadow "/bin/newuidmap")
795 (file-append shadow "/bin/newgidmap")
796 (file-append inetutils "/bin/ping")
797 (file-append inetutils "/bin/ping6")
798 (file-append sudo "/bin/sudo")
799 (file-append sudo "/bin/sudoedit")
800 (file-append fuse "/bin/fusermount"))))
801
802 (define %sudoers-specification
803 ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel'
804 ;; group can do anything. See
805 ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>.
806 ;; TODO: Add a declarative API.
807 (plain-file "sudoers" "\
808 root ALL=(ALL) ALL
809 %wheel ALL=(ALL) ALL\n"))
810
811 (define* (operating-system-activation-script os #:key container?)
812 "Return the activation script for OS---i.e., the code that \"activates\" the
813 stateful part of OS, including user accounts and groups, special directories,
814 etc."
815 (let* ((services (operating-system-services os #:container? container?))
816 (activation (fold-services services
817 #:target-type activation-service-type)))
818 (activation-service->script activation)))
819
820 (define* (operating-system-boot-script os #:key container?)
821 "Return the boot script for OS---i.e., the code started by the initrd once
822 we're running in the final root. When CONTAINER? is true, skip all
823 hardware-related operations as necessary when booting a Linux container."
824 (let* ((services (operating-system-services os #:container? container?))
825 (boot (fold-services services #:target-type boot-service-type)))
826 (service-value boot)))
827
828 (define (operating-system-user-accounts os)
829 "Return the list of user accounts of OS."
830 (let* ((services (operating-system-services os))
831 (account (fold-services services
832 #:target-type account-service-type)))
833 (filter user-account?
834 (service-value account))))
835
836 (define (operating-system-shepherd-service-names os)
837 "Return the list of Shepherd service names for OS."
838 (append-map shepherd-service-provision
839 (service-value
840 (fold-services (operating-system-services os)
841 #:target-type
842 shepherd-root-service-type))))
843
844 (define* (operating-system-derivation os #:key container?)
845 "Return a derivation that builds OS."
846 (let* ((services (operating-system-services os #:container? container?))
847 (system (fold-services services)))
848 ;; SYSTEM contains the derivation as a monadic value.
849 (service-value system)))
850
851 (define* (operating-system-profile os #:key container?)
852 "Return a derivation that builds the system profile of OS."
853 (mlet* %store-monad
854 ((services -> (operating-system-services os #:container? container?))
855 (profile (fold-services services
856 #:target-type profile-service-type)))
857 (match profile
858 (("profile" profile)
859 (return profile)))))
860
861 (define (operating-system-root-file-system os)
862 "Return the root file system of OS."
863 (find (lambda (fs)
864 (string=? "/" (file-system-mount-point fs)))
865 (operating-system-file-systems os)))
866
867 (define (operating-system-initrd-file os)
868 "Return a gexp denoting the initrd file of OS."
869 (define boot-file-systems
870 (filter file-system-needed-for-boot?
871 (operating-system-file-systems os)))
872
873 (define mapped-devices
874 (operating-system-boot-mapped-devices os))
875
876 (define make-initrd
877 (operating-system-initrd os))
878
879 (make-initrd boot-file-systems
880 #:linux (operating-system-kernel os)
881 #:linux-modules
882 (operating-system-initrd-modules os)
883 #:mapped-devices mapped-devices
884 #:keyboard-layout (operating-system-keyboard-layout os)))
885
886 (define (locale-name->definition* name)
887 "Variant of 'locale-name->definition' that raises an error upon failure."
888 (match (locale-name->definition name)
889 (#f
890 (raise (condition
891 (&message
892 (message (format #f (G_ "~a: invalid locale name") name))))))
893 (def def)))
894
895 (define (operating-system-locale-directory os)
896 "Return the directory containing the locales compiled for the definitions
897 listed in OS. The C library expects to find it under
898 /run/current-system/locale."
899 (define name
900 (operating-system-locale os))
901
902 (define definitions
903 ;; While we're at it, check whether NAME is defined and add it if needed.
904 (if (member name (map locale-definition-name
905 (operating-system-locale-definitions os)))
906 (operating-system-locale-definitions os)
907 (cons (locale-name->definition* name)
908 (operating-system-locale-definitions os))))
909
910 (locale-directory definitions
911 #:libcs (operating-system-locale-libcs os)))
912
913 (define (kernel->boot-label kernel)
914 "Return a label for the bootloader menu entry that boots KERNEL."
915 (cond ((package? kernel)
916 (string-append "GNU with "
917 (string-titlecase (package-name kernel)) " "
918 (package-version kernel)
919 " (beta)"))
920 ((inferior-package? kernel)
921 (string-append "GNU with "
922 (string-titlecase (inferior-package-name kernel)) " "
923 (inferior-package-version kernel)
924 " (beta)"))
925 (else "GNU")))
926
927 (define (store-file-system file-systems)
928 "Return the file system object among FILE-SYSTEMS that contains the store."
929 (match (filter (lambda (fs)
930 (and (file-system-mount? fs)
931 (not (memq 'bind-mount (file-system-flags fs)))
932 (string-prefix? (file-system-mount-point fs)
933 (%store-prefix))))
934 file-systems)
935 ((and candidates (head . tail))
936 (reduce (lambda (fs1 fs2)
937 (if (> (string-length (file-system-mount-point fs1))
938 (string-length (file-system-mount-point fs2)))
939 fs1
940 fs2))
941 head
942 candidates))))
943
944 (define (operating-system-store-file-system os)
945 "Return the file system that contains the store of OS."
946 (store-file-system (operating-system-file-systems os)))
947
948 (define* (operating-system-bootcfg os #:optional (old-entries '()))
949 "Return the bootloader configuration file for OS. Use OLD-ENTRIES,
950 a list of <menu-entry>, to populate the \"old entries\" menu."
951 (let* ((root-fs (operating-system-root-file-system os))
952 (root-device (file-system-device root-fs))
953 (params (operating-system-boot-parameters
954 os root-device
955 #:system-kernel-arguments? #t))
956 (entry (boot-parameters->menu-entry params))
957 (bootloader-conf (operating-system-bootloader os)))
958 (define generate-config-file
959 (bootloader-configuration-file-generator
960 (bootloader-configuration-bootloader bootloader-conf)))
961
962 (generate-config-file bootloader-conf (list entry)
963 #:old-entries old-entries)))
964
965 (define* (operating-system-boot-parameters os root-device
966 #:key system-kernel-arguments?)
967 "Return a monadic <boot-parameters> record that describes the boot
968 parameters of OS. When SYSTEM-KERNEL-ARGUMENTS? is true, add kernel arguments
969 such as '--root' and '--load' to <boot-parameters>."
970 (let* ((initrd (operating-system-initrd-file os))
971 (store (operating-system-store-file-system os))
972 (bootloader (bootloader-configuration-bootloader
973 (operating-system-bootloader os)))
974 (bootloader-name (bootloader-name bootloader))
975 (label (kernel->boot-label (operating-system-kernel os))))
976 (boot-parameters
977 (label label)
978 (root-device root-device)
979 (kernel (operating-system-kernel-file os))
980 (kernel-arguments
981 (if system-kernel-arguments?
982 (operating-system-kernel-arguments os root-device)
983 (operating-system-user-kernel-arguments os)))
984 (initrd initrd)
985 (bootloader-name bootloader-name)
986 (store-device (ensure-not-/dev (file-system-device store)))
987 (store-mount-point (file-system-mount-point store)))))
988
989 (define (device->sexp device)
990 "Serialize DEVICE as an sexp (really, as an object with a read syntax.)"
991 (match device
992 ((? uuid? uuid)
993 `(uuid ,(uuid-type uuid) ,(uuid-bytevector uuid)))
994 ((? file-system-label? label)
995 `(file-system-label ,(file-system-label->string label)))
996 (_
997 device)))
998
999 (define* (operating-system-boot-parameters-file os
1000 #:key system-kernel-arguments?)
1001 "Return a file that describes the boot parameters of OS. The primary use of
1002 this file is the reconstruction of GRUB menu entries for old configurations.
1003
1004 When SYSTEM-KERNEL-ARGUMENTS? is true, add kernel arguments such as '--root'
1005 and '--load' to the returned file (since the returned file is then usually
1006 stored into the content-addressed \"system\" directory, it's usually not a
1007 good idea to give it because the content hash would change by the content hash
1008 being stored into the \"parameters\" file)."
1009 (let* ((root (operating-system-root-file-system os))
1010 (device (file-system-device root))
1011 (params (operating-system-boot-parameters
1012 os device
1013 #:system-kernel-arguments?
1014 system-kernel-arguments?)))
1015 (gexp->file "parameters"
1016 #~(boot-parameters
1017 (version 0)
1018 (label #$(boot-parameters-label params))
1019 (root-device
1020 #$(device->sexp
1021 (boot-parameters-root-device params)))
1022 (kernel #$(boot-parameters-kernel params))
1023 (kernel-arguments
1024 #$(boot-parameters-kernel-arguments params))
1025 (initrd #$(boot-parameters-initrd params))
1026 (bootloader-name #$(boot-parameters-bootloader-name params))
1027 (store
1028 (device
1029 #$(device->sexp (boot-parameters-store-device params)))
1030 (mount-point #$(boot-parameters-store-mount-point params))))
1031 #:set-load-path? #f)))
1032
1033 (define-gexp-compiler (operating-system-compiler (os <operating-system>)
1034 system target)
1035 ((store-lift
1036 (lambda (store)
1037 ;; XXX: This is not super elegant but we can't pass SYSTEM and TARGET to
1038 ;; 'operating-system-derivation'.
1039 (run-with-store store (operating-system-derivation os)
1040 #:system system
1041 #:target target)))))
1042
1043 ;;; system.scm ends here