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 ;;; Copyright © 2020 Danny Milosavljevic <dannym@scratchpost.org>
10 ;;; This file is part of GNU Guix.
12 ;;; GNU Guix is free software; you can redistribute it and/or modify it
13 ;;; under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 3 of the License, or (at
15 ;;; your option) any later version.
17 ;;; GNU Guix is distributed in the hope that it will be useful, but
18 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
25 (define-module (gnu system)
26 #:use-module (guix inferior)
27 #:use-module (guix store)
28 #:use-module (guix monads)
29 #:use-module (guix gexp)
30 #:use-module (guix records)
31 #:use-module (guix packages)
32 #:use-module (guix derivations)
33 #:use-module (guix profiles)
34 #:use-module (guix ui)
35 #:use-module (gnu packages base)
36 #:use-module (gnu packages bash)
37 #:use-module (gnu packages guile)
38 #:use-module (gnu packages guile-xyz)
39 #:use-module (gnu packages admin)
40 #:use-module (gnu packages linux)
41 #:use-module (gnu packages pciutils)
42 #:use-module (gnu packages package-management)
43 #:use-module (gnu packages less)
44 #:use-module (gnu packages zile)
45 #:use-module (gnu packages nano)
46 #:use-module (gnu packages gawk)
47 #:use-module (gnu packages man)
48 #:use-module (gnu packages texinfo)
49 #:use-module (gnu packages compression)
50 #:use-module (gnu packages firmware)
51 #:use-module (gnu services)
52 #:use-module (gnu services shepherd)
53 #:use-module (gnu services base)
54 #:use-module (gnu bootloader)
55 #:use-module (gnu system shadow)
56 #:use-module (gnu system nss)
57 #:use-module (gnu system locale)
58 #:use-module (gnu system pam)
59 #:use-module (gnu system linux-initrd)
60 #:use-module (gnu system uuid)
61 #:use-module (gnu system file-systems)
62 #:use-module (gnu system mapped-devices)
63 #:use-module (ice-9 match)
64 #:use-module (srfi srfi-1)
65 #:use-module (srfi srfi-26)
66 #:use-module (srfi srfi-34)
67 #:use-module (srfi srfi-35)
68 #:use-module (rnrs bytevectors)
69 #:export (operating-system
73 operating-system-bootloader
74 operating-system-services
75 operating-system-essential-services
76 operating-system-default-essential-services
77 operating-system-user-services
78 operating-system-packages
79 operating-system-host-name
80 operating-system-hosts-file
81 operating-system-kernel
82 operating-system-kernel-file
83 operating-system-kernel-arguments
84 operating-system-label
85 operating-system-default-label
86 operating-system-initrd-modules
87 operating-system-initrd
88 operating-system-users
89 operating-system-groups
90 operating-system-issue
91 operating-system-timezone
92 operating-system-locale
93 operating-system-locale-definitions
94 operating-system-locale-libcs
95 operating-system-mapped-devices
96 operating-system-file-systems
97 operating-system-store-file-system
98 operating-system-user-mapped-devices
99 operating-system-boot-mapped-devices
100 operating-system-activation-script
101 operating-system-user-accounts
102 operating-system-shepherd-service-names
103 operating-system-user-kernel-arguments
104 operating-system-firmware
105 operating-system-keyboard-layout
106 operating-system-name-service-switch
107 operating-system-pam-services
108 operating-system-setuid-programs
109 operating-system-skeletons
110 operating-system-sudoers-file
111 operating-system-swap-devices
112 operating-system-kernel-loadable-modules
114 operating-system-derivation
115 operating-system-profile
116 operating-system-bootcfg
117 operating-system-etc-directory
118 operating-system-locale-directory
119 operating-system-boot-script
121 system-linux-image-file-name
122 operating-system-with-gc-roots
123 operating-system-with-provenance
127 boot-parameters-label
128 boot-parameters-root-device
129 boot-parameters-bootloader-name
130 boot-parameters-bootloader-menu-entries
131 boot-parameters-store-device
132 boot-parameters-store-mount-point
133 boot-parameters-kernel
134 boot-parameters-kernel-arguments
135 boot-parameters-initrd
137 read-boot-parameters-file
138 boot-parameters->menu-entry
143 %sudoers-specification
149 ;;; This module supports whole-system configuration.
153 (define (bootable-kernel-arguments system root-device)
154 "Return a list of kernel arguments (gexps) to boot SYSTEM from ROOT-DEVICE."
155 (list (string-append "--root="
156 ;; Note: Always use the DCE format because that's what
157 ;; (gnu build linux-boot) expects for the '--root'
158 ;; kernel command-line option.
159 (file-system-device->string root-device
161 #~(string-append "--system=" #$system)
162 #~(string-append "--load=" #$system "/boot")))
164 ;; System-wide configuration.
165 ;; TODO: Add per-field docstrings/stexi.
166 (define-record-type* <operating-system> operating-system
167 make-operating-system
169 this-operating-system
171 (kernel operating-system-kernel ; package
172 (default linux-libre))
173 (kernel-loadable-modules operating-system-kernel-loadable-modules
174 (default '())) ; list of packages
175 (kernel-arguments operating-system-user-kernel-arguments
176 (default '("quiet"))) ; list of gexps/strings
177 (bootloader operating-system-bootloader) ; <bootloader-configuration>
178 (label operating-system-label ; string
180 (default (operating-system-default-label this-operating-system)))
182 (keyboard-layout operating-system-keyboard-layout ;#f | <keyboard-layout>
184 (initrd operating-system-initrd ; (list fs) -> file-like
185 (default base-initrd))
186 (initrd-modules operating-system-initrd-modules ; list of strings
187 (thunked) ; it's system-dependent
188 (default %base-initrd-modules))
190 (firmware operating-system-firmware ; list of packages
191 (default %base-firmware))
193 (host-name operating-system-host-name) ; string
194 (hosts-file operating-system-hosts-file ; file-like | #f
197 (mapped-devices operating-system-mapped-devices ; list of <mapped-device>
199 (file-systems operating-system-file-systems) ; list of fs
200 (swap-devices operating-system-swap-devices ; list of strings
203 (users operating-system-users ; list of user accounts
204 (default %base-user-accounts))
205 (groups operating-system-groups ; list of user groups
206 (default %base-groups))
208 (skeletons operating-system-skeletons ; list of name/file-like value
209 (default (default-skeletons)))
210 (issue operating-system-issue ; string
211 (default %default-issue))
213 (packages operating-system-packages ; list of (PACKAGE OUTPUT...)
214 (default %base-packages)) ; or just PACKAGE
216 (timezone operating-system-timezone) ; string
217 (locale operating-system-locale ; string
218 (default "en_US.utf8"))
219 (locale-definitions operating-system-locale-definitions ; list of <locale-definition>
220 (default %default-locale-definitions))
221 (locale-libcs operating-system-locale-libcs ; list of <packages>
222 (default %default-locale-libcs))
223 (name-service-switch operating-system-name-service-switch ; <name-service-switch>
224 (default %default-nss))
226 (essential-services operating-system-essential-services ; list of services
228 (default (operating-system-default-essential-services
229 this-operating-system)))
230 (services operating-system-user-services ; list of services
231 (default %base-services))
233 (pam-services operating-system-pam-services ; list of PAM services
234 (default (base-pam-services)))
235 (setuid-programs operating-system-setuid-programs
236 (default %setuid-programs)) ; list of string-valued gexps
238 (sudoers-file operating-system-sudoers-file ; file-like
239 (default %sudoers-specification)))
241 (define (operating-system-kernel-arguments os root-device)
242 "Return all the kernel arguments, including the ones not specified
243 directly by the user."
244 (append (bootable-kernel-arguments os root-device)
245 (operating-system-user-kernel-arguments os)))
252 (define-record-type* <boot-parameters>
253 boot-parameters make-boot-parameters boot-parameters?
254 (label boot-parameters-label)
255 ;; Because we will use the 'store-device' to create the GRUB search command,
256 ;; the 'store-device' has slightly different semantics than 'root-device'.
257 ;; The 'store-device' can be a file system uuid, a file system label, or #f,
258 ;; but it cannot be a device path such as "/dev/sda3", since GRUB would not
259 ;; understand that. The 'root-device', on the other hand, corresponds
260 ;; exactly to the device field of the <file-system> object representing the
261 ;; OS's root file system, so it might be a device path like "/dev/sda3".
262 (root-device boot-parameters-root-device)
263 (bootloader-name boot-parameters-bootloader-name)
264 (bootloader-menu-entries ;list of <menu-entry>
265 boot-parameters-bootloader-menu-entries)
266 (store-device boot-parameters-store-device)
267 (store-mount-point boot-parameters-store-mount-point)
268 (kernel boot-parameters-kernel)
269 (kernel-arguments boot-parameters-kernel-arguments)
270 (initrd boot-parameters-initrd))
272 (define (ensure-not-/dev device)
273 "If DEVICE starts with a slash, return #f. This is meant to filter out
274 Linux device names such as /dev/sda, and to preserve GRUB device names and
276 (if (and (string? device) (string-prefix? "/" device))
280 (define (read-boot-parameters port)
281 "Read boot parameters from PORT and return the corresponding
282 <boot-parameters> object or #f if the format is unrecognized."
283 (define device-sexp->device
285 (('uuid (? symbol? type) (? bytevector? bv))
286 (bytevector->uuid bv type))
287 (('file-system-label (? string? label))
288 (file-system-label label))
289 ((? bytevector? bv) ;old format
290 (bytevector->uuid bv 'dce))
292 ;; It used to be that we would not distinguish between labels and
293 ;; device names. Try to infer the right thing here.
294 (if (string-prefix? "/dev/" device)
296 (file-system-label device)))))
299 (('boot-parameters ('version 0)
300 ('label label) ('root-device root)
305 (root-device (device-sexp->device root))
308 (match (assq 'bootloader-name rest)
310 (#f 'grub))) ; for compatibility reasons.
312 (bootloader-menu-entries
313 (match (assq 'bootloader-menu-entries rest)
314 ((_ entries) (map sexp->menu-entry entries))
317 ;; In the past, we would store the directory name of the kernel instead
318 ;; of the absolute file name of its image. Detect that and correct it.
319 (kernel (if (string=? linux (direct-store-path linux))
320 (string-append linux "/"
321 (system-linux-image-file-name))
325 (match (assq 'kernel-arguments rest)
327 (#f '()))) ;the old format
330 (match (assq 'initrd rest)
331 (('initrd ('string-append directory file)) ;the old format
332 (string-append directory file))
333 (('initrd (? string? file))
337 ;; Linux device names like "/dev/sda1" are not suitable GRUB device
338 ;; identifiers, so we just filter them out.
340 (match (assq 'store rest)
341 (('store ('device #f) _ ...)
343 (('store ('device device) _ ...)
344 (device-sexp->device device))
349 (match (assq 'store rest)
350 (('store ('device _) ('mount-point mount-point) _ ...)
354 (x ;unsupported format
355 (warning (G_ "unrecognized boot parameters at '~a'~%")
356 (port-filename port))
359 (define (read-boot-parameters-file system)
360 "Read boot parameters from SYSTEM's (system or generation) \"parameters\"
361 file and returns the corresponding <boot-parameters> object or #f if the
362 format is unrecognized.
363 The object has its kernel-arguments extended in order to make it bootable."
364 (let* ((file (string-append system "/parameters"))
365 (params (call-with-input-file file read-boot-parameters))
366 (root (boot-parameters-root-device params)))
369 (kernel-arguments (append (bootable-kernel-arguments system root)
370 (boot-parameters-kernel-arguments params))))))
372 (define (boot-parameters->menu-entry conf)
374 (label (boot-parameters-label conf))
375 (device (boot-parameters-store-device conf))
376 (device-mount-point (boot-parameters-store-mount-point conf))
377 (linux (boot-parameters-kernel conf))
378 (linux-arguments (boot-parameters-kernel-arguments conf))
379 (initrd (boot-parameters-initrd conf))))
387 (define (non-boot-file-system-service os)
388 "Return the file system service for the file systems of OS that are not
389 marked as 'needed-for-boot'."
391 (remove file-system-needed-for-boot?
392 (operating-system-file-systems os)))
394 (define mapped-devices-for-boot
395 (operating-system-boot-mapped-devices os))
397 (define (device-mappings fs)
398 (let ((device (file-system-device fs)))
399 (if (string? device) ;title is 'device
401 (string=? (string-append "/dev/mapper/"
402 (mapped-device-target md))
404 (operating-system-mapped-devices os))
407 (define (add-dependencies fs)
408 ;; Add the dependencies due to device mappings to FS.
413 (remove (cut member <> mapped-devices-for-boot)
414 (append (device-mappings fs)
415 (file-system-dependencies fs)))
418 (service file-system-service-type
419 (map add-dependencies file-systems)))
421 (define (mapped-device-users device file-systems)
422 "Return the subset of FILE-SYSTEMS that use DEVICE."
423 (let ((target (string-append "/dev/mapper/" (mapped-device-target device))))
425 (or (member device (file-system-dependencies fs))
426 (and (string? (file-system-device fs))
427 (string=? (file-system-device fs) target))))
430 (define (operating-system-user-mapped-devices os)
431 "Return the subset of mapped devices that can be installed in
432 user-land--i.e., those not needed during boot."
433 (let ((devices (operating-system-mapped-devices os))
434 (file-systems (operating-system-file-systems os)))
436 (let ((users (mapped-device-users md file-systems)))
437 (not (any file-system-needed-for-boot? users))))
440 (define (operating-system-boot-mapped-devices os)
441 "Return the subset of mapped devices that must be installed during boot,
443 (let ((devices (operating-system-mapped-devices os))
444 (file-systems (operating-system-file-systems os)))
446 (let ((users (mapped-device-users md file-systems)))
447 (any file-system-needed-for-boot? users)))
450 (define (device-mapping-services os)
451 "Return the list of device-mapping services for OS as a list."
452 (map device-mapping-service
453 (operating-system-user-mapped-devices os)))
455 (define (swap-services os)
456 "Return the list of swap services for OS."
457 (map swap-service (operating-system-swap-devices os)))
459 (define* (system-linux-image-file-name)
460 "Return the basename of the kernel image file for SYSTEM."
461 ;; FIXME: Evaluate the conditional based on the actual current system.
462 (let ((target (or (%current-target-system) (%current-system))))
464 ((string-prefix? "arm" target) "zImage")
465 ((string-prefix? "mips" target) "vmlinuz")
466 ((string-prefix? "aarch64" target) "Image")
469 (define (operating-system-kernel-file os)
470 "Return an object representing the absolute file name of the kernel image of
472 (file-append (operating-system-kernel os)
473 "/" (system-linux-image-file-name)))
475 (define* (operating-system-directory-base-entries os)
476 "Return the basic entries of the 'system' directory of OS for use as the
477 value of the SYSTEM-SERVICE-TYPE service."
478 (let ((locale (operating-system-locale-directory os)))
479 (mlet* %store-monad ((kernel -> (operating-system-kernel os))
481 (operating-system-kernel-loadable-modules os))
485 (cons kernel modules))
486 #:hooks (list linux-module-database)))
487 (initrd -> (operating-system-initrd-file os))
488 (params -> (operating-system-boot-parameters-file os)))
489 (return `(("kernel" ,kernel)
490 ("parameters" ,params)
492 ("locale" ,locale)))))) ;used by libc
494 (define (operating-system-default-essential-services os)
495 "Return the list of essential services for OS. These are special services
496 that implement part of what's declared in OS are responsible for low-level
499 (map file-system-mount-point (operating-system-file-systems os)))
501 (let* ((mappings (device-mapping-services os))
502 (root-fs (root-file-system-service))
503 (other-fs (non-boot-file-system-service os))
504 (swaps (swap-services os))
505 (procs (service user-processes-service-type))
506 (host-name (host-name-service (operating-system-host-name os)))
507 (entries (operating-system-directory-base-entries os)))
508 (cons* (service system-service-type entries)
511 ;; %SHEPHERD-ROOT-SERVICE must come last so that the gexp that
512 ;; execs shepherd comes last in the boot script (XXX). Likewise,
513 ;; the cleanup service must come first so that its gexp runs before
515 (service cleanup-service-type #f)
517 %shepherd-root-service
519 (pam-root-service (operating-system-pam-services os))
520 (account-service (append (operating-system-accounts os)
521 (operating-system-groups os))
522 (operating-system-skeletons os))
523 (operating-system-etc-service os)
524 (service fstab-service-type
525 (filter file-system-needed-for-boot?
526 (operating-system-file-systems os)))
527 (session-environment-service
528 (operating-system-environment-variables os))
529 host-name procs root-fs
530 (service setuid-program-service-type
531 (operating-system-setuid-programs os))
532 (service profile-service-type
533 (operating-system-packages os))
535 (append mappings swaps
537 ;; Add the firmware service.
538 (list %linux-bare-metal-service
539 (service firmware-service-type
540 (operating-system-firmware os)))))))
542 (define* (operating-system-services os)
543 "Return all the services of OS, including \"essential\" services."
544 (instantiate-missing-services
545 (append (operating-system-user-services os)
546 (operating-system-essential-services os))))
548 (define (operating-system-with-gc-roots os roots)
549 "Return a variant of OS where ROOTS are registered as GC roots."
553 ;; We use this procedure for the installation OS, which already defines GC
554 ;; roots. Add ROOTS to those.
555 (services (cons (simple-service 'extra-root
556 gc-root-service-type roots)
557 (operating-system-user-services os)))))
559 (define* (operating-system-with-provenance os #:optional config-file)
560 "Return a variant of OS that stores its own provenance information,
561 including CONFIG-FILE, if available. This is achieved by adding an instance
562 of PROVENANCE-SERVICE-TYPE to its services."
565 (services (cons (service provenance-service-type config-file)
566 (operating-system-user-services os)))))
573 (define %base-firmware
574 ;; Firmware usable by default.
575 (list ath9k-htc-firmware
578 (define %base-packages
579 ;; Default set of packages globally visible. It should include anything
580 ;; required for basic administrator tasks.
581 (cons* procps psmisc which less zile nano
585 (@ (gnu packages admin) shadow) ;for 'passwd'
587 ;; wireless-tools is deprecated in favor of iw, but it's still what
588 ;; many people are familiar with, so keep it around.
593 info-reader ;the standalone Info reader (no Perl)
595 ;; The 'sudo' command is already in %SETUID-PROGRAMS, but we also
596 ;; want the other commands and the man pages (notably because
597 ;; auto-completion in Emacs shell relies on man pages.)
600 ;; Get 'insmod' & co. from kmod, not module-init-tools, since udev
601 ;; already depends on it anyway.
608 ;; XXX: We don't use (canonical-package guile-2.2) here because that
609 ;; would create a collision in the global profile between the GMP
610 ;; variant propagated by 'guile-final' and the GMP variant propagated
611 ;; by 'gnutls', itself propagated by 'guix'.
613 guile-readline guile-colorized
615 ;; The packages below are also in %FINAL-INPUTS, so take them from
616 ;; there to avoid duplication.
617 (map canonical-package
618 (list bash coreutils findutils grep sed
619 diffutils patch gawk tar gzip bzip2 xz lzip))))
621 (define %default-issue
622 ;; Default contents for /etc/issue.
624 This is the GNU system. Welcome.\n")
626 (define (local-host-aliases host-name)
627 "Return aliases for HOST-NAME, to be used in /etc/hosts."
628 (string-append "127.0.0.1 localhost " host-name "\n"
629 "::1 localhost " host-name "\n"))
631 (define (default-/etc/hosts host-name)
632 "Return the default /etc/hosts file."
633 (plain-file "hosts" (local-host-aliases host-name)))
635 (define* (operating-system-etc-service os)
636 "Return a <service> that builds containing the static part of the /etc
639 (plain-file "login.defs"
641 "# Default paths for non-login shells started by su(1).\n"
642 "ENV_PATH /run/setuid-programs:"
643 "/run/current-system/profile/bin:"
644 "/run/current-system/profile/sbin\n"
645 "ENV_SUPATH /run/setuid-programs:"
646 "/run/current-system/profile/bin:"
647 "/run/current-system/profile/sbin\n")))
649 (issue (plain-file "issue" (operating-system-issue os)))
650 (nsswitch (plain-file "nsswitch.conf"
651 (name-service-switch->string
652 (operating-system-name-service-switch os))))
654 ;; Startup file for POSIX-compliant login shells, which set system-wide
655 ;; environment variables.
656 (profile (mixed-text-file "profile" "\
657 # Crucial variables that could be missing in the profiles' 'etc/profile'
658 # because they would require combining both profiles.
659 # FIXME: See <http://bugs.gnu.org/20255>.
660 export MANPATH=$HOME/.guix-profile/share/man:/run/current-system/profile/share/man
661 export INFOPATH=$HOME/.guix-profile/share/info:/run/current-system/profile/share/info
662 export XDG_DATA_DIRS=$HOME/.guix-profile/share:/run/current-system/profile/share
663 export XDG_CONFIG_DIRS=$HOME/.guix-profile/etc/xdg:/run/current-system/profile/etc/xdg
665 # Make sure libXcursor finds cursors installed into user or system profiles. See <http://bugs.gnu.org/24445>
666 export XCURSOR_PATH=$HOME/.icons:$HOME/.guix-profile/share/icons:/run/current-system/profile/share/icons
668 # Ignore the default value of 'PATH'.
671 # Load the system profile's settings.
672 GUIX_PROFILE=/run/current-system/profile ; \\
673 . /run/current-system/profile/etc/profile
675 # Since 'lshd' does not use pam_env, /etc/environment must be explicitly
676 # loaded when someone logs in via SSH. See <http://bugs.gnu.org/22175>.
677 # We need 'PATH' to be defined here, for 'cat' and 'cut'. Do this before
678 # reading the user's 'etc/profile' to allow variables to be overridden.
679 if [ -f /etc/environment -a -n \"$SSH_CLIENT\" \\
680 -a -z \"$LINUX_MODULE_DIRECTORY\" ]
683 export `cat /etc/environment | cut -d= -f1`
686 # Arrange so that ~/.config/guix/current comes first.
687 for profile in \"$HOME/.guix-profile\" \"$HOME/.config/guix/current\"
689 if [ -f \"$profile/etc/profile\" ]
691 # Load the user profile's settings.
692 GUIX_PROFILE=\"$profile\" ; \\
693 . \"$profile/etc/profile\"
695 # At least define this one so that basic things just work
696 # when the user installs their first package.
697 export PATH=\"$profile/bin:$PATH\"
701 # Prepend setuid programs.
702 export PATH=/run/setuid-programs:$PATH
704 # Arrange so that ~/.config/guix/current/share/info comes first.
705 export INFOPATH=\"$HOME/.config/guix/current/share/info:$INFOPATH\"
707 # Set the umask, notably for users logging in via 'lsh'.
708 # See <http://bugs.gnu.org/22650>.
711 # Allow Hunspell-based applications (IceCat, LibreOffice, etc.) to
713 export DICPATH=\"$HOME/.guix-profile/share/hunspell:/run/current-system/profile/share/hunspell\"
715 # Allow GStreamer-based applications to find plugins.
716 export GST_PLUGIN_PATH=\"$HOME/.guix-profile/lib/gstreamer-1.0\"
718 if [ -n \"$BASH_VERSION\" -a -f /etc/bashrc ]
720 # Load Bash-specific initialization code.
725 (bashrc (plain-file "bashrc" "\
726 # Bash-specific initialization.
728 # The 'bash-completion' package.
729 if [ -f /run/current-system/profile/etc/profile.d/bash_completion.sh ]
731 # Bash-completion sources ~/.bash_completion. It installs a dynamic
732 # completion loader that searches its own completion files as well
733 # as those in ~/.guix-profile and /run/current-system/profile.
734 source /run/current-system/profile/etc/profile.d/bash_completion.sh
737 `(("services" ,(file-append net-base "/etc/services"))
738 ("protocols" ,(file-append net-base "/etc/protocols"))
739 ("rpc" ,(file-append net-base "/etc/rpc"))
740 ("login.defs" ,#~#$login.defs)
742 ("nsswitch.conf" ,#~#$nsswitch)
743 ("profile" ,#~#$profile)
744 ("bashrc" ,#~#$bashrc)
745 ("hosts" ,#~#$(or (operating-system-hosts-file os)
746 (default-/etc/hosts (operating-system-host-name os))))
747 ;; Write the operating-system-host-name to /etc/hostname to prevent
748 ;; NetworkManager from changing the system's hostname when connecting
749 ;; to certain networks. Some discussion at
750 ;; https://lists.gnu.org/archive/html/help-guix/2017-09/msg00037.html
751 ("hostname" ,(plain-file "hostname" (operating-system-host-name os)))
752 ;; Some programs (e.g., GLib) look at /etc/timezone to find the
753 ;; name of the current timezone. For details, see
754 ;; https://lists.gnu.org/archive/html/guix-devel/2019-07/msg00166.html
755 ("timezone" ,(plain-file "timezone" (operating-system-timezone os)))
756 ("localtime" ,(file-append tzdata "/share/zoneinfo/"
757 (operating-system-timezone os)))
758 ("sudoers" ,(operating-system-sudoers-file os))))))
760 (define %root-account
761 ;; Default root account.
765 (uid 0) (group "root")
766 (comment "System administrator")
767 (home-directory "/root")))
769 (define (operating-system-accounts os)
770 "Return the user accounts for OS, including an obligatory 'root' account,
771 and excluding accounts requested by services."
772 ;; Make sure there's a root account.
773 (if (find (lambda (user)
774 (and=> (user-account-uid user) zero?))
775 (operating-system-users os))
776 (operating-system-users os)
777 (cons %root-account (operating-system-users os))))
779 (define (maybe-string->file file-name thing)
780 "If THING is a string, return a <plain-file> with THING as its content.
781 Otherwise just return THING.
783 This is for backward-compatibility of fields that used to be strings and are
784 now file-like objects.."
787 (warning (G_ "using a string for file '~a' is deprecated; \
788 use 'plain-file' instead~%")
790 (plain-file file-name thing))
794 (define (maybe-file->monadic file-name thing)
795 "If THING is a value in %STORE-MONAD, return it as is; otherwise return
796 THING in the %STORE-MONAD.
798 This is for backward-compatibility of fields that used to be monadic values
799 and are now file-like objects."
800 (with-monad %store-monad
803 (warning (G_ "using a monadic value for '~a' is deprecated; \
804 use 'plain-file' instead~%")
810 (define (operating-system-etc-directory os)
811 "Return that static part of the /etc directory of OS."
813 (fold-services (operating-system-services os)
814 #:target-type etc-service-type)))
816 (define (operating-system-environment-variables os)
817 "Return the environment variables of OS for
818 @var{session-environment-service-type}, to be used in @file{/etc/environment}."
819 `(("LANG" . ,(operating-system-locale os))
820 ;; Note: No need to set 'TZ' since (1) we provide /etc/localtime, and (2)
821 ;; it doesn't work for setuid binaries. See <https://bugs.gnu.org/29212>.
822 ("TZDIR" . ,(file-append tzdata "/share/zoneinfo"))
823 ;; Tell 'modprobe' & co. where to look for modules.
824 ("LINUX_MODULE_DIRECTORY" . "/run/booted-system/kernel/lib/modules")
825 ;; These variables are honored by OpenSSL (libssl) and Git.
826 ("SSL_CERT_DIR" . "/etc/ssl/certs")
827 ("SSL_CERT_FILE" . "/etc/ssl/certs/ca-certificates.crt")
828 ("GIT_SSL_CAINFO" . "/etc/ssl/certs/ca-certificates.crt")
830 ;; 'GTK_DATA_PREFIX' must name one directory where GTK+ themes are
832 ("GTK_DATA_PREFIX" . "/run/current-system/profile")
834 ;; By default, applications that use D-Bus, such as Emacs, abort at startup
835 ;; when /etc/machine-id is missing. Make sure these warnings are non-fatal.
836 ("DBUS_FATAL_WARNINGS" . "0")
838 ;; XXX: Normally we wouldn't need to do this, but our glibc@2.23 package
839 ;; used to look things up in 'PREFIX/lib/locale' instead of
840 ;; '/run/current-system/locale' as was intended. Keep this hack around so
841 ;; that people who still have glibc@2.23-using packages in their profiles
842 ;; can use them correctly.
843 ;; TODO: Remove when glibc@2.23 is long gone.
844 ("GUIX_LOCPATH" . "/run/current-system/locale")))
846 (define %setuid-programs
847 ;; Default set of setuid-root programs.
848 (let ((shadow (@ (gnu packages admin) shadow)))
849 (list (file-append shadow "/bin/passwd")
850 (file-append shadow "/bin/su")
851 (file-append shadow "/bin/newuidmap")
852 (file-append shadow "/bin/newgidmap")
853 (file-append inetutils "/bin/ping")
854 (file-append inetutils "/bin/ping6")
855 (file-append sudo "/bin/sudo")
856 (file-append sudo "/bin/sudoedit")
857 (file-append fuse "/bin/fusermount")
859 ;; To allow mounts with the "user" option, "mount" and "umount" must
861 (file-append util-linux "/bin/mount")
862 (file-append util-linux "/bin/umount"))))
864 (define %sudoers-specification
865 ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel'
866 ;; group can do anything. See
867 ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>.
868 ;; TODO: Add a declarative API.
869 (plain-file "sudoers" "\
871 %wheel ALL=(ALL) ALL\n"))
873 (define* (operating-system-activation-script os)
874 "Return the activation script for OS---i.e., the code that \"activates\" the
875 stateful part of OS, including user accounts and groups, special directories,
877 (let* ((services (operating-system-services os))
878 (activation (fold-services services
879 #:target-type activation-service-type)))
880 (activation-service->script activation)))
882 (define* (operating-system-boot-script os)
883 "Return the boot script for OS---i.e., the code started by the initrd once
884 we're running in the final root."
885 (let* ((services (operating-system-services os))
886 (boot (fold-services services #:target-type boot-service-type)))
887 (service-value boot)))
889 (define (operating-system-user-accounts os)
890 "Return the list of user accounts of OS."
891 (let* ((services (operating-system-services os))
892 (account (fold-services services
893 #:target-type account-service-type)))
894 (filter user-account?
895 (service-value account))))
897 (define (operating-system-shepherd-service-names os)
898 "Return the list of Shepherd service names for OS."
899 (append-map shepherd-service-provision
901 (fold-services (operating-system-services os)
903 shepherd-root-service-type))))
905 (define* (operating-system-derivation os)
906 "Return a derivation that builds OS."
907 (let* ((services (operating-system-services os))
908 (system (fold-services services)))
909 ;; SYSTEM contains the derivation as a monadic value.
910 (service-value system)))
912 (define* (operating-system-profile os)
913 "Return a derivation that builds the system profile of OS."
915 ((services -> (operating-system-services os))
916 (profile (fold-services services
917 #:target-type profile-service-type)))
922 (define (operating-system-root-file-system os)
923 "Return the root file system of OS."
925 (string=? "/" (file-system-mount-point fs)))
926 (operating-system-file-systems os)))
928 (define (operating-system-initrd-file os)
929 "Return a gexp denoting the initrd file of OS."
930 (define boot-file-systems
931 (filter file-system-needed-for-boot?
932 (operating-system-file-systems os)))
934 (define mapped-devices
935 (operating-system-boot-mapped-devices os))
938 (operating-system-initrd os))
940 (make-initrd boot-file-systems
941 #:linux (operating-system-kernel os)
943 (operating-system-initrd-modules os)
944 #:mapped-devices mapped-devices
945 #:keyboard-layout (operating-system-keyboard-layout os)))
947 (define (locale-name->definition* name)
948 "Variant of 'locale-name->definition' that raises an error upon failure."
949 (match (locale-name->definition name)
953 (message (format #f (G_ "~a: invalid locale name") name))))))
956 (define (operating-system-locale-directory os)
957 "Return the directory containing the locales compiled for the definitions
958 listed in OS. The C library expects to find it under
959 /run/current-system/locale."
961 (operating-system-locale os))
964 ;; While we're at it, check whether NAME is defined and add it if needed.
965 (if (member name (map locale-definition-name
966 (operating-system-locale-definitions os)))
967 (operating-system-locale-definitions os)
968 (cons (locale-name->definition* name)
969 (operating-system-locale-definitions os))))
971 (locale-directory definitions
972 #:libcs (operating-system-locale-libcs os)))
974 (define (kernel->boot-label kernel)
975 "Return a label for the bootloader menu entry that boots KERNEL."
976 (cond ((package? kernel)
977 (string-append "GNU with "
978 (string-titlecase (package-name kernel)) " "
979 (package-version kernel)))
980 ((inferior-package? kernel)
981 (string-append "GNU with "
982 (string-titlecase (inferior-package-name kernel)) " "
983 (inferior-package-version kernel)))
986 (define (operating-system-default-label os)
987 "Return the default label for OS, as it will appear in the bootloader menu
989 (kernel->boot-label (operating-system-kernel os)))
991 (define (store-file-system file-systems)
992 "Return the file system object among FILE-SYSTEMS that contains the store."
993 (match (filter (lambda (fs)
994 (and (file-system-mount? fs)
995 (not (memq 'bind-mount (file-system-flags fs)))
996 (string-prefix? (file-system-mount-point fs)
999 ((and candidates (head . tail))
1000 (reduce (lambda (fs1 fs2)
1001 (if (> (string-length (file-system-mount-point fs1))
1002 (string-length (file-system-mount-point fs2)))
1008 (define (operating-system-store-file-system os)
1009 "Return the file system that contains the store of OS."
1010 (store-file-system (operating-system-file-systems os)))
1012 (define* (operating-system-bootcfg os #:optional (old-entries '()))
1013 "Return the bootloader configuration file for OS. Use OLD-ENTRIES,
1014 a list of <menu-entry>, to populate the \"old entries\" menu."
1015 (let* ((root-fs (operating-system-root-file-system os))
1016 (root-device (file-system-device root-fs))
1017 (params (operating-system-boot-parameters
1019 #:system-kernel-arguments? #t))
1020 (entry (boot-parameters->menu-entry params))
1021 (bootloader-conf (operating-system-bootloader os)))
1022 (define generate-config-file
1023 (bootloader-configuration-file-generator
1024 (bootloader-configuration-bootloader bootloader-conf)))
1026 (generate-config-file bootloader-conf (list entry)
1027 #:old-entries old-entries)))
1029 (define* (operating-system-boot-parameters os root-device
1030 #:key system-kernel-arguments?)
1031 "Return a monadic <boot-parameters> record that describes the boot
1032 parameters of OS. When SYSTEM-KERNEL-ARGUMENTS? is true, add kernel arguments
1033 such as '--root' and '--load' to <boot-parameters>."
1034 (let* ((initrd (operating-system-initrd-file os))
1035 (store (operating-system-store-file-system os))
1036 (bootloader (bootloader-configuration-bootloader
1037 (operating-system-bootloader os)))
1038 (bootloader-name (bootloader-name bootloader))
1039 (label (operating-system-label os)))
1042 (root-device root-device)
1043 (kernel (operating-system-kernel-file os))
1045 (if system-kernel-arguments?
1046 (operating-system-kernel-arguments os root-device)
1047 (operating-system-user-kernel-arguments os)))
1049 (bootloader-name bootloader-name)
1050 (bootloader-menu-entries
1051 (bootloader-configuration-menu-entries (operating-system-bootloader os)))
1052 (store-device (ensure-not-/dev (file-system-device store)))
1053 (store-mount-point (file-system-mount-point store)))))
1055 (define (device->sexp device)
1056 "Serialize DEVICE as an sexp (really, as an object with a read syntax.)"
1059 `(uuid ,(uuid-type uuid) ,(uuid-bytevector uuid)))
1060 ((? file-system-label? label)
1061 `(file-system-label ,(file-system-label->string label)))
1065 (define* (operating-system-boot-parameters-file os
1066 #:key system-kernel-arguments?)
1067 "Return a file that describes the boot parameters of OS. The primary use of
1068 this file is the reconstruction of GRUB menu entries for old configurations.
1070 When SYSTEM-KERNEL-ARGUMENTS? is true, add kernel arguments such as '--root'
1071 and '--load' to the returned file (since the returned file is then usually
1072 stored into the content-addressed \"system\" directory, it's usually not a
1073 good idea to give it because the content hash would change by the content hash
1074 being stored into the \"parameters\" file)."
1075 (let* ((root (operating-system-root-file-system os))
1076 (device (file-system-device root))
1077 (params (operating-system-boot-parameters
1079 #:system-kernel-arguments?
1080 system-kernel-arguments?)))
1081 (scheme-file "parameters"
1084 (label #$(boot-parameters-label params))
1087 (boot-parameters-root-device params)))
1088 (kernel #$(boot-parameters-kernel params))
1090 #$(boot-parameters-kernel-arguments params))
1091 (initrd #$(boot-parameters-initrd params))
1092 (bootloader-name #$(boot-parameters-bootloader-name params))
1093 (bootloader-menu-entries
1094 #$(map menu-entry->sexp
1095 (or (and=> (operating-system-bootloader os)
1096 bootloader-configuration-menu-entries)
1100 #$(device->sexp (boot-parameters-store-device params)))
1101 (mount-point #$(boot-parameters-store-mount-point
1103 #:set-load-path? #f)))
1105 (define-gexp-compiler (operating-system-compiler (os <operating-system>)
1109 ;; XXX: This is not super elegant but we can't pass SYSTEM and TARGET to
1110 ;; 'operating-system-derivation'.
1111 (run-with-store store (operating-system-derivation os)
1113 #:target target)))))
1115 ;;; system.scm ends here