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