1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018 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>
8 ;;; This file is part of GNU Guix.
10 ;;; GNU Guix is free software; you can redistribute it and/or modify it
11 ;;; under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3 of the License, or (at
13 ;;; your option) any later version.
15 ;;; GNU Guix is distributed in the hope that it will be useful, but
16 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23 (define-module (gnu system)
24 #:use-module (guix store)
25 #:use-module (guix monads)
26 #:use-module (guix gexp)
27 #:use-module (guix records)
28 #:use-module (guix packages)
29 #:use-module (guix derivations)
30 #:use-module (guix profiles)
31 #:use-module (guix ui)
32 #:use-module (gnu packages base)
33 #:use-module (gnu packages bash)
34 #:use-module (gnu packages guile)
35 #:use-module (gnu packages admin)
36 #:use-module (gnu packages linux)
37 #:use-module (gnu packages pciutils)
38 #:use-module (gnu packages package-management)
39 #:use-module (gnu packages less)
40 #:use-module (gnu packages zile)
41 #:use-module (gnu packages nano)
42 #:use-module (gnu packages gawk)
43 #:use-module (gnu packages man)
44 #:use-module (gnu packages texinfo)
45 #:use-module (gnu packages compression)
46 #:use-module (gnu packages firmware)
47 #:use-module (gnu services)
48 #:use-module (gnu services shepherd)
49 #:use-module (gnu services base)
50 #:use-module (gnu bootloader)
51 #:use-module (gnu system shadow)
52 #:use-module (gnu system nss)
53 #:use-module (gnu system locale)
54 #:use-module (gnu system pam)
55 #:use-module (gnu system linux-initrd)
56 #:use-module (gnu system uuid)
57 #:use-module (gnu system file-systems)
58 #:use-module (gnu system mapped-devices)
59 #:use-module (ice-9 match)
60 #:use-module (srfi srfi-1)
61 #:use-module (srfi srfi-26)
62 #:use-module (srfi srfi-34)
63 #:use-module (srfi srfi-35)
64 #:use-module (rnrs bytevectors)
65 #:export (operating-system
68 operating-system-bootloader
69 operating-system-services
70 operating-system-user-services
71 operating-system-packages
72 operating-system-host-name
73 operating-system-hosts-file
74 operating-system-kernel
75 operating-system-kernel-file
76 operating-system-kernel-arguments
77 operating-system-initrd-modules
78 operating-system-initrd
79 operating-system-users
80 operating-system-groups
81 operating-system-issue
82 operating-system-timezone
83 operating-system-locale
84 operating-system-locale-definitions
85 operating-system-locale-libcs
86 operating-system-mapped-devices
87 operating-system-file-systems
88 operating-system-store-file-system
89 operating-system-user-mapped-devices
90 operating-system-boot-mapped-devices
91 operating-system-activation-script
92 operating-system-user-accounts
93 operating-system-shepherd-service-names
94 operating-system-user-kernel-arguments
96 operating-system-derivation
97 operating-system-profile
98 operating-system-bootcfg
99 operating-system-etc-directory
100 operating-system-locale-directory
101 operating-system-boot-script
103 system-linux-image-file-name
107 boot-parameters-label
108 boot-parameters-root-device
109 boot-parameters-bootloader-name
110 boot-parameters-store-device
111 boot-parameters-store-mount-point
112 boot-parameters-kernel
113 boot-parameters-kernel-arguments
114 boot-parameters-initrd
116 read-boot-parameters-file
117 boot-parameters->menu-entry
126 ;;; This module supports whole-system configuration.
130 (define (bootable-kernel-arguments kernel-arguments system.drv root-device)
131 "Prepend extra arguments to KERNEL-ARGUMENTS that allow SYSTEM.DRV to be
132 booted from ROOT-DEVICE"
133 (cons* (string-append "--root="
134 (cond ((uuid? root-device)
136 ;; Note: Always use the DCE format because that's
137 ;; what (gnu build linux-boot) expects for the
138 ;; '--root' kernel command-line option.
139 (uuid->string (uuid-bytevector root-device)
141 ((file-system-label? root-device)
142 (file-system-label->string root-device))
144 #~(string-append "--system=" #$system.drv)
145 #~(string-append "--load=" #$system.drv "/boot")
148 ;; System-wide configuration.
149 ;; TODO: Add per-field docstrings/stexi.
150 (define-record-type* <operating-system> operating-system
151 make-operating-system
153 (kernel operating-system-kernel ; package
154 (default linux-libre))
155 (kernel-arguments operating-system-user-kernel-arguments
156 (default '())) ; list of gexps/strings
157 (bootloader operating-system-bootloader) ; <bootloader-configuration>
159 (initrd operating-system-initrd ; (list fs) -> M derivation
160 (default base-initrd))
161 (initrd-modules operating-system-initrd-modules ; list of strings
162 (thunked) ; it's system-dependent
163 (default %base-initrd-modules))
165 (firmware operating-system-firmware ; list of packages
166 (default %base-firmware))
168 (host-name operating-system-host-name) ; string
169 (hosts-file operating-system-hosts-file ; file-like | #f
172 (mapped-devices operating-system-mapped-devices ; list of <mapped-device>
174 (file-systems operating-system-file-systems) ; list of fs
175 (swap-devices operating-system-swap-devices ; list of strings
178 (users operating-system-users ; list of user accounts
179 (default %base-user-accounts))
180 (groups operating-system-groups ; list of user groups
181 (default %base-groups))
183 (skeletons operating-system-skeletons ; list of name/monadic value
184 (default (default-skeletons)))
185 (issue operating-system-issue ; string
186 (default %default-issue))
188 (packages operating-system-packages ; list of (PACKAGE OUTPUT...)
189 (default %base-packages)) ; or just PACKAGE
191 (timezone operating-system-timezone) ; string
192 (locale operating-system-locale ; string
193 (default "en_US.utf8"))
194 (locale-definitions operating-system-locale-definitions ; list of <locale-definition>
195 (default %default-locale-definitions))
196 (locale-libcs operating-system-locale-libcs ; list of <packages>
197 (default %default-locale-libcs))
198 (name-service-switch operating-system-name-service-switch ; <name-service-switch>
199 (default %default-nss))
201 (services operating-system-user-services ; list of monadic services
202 (default %base-services))
204 (pam-services operating-system-pam-services ; list of PAM services
205 (default (base-pam-services)))
206 (setuid-programs operating-system-setuid-programs
207 (default %setuid-programs)) ; list of string-valued gexps
209 (sudoers-file operating-system-sudoers-file ; file-like
210 (default %sudoers-specification)))
212 (define (operating-system-kernel-arguments os system.drv root-device)
213 "Return all the kernel arguments, including the ones not specified
214 directly by the user."
215 (bootable-kernel-arguments (operating-system-user-kernel-arguments os)
224 (define-record-type* <boot-parameters>
225 boot-parameters make-boot-parameters boot-parameters?
226 (label boot-parameters-label)
227 ;; Because we will use the 'store-device' to create the GRUB search command,
228 ;; the 'store-device' has slightly different semantics than 'root-device'.
229 ;; The 'store-device' can be a file system uuid, a file system label, or #f,
230 ;; but it cannot be a device path such as "/dev/sda3", since GRUB would not
231 ;; understand that. The 'root-device', on the other hand, corresponds
232 ;; exactly to the device field of the <file-system> object representing the
233 ;; OS's root file system, so it might be a device path like "/dev/sda3".
234 (root-device boot-parameters-root-device)
235 (bootloader-name boot-parameters-bootloader-name)
236 (store-device boot-parameters-store-device)
237 (store-mount-point boot-parameters-store-mount-point)
238 (kernel boot-parameters-kernel)
239 (kernel-arguments boot-parameters-kernel-arguments)
240 (initrd boot-parameters-initrd))
242 (define (ensure-not-/dev device)
243 "If DEVICE starts with a slash, return #f. This is meant to filter out
244 Linux device names such as /dev/sda, and to preserve GRUB device names and
246 (if (and (string? device) (string-prefix? "/" device))
250 (define (read-boot-parameters port)
251 "Read boot parameters from PORT and return the corresponding
252 <boot-parameters> object or #f if the format is unrecognized."
253 (define device-sexp->device
255 (('uuid (? symbol? type) (? bytevector? bv))
256 (bytevector->uuid bv type))
257 (('file-system-label (? string? label))
258 (file-system-label label))
259 ((? bytevector? bv) ;old format
260 (bytevector->uuid bv 'dce))
262 ;; It used to be that we would not distinguish between labels and
263 ;; device names. Try to infer the right thing here.
264 (if (string-prefix? "/dev/" device)
266 (file-system-label device)))))
269 (('boot-parameters ('version 0)
270 ('label label) ('root-device root)
275 (root-device (device-sexp->device root))
278 (match (assq 'bootloader-name rest)
280 (#f 'grub))) ; for compatibility reasons.
282 ;; In the past, we would store the directory name of the kernel instead
283 ;; of the absolute file name of its image. Detect that and correct it.
284 (kernel (if (string=? linux (direct-store-path linux))
285 (string-append linux "/"
286 (system-linux-image-file-name))
290 (match (assq 'kernel-arguments rest)
292 (#f '()))) ;the old format
295 (match (assq 'initrd rest)
296 (('initrd ('string-append directory file)) ;the old format
297 (string-append directory file))
298 (('initrd (? string? file))
302 ;; Linux device names like "/dev/sda1" are not suitable GRUB device
303 ;; identifiers, so we just filter them out.
305 (match (assq 'store rest)
306 (('store ('device #f) _ ...)
308 (('store ('device device) _ ...)
309 (device-sexp->device device))
314 (match (assq 'store rest)
315 (('store ('device _) ('mount-point mount-point) _ ...)
319 (x ;unsupported format
320 (warning (G_ "unrecognized boot parameters at '~a'~%")
321 (port-filename port))
324 (define (read-boot-parameters-file system)
325 "Read boot parameters from SYSTEM's (system or generation) \"parameters\"
326 file and returns the corresponding <boot-parameters> object or #f if the
327 format is unrecognized.
328 The object has its kernel-arguments extended in order to make it bootable."
329 (let* ((file (string-append system "/parameters"))
330 (params (call-with-input-file file read-boot-parameters))
331 (root (boot-parameters-root-device params))
332 (kernel-arguments (boot-parameters-kernel-arguments params)))
336 (kernel-arguments (bootable-kernel-arguments kernel-arguments
340 (define (boot-parameters->menu-entry conf)
342 (label (boot-parameters-label conf))
343 (device (boot-parameters-store-device conf))
344 (device-mount-point (boot-parameters-store-mount-point conf))
345 (linux (boot-parameters-kernel conf))
346 (linux-arguments (boot-parameters-kernel-arguments conf))
347 (initrd (boot-parameters-initrd conf))))
355 (define (non-boot-file-system-service os)
356 "Return the file system service for the file systems of OS that are not
357 marked as 'needed-for-boot'."
359 (remove file-system-needed-for-boot?
360 (operating-system-file-systems os)))
362 (define (device-mappings fs)
363 (let ((device (file-system-device fs)))
364 (if (string? device) ;title is 'device
366 (string=? (string-append "/dev/mapper/"
367 (mapped-device-target md))
369 (operating-system-mapped-devices os))
372 (define (add-dependencies fs)
373 ;; Add the dependencies due to device mappings to FS.
377 (delete-duplicates (append (device-mappings fs)
378 (file-system-dependencies fs))
381 (service file-system-service-type
382 (map add-dependencies file-systems)))
384 (define (mapped-device-user device file-systems)
385 "Return a file system among FILE-SYSTEMS that uses DEVICE, or #f."
386 (let ((target (string-append "/dev/mapper/" (mapped-device-target device))))
388 (or (member device (file-system-dependencies fs))
389 (and (string? (file-system-device fs))
390 (string=? (file-system-device fs) target))))
393 (define (operating-system-user-mapped-devices os)
394 "Return the subset of mapped devices that can be installed in
395 user-land--i.e., those not needed during boot."
396 (let ((devices (operating-system-mapped-devices os))
397 (file-systems (operating-system-file-systems os)))
399 (let ((user (mapped-device-user md file-systems)))
401 (not (file-system-needed-for-boot? user)))))
404 (define (operating-system-boot-mapped-devices os)
405 "Return the subset of mapped devices that must be installed during boot,
407 (let ((devices (operating-system-mapped-devices os))
408 (file-systems (operating-system-file-systems os)))
410 (let ((user (mapped-device-user md file-systems)))
411 (and user (file-system-needed-for-boot? user))))
414 (define (device-mapping-services os)
415 "Return the list of device-mapping services for OS as a list."
416 (map device-mapping-service
417 (operating-system-user-mapped-devices os)))
419 (define (swap-services os)
420 "Return the list of swap services for OS."
421 (map swap-service (operating-system-swap-devices os)))
423 (define* (system-linux-image-file-name #:optional (system (%current-system)))
424 "Return the basename of the kernel image file for SYSTEM."
425 ;; FIXME: Evaluate the conditional based on the actual current system.
427 ((string-prefix? "arm" (%current-system)) "zImage")
428 ((string-prefix? "mips" (%current-system)) "vmlinuz")
429 ((string-prefix? "aarch64" (%current-system)) "Image")
432 (define (operating-system-kernel-file os)
433 "Return an object representing the absolute file name of the kernel image of
435 (file-append (operating-system-kernel os)
436 "/" (system-linux-image-file-name os)))
438 (define* (operating-system-directory-base-entries os #:key container?)
439 "Return the basic entries of the 'system' directory of OS for use as the
440 value of the SYSTEM-SERVICE-TYPE service."
441 (let ((locale (operating-system-locale-directory os)))
442 (with-monad %store-monad
444 (return `(("locale" ,locale)))
446 ((kernel -> (operating-system-kernel os))
447 (initrd (operating-system-initrd-file os))
448 (params (operating-system-boot-parameters-file os)))
449 (return `(("kernel" ,kernel)
450 ("parameters" ,params)
452 ("locale" ,locale)))))))) ;used by libc
454 (define* (essential-services os #:key container?)
455 "Return the list of essential services for OS. These are special services
456 that implement part of what's declared in OS are responsible for low-level
457 bookkeeping. CONTAINER? determines whether to return the list of services for
458 a container or that of a \"bare metal\" system."
460 (map file-system-mount-point (operating-system-file-systems os)))
462 (let* ((mappings (device-mapping-services os))
463 (root-fs (root-file-system-service))
464 (other-fs (non-boot-file-system-service os))
465 (swaps (swap-services os))
466 (procs (service user-processes-service-type))
467 (host-name (host-name-service (operating-system-host-name os)))
468 (entries (operating-system-directory-base-entries
469 os #:container? container?)))
470 (cons* (service system-service-type entries)
473 ;; %SHEPHERD-ROOT-SERVICE must come first so that the gexp that
474 ;; execs shepherd comes last in the boot script (XXX). Likewise,
475 ;; the cleanup service must come last so that its gexp runs before
477 %shepherd-root-service
479 (service cleanup-service-type #f)
481 (pam-root-service (operating-system-pam-services os))
482 (account-service (append (operating-system-accounts os)
483 (operating-system-groups os))
484 (operating-system-skeletons os))
485 (operating-system-etc-service os)
486 (service fstab-service-type '())
487 (session-environment-service
488 (operating-system-environment-variables os))
489 host-name procs root-fs
490 (service setuid-program-service-type
491 (operating-system-setuid-programs os))
492 (service profile-service-type
493 (operating-system-packages os))
495 (append mappings swaps
497 ;; Add the firmware service, unless we are building for a
501 (list %linux-bare-metal-service
502 (service firmware-service-type
503 (operating-system-firmware os))))))))
505 (define* (operating-system-services os #:key container?)
506 "Return all the services of OS, including \"internal\" services that do not
507 explicitly appear in OS."
508 (instantiate-missing-services
509 (append (operating-system-user-services os)
510 (essential-services os #:container? container?))))
517 (define %base-firmware
518 ;; Firmware usable by default.
519 (list ath9k-htc-firmware
522 (define %base-packages
523 ;; Default set of packages globally visible. It should include anything
524 ;; required for basic administrator tasks.
525 (cons* procps psmisc which less zile nano
529 (@ (gnu packages admin) shadow) ;for 'passwd'
531 ;; wireless-tools is deprecated in favor of iw, but it's still what
532 ;; many people are familiar with, so keep it around.
536 net-tools ; XXX: remove when Inetutils suffices
538 info-reader ;the standalone Info reader (no Perl)
540 ;; The 'sudo' command is already in %SETUID-PROGRAMS, but we also
541 ;; want the other commands and the man pages (notably because
542 ;; auto-completion in Emacs shell relies on man pages.)
545 ;; Get 'insmod' & co. from kmod, not module-init-tools, since udev
546 ;; already depends on it anyway.
553 ;; XXX: We don't use (canonical-package guile-2.2) here because that
554 ;; would create a collision in the global profile between the GMP
555 ;; variant propagated by 'guile-final' and the GMP variant propagated
556 ;; by 'gnutls', itself propagated by 'guix'.
559 ;; The packages below are also in %FINAL-INPUTS, so take them from
560 ;; there to avoid duplication.
561 (map canonical-package
562 (list bash coreutils findutils grep sed
563 diffutils patch gawk tar gzip bzip2 xz lzip))))
565 (define %default-issue
566 ;; Default contents for /etc/issue.
568 This is the GNU system. Welcome.\n")
570 (define (local-host-aliases host-name)
571 "Return aliases for HOST-NAME, to be used in /etc/hosts."
572 (string-append "127.0.0.1 localhost " host-name "\n"
573 "::1 localhost " host-name "\n"))
575 (define (default-/etc/hosts host-name)
576 "Return the default /etc/hosts file."
577 (plain-file "hosts" (local-host-aliases host-name)))
579 (define* (operating-system-etc-service os)
580 "Return a <service> that builds containing the static part of the /etc
583 (plain-file "login.defs"
585 "# Default paths for non-login shells started by su(1).\n"
586 "ENV_PATH /run/setuid-programs:"
587 "/run/current-system/profile/bin:"
588 "/run/current-system/profile/sbin\n"
589 "ENV_SUPATH /run/setuid-programs:"
590 "/run/current-system/profile/bin:"
591 "/run/current-system/profile/sbin\n")))
593 (issue (plain-file "issue" (operating-system-issue os)))
594 (nsswitch (plain-file "nsswitch.conf"
595 (name-service-switch->string
596 (operating-system-name-service-switch os))))
598 ;; Startup file for POSIX-compliant login shells, which set system-wide
599 ;; environment variables.
600 (profile (mixed-text-file "profile" "\
601 # Crucial variables that could be missing in the profiles' 'etc/profile'
602 # because they would require combining both profiles.
603 # FIXME: See <http://bugs.gnu.org/20255>.
604 export MANPATH=$HOME/.guix-profile/share/man:/run/current-system/profile/share/man
605 export INFOPATH=$HOME/.guix-profile/share/info:/run/current-system/profile/share/info
606 export XDG_DATA_DIRS=$HOME/.guix-profile/share:/run/current-system/profile/share
607 export XDG_CONFIG_DIRS=$HOME/.guix-profile/etc/xdg:/run/current-system/profile/etc/xdg
609 # Make sure libXcursor finds cursors installed into user or system profiles. See <http://bugs.gnu.org/24445>
610 export XCURSOR_PATH=$HOME/.icons:$HOME/.guix-profile/share/icons:/run/current-system/profile/share/icons
612 # Ignore the default value of 'PATH'.
615 # Load the system profile's settings.
616 GUIX_PROFILE=/run/current-system/profile ; \\
617 . /run/current-system/profile/etc/profile
619 # Prepend setuid programs.
620 export PATH=/run/setuid-programs:$PATH
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\" ]
630 export `cat /etc/environment | cut -d= -f1`
633 if [ -f \"$HOME/.guix-profile/etc/profile\" ]
635 # Load the user profile's settings.
636 GUIX_PROFILE=\"$HOME/.guix-profile\" ; \\
637 . \"$HOME/.guix-profile/etc/profile\"
639 # At least define this one so that basic things just work
640 # when the user installs their first package.
641 export PATH=\"$HOME/.guix-profile/bin:$PATH\"
644 # Set the umask, notably for users logging in via 'lsh'.
645 # See <http://bugs.gnu.org/22650>.
648 # Allow Hunspell-based applications (IceCat, LibreOffice, etc.) to
650 export DICPATH=\"$HOME/.guix-profile/share/hunspell:/run/current-system/profile/share/hunspell\"
652 # Allow GStreamer-based applications to find plugins.
653 export GST_PLUGIN_PATH=\"$HOME/.guix-profile/lib/gstreamer-1.0\"
655 if [ -n \"$BASH_VERSION\" -a -f /etc/bashrc ]
657 # Load Bash-specific initialization code.
662 (bashrc (plain-file "bashrc" "\
663 # Bash-specific initialization.
665 # The 'bash-completion' package.
666 if [ -f /run/current-system/profile/etc/profile.d/bash_completion.sh ]
668 # Bash-completion sources ~/.bash_completion. It installs a dynamic
669 # completion loader that searches its own completion files as well
670 # as those in ~/.guix-profile and /run/current-system/profile.
671 source /run/current-system/profile/etc/profile.d/bash_completion.sh
674 `(("services" ,(file-append net-base "/etc/services"))
675 ("protocols" ,(file-append net-base "/etc/protocols"))
676 ("rpc" ,(file-append net-base "/etc/rpc"))
677 ("login.defs" ,#~#$login.defs)
679 ("nsswitch.conf" ,#~#$nsswitch)
680 ("profile" ,#~#$profile)
681 ("bashrc" ,#~#$bashrc)
682 ("hosts" ,#~#$(or (operating-system-hosts-file os)
683 (default-/etc/hosts (operating-system-host-name os))))
684 ;; Write the operating-system-host-name to /etc/hostname to prevent
685 ;; NetworkManager from changing the system's hostname when connecting
686 ;; to certain networks. Some discussion at
687 ;; https://lists.gnu.org/archive/html/help-guix/2017-09/msg00037.html
688 ("hostname" ,(plain-file "hostname" (operating-system-host-name os)))
689 ("localtime" ,(file-append tzdata "/share/zoneinfo/"
690 (operating-system-timezone os)))
691 ("sudoers" ,(operating-system-sudoers-file os))))))
693 (define %root-account
694 ;; Default root account.
698 (uid 0) (group "root")
699 (comment "System administrator")
700 (home-directory "/root")))
702 (define (operating-system-accounts os)
703 "Return the user accounts for OS, including an obligatory 'root' account,
704 and excluding accounts requested by services."
705 ;; Make sure there's a root account.
706 (if (find (lambda (user)
707 (and=> (user-account-uid user) zero?))
708 (operating-system-users os))
709 (operating-system-users os)
710 (cons %root-account (operating-system-users os))))
712 (define (maybe-string->file file-name thing)
713 "If THING is a string, return a <plain-file> with THING as its content.
714 Otherwise just return THING.
716 This is for backward-compatibility of fields that used to be strings and are
717 now file-like objects.."
720 (warning (G_ "using a string for file '~a' is deprecated; \
721 use 'plain-file' instead~%")
723 (plain-file file-name thing))
727 (define (maybe-file->monadic file-name thing)
728 "If THING is a value in %STORE-MONAD, return it as is; otherwise return
729 THING in the %STORE-MONAD.
731 This is for backward-compatibility of fields that used to be monadic values
732 and are now file-like objects."
733 (with-monad %store-monad
736 (warning (G_ "using a monadic value for '~a' is deprecated; \
737 use 'plain-file' instead~%")
743 (define (operating-system-etc-directory os)
744 "Return that static part of the /etc directory of OS."
746 (fold-services (operating-system-services os)
747 #:target-type etc-service-type)))
749 (define (operating-system-environment-variables os)
750 "Return the environment variables of OS for
751 @var{session-environment-service-type}, to be used in @file{/etc/environment}."
752 `(("LANG" . ,(operating-system-locale os))
753 ;; Note: No need to set 'TZ' since (1) we provide /etc/localtime, and (2)
754 ;; it doesn't work for setuid binaries. See <https://bugs.gnu.org/29212>.
755 ("TZDIR" . ,(file-append tzdata "/share/zoneinfo"))
756 ;; Tell 'modprobe' & co. where to look for modules.
757 ("LINUX_MODULE_DIRECTORY" . "/run/booted-system/kernel/lib/modules")
758 ;; These variables are honored by OpenSSL (libssl) and Git.
759 ("SSL_CERT_DIR" . "/etc/ssl/certs")
760 ("SSL_CERT_FILE" . "/etc/ssl/certs/ca-certificates.crt")
761 ("GIT_SSL_CAINFO" . "/etc/ssl/certs/ca-certificates.crt")
763 ;; 'GTK_DATA_PREFIX' must name one directory where GTK+ themes are
765 ("GTK_DATA_PREFIX" . "/run/current-system/profile")
767 ;; By default, applications that use D-Bus, such as Emacs, abort at startup
768 ;; when /etc/machine-id is missing. Make sure these warnings are non-fatal.
769 ("DBUS_FATAL_WARNINGS" . "0")
771 ;; XXX: Normally we wouldn't need to do this, but our glibc@2.23 package
772 ;; used to look things up in 'PREFIX/lib/locale' instead of
773 ;; '/run/current-system/locale' as was intended. Keep this hack around so
774 ;; that people who still have glibc@2.23-using packages in their profiles
775 ;; can use them correctly.
776 ;; TODO: Remove when glibc@2.23 is long gone.
777 ("GUIX_LOCPATH" . "/run/current-system/locale")))
779 (define %setuid-programs
780 ;; Default set of setuid-root programs.
781 (let ((shadow (@ (gnu packages admin) shadow)))
782 (list (file-append shadow "/bin/passwd")
783 (file-append shadow "/bin/su")
784 (file-append shadow "/bin/newuidmap")
785 (file-append shadow "/bin/newgidmap")
786 (file-append inetutils "/bin/ping")
787 (file-append inetutils "/bin/ping6")
788 (file-append sudo "/bin/sudo")
789 (file-append fuse "/bin/fusermount"))))
791 (define %sudoers-specification
792 ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel'
793 ;; group can do anything. See
794 ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>.
795 ;; TODO: Add a declarative API.
796 (plain-file "sudoers" "\
798 %wheel ALL=(ALL) ALL\n"))
800 (define* (operating-system-activation-script os #:key container?)
801 "Return the activation script for OS---i.e., the code that \"activates\" the
802 stateful part of OS, including user accounts and groups, special directories,
804 (let* ((services (operating-system-services os #:container? container?))
805 (activation (fold-services services
806 #:target-type activation-service-type)))
807 (activation-service->script activation)))
809 (define* (operating-system-boot-script os #:key container?)
810 "Return the boot script for OS---i.e., the code started by the initrd once
811 we're running in the final root. When CONTAINER? is true, skip all
812 hardware-related operations as necessary when booting a Linux container."
813 (let* ((services (operating-system-services os #:container? container?))
814 (boot (fold-services services #:target-type boot-service-type)))
815 ;; BOOT is the script as a monadic value.
816 (service-value boot)))
818 (define (operating-system-user-accounts os)
819 "Return the list of user accounts of OS."
820 (let* ((services (operating-system-services os))
821 (account (fold-services services
822 #:target-type account-service-type)))
823 (filter user-account?
824 (service-value account))))
826 (define (operating-system-shepherd-service-names os)
827 "Return the list of Shepherd service names for OS."
828 (append-map shepherd-service-provision
830 (fold-services (operating-system-services os)
832 shepherd-root-service-type))))
834 (define* (operating-system-derivation os #:key container?)
835 "Return a derivation that builds OS."
836 (let* ((services (operating-system-services os #:container? container?))
837 (system (fold-services services)))
838 ;; SYSTEM contains the derivation as a monadic value.
839 (service-value system)))
841 (define* (operating-system-profile os #:key container?)
842 "Return a derivation that builds the system profile of OS."
844 ((services -> (operating-system-services os #:container? container?))
845 (profile (fold-services services
846 #:target-type profile-service-type)))
851 (define (operating-system-root-file-system os)
852 "Return the root file system of OS."
854 (string=? "/" (file-system-mount-point fs)))
855 (operating-system-file-systems os)))
857 (define (operating-system-initrd-file os)
858 "Return a gexp denoting the initrd file of OS."
859 (define boot-file-systems
860 (filter file-system-needed-for-boot?
861 (operating-system-file-systems os)))
863 (define mapped-devices
864 (operating-system-boot-mapped-devices os))
867 (operating-system-initrd os))
869 (mlet %store-monad ((initrd (make-initrd boot-file-systems
870 #:linux (operating-system-kernel os)
872 (operating-system-initrd-modules os)
873 #:mapped-devices mapped-devices)))
874 (return (file-append initrd "/initrd"))))
876 (define (locale-name->definition* name)
877 "Variant of 'locale-name->definition' that raises an error upon failure."
878 (match (locale-name->definition name)
882 (message (format #f (G_ "~a: invalid locale name") name))))))
885 (define (operating-system-locale-directory os)
886 "Return the directory containing the locales compiled for the definitions
887 listed in OS. The C library expects to find it under
888 /run/current-system/locale."
890 (operating-system-locale os))
893 ;; While we're at it, check whether NAME is defined and add it if needed.
894 (if (member name (map locale-definition-name
895 (operating-system-locale-definitions os)))
896 (operating-system-locale-definitions os)
897 (cons (locale-name->definition* name)
898 (operating-system-locale-definitions os))))
900 (locale-directory definitions
901 #:libcs (operating-system-locale-libcs os)))
903 (define (kernel->boot-label kernel)
904 "Return a label for the bootloader menu entry that boots KERNEL."
905 (string-append "GNU with "
906 (string-titlecase (package-name kernel)) " "
907 (package-version kernel)
910 (define (store-file-system file-systems)
911 "Return the file system object among FILE-SYSTEMS that contains the store."
912 (match (filter (lambda (fs)
913 (and (file-system-mount? fs)
914 (not (memq 'bind-mount (file-system-flags fs)))
915 (string-prefix? (file-system-mount-point fs)
918 ((and candidates (head . tail))
919 (reduce (lambda (fs1 fs2)
920 (if (> (string-length (file-system-mount-point fs1))
921 (string-length (file-system-mount-point fs2)))
927 (define (operating-system-store-file-system os)
928 "Return the file system that contains the store of OS."
929 (store-file-system (operating-system-file-systems os)))
931 (define* (operating-system-bootcfg os #:optional (old-entries '()))
932 "Return the bootloader configuration file for OS. Use OLD-ENTRIES
933 (which is a list of <menu-entry>) to populate the \"old entries\" menu."
935 ((system (operating-system-derivation os))
936 (root-fs -> (operating-system-root-file-system os))
937 (root-device -> (file-system-device root-fs))
938 (params (operating-system-boot-parameters os system root-device))
939 (entry -> (boot-parameters->menu-entry params))
940 (bootloader-conf -> (operating-system-bootloader os)))
941 ((bootloader-configuration-file-generator
942 (bootloader-configuration-bootloader bootloader-conf))
943 bootloader-conf (list entry) #:old-entries old-entries)))
945 (define (operating-system-boot-parameters os system.drv root-device)
946 "Return a monadic <boot-parameters> record that describes the boot parameters
947 of OS. SYSTEM.DRV is either a derivation or #f. If it's a derivation, adds
948 kernel arguments for that derivation to <boot-parameters>."
950 ((initrd (operating-system-initrd-file os))
951 (store -> (operating-system-store-file-system os))
952 (bootloader -> (bootloader-configuration-bootloader
953 (operating-system-bootloader os)))
954 (bootloader-name -> (bootloader-name bootloader))
955 (label -> (kernel->boot-label (operating-system-kernel os))))
956 (return (boot-parameters
958 (root-device root-device)
959 (kernel (operating-system-kernel-file os))
962 (operating-system-kernel-arguments os system.drv root-device)
963 (operating-system-user-kernel-arguments os)))
965 (bootloader-name bootloader-name)
966 (store-device (ensure-not-/dev (file-system-device store)))
967 (store-mount-point (file-system-mount-point store))))))
969 (define (device->sexp device)
970 "Serialize DEVICE as an sexp (really, as an object with a read syntax.)"
973 `(uuid ,(uuid-type uuid) ,(uuid-bytevector uuid)))
974 ((? file-system-label? label)
975 `(file-system-label ,(file-system-label->string label)))
979 (define* (operating-system-boot-parameters-file os #:optional (system.drv #f))
980 "Return a file that describes the boot parameters of OS. The primary use of
981 this file is the reconstruction of GRUB menu entries for old configurations.
982 SYSTEM.DRV is optional. If given, adds kernel arguments for that system to the
983 returned file (since the returned file is then usually stored into the
984 content-addressed \"system\" directory, it's usually not a good idea
985 to give it because the content hash would change by the content hash
986 being stored into the \"parameters\" file)."
987 (mlet* %store-monad ((root -> (operating-system-root-file-system os))
988 (device -> (file-system-device root))
989 (params (operating-system-boot-parameters os
992 (gexp->file "parameters"
995 (label #$(boot-parameters-label params))
998 (boot-parameters-root-device params)))
999 (kernel #$(boot-parameters-kernel params))
1001 #$(boot-parameters-kernel-arguments params))
1002 (initrd #$(boot-parameters-initrd params))
1003 (bootloader-name #$(boot-parameters-bootloader-name params))
1006 #$(device->sexp (boot-parameters-store-device params)))
1007 (mount-point #$(boot-parameters-store-mount-point params))))
1008 #:set-load-path? #f)))
1010 (define-gexp-compiler (operating-system-compiler (os <operating-system>)
1014 ;; XXX: This is not super elegant but we can't pass SYSTEM and TARGET to
1015 ;; 'operating-system-derivation'.
1016 (run-with-store store (operating-system-derivation os)
1018 #:target target)))))
1020 ;;; system.scm ends here