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