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