1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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>
9 ;;; Copyright © 2020 Brice Waegeneire <brice@waegenei.re>
10 ;;; Copyright © 2020 Florian Pelz <pelzflorian@pelzflorian.de>
11 ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
12 ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <jannek@gnu.org>
14 ;;; This file is part of GNU Guix.
16 ;;; GNU Guix is free software; you can redistribute it and/or modify it
17 ;;; under the terms of the GNU General Public License as published by
18 ;;; the Free Software Foundation; either version 3 of the License, or (at
19 ;;; your option) any later version.
21 ;;; GNU Guix is distributed in the hope that it will be useful, but
22 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;;; GNU General Public License for more details.
26 ;;; You should have received a copy of the GNU General Public License
27 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
29 (define-module (gnu system)
30 #:use-module (guix inferior)
31 #:use-module (guix store)
32 #:use-module (guix monads)
33 #:use-module (guix gexp)
34 #:use-module (guix records)
35 #:use-module (guix packages)
36 #:use-module (guix derivations)
37 #:use-module (guix profiles)
38 #:use-module (guix ui)
39 #:use-module (guix utils)
40 #:use-module (gnu packages base)
41 #:use-module (gnu packages bash)
42 #:use-module (gnu packages cross-base)
43 #:use-module (gnu packages guile)
44 #:use-module (gnu packages guile-xyz)
45 #:use-module (gnu packages admin)
46 #:use-module (gnu packages hurd)
47 #:use-module (gnu packages linux)
48 #:use-module (gnu packages pciutils)
49 #:use-module (gnu packages package-management)
50 #:use-module (gnu packages less)
51 #:use-module (gnu packages zile)
52 #:use-module (gnu packages nano)
53 #:use-module (gnu packages gawk)
54 #:use-module (gnu packages man)
55 #:use-module (gnu packages texinfo)
56 #:use-module (gnu packages compression)
57 #:use-module (gnu packages firmware)
58 #:use-module (gnu services)
59 #:use-module (gnu services shepherd)
60 #:use-module (gnu services base)
61 #:use-module (gnu bootloader)
62 #:use-module (gnu system shadow)
63 #:use-module (gnu system nss)
64 #:use-module (gnu system locale)
65 #:use-module (gnu system pam)
66 #:use-module (gnu system linux-initrd)
67 #:use-module (gnu system uuid)
68 #:use-module (gnu system file-systems)
69 #:use-module (gnu system mapped-devices)
70 #:use-module (ice-9 match)
71 #:use-module (srfi srfi-1)
72 #:use-module (srfi srfi-26)
73 #:use-module (srfi srfi-34)
74 #:use-module (srfi srfi-35)
75 #:use-module (rnrs bytevectors)
76 #:export (operating-system
80 operating-system-bootloader
81 operating-system-services
82 operating-system-essential-services
83 operating-system-default-essential-services
84 operating-system-user-services
85 operating-system-packages
86 operating-system-host-name
87 operating-system-hosts-file
89 operating-system-kernel
90 operating-system-kernel-file
91 operating-system-kernel-arguments
92 operating-system-label
93 operating-system-default-label
94 operating-system-initrd-modules
95 operating-system-initrd
96 operating-system-users
97 operating-system-groups
98 operating-system-issue
99 operating-system-timezone
100 operating-system-locale
101 operating-system-locale-definitions
102 operating-system-locale-libcs
103 operating-system-mapped-devices
104 operating-system-file-systems
105 operating-system-store-file-system
106 operating-system-user-mapped-devices
107 operating-system-boot-mapped-devices
108 operating-system-activation-script
109 operating-system-user-accounts
110 operating-system-shepherd-service-names
111 operating-system-user-kernel-arguments
112 operating-system-firmware
113 operating-system-keyboard-layout
114 operating-system-name-service-switch
115 operating-system-pam-services
116 operating-system-setuid-programs
117 operating-system-skeletons
118 operating-system-sudoers-file
119 operating-system-swap-devices
120 operating-system-kernel-loadable-modules
121 operating-system-location
123 operating-system-derivation
124 operating-system-profile
125 operating-system-bootcfg
126 operating-system-etc-directory
127 operating-system-locale-directory
128 operating-system-boot-script
129 operating-system-uuid
131 system-linux-image-file-name
132 operating-system-with-gc-roots
133 operating-system-with-provenance
135 hurd-default-essential-services
139 boot-parameters-label
140 boot-parameters-root-device
141 boot-parameters-bootloader-name
142 boot-parameters-bootloader-menu-entries
143 boot-parameters-store-device
144 boot-parameters-store-mount-point
145 boot-parameters-kernel
146 boot-parameters-kernel-arguments
147 boot-parameters-initrd
148 boot-parameters-multiboot-modules
150 read-boot-parameters-file
151 boot-parameters->menu-entry
156 %sudoers-specification
158 %base-packages-interactive
160 %base-packages-networking
163 %default-kernel-arguments))
167 ;;; This module supports whole-system configuration.
171 (define (bootable-kernel-arguments system root-device)
172 "Return a list of kernel arguments (gexps) to boot SYSTEM from ROOT-DEVICE."
173 (list (string-append "--root="
174 ;; Note: Always use the DCE format because that's what
175 ;; (gnu build linux-boot) expects for the '--root'
176 ;; kernel command-line option.
177 (file-system-device->string root-device
179 #~(string-append "--system=" #$system)
180 #~(string-append "--load=" #$system "/boot")))
182 ;; System-wide configuration.
183 ;; TODO: Add per-field docstrings/stexi.
184 (define-record-type* <operating-system> operating-system
185 make-operating-system
187 this-operating-system
189 (kernel operating-system-kernel ; package
190 (default linux-libre))
191 (kernel-loadable-modules operating-system-kernel-loadable-modules
192 (default '())) ; list of packages
193 (kernel-arguments operating-system-user-kernel-arguments
194 (default %default-kernel-arguments)) ; list of gexps/strings
195 (hurd operating-system-hurd
196 (default #f)) ; package
197 (bootloader operating-system-bootloader) ; <bootloader-configuration>
198 (label operating-system-label ; string
200 (default (operating-system-default-label this-operating-system)))
202 (keyboard-layout operating-system-keyboard-layout ;#f | <keyboard-layout>
204 (initrd operating-system-initrd ; (list fs) -> file-like
205 (default base-initrd))
206 (initrd-modules operating-system-initrd-modules ; list of strings
207 (thunked) ; it's system-dependent
208 (default %base-initrd-modules))
210 (firmware operating-system-firmware ; list of packages
211 (default %base-firmware))
213 (host-name operating-system-host-name) ; string
214 (hosts-file operating-system-hosts-file ; file-like | #f
217 (mapped-devices operating-system-mapped-devices ; list of <mapped-device>
219 (file-systems operating-system-file-systems) ; list of fs
220 (swap-devices operating-system-swap-devices ; list of strings
223 (users operating-system-users ; list of user accounts
224 (default %base-user-accounts))
225 (groups operating-system-groups ; list of user groups
226 (default %base-groups))
228 (skeletons operating-system-skeletons ; list of name/file-like value
229 (default (default-skeletons)))
230 (issue operating-system-issue ; string
231 (default %default-issue))
233 (packages operating-system-packages ; list of (PACKAGE OUTPUT...)
234 (default %base-packages)) ; or just PACKAGE
236 (timezone operating-system-timezone) ; string
237 (locale operating-system-locale ; string
238 (default "en_US.utf8"))
239 (locale-definitions operating-system-locale-definitions ; list of <locale-definition>
240 (default %default-locale-definitions))
241 (locale-libcs operating-system-locale-libcs ; list of <packages>
242 (default %default-locale-libcs))
243 (name-service-switch operating-system-name-service-switch ; <name-service-switch>
244 (default %default-nss))
246 (essential-services operating-system-essential-services ; list of services
248 (default (operating-system-default-essential-services
249 this-operating-system)))
250 (services operating-system-user-services ; list of services
251 (default %base-services))
253 (pam-services operating-system-pam-services ; list of PAM services
254 (default (base-pam-services)))
255 (setuid-programs operating-system-setuid-programs
256 (default %setuid-programs)) ; list of string-valued gexps
258 (sudoers-file operating-system-sudoers-file ; file-like
259 (default %sudoers-specification))
261 (location operating-system-location ; <location>
262 (default (and=> (current-source-location)
263 source-properties->location))
266 (define (operating-system-kernel-arguments os root-device)
267 "Return all the kernel arguments, including the ones not specified
268 directly by the user."
269 (append (bootable-kernel-arguments os root-device)
270 (operating-system-user-kernel-arguments os)))
277 (define-record-type* <boot-parameters>
278 boot-parameters make-boot-parameters boot-parameters?
279 (label boot-parameters-label)
280 ;; Because we will use the 'store-device' to create the GRUB search command,
281 ;; the 'store-device' has slightly different semantics than 'root-device'.
282 ;; The 'store-device' can be a file system uuid, a file system label, or #f,
283 ;; but it cannot be a device path such as "/dev/sda3", since GRUB would not
284 ;; understand that. The 'root-device', on the other hand, corresponds
285 ;; exactly to the device field of the <file-system> object representing the
286 ;; OS's root file system, so it might be a device path like "/dev/sda3".
287 (root-device boot-parameters-root-device)
288 (bootloader-name boot-parameters-bootloader-name)
289 (bootloader-menu-entries ;list of <menu-entry>
290 boot-parameters-bootloader-menu-entries)
291 (store-device boot-parameters-store-device)
292 (store-mount-point boot-parameters-store-mount-point)
293 (kernel boot-parameters-kernel)
294 (kernel-arguments boot-parameters-kernel-arguments)
295 (initrd boot-parameters-initrd)
296 (multiboot-modules boot-parameters-multiboot-modules))
298 (define (ensure-not-/dev device)
299 "If DEVICE starts with a slash, return #f. This is meant to filter out
300 Linux device names such as /dev/sda, and to preserve GRUB device names and
302 (if (and (string? device) (string-prefix? "/" device))
306 (define (read-boot-parameters port)
307 "Read boot parameters from PORT and return the corresponding
308 <boot-parameters> object or #f if the format is unrecognized."
309 (define device-sexp->device
311 (('uuid (? symbol? type) (? bytevector? bv))
312 (bytevector->uuid bv type))
313 (('file-system-label (? string? label))
314 (file-system-label label))
315 ((? bytevector? bv) ;old format
316 (bytevector->uuid bv 'dce))
318 ;; It used to be that we would not distinguish between labels and
319 ;; device names. Try to infer the right thing here.
320 (if (string-prefix? "/dev/" device)
322 (file-system-label device)))))
325 (('boot-parameters ('version 0)
326 ('label label) ('root-device root)
331 (root-device (device-sexp->device root))
334 (match (assq 'bootloader-name rest)
336 (#f 'grub))) ; for compatibility reasons.
338 (bootloader-menu-entries
339 (match (assq 'bootloader-menu-entries rest)
340 ((_ entries) (map sexp->menu-entry entries))
343 ;; In the past, we would store the directory name of linux instead of
344 ;; the absolute file name of its image. Detect that and correct it.
345 (kernel (if (string=? kernel (direct-store-path kernel))
346 (string-append kernel "/"
347 (system-linux-image-file-name))
351 (match (assq 'kernel-arguments rest)
353 (#f '()))) ;the old format
356 (match (assq 'initrd rest)
357 (('initrd ('string-append directory file)) ;the old format
358 (string-append directory file))
359 (('initrd (? string? file))
364 (match (assq 'multiboot-modules rest)
369 ;; Linux device names like "/dev/sda1" are not suitable GRUB device
370 ;; identifiers, so we just filter them out.
372 (match (assq 'store rest)
373 (('store ('device #f) _ ...)
375 (('store ('device device) _ ...)
376 (device-sexp->device device))
381 (match (assq 'store rest)
382 (('store ('device _) ('mount-point mount-point) _ ...)
386 (x ;unsupported format
387 (warning (G_ "unrecognized boot parameters at '~a'~%")
388 (port-filename port))
391 (define (read-boot-parameters-file system)
392 "Read boot parameters from SYSTEM's (system or generation) \"parameters\"
393 file and returns the corresponding <boot-parameters> object or #f if the
394 format is unrecognized.
395 The object has its kernel-arguments extended in order to make it bootable."
396 (let* ((file (string-append system "/parameters"))
397 (params (call-with-input-file file read-boot-parameters))
398 (root (boot-parameters-root-device params)))
401 (kernel-arguments (append (bootable-kernel-arguments system root)
402 (boot-parameters-kernel-arguments params))))))
404 (define (boot-parameters->menu-entry conf)
405 (let* ((kernel (boot-parameters-kernel conf))
406 (multiboot-modules (boot-parameters-multiboot-modules conf))
407 (multiboot? (pair? multiboot-modules)))
409 (label (boot-parameters-label conf))
410 (device (boot-parameters-store-device conf))
411 (device-mount-point (boot-parameters-store-mount-point conf))
412 (linux (and (not multiboot?) kernel))
413 (linux-arguments (if (not multiboot?)
414 (boot-parameters-kernel-arguments conf)
416 (initrd (boot-parameters-initrd conf))
417 (multiboot-kernel (and multiboot? kernel))
418 (multiboot-arguments (if multiboot?
419 (boot-parameters-kernel-arguments conf)
421 (multiboot-modules (if multiboot?
422 (boot-parameters-multiboot-modules conf)
430 (define (non-boot-file-system-service os)
431 "Return the file system service for the file systems of OS that are not
432 marked as 'needed-for-boot'."
434 (remove file-system-needed-for-boot?
435 (operating-system-file-systems os)))
437 (define mapped-devices-for-boot
438 (operating-system-boot-mapped-devices os))
440 (define (device-mappings fs)
441 (let ((device (file-system-device fs)))
442 (if (string? device) ;title is 'device
444 (string=? (string-append "/dev/mapper/"
445 (mapped-device-target md))
447 (operating-system-mapped-devices os))
450 (define (add-dependencies fs)
451 ;; Add the dependencies due to device mappings to FS.
456 (remove (cut member <> mapped-devices-for-boot)
457 (append (device-mappings fs)
458 (file-system-dependencies fs)))
461 (service file-system-service-type
462 (map add-dependencies file-systems)))
464 (define (mapped-device-users device file-systems)
465 "Return the subset of FILE-SYSTEMS that use DEVICE."
466 (let ((target (string-append "/dev/mapper/" (mapped-device-target device))))
468 (or (member device (file-system-dependencies fs))
469 (and (string? (file-system-device fs))
470 (string=? (file-system-device fs) target))))
473 (define (operating-system-user-mapped-devices os)
474 "Return the subset of mapped devices that can be installed in
475 user-land--i.e., those not needed during boot."
476 (let ((devices (operating-system-mapped-devices os))
477 (file-systems (operating-system-file-systems os)))
479 (let ((users (mapped-device-users md file-systems)))
480 (not (any file-system-needed-for-boot? users))))
483 (define (operating-system-boot-mapped-devices os)
484 "Return the subset of mapped devices that must be installed during boot,
486 (let ((devices (operating-system-mapped-devices os))
487 (file-systems (operating-system-file-systems os)))
489 (let ((users (mapped-device-users md file-systems)))
490 (any file-system-needed-for-boot? users)))
493 (define (device-mapping-services os)
494 "Return the list of device-mapping services for OS as a list."
495 (map device-mapping-service
496 (operating-system-user-mapped-devices os)))
498 (define (swap-services os)
499 "Return the list of swap services for OS."
500 (map swap-service (operating-system-swap-devices os)))
502 (define* (system-linux-image-file-name #:optional
503 (target (or (%current-target-system)
505 "Return the basename of the kernel image file for TARGET."
507 ((string-prefix? "arm" target) "zImage")
508 ((string-prefix? "mips" target) "vmlinuz")
509 ((string-prefix? "aarch64" target) "Image")
512 (define (operating-system-kernel-file os)
513 "Return an object representing the absolute file name of the kernel image of
515 (if (operating-system-hurd os)
516 (file-append (operating-system-kernel os) "/boot/gnumach")
517 (file-append (operating-system-kernel os)
518 "/" (system-linux-image-file-name))))
520 (define (package-for-kernel target-kernel module-package)
521 "Return a package like MODULE-PACKAGE, adapted for TARGET-KERNEL, if
522 possible (that is if there's a LINUX keyword argument in the build system)."
524 (inherit module-package)
526 (substitute-keyword-arguments (package-arguments module-package)
530 (define %default-modprobe-blacklist
531 ;; List of kernel modules to blacklist by default.
532 '("usbmouse" ;races with bcm5974, see <https://bugs.gnu.org/35574>
533 "usbkbd")) ;races with usbhid, see <https://issues.guix.gnu.org/35574#18>
535 (define %default-kernel-arguments
536 ;; Default arguments passed to the kernel.
537 (list (string-append "modprobe.blacklist="
538 (string-join %default-modprobe-blacklist ","))
541 (define* (operating-system-directory-base-entries os)
542 "Return the basic entries of the 'system' directory of OS for use as the
543 value of the SYSTEM-SERVICE-TYPE service."
544 (let* ((locale (operating-system-locale-directory os))
545 (kernel (operating-system-kernel os))
546 (hurd (operating-system-hurd os))
547 (modules (operating-system-kernel-loadable-modules os))
551 (content (packages->manifest
553 (map (lambda (module)
554 (if (package? module)
555 (package-for-kernel kernel
559 (hooks (list linux-module-database)))))
560 (initrd (and (not hurd) (operating-system-initrd-file os)))
561 (params (operating-system-boot-parameters-file os)))
563 ,@(if hurd `(("hurd" ,hurd)) '())
564 ("parameters" ,params)
565 ,@(if initrd `(("initrd" ,initrd)) '())
566 ("locale" ,locale)))) ;used by libc
568 (define (operating-system-default-essential-services os)
569 "Return the list of essential services for OS. These are special services
570 that implement part of what's declared in OS are responsible for low-level
573 (map file-system-mount-point (operating-system-file-systems os)))
575 (let* ((mappings (device-mapping-services os))
576 (root-fs (root-file-system-service))
577 (other-fs (non-boot-file-system-service os))
578 (swaps (swap-services os))
579 (procs (service user-processes-service-type))
580 (host-name (host-name-service (operating-system-host-name os)))
581 (entries (operating-system-directory-base-entries os)))
582 (cons* (service system-service-type entries)
585 ;; %SHEPHERD-ROOT-SERVICE must come last so that the gexp that
586 ;; execs shepherd comes last in the boot script (XXX). Likewise,
587 ;; the cleanup service must come first so that its gexp runs before
589 (service cleanup-service-type #f)
591 %shepherd-root-service
593 (pam-root-service (operating-system-pam-services os))
594 (account-service (append (operating-system-accounts os)
595 (operating-system-groups os))
596 (operating-system-skeletons os))
597 (operating-system-etc-service os)
598 (service fstab-service-type
599 (filter file-system-needed-for-boot?
600 (operating-system-file-systems os)))
601 (session-environment-service
602 (operating-system-environment-variables os))
603 host-name procs root-fs
604 (service setuid-program-service-type
605 (operating-system-setuid-programs os))
606 (service profile-service-type
607 (operating-system-packages os))
609 (append mappings swaps
611 ;; Add the firmware service.
612 (list %linux-bare-metal-service
613 (service firmware-service-type
614 (operating-system-firmware os)))))))
616 (define (hurd-default-essential-services os)
617 (let ((entries (operating-system-directory-base-entries os)))
618 (list (service system-service-type entries)
620 %hurd-startup-service
622 %shepherd-root-service
623 (service user-processes-service-type)
624 (account-service (append (operating-system-accounts os)
625 (operating-system-groups os))
626 (operating-system-skeletons os))
627 (root-file-system-service)
628 (service file-system-service-type '())
629 (service fstab-service-type
630 (filter file-system-needed-for-boot?
631 (operating-system-file-systems os)))
632 (pam-root-service (operating-system-pam-services os))
633 (operating-system-etc-service os)
634 (service profile-service-type (operating-system-packages os)))))
636 (define* (operating-system-services os)
637 "Return all the services of OS, including \"essential\" services."
638 (instantiate-missing-services
639 (append (operating-system-user-services os)
640 (operating-system-essential-services os))))
642 (define (operating-system-with-gc-roots os roots)
643 "Return a variant of OS where ROOTS are registered as GC roots."
647 ;; We use this procedure for the installation OS, which already defines GC
648 ;; roots. Add ROOTS to those.
649 (services (cons (simple-service 'extra-root
650 gc-root-service-type roots)
651 (operating-system-user-services os)))))
653 (define* (operating-system-with-provenance os #:optional config-file)
654 "Return a variant of OS that stores its own provenance information,
655 including CONFIG-FILE, if available. This is achieved by adding an instance
656 of PROVENANCE-SERVICE-TYPE to its services."
659 (services (cons (service provenance-service-type config-file)
660 (operating-system-user-services os)))))
667 (define %base-firmware
668 ;; Firmware usable by default.
669 (list ath9k-htc-firmware
672 (define %base-packages-utils
673 ;; Default set of utilities packages.
674 (cons* procps psmisc which
675 (@ (gnu packages admin) shadow) ;for 'passwd'
679 ;; The packages below are also in %FINAL-INPUTS, so take them from
680 ;; there to avoid duplication.
681 (list bash coreutils findutils grep sed
682 diffutils patch gawk tar gzip bzip2 xz lzip)))
684 (define %base-packages-linux
685 ;; Default set of linux specific packages.
686 (list pciutils usbutils
688 ;; Get 'insmod' & co. from kmod, not module-init-tools, since udev
689 ;; already depends on it anyway.
692 (define %base-packages-interactive
693 ;; Default set of common interactive packages.
696 info-reader ;the standalone Info reader (no Perl)
699 ;; The 'sudo' command is already in %SETUID-PROGRAMS, but we also
700 ;; want the other commands and the man pages (notably because
701 ;; auto-completion in Emacs shell relies on man pages.)
703 guile-readline guile-colorized))
705 (define %base-packages-networking
706 ;; Default set of networking packages.
707 (list inetutils isc-dhcp
709 ;; wireless-tools is deprecated in favor of iw, but it's still what
710 ;; many people are familiar with, so keep it around.
713 (define %base-packages
714 ;; Default set of packages globally visible. It should include anything
715 ;; required for basic administrator tasks.
716 (append (list e2fsprogs)
717 %base-packages-interactive
719 %base-packages-networking
720 %base-packages-utils))
722 (define %default-issue
723 ;; Default contents for /etc/issue.
725 This is the GNU system. Welcome.\n")
727 (define (local-host-aliases host-name)
728 "Return aliases for HOST-NAME, to be used in /etc/hosts."
729 (string-append "127.0.0.1 localhost " host-name "\n"
730 "::1 localhost " host-name "\n"))
732 (define (default-/etc/hosts host-name)
733 "Return the default /etc/hosts file."
734 (plain-file "hosts" (local-host-aliases host-name)))
736 (define* (operating-system-etc-service os)
737 "Return a <service> that builds containing the static part of the /etc
740 (plain-file "login.defs"
742 "# Default paths for non-login shells started by su(1).\n"
743 "ENV_PATH /run/setuid-programs:"
744 "/run/current-system/profile/bin:"
745 "/run/current-system/profile/sbin\n"
746 "ENV_SUPATH /run/setuid-programs:"
747 "/run/current-system/profile/bin:"
748 "/run/current-system/profile/sbin\n")))
750 (hurd (operating-system-hurd os))
751 (issue (plain-file "issue" (operating-system-issue os)))
752 (nsswitch (operating-system-name-service-switch os))
753 (nsswitch (and nsswitch
754 (plain-file "nsswitch.conf"
755 (name-service-switch->string nsswitch))))
756 (sudoers (operating-system-sudoers-file os))
758 ;; Startup file for POSIX-compliant login shells, which set system-wide
759 ;; environment variables.
760 (profile (mixed-text-file "profile" "\
761 # Crucial variables that could be missing in the profiles' 'etc/profile'
762 # because they would require combining both profiles.
763 # FIXME: See <http://bugs.gnu.org/20255>.
764 export MANPATH=$HOME/.guix-profile/share/man:/run/current-system/profile/share/man
765 export INFOPATH=$HOME/.guix-profile/share/info:/run/current-system/profile/share/info
766 export XDG_DATA_DIRS=$HOME/.guix-profile/share:/run/current-system/profile/share
767 export XDG_CONFIG_DIRS=$HOME/.guix-profile/etc/xdg:/run/current-system/profile/etc/xdg
769 # Make sure libXcursor finds cursors installed into user or system profiles. See <http://bugs.gnu.org/24445>
770 export XCURSOR_PATH=$HOME/.icons:$HOME/.guix-profile/share/icons:/run/current-system/profile/share/icons
772 # Ignore the default value of 'PATH'.
775 # Load the system profile's settings.
776 GUIX_PROFILE=/run/current-system/profile ; \\
777 . /run/current-system/profile/etc/profile
779 # Since 'lshd' does not use pam_env, /etc/environment must be explicitly
780 # loaded when someone logs in via SSH. See <http://bugs.gnu.org/22175>.
781 # We need 'PATH' to be defined here, for 'cat' and 'cut'. Do this before
782 # reading the user's 'etc/profile' to allow variables to be overridden.
783 if [ -f /etc/environment -a -n \"$SSH_CLIENT\" \\
784 -a -z \"$LINUX_MODULE_DIRECTORY\" ]
787 export `cat /etc/environment | cut -d= -f1`
790 # Arrange so that ~/.config/guix/current comes first.
791 for profile in \"$HOME/.guix-profile\" \"$HOME/.config/guix/current\"
793 if [ -f \"$profile/etc/profile\" ]
795 # Load the user profile's settings.
796 GUIX_PROFILE=\"$profile\" ; \\
797 . \"$profile/etc/profile\"
799 # At least define this one so that basic things just work
800 # when the user installs their first package.
801 export PATH=\"$profile/bin:$PATH\"
805 # Prepend setuid programs.
806 export PATH=/run/setuid-programs:$PATH
808 # Arrange so that ~/.config/guix/current/share/info comes first.
809 export INFOPATH=\"$HOME/.config/guix/current/share/info:$INFOPATH\"
811 # Set the umask, notably for users logging in via 'lsh'.
812 # See <http://bugs.gnu.org/22650>.
815 # Allow Hunspell-based applications (IceCat, LibreOffice, etc.) to
817 export DICPATH=\"$HOME/.guix-profile/share/hunspell:/run/current-system/profile/share/hunspell\"
819 # Allow GStreamer-based applications to find plugins.
820 export GST_PLUGIN_PATH=\"$HOME/.guix-profile/lib/gstreamer-1.0\"
822 if [ -n \"$BASH_VERSION\" -a -f /etc/bashrc ]
824 # Load Bash-specific initialization code.
829 (bashrc (plain-file "bashrc" "\
830 # Bash-specific initialization.
832 # The 'bash-completion' package.
833 if [ -f /run/current-system/profile/etc/profile.d/bash_completion.sh ]
835 # Bash-completion sources ~/.bash_completion. It installs a dynamic
836 # completion loader that searches its own completion files as well
837 # as those in ~/.guix-profile and /run/current-system/profile.
838 source /run/current-system/profile/etc/profile.d/bash_completion.sh
841 `(("services" ,(file-append net-base "/etc/services"))
842 ("protocols" ,(file-append net-base "/etc/protocols"))
843 ("rpc" ,(file-append net-base "/etc/rpc"))
844 ("login.defs" ,#~#$login.defs)
846 ,@(if nsswitch `(("nsswitch.conf" ,#~#$nsswitch)) '())
847 ("profile" ,#~#$profile)
848 ("bashrc" ,#~#$bashrc)
849 ("hosts" ,#~#$(or (operating-system-hosts-file os)
850 (default-/etc/hosts (operating-system-host-name os))))
851 ;; Write the operating-system-host-name to /etc/hostname to prevent
852 ;; NetworkManager from changing the system's hostname when connecting
853 ;; to certain networks. Some discussion at
854 ;; https://lists.gnu.org/archive/html/help-guix/2017-09/msg00037.html
855 ("hostname" ,(plain-file "hostname" (operating-system-host-name os)))
856 ;; Some programs (e.g., GLib) look at /etc/timezone to find the
857 ;; name of the current timezone. For details, see
858 ;; https://lists.gnu.org/archive/html/guix-devel/2019-07/msg00166.html
859 ("timezone" ,(plain-file "timezone" (operating-system-timezone os)))
860 ("localtime" ,(file-append tzdata "/share/zoneinfo/"
861 (operating-system-timezone os)))
862 ,@(if sudoers `(("sudoers" ,sudoers)) '())
864 `(("login" ,(file-append hurd "/etc/login"))
865 ("motd" ,(file-append hurd "/etc/motd"))
866 ("ttys" ,(file-append hurd "/etc/ttys")))
869 (define %root-account
870 ;; Default root account.
874 (uid 0) (group "root")
875 (comment "System administrator")
876 (home-directory "/root")))
878 (define (operating-system-accounts os)
879 "Return the user accounts for OS, including an obligatory 'root' account,
880 and excluding accounts requested by services."
881 ;; Make sure there's a root account.
882 (if (find (lambda (user)
883 (and=> (user-account-uid user) zero?))
884 (operating-system-users os))
885 (operating-system-users os)
886 (cons %root-account (operating-system-users os))))
888 (define (maybe-string->file file-name thing)
889 "If THING is a string, return a <plain-file> with THING as its content.
890 Otherwise just return THING.
892 This is for backward-compatibility of fields that used to be strings and are
893 now file-like objects.."
896 (warning (G_ "using a string for file '~a' is deprecated; \
897 use 'plain-file' instead~%")
899 (plain-file file-name thing))
903 (define (maybe-file->monadic file-name thing)
904 "If THING is a value in %STORE-MONAD, return it as is; otherwise return
905 THING in the %STORE-MONAD.
907 This is for backward-compatibility of fields that used to be monadic values
908 and are now file-like objects."
909 (with-monad %store-monad
912 (warning (G_ "using a monadic value for '~a' is deprecated; \
913 use 'plain-file' instead~%")
919 (define (operating-system-etc-directory os)
920 "Return that static part of the /etc directory of OS."
922 (fold-services (operating-system-services os)
923 #:target-type etc-service-type)))
925 (define (operating-system-environment-variables os)
926 "Return the environment variables of OS for
927 @var{session-environment-service-type}, to be used in @file{/etc/environment}."
928 `(("LANG" . ,(operating-system-locale os))
929 ;; Note: No need to set 'TZ' since (1) we provide /etc/localtime, and (2)
930 ;; it doesn't work for setuid binaries. See <https://bugs.gnu.org/29212>.
931 ("TZDIR" . ,(file-append tzdata "/share/zoneinfo"))
932 ;; Tell 'modprobe' & co. where to look for modules.
933 ("LINUX_MODULE_DIRECTORY" . "/run/booted-system/kernel/lib/modules")
934 ;; These variables are honored by OpenSSL (libssl) and Git.
935 ("SSL_CERT_DIR" . "/etc/ssl/certs")
936 ("SSL_CERT_FILE" . "/etc/ssl/certs/ca-certificates.crt")
937 ("GIT_SSL_CAINFO" . "/etc/ssl/certs/ca-certificates.crt")
939 ;; 'GTK_DATA_PREFIX' must name one directory where GTK+ themes are
941 ("GTK_DATA_PREFIX" . "/run/current-system/profile")
943 ;; By default, applications that use D-Bus, such as Emacs, abort at startup
944 ;; when /etc/machine-id is missing. Make sure these warnings are non-fatal.
945 ("DBUS_FATAL_WARNINGS" . "0")
947 ;; XXX: Normally we wouldn't need to do this, but our glibc@2.23 package
948 ;; used to look things up in 'PREFIX/lib/locale' instead of
949 ;; '/run/current-system/locale' as was intended. Keep this hack around so
950 ;; that people who still have glibc@2.23-using packages in their profiles
951 ;; can use them correctly.
952 ;; TODO: Remove when glibc@2.23 is long gone.
953 ("GUIX_LOCPATH" . "/run/current-system/locale")))
955 (define %setuid-programs
956 ;; Default set of setuid-root programs.
957 (let ((shadow (@ (gnu packages admin) shadow)))
958 (list (file-append shadow "/bin/passwd")
959 (file-append shadow "/bin/sg")
960 (file-append shadow "/bin/su")
961 (file-append shadow "/bin/newgrp")
962 (file-append shadow "/bin/newuidmap")
963 (file-append shadow "/bin/newgidmap")
964 (file-append inetutils "/bin/ping")
965 (file-append inetutils "/bin/ping6")
966 (file-append sudo "/bin/sudo")
967 (file-append sudo "/bin/sudoedit")
968 (file-append fuse "/bin/fusermount")
970 ;; To allow mounts with the "user" option, "mount" and "umount" must
972 (file-append util-linux "/bin/mount")
973 (file-append util-linux "/bin/umount"))))
975 (define %sudoers-specification
976 ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel'
977 ;; group can do anything. See
978 ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>.
979 ;; TODO: Add a declarative API.
980 (plain-file "sudoers" "\
982 %wheel ALL=(ALL) ALL\n"))
984 (define* (operating-system-activation-script os)
985 "Return the activation script for OS---i.e., the code that \"activates\" the
986 stateful part of OS, including user accounts and groups, special directories,
988 (let* ((services (operating-system-services os))
989 (activation (fold-services services
990 #:target-type activation-service-type)))
991 (activation-service->script activation)))
993 (define* (operating-system-boot-script os)
994 "Return the boot script for OS---i.e., the code started by the initrd once
995 we're running in the final root."
996 (let* ((services (operating-system-services os))
997 (boot (fold-services services #:target-type boot-service-type)))
998 (service-value boot)))
1000 (define (operating-system-user-accounts os)
1001 "Return the list of user accounts of OS."
1002 (let* ((services (operating-system-services os))
1003 (account (fold-services services
1004 #:target-type account-service-type)))
1005 (filter user-account?
1006 (service-value account))))
1008 (define (operating-system-shepherd-service-names os)
1009 "Return the list of Shepherd service names for OS."
1010 (append-map shepherd-service-provision
1012 (fold-services (operating-system-services os)
1014 shepherd-root-service-type))))
1016 (define* (operating-system-derivation os)
1017 "Return a derivation that builds OS."
1018 (let* ((services (operating-system-services os))
1019 (system (fold-services services)))
1020 ;; SYSTEM contains the derivation as a monadic value.
1021 (service-value system)))
1023 (define* (operating-system-profile os)
1024 "Return a derivation that builds the system profile of OS."
1026 ((services -> (operating-system-services os))
1027 (profile (fold-services services
1028 #:target-type profile-service-type)))
1030 (("profile" profile)
1031 (return profile)))))
1033 (define (operating-system-root-file-system os)
1034 "Return the root file system of OS."
1035 (or (find (lambda (fs)
1036 (string=? "/" (file-system-mount-point fs)))
1037 (operating-system-file-systems os))
1039 (&message (message "missing root file system"))
1041 (location (operating-system-location os)))))))
1043 (define (operating-system-initrd-file os)
1044 "Return a gexp denoting the initrd file of OS."
1045 (define boot-file-systems
1046 (filter file-system-needed-for-boot?
1047 (operating-system-file-systems os)))
1049 (define mapped-devices
1050 (operating-system-boot-mapped-devices os))
1053 (operating-system-initrd os))
1055 (make-initrd boot-file-systems
1056 #:linux (operating-system-kernel os)
1058 (operating-system-initrd-modules os)
1059 #:mapped-devices mapped-devices
1060 #:keyboard-layout (operating-system-keyboard-layout os)))
1062 (define* (operating-system-uuid os #:optional (type 'dce))
1063 "Compute UUID object with a deterministic \"UUID\" for OS, of the given
1064 TYPE (one of 'iso9660 or 'dce). Return a UUID object."
1065 ;; Note: For this to be deterministic, we must not hash things that contains
1066 ;; (directly or indirectly) procedures, for example. That rules out
1067 ;; anything that contains gexps, thunk or delayed record fields, etc.
1069 (define service-name
1070 (compose service-type-name service-kind))
1072 (define (file-system-digest fs)
1073 ;; Return a hashable digest that does not contain 'dependencies' since
1074 ;; this field can contain procedures.
1075 (let ((device (file-system-device fs)))
1076 (list (file-system-mount-point fs)
1077 (file-system-type fs)
1078 (file-system-device->string device)
1079 (file-system-options fs))))
1081 (if (eq? type 'iso9660)
1082 (let ((pad (compose (cut string-pad <> 2 #\0)
1084 (h (hash (map service-name (operating-system-services os))
1087 (string->iso9660-uuid
1088 (string-append "1970-01-01-"
1089 (pad (hash (operating-system-host-name os) 24)) "-"
1090 (pad (quotient h 60)) "-"
1091 (pad (modulo h 60)) "-"
1092 (pad (hash (map file-system-digest
1093 (operating-system-file-systems os))
1097 (uint-list->bytevector
1098 (list (hash (map file-system-digest
1099 (operating-system-file-systems os))
1101 (hash (operating-system-host-name os)
1103 (hash (map service-name (operating-system-services os))
1105 (hash (map file-system-digest (operating-system-file-systems os))
1111 (define (locale-name->definition* name)
1112 "Variant of 'locale-name->definition' that raises an error upon failure."
1113 (match (locale-name->definition name)
1117 (message (format #f (G_ "~a: invalid locale name") name))))))
1120 (define (operating-system-locale-directory os)
1121 "Return the directory containing the locales compiled for the definitions
1122 listed in OS. The C library expects to find it under
1123 /run/current-system/locale."
1125 (operating-system-locale os))
1128 ;; While we're at it, check whether NAME is defined and add it if needed.
1129 (if (member name (map locale-definition-name
1130 (operating-system-locale-definitions os)))
1131 (operating-system-locale-definitions os)
1132 (cons (locale-name->definition* name)
1133 (operating-system-locale-definitions os))))
1135 (locale-directory definitions
1136 #:libcs (operating-system-locale-libcs os)))
1138 (define* (kernel->boot-label kernel #:key hurd)
1139 "Return a label for the bootloader menu entry that boots KERNEL."
1140 (cond ((package? hurd)
1141 (string-append "GNU with the "
1142 (string-titlecase (package-name hurd)) " "
1143 (package-version hurd)))
1145 (string-append "GNU with "
1146 (string-titlecase (package-name kernel)) " "
1147 (package-version kernel)))
1148 ((inferior-package? kernel)
1149 (string-append "GNU with "
1150 (string-titlecase (inferior-package-name kernel)) " "
1151 (inferior-package-version kernel)))
1154 (define (operating-system-default-label os)
1155 "Return the default label for OS, as it will appear in the bootloader menu
1157 (kernel->boot-label (operating-system-kernel os)
1158 #:hurd (operating-system-hurd os)))
1160 (define (store-file-system file-systems)
1161 "Return the file system object among FILE-SYSTEMS that contains the store."
1162 (match (filter (lambda (fs)
1163 (and (file-system-mount? fs)
1164 (not (memq 'bind-mount (file-system-flags fs)))
1165 (string-prefix? (file-system-mount-point fs)
1168 ((and candidates (head . tail))
1169 (reduce (lambda (fs1 fs2)
1170 (if (> (string-length (file-system-mount-point fs1))
1171 (string-length (file-system-mount-point fs2)))
1177 (define (operating-system-store-file-system os)
1178 "Return the file system that contains the store of OS."
1179 (store-file-system (operating-system-file-systems os)))
1181 (define* (operating-system-bootcfg os #:optional (old-entries '()))
1182 "Return the bootloader configuration file for OS. Use OLD-ENTRIES,
1183 a list of <menu-entry>, to populate the \"old entries\" menu."
1184 (let* ((file-systems (operating-system-file-systems os))
1185 (root-fs (operating-system-root-file-system os))
1186 (root-device (file-system-device root-fs))
1187 (params (operating-system-boot-parameters
1189 #:system-kernel-arguments? #t))
1190 (entry (boot-parameters->menu-entry params))
1191 (bootloader-conf (operating-system-bootloader os)))
1193 (define generate-config-file
1194 (bootloader-configuration-file-generator
1195 (bootloader-configuration-bootloader bootloader-conf)))
1197 (generate-config-file bootloader-conf (list entry)
1198 #:old-entries old-entries
1199 #:store-directory-prefix
1200 (btrfs-store-subvolume-file-name file-systems))))
1202 (define (operating-system-multiboot-modules os)
1203 (if (operating-system-hurd os) (hurd-multiboot-modules os) '()))
1205 (define (hurd-multiboot-modules os)
1206 (let* ((hurd (operating-system-hurd os))
1207 (root-file-system-command
1208 (list (file-append hurd "/hurd/ext2fs.static")
1210 "--multiboot-command-line='${kernel-command-line}'"
1211 "--host-priv-port='${host-port}'"
1212 "--device-master-port='${device-port}'"
1213 "--exec-server-task='${exec-task}'"
1214 "--store-type=typed"
1215 "--x-xattr-translator-records"
1216 "'${root}'" "'$(task-create)'" "'$(task-resume)'"))
1217 (target (%current-target-system))
1219 (with-parameters ((%current-target-system #f))
1220 ;; TODO: cross-libc has extra patches for the Hurd;
1221 ;; remove in next rebuild cycle
1222 (cross-libc target))
1224 (exec-server-command
1225 (list (file-append libc "/lib/ld.so.1") "exec"
1226 (file-append hurd "/hurd/exec") "'$(exec-task=task-create)'")))
1227 (list root-file-system-command exec-server-command)))
1229 (define* (operating-system-boot-parameters os root-device
1230 #:key system-kernel-arguments?)
1231 "Return a monadic <boot-parameters> record that describes the boot
1232 parameters of OS. When SYSTEM-KERNEL-ARGUMENTS? is true, add kernel arguments
1233 such as '--root' and '--load' to <boot-parameters>."
1234 (let* ((initrd (and (not (operating-system-hurd os))
1235 (operating-system-initrd-file os)))
1236 (store (operating-system-store-file-system os))
1237 (bootloader (bootloader-configuration-bootloader
1238 (operating-system-bootloader os)))
1239 (bootloader-name (bootloader-name bootloader))
1240 (label (operating-system-label os))
1241 (multiboot-modules (operating-system-multiboot-modules os)))
1244 (root-device root-device)
1245 (kernel (operating-system-kernel-file os))
1247 (if system-kernel-arguments?
1248 (operating-system-kernel-arguments os root-device)
1249 (operating-system-user-kernel-arguments os)))
1251 (multiboot-modules multiboot-modules)
1252 (bootloader-name bootloader-name)
1253 (bootloader-menu-entries
1254 (bootloader-configuration-menu-entries (operating-system-bootloader os)))
1255 (store-device (ensure-not-/dev (file-system-device store)))
1256 (store-mount-point (file-system-mount-point store)))))
1258 (define (device->sexp device)
1259 "Serialize DEVICE as an sexp (really, as an object with a read syntax.)"
1262 `(uuid ,(uuid-type uuid) ,(uuid-bytevector uuid)))
1263 ((? file-system-label? label)
1264 `(file-system-label ,(file-system-label->string label)))
1268 (define* (operating-system-boot-parameters-file os
1269 #:key system-kernel-arguments?)
1270 "Return a file that describes the boot parameters of OS. The primary use of
1271 this file is the reconstruction of GRUB menu entries for old configurations.
1273 When SYSTEM-KERNEL-ARGUMENTS? is true, add kernel arguments such as '--root'
1274 and '--load' to the returned file (since the returned file is then usually
1275 stored into the content-addressed \"system\" directory, it's usually not a
1276 good idea to give it because the content hash would change by the content hash
1277 being stored into the \"parameters\" file)."
1278 (let* ((root (operating-system-root-file-system os))
1279 (device (file-system-device root))
1280 (params (operating-system-boot-parameters
1282 #:system-kernel-arguments?
1283 system-kernel-arguments?)))
1284 (scheme-file "parameters"
1287 (label #$(boot-parameters-label params))
1290 (boot-parameters-root-device params)))
1291 (kernel #$(boot-parameters-kernel params))
1293 #$(boot-parameters-kernel-arguments params))
1294 #$@(if (boot-parameters-initrd params)
1295 #~((initrd #$(boot-parameters-initrd params)))
1297 #$@(if (pair? (boot-parameters-multiboot-modules params))
1298 #~((multiboot-modules
1299 #$(boot-parameters-multiboot-modules params)))
1301 (bootloader-name #$(boot-parameters-bootloader-name params))
1302 (bootloader-menu-entries
1303 #$(map menu-entry->sexp
1304 (or (and=> (operating-system-bootloader os)
1305 bootloader-configuration-menu-entries)
1309 #$(device->sexp (boot-parameters-store-device params)))
1310 (mount-point #$(boot-parameters-store-mount-point
1312 #:set-load-path? #f)))
1314 (define-gexp-compiler (operating-system-compiler (os <operating-system>)
1318 ;; XXX: This is not super elegant but we can't pass SYSTEM and TARGET to
1319 ;; 'operating-system-derivation'.
1320 (run-with-store store (operating-system-derivation os)
1322 #:target target)))))
1324 ;;; system.scm ends here