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