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