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 system root-device)
131 "Return a list of kernel arguments (gexps) to boot SYSTEM from ROOT-DEVICE."
132 (list (string-append "--root="
133 (cond ((uuid? root-device)
135 ;; Note: Always use the DCE format because that's
136 ;; what (gnu build linux-boot) expects for the
137 ;; '--root' kernel command-line option.
138 (uuid->string (uuid-bytevector root-device)
140 ((file-system-label? root-device)
141 (file-system-label->string root-device))
143 #~(string-append "--system=" #$system)
144 #~(string-append "--load=" #$system "/boot")))
146 ;; System-wide configuration.
147 ;; TODO: Add per-field docstrings/stexi.
148 (define-record-type* <operating-system> operating-system
149 make-operating-system
151 (kernel operating-system-kernel ; package
152 (default linux-libre))
153 (kernel-arguments operating-system-user-kernel-arguments
154 (default '())) ; list of gexps/strings
155 (bootloader operating-system-bootloader) ; <bootloader-configuration>
157 (initrd operating-system-initrd ; (list fs) -> file-like
158 (default base-initrd))
159 (initrd-modules operating-system-initrd-modules ; list of strings
160 (thunked) ; it's system-dependent
161 (default %base-initrd-modules))
163 (firmware operating-system-firmware ; list of packages
164 (default %base-firmware))
166 (host-name operating-system-host-name) ; string
167 (hosts-file operating-system-hosts-file ; file-like | #f
170 (mapped-devices operating-system-mapped-devices ; list of <mapped-device>
172 (file-systems operating-system-file-systems) ; list of fs
173 (swap-devices operating-system-swap-devices ; list of strings
176 (users operating-system-users ; list of user accounts
177 (default %base-user-accounts))
178 (groups operating-system-groups ; list of user groups
179 (default %base-groups))
181 (skeletons operating-system-skeletons ; list of name/monadic value
182 (default (default-skeletons)))
183 (issue operating-system-issue ; string
184 (default %default-issue))
186 (packages operating-system-packages ; list of (PACKAGE OUTPUT...)
187 (default %base-packages)) ; or just PACKAGE
189 (timezone operating-system-timezone) ; string
190 (locale operating-system-locale ; string
191 (default "en_US.utf8"))
192 (locale-definitions operating-system-locale-definitions ; list of <locale-definition>
193 (default %default-locale-definitions))
194 (locale-libcs operating-system-locale-libcs ; list of <packages>
195 (default %default-locale-libcs))
196 (name-service-switch operating-system-name-service-switch ; <name-service-switch>
197 (default %default-nss))
199 (services operating-system-user-services ; list of monadic services
200 (default %base-services))
202 (pam-services operating-system-pam-services ; list of PAM services
203 (default (base-pam-services)))
204 (setuid-programs operating-system-setuid-programs
205 (default %setuid-programs)) ; list of string-valued gexps
207 (sudoers-file operating-system-sudoers-file ; file-like
208 (default %sudoers-specification)))
210 (define (operating-system-kernel-arguments os root-device)
211 "Return all the kernel arguments, including the ones not specified
212 directly by the user."
213 (append (bootable-kernel-arguments os root-device)
214 (operating-system-user-kernel-arguments os)))
221 (define-record-type* <boot-parameters>
222 boot-parameters make-boot-parameters boot-parameters?
223 (label boot-parameters-label)
224 ;; Because we will use the 'store-device' to create the GRUB search command,
225 ;; the 'store-device' has slightly different semantics than 'root-device'.
226 ;; The 'store-device' can be a file system uuid, a file system label, or #f,
227 ;; but it cannot be a device path such as "/dev/sda3", since GRUB would not
228 ;; understand that. The 'root-device', on the other hand, corresponds
229 ;; exactly to the device field of the <file-system> object representing the
230 ;; OS's root file system, so it might be a device path like "/dev/sda3".
231 (root-device boot-parameters-root-device)
232 (bootloader-name boot-parameters-bootloader-name)
233 (store-device boot-parameters-store-device)
234 (store-mount-point boot-parameters-store-mount-point)
235 (kernel boot-parameters-kernel)
236 (kernel-arguments boot-parameters-kernel-arguments)
237 (initrd boot-parameters-initrd))
239 (define (ensure-not-/dev device)
240 "If DEVICE starts with a slash, return #f. This is meant to filter out
241 Linux device names such as /dev/sda, and to preserve GRUB device names and
243 (if (and (string? device) (string-prefix? "/" device))
247 (define (read-boot-parameters port)
248 "Read boot parameters from PORT and return the corresponding
249 <boot-parameters> object or #f if the format is unrecognized."
250 (define device-sexp->device
252 (('uuid (? symbol? type) (? bytevector? bv))
253 (bytevector->uuid bv type))
254 (('file-system-label (? string? label))
255 (file-system-label label))
256 ((? bytevector? bv) ;old format
257 (bytevector->uuid bv 'dce))
259 ;; It used to be that we would not distinguish between labels and
260 ;; device names. Try to infer the right thing here.
261 (if (string-prefix? "/dev/" device)
263 (file-system-label device)))))
266 (('boot-parameters ('version 0)
267 ('label label) ('root-device root)
272 (root-device (device-sexp->device root))
275 (match (assq 'bootloader-name rest)
277 (#f 'grub))) ; for compatibility reasons.
279 ;; In the past, we would store the directory name of the kernel instead
280 ;; of the absolute file name of its image. Detect that and correct it.
281 (kernel (if (string=? linux (direct-store-path linux))
282 (string-append linux "/"
283 (system-linux-image-file-name))
287 (match (assq 'kernel-arguments rest)
289 (#f '()))) ;the old format
292 (match (assq 'initrd rest)
293 (('initrd ('string-append directory file)) ;the old format
294 (string-append directory file))
295 (('initrd (? string? file))
299 ;; Linux device names like "/dev/sda1" are not suitable GRUB device
300 ;; identifiers, so we just filter them out.
302 (match (assq 'store rest)
303 (('store ('device #f) _ ...)
305 (('store ('device device) _ ...)
306 (device-sexp->device device))
311 (match (assq 'store rest)
312 (('store ('device _) ('mount-point mount-point) _ ...)
316 (x ;unsupported format
317 (warning (G_ "unrecognized boot parameters at '~a'~%")
318 (port-filename port))
321 (define (read-boot-parameters-file system)
322 "Read boot parameters from SYSTEM's (system or generation) \"parameters\"
323 file and returns the corresponding <boot-parameters> object or #f if the
324 format is unrecognized.
325 The object has its kernel-arguments extended in order to make it bootable."
326 (let* ((file (string-append system "/parameters"))
327 (params (call-with-input-file file read-boot-parameters))
328 (root (boot-parameters-root-device params)))
331 (kernel-arguments (append (bootable-kernel-arguments system root)
332 (boot-parameters-kernel-arguments params))))))
334 (define (boot-parameters->menu-entry conf)
336 (label (boot-parameters-label conf))
337 (device (boot-parameters-store-device conf))
338 (device-mount-point (boot-parameters-store-mount-point conf))
339 (linux (boot-parameters-kernel conf))
340 (linux-arguments (boot-parameters-kernel-arguments conf))
341 (initrd (boot-parameters-initrd conf))))
349 (define (non-boot-file-system-service os)
350 "Return the file system service for the file systems of OS that are not
351 marked as 'needed-for-boot'."
353 (remove file-system-needed-for-boot?
354 (operating-system-file-systems os)))
356 (define mapped-devices-for-boot
357 (operating-system-boot-mapped-devices os))
359 (define (device-mappings fs)
360 (let ((device (file-system-device fs)))
361 (if (string? device) ;title is 'device
363 (string=? (string-append "/dev/mapper/"
364 (mapped-device-target md))
366 (operating-system-mapped-devices os))
369 (define (add-dependencies fs)
370 ;; Add the dependencies due to device mappings to FS.
375 (remove (cut member <> mapped-devices-for-boot)
376 (append (device-mappings fs)
377 (file-system-dependencies fs)))
380 (service file-system-service-type
381 (map add-dependencies file-systems)))
383 (define (mapped-device-users device file-systems)
384 "Return the subset of FILE-SYSTEMS that use DEVICE."
385 (let ((target (string-append "/dev/mapper/" (mapped-device-target device))))
387 (or (member device (file-system-dependencies fs))
388 (and (string? (file-system-device fs))
389 (string=? (file-system-device fs) target))))
392 (define (operating-system-user-mapped-devices os)
393 "Return the subset of mapped devices that can be installed in
394 user-land--i.e., those not needed during boot."
395 (let ((devices (operating-system-mapped-devices os))
396 (file-systems (operating-system-file-systems os)))
398 (let ((users (mapped-device-users md file-systems)))
399 (not (any file-system-needed-for-boot? users))))
402 (define (operating-system-boot-mapped-devices os)
403 "Return the subset of mapped devices that must be installed during boot,
405 (let ((devices (operating-system-mapped-devices os))
406 (file-systems (operating-system-file-systems os)))
408 (let ((users (mapped-device-users md file-systems)))
409 (any file-system-needed-for-boot? users)))
412 (define (device-mapping-services os)
413 "Return the list of device-mapping services for OS as a list."
414 (map device-mapping-service
415 (operating-system-user-mapped-devices os)))
417 (define (swap-services os)
418 "Return the list of swap services for OS."
419 (map swap-service (operating-system-swap-devices os)))
421 (define* (system-linux-image-file-name #:optional (system (%current-system)))
422 "Return the basename of the kernel image file for SYSTEM."
423 ;; FIXME: Evaluate the conditional based on the actual current system.
425 ((string-prefix? "arm" (%current-system)) "zImage")
426 ((string-prefix? "mips" (%current-system)) "vmlinuz")
427 ((string-prefix? "aarch64" (%current-system)) "Image")
430 (define (operating-system-kernel-file os)
431 "Return an object representing the absolute file name of the kernel image of
433 (file-append (operating-system-kernel os)
434 "/" (system-linux-image-file-name os)))
436 (define* (operating-system-directory-base-entries os #:key container?)
437 "Return the basic entries of the 'system' directory of OS for use as the
438 value of the SYSTEM-SERVICE-TYPE service."
439 (let ((locale (operating-system-locale-directory os)))
440 (with-monad %store-monad
442 (return `(("locale" ,locale)))
444 ((kernel -> (operating-system-kernel os))
445 (initrd -> (operating-system-initrd-file os))
446 (params (operating-system-boot-parameters-file os)))
447 (return `(("kernel" ,kernel)
448 ("parameters" ,params)
450 ("locale" ,locale)))))))) ;used by libc
452 (define* (essential-services os #:key container?)
453 "Return the list of essential services for OS. These are special services
454 that implement part of what's declared in OS are responsible for low-level
455 bookkeeping. CONTAINER? determines whether to return the list of services for
456 a container or that of a \"bare metal\" system."
458 (map file-system-mount-point (operating-system-file-systems os)))
460 (let* ((mappings (device-mapping-services os))
461 (root-fs (root-file-system-service))
462 (other-fs (non-boot-file-system-service os))
463 (swaps (swap-services os))
464 (procs (service user-processes-service-type))
465 (host-name (host-name-service (operating-system-host-name os)))
466 (entries (operating-system-directory-base-entries
467 os #:container? container?)))
468 (cons* (service system-service-type entries)
471 ;; %SHEPHERD-ROOT-SERVICE must come last so that the gexp that
472 ;; execs shepherd comes last in the boot script (XXX). Likewise,
473 ;; the cleanup service must come first so that its gexp runs before
475 (service cleanup-service-type #f)
477 %shepherd-root-service
479 (pam-root-service (operating-system-pam-services os))
480 (account-service (append (operating-system-accounts os)
481 (operating-system-groups os))
482 (operating-system-skeletons os))
483 (operating-system-etc-service os)
484 (service fstab-service-type '())
485 (session-environment-service
486 (operating-system-environment-variables os))
487 host-name procs root-fs
488 (service setuid-program-service-type
489 (operating-system-setuid-programs os))
490 (service profile-service-type
491 (operating-system-packages os))
493 (append mappings swaps
495 ;; Add the firmware service, unless we are building for a
498 (list %containerized-shepherd-service)
499 (list %linux-bare-metal-service
500 (service firmware-service-type
501 (operating-system-firmware os))))))))
503 (define* (operating-system-services os #:key container?)
504 "Return all the services of OS, including \"internal\" services that do not
505 explicitly appear in OS."
506 (instantiate-missing-services
507 (append (operating-system-user-services os)
508 (essential-services os #:container? container?))))
515 (define %base-firmware
516 ;; Firmware usable by default.
517 (list ath9k-htc-firmware
520 (define %base-packages
521 ;; Default set of packages globally visible. It should include anything
522 ;; required for basic administrator tasks.
523 (cons* procps psmisc which less zile nano
527 (@ (gnu packages admin) shadow) ;for 'passwd'
529 ;; wireless-tools is deprecated in favor of iw, but it's still what
530 ;; many people are familiar with, so keep it around.
534 net-tools ; XXX: remove when Inetutils suffices
536 info-reader ;the standalone Info reader (no Perl)
538 ;; The 'sudo' command is already in %SETUID-PROGRAMS, but we also
539 ;; want the other commands and the man pages (notably because
540 ;; auto-completion in Emacs shell relies on man pages.)
543 ;; Get 'insmod' & co. from kmod, not module-init-tools, since udev
544 ;; already depends on it anyway.
551 ;; XXX: We don't use (canonical-package guile-2.2) here because that
552 ;; would create a collision in the global profile between the GMP
553 ;; variant propagated by 'guile-final' and the GMP variant propagated
554 ;; by 'gnutls', itself propagated by 'guix'.
557 ;; The packages below are also in %FINAL-INPUTS, so take them from
558 ;; there to avoid duplication.
559 (map canonical-package
560 (list bash coreutils findutils grep sed
561 diffutils patch gawk tar gzip bzip2 xz lzip))))
563 (define %default-issue
564 ;; Default contents for /etc/issue.
566 This is the GNU system. Welcome.\n")
568 (define (local-host-aliases host-name)
569 "Return aliases for HOST-NAME, to be used in /etc/hosts."
570 (string-append "127.0.0.1 localhost " host-name "\n"
571 "::1 localhost " host-name "\n"))
573 (define (default-/etc/hosts host-name)
574 "Return the default /etc/hosts file."
575 (plain-file "hosts" (local-host-aliases host-name)))
577 (define* (operating-system-etc-service os)
578 "Return a <service> that builds containing the static part of the /etc
581 (plain-file "login.defs"
583 "# Default paths for non-login shells started by su(1).\n"
584 "ENV_PATH /run/setuid-programs:"
585 "/run/current-system/profile/bin:"
586 "/run/current-system/profile/sbin\n"
587 "ENV_SUPATH /run/setuid-programs:"
588 "/run/current-system/profile/bin:"
589 "/run/current-system/profile/sbin\n")))
591 (issue (plain-file "issue" (operating-system-issue os)))
592 (nsswitch (plain-file "nsswitch.conf"
593 (name-service-switch->string
594 (operating-system-name-service-switch os))))
596 ;; Startup file for POSIX-compliant login shells, which set system-wide
597 ;; environment variables.
598 (profile (mixed-text-file "profile" "\
599 # Crucial variables that could be missing in the profiles' 'etc/profile'
600 # because they would require combining both profiles.
601 # FIXME: See <http://bugs.gnu.org/20255>.
602 export MANPATH=$HOME/.guix-profile/share/man:/run/current-system/profile/share/man
603 export INFOPATH=$HOME/.guix-profile/share/info:/run/current-system/profile/share/info
604 export XDG_DATA_DIRS=$HOME/.guix-profile/share:/run/current-system/profile/share
605 export XDG_CONFIG_DIRS=$HOME/.guix-profile/etc/xdg:/run/current-system/profile/etc/xdg
607 # Make sure libXcursor finds cursors installed into user or system profiles. See <http://bugs.gnu.org/24445>
608 export XCURSOR_PATH=$HOME/.icons:$HOME/.guix-profile/share/icons:/run/current-system/profile/share/icons
610 # Ignore the default value of 'PATH'.
613 # Load the system profile's settings.
614 GUIX_PROFILE=/run/current-system/profile ; \\
615 . /run/current-system/profile/etc/profile
617 # Since 'lshd' does not use pam_env, /etc/environment must be explicitly
618 # loaded when someone logs in via SSH. See <http://bugs.gnu.org/22175>.
619 # We need 'PATH' to be defined here, for 'cat' and 'cut'. Do this before
620 # reading the user's 'etc/profile' to allow variables to be overridden.
621 if [ -f /etc/environment -a -n \"$SSH_CLIENT\" \\
622 -a -z \"$LINUX_MODULE_DIRECTORY\" ]
625 export `cat /etc/environment | cut -d= -f1`
628 # Arrange so that ~/.config/guix/current comes first.
629 for profile in \"$HOME/.guix-profile\" \"$HOME/.config/guix/current\"
631 if [ -f \"$profile/etc/profile\" ]
633 # Load the user profile's settings.
634 GUIX_PROFILE=\"$profile\" ; \\
635 . \"$profile/etc/profile\"
637 # At least define this one so that basic things just work
638 # when the user installs their first package.
639 export PATH=\"$profile/bin:$PATH\"
643 # Prepend setuid programs.
644 export PATH=/run/setuid-programs:$PATH
646 # Arrange so that ~/.config/guix/current/share/info comes first.
647 export INFOPATH=\"$HOME/.config/guix/current/share/info:$INFOPATH\"
649 # Set the umask, notably for users logging in via 'lsh'.
650 # See <http://bugs.gnu.org/22650>.
653 # Allow Hunspell-based applications (IceCat, LibreOffice, etc.) to
655 export DICPATH=\"$HOME/.guix-profile/share/hunspell:/run/current-system/profile/share/hunspell\"
657 # Allow GStreamer-based applications to find plugins.
658 export GST_PLUGIN_PATH=\"$HOME/.guix-profile/lib/gstreamer-1.0\"
660 if [ -n \"$BASH_VERSION\" -a -f /etc/bashrc ]
662 # Load Bash-specific initialization code.
667 (bashrc (plain-file "bashrc" "\
668 # Bash-specific initialization.
670 # The 'bash-completion' package.
671 if [ -f /run/current-system/profile/etc/profile.d/bash_completion.sh ]
673 # Bash-completion sources ~/.bash_completion. It installs a dynamic
674 # completion loader that searches its own completion files as well
675 # as those in ~/.guix-profile and /run/current-system/profile.
676 source /run/current-system/profile/etc/profile.d/bash_completion.sh
679 `(("services" ,(file-append net-base "/etc/services"))
680 ("protocols" ,(file-append net-base "/etc/protocols"))
681 ("rpc" ,(file-append net-base "/etc/rpc"))
682 ("login.defs" ,#~#$login.defs)
684 ("nsswitch.conf" ,#~#$nsswitch)
685 ("profile" ,#~#$profile)
686 ("bashrc" ,#~#$bashrc)
687 ("hosts" ,#~#$(or (operating-system-hosts-file os)
688 (default-/etc/hosts (operating-system-host-name os))))
689 ;; Write the operating-system-host-name to /etc/hostname to prevent
690 ;; NetworkManager from changing the system's hostname when connecting
691 ;; to certain networks. Some discussion at
692 ;; https://lists.gnu.org/archive/html/help-guix/2017-09/msg00037.html
693 ("hostname" ,(plain-file "hostname" (operating-system-host-name os)))
694 ("localtime" ,(file-append tzdata "/share/zoneinfo/"
695 (operating-system-timezone os)))
696 ("sudoers" ,(operating-system-sudoers-file os))))))
698 (define %root-account
699 ;; Default root account.
703 (uid 0) (group "root")
704 (comment "System administrator")
705 (home-directory "/root")))
707 (define (operating-system-accounts os)
708 "Return the user accounts for OS, including an obligatory 'root' account,
709 and excluding accounts requested by services."
710 ;; Make sure there's a root account.
711 (if (find (lambda (user)
712 (and=> (user-account-uid user) zero?))
713 (operating-system-users os))
714 (operating-system-users os)
715 (cons %root-account (operating-system-users os))))
717 (define (maybe-string->file file-name thing)
718 "If THING is a string, return a <plain-file> with THING as its content.
719 Otherwise just return THING.
721 This is for backward-compatibility of fields that used to be strings and are
722 now file-like objects.."
725 (warning (G_ "using a string for file '~a' is deprecated; \
726 use 'plain-file' instead~%")
728 (plain-file file-name thing))
732 (define (maybe-file->monadic file-name thing)
733 "If THING is a value in %STORE-MONAD, return it as is; otherwise return
734 THING in the %STORE-MONAD.
736 This is for backward-compatibility of fields that used to be monadic values
737 and are now file-like objects."
738 (with-monad %store-monad
741 (warning (G_ "using a monadic value for '~a' is deprecated; \
742 use 'plain-file' instead~%")
748 (define (operating-system-etc-directory os)
749 "Return that static part of the /etc directory of OS."
751 (fold-services (operating-system-services os)
752 #:target-type etc-service-type)))
754 (define (operating-system-environment-variables os)
755 "Return the environment variables of OS for
756 @var{session-environment-service-type}, to be used in @file{/etc/environment}."
757 `(("LANG" . ,(operating-system-locale os))
758 ;; Note: No need to set 'TZ' since (1) we provide /etc/localtime, and (2)
759 ;; it doesn't work for setuid binaries. See <https://bugs.gnu.org/29212>.
760 ("TZDIR" . ,(file-append tzdata "/share/zoneinfo"))
761 ;; Tell 'modprobe' & co. where to look for modules.
762 ("LINUX_MODULE_DIRECTORY" . "/run/booted-system/kernel/lib/modules")
763 ;; These variables are honored by OpenSSL (libssl) and Git.
764 ("SSL_CERT_DIR" . "/etc/ssl/certs")
765 ("SSL_CERT_FILE" . "/etc/ssl/certs/ca-certificates.crt")
766 ("GIT_SSL_CAINFO" . "/etc/ssl/certs/ca-certificates.crt")
768 ;; 'GTK_DATA_PREFIX' must name one directory where GTK+ themes are
770 ("GTK_DATA_PREFIX" . "/run/current-system/profile")
772 ;; By default, applications that use D-Bus, such as Emacs, abort at startup
773 ;; when /etc/machine-id is missing. Make sure these warnings are non-fatal.
774 ("DBUS_FATAL_WARNINGS" . "0")
776 ;; XXX: Normally we wouldn't need to do this, but our glibc@2.23 package
777 ;; used to look things up in 'PREFIX/lib/locale' instead of
778 ;; '/run/current-system/locale' as was intended. Keep this hack around so
779 ;; that people who still have glibc@2.23-using packages in their profiles
780 ;; can use them correctly.
781 ;; TODO: Remove when glibc@2.23 is long gone.
782 ("GUIX_LOCPATH" . "/run/current-system/locale")))
784 (define %setuid-programs
785 ;; Default set of setuid-root programs.
786 (let ((shadow (@ (gnu packages admin) shadow)))
787 (list (file-append shadow "/bin/passwd")
788 (file-append shadow "/bin/su")
789 (file-append shadow "/bin/newuidmap")
790 (file-append shadow "/bin/newgidmap")
791 (file-append inetutils "/bin/ping")
792 (file-append inetutils "/bin/ping6")
793 (file-append sudo "/bin/sudo")
794 (file-append fuse "/bin/fusermount"))))
796 (define %sudoers-specification
797 ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel'
798 ;; group can do anything. See
799 ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>.
800 ;; TODO: Add a declarative API.
801 (plain-file "sudoers" "\
803 %wheel ALL=(ALL) ALL\n"))
805 (define* (operating-system-activation-script os #:key container?)
806 "Return the activation script for OS---i.e., the code that \"activates\" the
807 stateful part of OS, including user accounts and groups, special directories,
809 (let* ((services (operating-system-services os #:container? container?))
810 (activation (fold-services services
811 #:target-type activation-service-type)))
812 (activation-service->script activation)))
814 (define* (operating-system-boot-script os #:key container?)
815 "Return the boot script for OS---i.e., the code started by the initrd once
816 we're running in the final root. When CONTAINER? is true, skip all
817 hardware-related operations as necessary when booting a Linux container."
818 (let* ((services (operating-system-services os #:container? container?))
819 (boot (fold-services services #:target-type boot-service-type)))
820 (service-value boot)))
822 (define (operating-system-user-accounts os)
823 "Return the list of user accounts of OS."
824 (let* ((services (operating-system-services os))
825 (account (fold-services services
826 #:target-type account-service-type)))
827 (filter user-account?
828 (service-value account))))
830 (define (operating-system-shepherd-service-names os)
831 "Return the list of Shepherd service names for OS."
832 (append-map shepherd-service-provision
834 (fold-services (operating-system-services os)
836 shepherd-root-service-type))))
838 (define* (operating-system-derivation os #:key container?)
839 "Return a derivation that builds OS."
840 (let* ((services (operating-system-services os #:container? container?))
841 (system (fold-services services)))
842 ;; SYSTEM contains the derivation as a monadic value.
843 (service-value system)))
845 (define* (operating-system-profile os #:key container?)
846 "Return a derivation that builds the system profile of OS."
848 ((services -> (operating-system-services os #:container? container?))
849 (profile (fold-services services
850 #:target-type profile-service-type)))
855 (define (operating-system-root-file-system os)
856 "Return the root file system of OS."
858 (string=? "/" (file-system-mount-point fs)))
859 (operating-system-file-systems os)))
861 (define (operating-system-initrd-file os)
862 "Return a gexp denoting the initrd file of OS."
863 (define boot-file-systems
864 (filter file-system-needed-for-boot?
865 (operating-system-file-systems os)))
867 (define mapped-devices
868 (operating-system-boot-mapped-devices os))
871 (operating-system-initrd os))
873 (make-initrd boot-file-systems
874 #:linux (operating-system-kernel os)
876 (operating-system-initrd-modules os)
877 #:mapped-devices mapped-devices))
879 (define (locale-name->definition* name)
880 "Variant of 'locale-name->definition' that raises an error upon failure."
881 (match (locale-name->definition name)
885 (message (format #f (G_ "~a: invalid locale name") name))))))
888 (define (operating-system-locale-directory os)
889 "Return the directory containing the locales compiled for the definitions
890 listed in OS. The C library expects to find it under
891 /run/current-system/locale."
893 (operating-system-locale os))
896 ;; While we're at it, check whether NAME is defined and add it if needed.
897 (if (member name (map locale-definition-name
898 (operating-system-locale-definitions os)))
899 (operating-system-locale-definitions os)
900 (cons (locale-name->definition* name)
901 (operating-system-locale-definitions os))))
903 (locale-directory definitions
904 #:libcs (operating-system-locale-libcs os)))
906 (define (kernel->boot-label kernel)
907 "Return a label for the bootloader menu entry that boots KERNEL."
908 (string-append "GNU with "
909 (string-titlecase (package-name kernel)) " "
910 (package-version kernel)
913 (define (store-file-system file-systems)
914 "Return the file system object among FILE-SYSTEMS that contains the store."
915 (match (filter (lambda (fs)
916 (and (file-system-mount? fs)
917 (not (memq 'bind-mount (file-system-flags fs)))
918 (string-prefix? (file-system-mount-point fs)
921 ((and candidates (head . tail))
922 (reduce (lambda (fs1 fs2)
923 (if (> (string-length (file-system-mount-point fs1))
924 (string-length (file-system-mount-point fs2)))
930 (define (operating-system-store-file-system os)
931 "Return the file system that contains the store of OS."
932 (store-file-system (operating-system-file-systems os)))
934 (define* (operating-system-bootcfg os #:optional (old-entries '()))
935 "Return the bootloader configuration file for OS. Use OLD-ENTRIES,
936 a list of <menu-entry>, to populate the \"old entries\" menu."
937 (let* ((root-fs (operating-system-root-file-system os))
938 (root-device (file-system-device root-fs))
939 (params (operating-system-boot-parameters
941 #:system-kernel-arguments? #t))
942 (entry (boot-parameters->menu-entry params))
943 (bootloader-conf (operating-system-bootloader os)))
944 (define generate-config-file
945 (bootloader-configuration-file-generator
946 (bootloader-configuration-bootloader bootloader-conf)))
948 (generate-config-file bootloader-conf (list entry)
949 #:old-entries old-entries)))
951 (define* (operating-system-boot-parameters os root-device
952 #:key system-kernel-arguments?)
953 "Return a monadic <boot-parameters> record that describes the boot
954 parameters of OS. When SYSTEM-KERNEL-ARGUMENTS? is true, add kernel arguments
955 such as '--root' and '--load' to <boot-parameters>."
956 (let* ((initrd (operating-system-initrd-file os))
957 (store (operating-system-store-file-system os))
958 (bootloader (bootloader-configuration-bootloader
959 (operating-system-bootloader os)))
960 (bootloader-name (bootloader-name bootloader))
961 (label (kernel->boot-label (operating-system-kernel os))))
964 (root-device root-device)
965 (kernel (operating-system-kernel-file os))
967 (if system-kernel-arguments?
968 (operating-system-kernel-arguments os root-device)
969 (operating-system-user-kernel-arguments os)))
971 (bootloader-name bootloader-name)
972 (store-device (ensure-not-/dev (file-system-device store)))
973 (store-mount-point (file-system-mount-point store)))))
975 (define (device->sexp device)
976 "Serialize DEVICE as an sexp (really, as an object with a read syntax.)"
979 `(uuid ,(uuid-type uuid) ,(uuid-bytevector uuid)))
980 ((? file-system-label? label)
981 `(file-system-label ,(file-system-label->string label)))
985 (define* (operating-system-boot-parameters-file os
986 #:key system-kernel-arguments?)
987 "Return a file that describes the boot parameters of OS. The primary use of
988 this file is the reconstruction of GRUB menu entries for old configurations.
990 When SYSTEM-KERNEL-ARGUMENTS? is true, add kernel arguments such as '--root'
991 and '--load' to the returned file (since the returned file is then usually
992 stored into the content-addressed \"system\" directory, it's usually not a
993 good idea to give it because the content hash would change by the content hash
994 being stored into the \"parameters\" file)."
995 (let* ((root (operating-system-root-file-system os))
996 (device (file-system-device root))
997 (params (operating-system-boot-parameters
999 #:system-kernel-arguments?
1000 system-kernel-arguments?)))
1001 (gexp->file "parameters"
1004 (label #$(boot-parameters-label params))
1007 (boot-parameters-root-device params)))
1008 (kernel #$(boot-parameters-kernel params))
1010 #$(boot-parameters-kernel-arguments params))
1011 (initrd #$(boot-parameters-initrd params))
1012 (bootloader-name #$(boot-parameters-bootloader-name params))
1015 #$(device->sexp (boot-parameters-store-device params)))
1016 (mount-point #$(boot-parameters-store-mount-point params))))
1017 #:set-load-path? #f)))
1019 (define-gexp-compiler (operating-system-compiler (os <operating-system>)
1023 ;; XXX: This is not super elegant but we can't pass SYSTEM and TARGET to
1024 ;; 'operating-system-derivation'.
1025 (run-with-store store (operating-system-derivation os)
1027 #:target target)))))
1029 ;;; system.scm ends here