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