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