gnu: xsecurelock: Remove input labels.
[jackhill/guix/guix.git] / gnu / system.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013-2022 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 © 2019, 2020 Miguel Ángel Arruga Vivas <rosen644835@gmail.com>
9 ;;; Copyright © 2020 Danny Milosavljevic <dannym@scratchpost.org>
10 ;;; Copyright © 2020, 2021 Brice Waegeneire <brice@waegenei.re>
11 ;;; Copyright © 2020 Florian Pelz <pelzflorian@pelzflorian.de>
12 ;;; Copyright © 2020, 2022 Maxim Cournoyer <maxim.cournoyer@gmail.com>
13 ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <jannek@gnu.org>
14 ;;; Copyright © 2020, 2022 Efraim Flashner <efraim@flashner.co.il>
15 ;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
16 ;;; Copyright © 2021 raid5atemyhomework <raid5atemyhomework@protonmail.com>
17 ;;;
18 ;;; This file is part of GNU Guix.
19 ;;;
20 ;;; GNU Guix is free software; you can redistribute it and/or modify it
21 ;;; under the terms of the GNU General Public License as published by
22 ;;; the Free Software Foundation; either version 3 of the License, or (at
23 ;;; your option) any later version.
24 ;;;
25 ;;; GNU Guix is distributed in the hope that it will be useful, but
26 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
27 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 ;;; GNU General Public License for more details.
29 ;;;
30 ;;; You should have received a copy of the GNU General Public License
31 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
32
33 (define-module (gnu system)
34 #:use-module (guix inferior)
35 #:use-module (guix store)
36 #:use-module (guix memoization)
37 #:use-module (guix monads)
38 #:use-module (guix gexp)
39 #:use-module (guix records)
40 #:use-module (guix packages)
41 #:use-module (guix derivations)
42 #:use-module (guix profiles)
43 #:use-module ((guix utils) #:select (substitute-keyword-arguments))
44 #:use-module (guix i18n)
45 #:use-module (guix diagnostics)
46 #:use-module (guix ui)
47 #:use-module (gnu packages admin)
48 #:use-module (gnu packages base)
49 #:use-module (gnu packages bash)
50 #:use-module (gnu packages compression)
51 #:use-module (gnu packages cross-base)
52 #:use-module (gnu packages cryptsetup)
53 #:use-module (gnu packages disk)
54 #:use-module (gnu packages file-systems)
55 #:use-module (gnu packages firmware)
56 #:use-module (gnu packages gawk)
57 #:use-module (gnu packages guile)
58 #:use-module (gnu packages guile-xyz)
59 #:use-module (gnu packages hurd)
60 #:use-module (gnu packages less)
61 #:use-module (gnu packages linux)
62 #:use-module (gnu packages man)
63 #:use-module (gnu packages nano)
64 #:use-module (gnu packages nvi)
65 #:use-module (gnu packages package-management)
66 #:use-module (gnu packages pciutils)
67 #:use-module (gnu packages texinfo)
68 #:use-module (gnu packages text-editors)
69 #:use-module (gnu packages wget)
70 #:use-module (gnu services)
71 #:use-module (gnu services shepherd)
72 #:use-module (gnu services base)
73 #:use-module (gnu bootloader)
74 #:use-module (gnu system shadow)
75 #:use-module (gnu system nss)
76 #:use-module (gnu system locale)
77 #:use-module (gnu system pam)
78 #:use-module (gnu system linux-initrd)
79 #:use-module (gnu system setuid)
80 #:use-module (gnu system uuid)
81 #:use-module (gnu system file-systems)
82 #:use-module (gnu system mapped-devices)
83 #:use-module (ice-9 format)
84 #:use-module (ice-9 match)
85 #:use-module (srfi srfi-1)
86 #:use-module (srfi srfi-26)
87 #:use-module (srfi srfi-34)
88 #:use-module (srfi srfi-35)
89 #:use-module (srfi srfi-71)
90 #:use-module (rnrs bytevectors)
91 #:export (operating-system
92 operating-system?
93 this-operating-system
94
95 operating-system-bootloader
96 operating-system-services
97 operating-system-essential-services
98 operating-system-default-essential-services
99 operating-system-user-services
100 operating-system-packages
101 operating-system-host-name
102 operating-system-hosts-file
103 operating-system-hurd
104 operating-system-kernel
105 operating-system-kernel-file
106 operating-system-kernel-arguments
107 operating-system-label
108 operating-system-default-label
109 operating-system-initrd-modules
110 operating-system-initrd
111 operating-system-users
112 operating-system-groups
113 operating-system-issue
114 operating-system-timezone
115 operating-system-locale
116 operating-system-locale-definitions
117 operating-system-locale-libcs
118 operating-system-mapped-devices
119 operating-system-file-systems
120 operating-system-store-file-system
121 operating-system-user-mapped-devices
122 operating-system-boot-mapped-devices
123 operating-system-bootloader-crypto-devices
124 operating-system-activation-script
125 operating-system-user-accounts
126 operating-system-shepherd-service-names
127 operating-system-user-kernel-arguments
128 operating-system-firmware
129 operating-system-keyboard-layout
130 operating-system-name-service-switch
131 operating-system-pam-services
132 operating-system-setuid-programs
133 operating-system-skeletons
134 operating-system-sudoers-file
135 operating-system-swap-devices
136 operating-system-kernel-loadable-modules
137 operating-system-location
138
139 operating-system-derivation
140 operating-system-profile
141 operating-system-bootcfg
142 operating-system-etc-directory
143 operating-system-locale-directory
144 operating-system-boot-script
145 operating-system-uuid
146
147 system-linux-image-file-name
148 operating-system-with-gc-roots
149 operating-system-with-provenance
150
151 hurd-default-essential-services
152
153 boot-parameters
154 boot-parameters?
155 boot-parameters-label
156 boot-parameters-root-device
157 boot-parameters-bootloader-name
158 boot-parameters-bootloader-menu-entries
159 boot-parameters-store-crypto-devices
160 boot-parameters-store-device
161 boot-parameters-store-directory-prefix
162 boot-parameters-store-mount-point
163 boot-parameters-locale
164 boot-parameters-kernel
165 boot-parameters-kernel-arguments
166 boot-parameters-initrd
167 boot-parameters-multiboot-modules
168 boot-parameters-version
169 %boot-parameters-version
170 read-boot-parameters
171 read-boot-parameters-file
172 boot-parameters->menu-entry
173
174 local-host-aliases
175 %root-account
176 %setuid-programs
177 %sudoers-specification
178 %base-packages
179 %base-packages-artwork
180 %base-packages-interactive
181 %base-packages-linux
182 %base-packages-networking
183 %base-packages-disk-utilities
184 %base-packages-utils
185 %base-firmware
186 %default-kernel-arguments))
187
188 ;;; Commentary:
189 ;;;
190 ;;; This module supports whole-system configuration.
191 ;;;
192 ;;; Code:
193
194 (define* (bootable-kernel-arguments system root-device version)
195 "Return a list of kernel arguments (gexps) to boot SYSTEM from ROOT-DEVICE.
196 VERSION is the target version of the boot-parameters record."
197 ;; If the version is newer than 0, we use the new style initrd parameter
198 ;; names, otherwise we use the legacy ones. This is to maintain backward
199 ;; compatibility when producing bootloader configurations for older
200 ;; generations.
201 (define version>0? (> version 0))
202 (list (string-append (if version>0? "root=" "--root=")
203 ;; Note: Always use the DCE format because that's what
204 ;; (gnu build linux-boot) expects for the 'root'
205 ;; kernel command-line option.
206 (file-system-device->string root-device
207 #:uuid-type 'dce))
208 #~(string-append (if #$version>0? "gnu.system=" "--system=") #$system)
209 #~(string-append (if #$version>0? "gnu.load=" "--load=")
210 #$system "/boot")))
211
212 ;; System-wide configuration.
213 ;; TODO: Add per-field docstrings/stexi.
214 (define-record-type* <operating-system> operating-system
215 make-operating-system
216 operating-system?
217 this-operating-system
218
219 (kernel operating-system-kernel ; package
220 (default linux-libre))
221 (kernel-loadable-modules operating-system-kernel-loadable-modules
222 (default '())) ; list of packages
223 (kernel-arguments operating-system-user-kernel-arguments
224 (default %default-kernel-arguments)) ; list of gexps/strings
225 (hurd operating-system-hurd
226 (default #f)) ; package
227 (bootloader operating-system-bootloader) ; <bootloader-configuration>
228 (label operating-system-label ; string
229 (thunked)
230 (default (operating-system-default-label this-operating-system)))
231
232 (keyboard-layout operating-system-keyboard-layout ;#f | <keyboard-layout>
233 (default #f))
234 (initrd operating-system-initrd ; (list fs) -> file-like
235 (default base-initrd))
236 (initrd-modules operating-system-initrd-modules ; list of strings
237 (thunked) ; it's system-dependent
238 (default %base-initrd-modules))
239
240 (firmware operating-system-firmware ; list of packages
241 (default %base-firmware))
242
243 (host-name operating-system-host-name) ; string
244 (hosts-file operating-system-hosts-file ; file-like | #f
245 (default #f))
246
247 (mapped-devices operating-system-mapped-devices ; list of <mapped-device>
248 (default '()))
249 (file-systems operating-system-file-systems) ; list of fs
250 (swap-devices operating-system-swap-devices ; list of string | <swap-space>
251 (default '())
252 (delayed)
253 (sanitize warn-swap-devices-change))
254
255 (users operating-system-users ; list of user accounts
256 (default %base-user-accounts))
257 (groups operating-system-groups ; list of user groups
258 (default %base-groups))
259
260 (skeletons operating-system-skeletons ; list of name/file-like value
261 (default (default-skeletons)))
262 (issue operating-system-issue ; string
263 (default %default-issue))
264
265 (packages operating-system-packages ; list of (PACKAGE OUTPUT...)
266 (default %base-packages)) ; or just PACKAGE
267
268 (timezone operating-system-timezone) ; string
269 (locale operating-system-locale ; string
270 (default "en_US.utf8"))
271 (locale-definitions operating-system-locale-definitions ; list of <locale-definition>
272 (default %default-locale-definitions))
273 (locale-libcs operating-system-locale-libcs ; list of <packages>
274 (default %default-locale-libcs))
275 (name-service-switch operating-system-name-service-switch ; <name-service-switch>
276 (default %default-nss))
277
278 (essential-services operating-system-essential-services ; list of services
279 (thunked)
280 (default (operating-system-default-essential-services
281 this-operating-system)))
282 (services operating-system-user-services ; list of services
283 (thunked) ;allow for system-dependent services
284 (default %base-services))
285
286 (pam-services operating-system-pam-services ; list of PAM services
287 (default (base-pam-services)))
288 (setuid-programs operating-system-setuid-programs
289 (default %setuid-programs) ; list of <setuid-program>
290 (sanitize ensure-setuid-program-list))
291
292 (sudoers-file operating-system-sudoers-file ; file-like
293 (default %sudoers-specification))
294
295 (location operating-system-location ; <location>
296 (default (and=> (current-source-location)
297 source-properties->location))
298 (innate)))
299
300 (define* (operating-system-kernel-arguments
301 os root-device #:key (version %boot-parameters-version))
302 "Return all the kernel arguments, including the ones not specified directly
303 by the user. VERSION should match that of the target <boot-parameter> record
304 object that will contain the kernel parameters."
305 (append (bootable-kernel-arguments os root-device version)
306 (operating-system-user-kernel-arguments os)))
307
308 \f
309 ;;;
310 ;;; Boot parameters
311 ;;;
312
313 ;;; Version 1 was introduced early 2022 to mark the departure from long option
314 ;;; names such as '--load' to the more conventional initrd option names like
315 ;;; 'gnu.load'.
316 ;;;
317 ;;; When bumping the boot-parameters version, increment it by one (1).
318 (define %boot-parameters-version 1)
319
320 (define-record-type* <boot-parameters>
321 boot-parameters make-boot-parameters boot-parameters?
322 (label boot-parameters-label)
323 ;; Because we will use the 'store-device' to create the GRUB search command,
324 ;; the 'store-device' has slightly different semantics than 'root-device'.
325 ;; The 'store-device' can be a file system uuid, a file system label, or #f,
326 ;; but it cannot be a device file name such as "/dev/sda3", since GRUB would
327 ;; not understand that. The 'root-device', on the other hand, corresponds
328 ;; exactly to the device field of the <file-system> object representing the
329 ;; OS's root file system, so it might be a device file name like
330 ;; "/dev/sda3". The 'store-directory-prefix' field contains #f or the store
331 ;; file name inside the 'store-device' as it is seen by GRUB, e.g. it would
332 ;; contain "/storefs" if the store is located in that subvolume of a btrfs
333 ;; partition.
334 (root-device boot-parameters-root-device)
335 (bootloader-name boot-parameters-bootloader-name)
336 (bootloader-menu-entries ;list of <menu-entry>
337 boot-parameters-bootloader-menu-entries)
338 (store-device boot-parameters-store-device)
339 (store-mount-point boot-parameters-store-mount-point)
340 (store-directory-prefix boot-parameters-store-directory-prefix)
341 (store-crypto-devices boot-parameters-store-crypto-devices
342 (default '()))
343 (locale boot-parameters-locale)
344 (kernel boot-parameters-kernel)
345 (kernel-arguments boot-parameters-kernel-arguments)
346 (initrd boot-parameters-initrd)
347 (multiboot-modules boot-parameters-multiboot-modules)
348 (version boot-parameters-version ;positive integer
349 (default %boot-parameters-version)))
350
351 (define (ensure-not-/dev device)
352 "If DEVICE starts with a slash, return #f. This is meant to filter out
353 Linux device names such as /dev/sda, and to preserve GRUB device names and
354 file system labels."
355 (if (and (string? device) (string-prefix? "/" device))
356 #f
357 device))
358
359 (define (read-boot-parameters port)
360 "Read boot parameters from PORT and return the corresponding
361 <boot-parameters> object. Raise an error if the format is unrecognized."
362 (define device-sexp->device
363 (match-lambda
364 (('uuid (? symbol? type) (? bytevector? bv))
365 (bytevector->uuid bv type))
366 (('file-system-label (? string? label))
367 (file-system-label label))
368 ((? bytevector? bv) ;old format
369 (bytevector->uuid bv 'dce))
370 ((? string? device)
371 (if (string-contains device ":/")
372 device ; nfs-root
373 ;; It used to be that we would not distinguish between labels and
374 ;; device names. Try to infer the right thing here.
375 (if (string-prefix? "/" device)
376 device
377 (file-system-label device))))))
378 (define uuid-sexp->uuid
379 (match-lambda
380 (('uuid (? symbol? type) (? bytevector? bv))
381 (bytevector->uuid bv type))
382 (x
383 (warning (G_ "unrecognized uuid ~a at '~a'~%") x (port-filename port))
384 #f)))
385
386 ;; New versions are not backward-compatible, so only accept past and current
387 ;; versions, not future ones.
388 (define (version? n)
389 (member n (iota (1+ %boot-parameters-version))))
390
391 (match (read port)
392 (('boot-parameters ('version (? version? version))
393 ('label label) ('root-device root)
394 ('kernel kernel)
395 rest ...)
396 (boot-parameters
397 (version version)
398 (label label)
399 (root-device (device-sexp->device root))
400
401 (bootloader-name
402 (match (assq 'bootloader-name rest)
403 ((_ args) args)
404 (#f 'grub))) ; for compatibility reasons.
405
406 (bootloader-menu-entries
407 (match (assq 'bootloader-menu-entries rest)
408 ((_ entries) (map sexp->menu-entry entries))
409 (#f '())))
410
411 ;; In the past, we would store the directory name of linux instead of
412 ;; the absolute file name of its image. Detect that and correct it.
413 (kernel (if (string=? kernel (direct-store-path kernel))
414 (string-append kernel "/"
415 (system-linux-image-file-name))
416 kernel))
417
418 (kernel-arguments
419 (match (assq 'kernel-arguments rest)
420 ((_ args) args)
421 (#f '()))) ;the old format
422
423 (initrd
424 (match (assq 'initrd rest)
425 (('initrd ('string-append directory file)) ;the old format
426 (string-append directory file))
427 (('initrd (? string? file))
428 file)
429 (#f #f)))
430
431 (multiboot-modules
432 (match (assq 'multiboot-modules rest)
433 ((_ args) args)
434 (#f '())))
435
436 (locale
437 (match (assq 'locale rest)
438 ((_ locale) locale)
439 (#f #f)))
440
441 (store-device
442 ;; Linux device names like "/dev/sda1" are not suitable GRUB device
443 ;; identifiers, so we just filter them out.
444 (ensure-not-/dev
445 (match (assq 'store rest)
446 (('store ('device #f) _ ...)
447 root-device)
448 (('store ('device device) _ ...)
449 (device-sexp->device device))
450 (_ ;the old format
451 root-device))))
452
453 (store-directory-prefix
454 (match (assq 'store rest)
455 (('store . store-data)
456 (match (assq 'directory-prefix store-data)
457 (('directory-prefix prefix) prefix)
458 ;; No directory-prefix found.
459 (_ #f)))
460 (_
461 ;; No store found, old format.
462 #f)))
463
464 (store-crypto-devices
465 (match (assq 'store rest)
466 (('store . store-data)
467 (match (assq 'crypto-devices store-data)
468 (('crypto-devices (devices ...))
469 (map uuid-sexp->uuid devices))
470 (('crypto-devices dev)
471 (warning (G_ "unrecognized crypto-devices ~S at '~a'~%")
472 dev (port-filename port))
473 '())
474 (_
475 ;; No crypto-devices found.
476 '())))
477 (_
478 ;; No store found, old format.
479 '())))
480
481 (store-mount-point
482 (match (assq 'store rest)
483 (('store ('device _) ('mount-point mount-point) _ ...)
484 mount-point)
485 (_ ;the old format
486 "/")))))
487 (x ;unsupported format
488 (raise
489 (make-compound-condition
490 (formatted-message
491 (G_ "unrecognized boot parameters at '~a'~%")
492 (port-filename port))
493 (condition
494 (&fix-hint (hint (format #f (G_ "This probably means that this version
495 of Guix is older than the one that created @file{~a}. To address this, you
496 need to update Guix:
497
498 @example
499 guix pull
500 @end example")
501 (port-filename port))))))))))
502
503 (define (read-boot-parameters-file system)
504 "Read boot parameters from SYSTEM's (system or generation) \"parameters\"
505 file and returns the corresponding <boot-parameters> object or #f if the
506 format is unrecognized.
507 The object has its kernel-arguments extended in order to make it bootable."
508 (let* ((file (string-append system "/parameters"))
509 (params (call-with-input-file file read-boot-parameters))
510 (root (boot-parameters-root-device params))
511 (version (boot-parameters-version params)))
512 (boot-parameters
513 (inherit params)
514 (kernel-arguments (append (bootable-kernel-arguments system root version)
515 (boot-parameters-kernel-arguments params))))))
516
517 (define (boot-parameters->menu-entry conf)
518 (let* ((kernel (boot-parameters-kernel conf))
519 (multiboot-modules (boot-parameters-multiboot-modules conf))
520 (multiboot? (pair? multiboot-modules)))
521 (menu-entry
522 (label (boot-parameters-label conf))
523 (device (boot-parameters-store-device conf))
524 (device-mount-point (boot-parameters-store-mount-point conf))
525 (linux (and (not multiboot?) kernel))
526 (linux-arguments (if (not multiboot?)
527 (boot-parameters-kernel-arguments conf)
528 '()))
529 (initrd (boot-parameters-initrd conf))
530 (multiboot-kernel (and multiboot? kernel))
531 (multiboot-arguments (if multiboot?
532 (boot-parameters-kernel-arguments conf)
533 '()))
534 (multiboot-modules (if multiboot?
535 (boot-parameters-multiboot-modules conf)
536 '())))))
537
538 \f
539 ;;;
540 ;;; Services.
541 ;;;
542
543 (define (non-boot-file-system-service os)
544 "Return the file system service for the file systems of OS that are not
545 marked as 'needed-for-boot'."
546 (define file-systems
547 (remove file-system-needed-for-boot?
548 (operating-system-file-systems os)))
549
550 (define mapped-devices-for-boot
551 (operating-system-boot-mapped-devices os))
552
553 (define (device-mappings fs)
554 (let ((device (file-system-device fs)))
555 (if (string? device) ;title is 'device
556 (filter (lambda (md)
557 (any (cut string=? device <>)
558 (map (cut string-append "/dev/mapper" <>)
559 (mapped-device-targets md))))
560 (operating-system-mapped-devices os))
561 '())))
562
563 (define (add-dependencies fs)
564 ;; Add the dependencies due to device mappings to FS.
565 (file-system
566 (inherit fs)
567 (dependencies
568 (delete-duplicates
569 (remove (cut member <> mapped-devices-for-boot)
570 (append (device-mappings fs)
571 (file-system-dependencies fs)))
572 eq?))))
573
574 (service file-system-service-type
575 (map add-dependencies file-systems)))
576
577 (define (mapped-device-users device file-systems)
578 "Return the subset of FILE-SYSTEMS that use DEVICE."
579 (let ((targets (map (cut string-append "/dev/mapper/" <>)
580 (mapped-device-targets device))))
581 (filter (lambda (fs)
582 (or (member device (file-system-dependencies fs))
583 (and (string? (file-system-device fs))
584 (any (cut string=? (file-system-device fs) <>) targets))))
585 file-systems)))
586
587 (define (operating-system-user-mapped-devices os)
588 "Return the subset of mapped devices that can be installed in
589 user-land--i.e., those not needed during boot."
590 (let ((devices (operating-system-mapped-devices os))
591 (file-systems (operating-system-file-systems os)))
592 (filter (lambda (md)
593 (let ((users (mapped-device-users md file-systems)))
594 (not (any file-system-needed-for-boot? users))))
595 devices)))
596
597 (define (operating-system-boot-mapped-devices os)
598 "Return the subset of mapped devices that must be installed during boot,
599 from the initrd."
600 (let ((devices (operating-system-mapped-devices os))
601 (file-systems (operating-system-file-systems os)))
602 (filter (lambda (md)
603 (let ((users (mapped-device-users md file-systems)))
604 (any file-system-needed-for-boot? users)))
605 devices)))
606
607 (define operating-system-bootloader-crypto-devices
608 (mlambdaq (os) ;to avoid duplicated output
609 "Return the sources of the LUKS mapped devices specified by UUID."
610 ;; XXX: Device ordering is important, we trust the returned one.
611 (let* ((luks-devices (filter (lambda (m)
612 (eq? luks-device-mapping
613 (mapped-device-type m)))
614 (operating-system-boot-mapped-devices os)))
615 (uuid-crypto-devices non-uuid-crypto-devices
616 (partition (compose uuid? mapped-device-source)
617 luks-devices)))
618 (when (not (null? non-uuid-crypto-devices))
619 (for-each (lambda (dev)
620 (warning
621 (source-properties->location (mapped-device-location dev))
622 (G_ "mapped device '~a' may be ignored by bootloader~%")
623 (mapped-device-source dev)))
624 non-uuid-crypto-devices)
625 (display-hint "Specify mapped device sources via their LUKS UUID."))
626 (map mapped-device-source uuid-crypto-devices))))
627
628 (define (device-mapping-services os)
629 "Return the list of device-mapping services for OS as a list."
630 (map device-mapping-service
631 (operating-system-user-mapped-devices os)))
632
633 (define-syntax-rule (warn-swap-devices-change value)
634 (%warn-swap-devices-change value (current-source-location)))
635
636 (define (%warn-swap-devices-change value location)
637 (map (lambda (x)
638 (unless (swap-space? x)
639 (warning
640 (source-properties->location
641 location)
642 (G_ "List elements of the field 'swap-devices' should \
643 now use the <swap-space> record, as the old method is deprecated. \
644 See \"(guix) operating-system Reference\" for more details.~%")))
645 x) value))
646
647 (define (swap-services os)
648 "Return the list of swap services for OS."
649 (define early-userspace-file-systems
650 (filter file-system-needed-for-boot?
651 (operating-system-file-systems os)))
652
653 (define early-userspace-mapped-devices
654 (operating-system-boot-mapped-devices os))
655
656 (define (filter-deps swap)
657 (if (swap-space? swap)
658 (swap-space
659 (inherit swap)
660 (dependencies (remove (lambda (dep)
661 (or (member dep early-userspace-mapped-devices)
662 (member dep early-userspace-file-systems)))
663 (swap-space-dependencies swap))))
664 swap))
665
666 (map (compose swap-service filter-deps)
667 (operating-system-swap-devices os)))
668
669 (define* (system-linux-image-file-name #:optional
670 (target (or (%current-target-system)
671 (%current-system))))
672 "Return the basename of the kernel image file for TARGET."
673 (cond
674 ((string-prefix? "arm" target) "zImage")
675 ((string-prefix? "mips" target) "vmlinuz")
676 ((string-prefix? "aarch64" target) "Image")
677 ((string-prefix? "riscv64" target) "Image")
678 (else "bzImage")))
679
680 (define (operating-system-kernel-file os)
681 "Return an object representing the absolute file name of the kernel image of
682 OS."
683 (if (operating-system-hurd os)
684 (file-append (operating-system-kernel os) "/boot/gnumach")
685 (file-append (operating-system-kernel os)
686 "/" (system-linux-image-file-name))))
687
688 (define %default-modprobe-blacklist
689 ;; List of kernel modules to blacklist by default.
690 '("usbmouse" ;races with bcm5974, see <https://bugs.gnu.org/35574>
691 "usbkbd")) ;races with usbhid, see <https://issues.guix.gnu.org/35574#18>
692
693 (define %default-kernel-arguments
694 ;; Default arguments passed to the kernel.
695 (list (string-append "modprobe.blacklist="
696 (string-join %default-modprobe-blacklist ","))
697 "quiet"))
698
699 (define* (operating-system-directory-base-entries os)
700 "Return the basic entries of the 'system' directory of OS for use as the
701 value of the SYSTEM-SERVICE-TYPE service."
702 (let* ((locale (operating-system-locale-directory os))
703 (kernel (operating-system-kernel os))
704 (hurd (operating-system-hurd os))
705 (initrd (and (not hurd) (operating-system-initrd-file os)))
706 (params (operating-system-boot-parameters-file os)))
707 `(,@(if hurd
708 `(("hurd" ,hurd)
709 ("kernel" ,kernel))
710 '())
711 ("parameters" ,params)
712 ,@(if initrd `(("initrd" ,initrd)) '())
713 ("locale" ,locale)))) ;used by libc
714
715 (define (operating-system-default-essential-services os)
716 "Return the list of essential services for OS. These are special services
717 that implement part of what's declared in OS are responsible for low-level
718 bookkeeping."
719 (define known-fs
720 (map file-system-mount-point (operating-system-file-systems os)))
721
722 (let* ((mappings (device-mapping-services os))
723 (root-fs (root-file-system-service))
724 (other-fs (non-boot-file-system-service os))
725 (swaps (swap-services os))
726 (procs (service user-processes-service-type))
727 (host-name (host-name-service (operating-system-host-name os)))
728 (entries (operating-system-directory-base-entries os)))
729 (cons* (service system-service-type entries)
730 (service linux-builder-service-type
731 (linux-builder-configuration
732 (kernel (operating-system-kernel os))
733 (modules (operating-system-kernel-loadable-modules os))))
734 %boot-service
735
736 ;; %SHEPHERD-ROOT-SERVICE must come last so that the gexp that
737 ;; execs shepherd comes last in the boot script (XXX). Likewise,
738 ;; the cleanup service must come first so that its gexp runs before
739 ;; activation code.
740 (service cleanup-service-type #f)
741 %activation-service
742 %shepherd-root-service
743
744 (pam-root-service (operating-system-pam-services os))
745 (account-service (append (operating-system-accounts os)
746 (operating-system-groups os))
747 (operating-system-skeletons os))
748 (operating-system-etc-service os)
749 (service fstab-service-type
750 (filter file-system-needed-for-boot?
751 (operating-system-file-systems os)))
752 (session-environment-service
753 (operating-system-environment-variables os))
754 host-name procs root-fs
755 (service setuid-program-service-type
756 (operating-system-setuid-programs os))
757 (service profile-service-type
758 (operating-system-packages os))
759 other-fs
760 (append mappings swaps
761
762 ;; Add the firmware service.
763 (list %linux-bare-metal-service
764 (service firmware-service-type
765 (operating-system-firmware os)))))))
766
767 (define (hurd-default-essential-services os)
768 (let ((entries (operating-system-directory-base-entries os)))
769 (list (service system-service-type entries)
770 %boot-service
771 %hurd-startup-service
772 %activation-service
773 (service shepherd-root-service-type
774 (shepherd-configuration
775 (shepherd shepherd-0.8))) ;no Fibers
776
777 (service user-processes-service-type)
778 (account-service (append (operating-system-accounts os)
779 (operating-system-groups os))
780 (operating-system-skeletons os))
781 (root-file-system-service)
782 (service file-system-service-type '())
783 (service fstab-service-type
784 (filter file-system-needed-for-boot?
785 (operating-system-file-systems os)))
786 (pam-root-service (operating-system-pam-services os))
787 (operating-system-etc-service os)
788 (service setuid-program-service-type
789 (operating-system-setuid-programs os))
790 (service profile-service-type (operating-system-packages os)))))
791
792 (define* (operating-system-services os)
793 "Return all the services of OS, including \"essential\" services."
794 (instantiate-missing-services
795 (append (operating-system-user-services os)
796 (operating-system-essential-services os))))
797
798 (define (operating-system-with-gc-roots os roots)
799 "Return a variant of OS where ROOTS are registered as GC roots."
800 (operating-system
801 (inherit os)
802
803 ;; We use this procedure for the installation OS, which already defines GC
804 ;; roots. Add ROOTS to those.
805 (services (cons (simple-service 'extra-root
806 gc-root-service-type roots)
807 (operating-system-user-services os)))))
808
809 (define (operating-system-configuration-file os)
810 "Return the configuration file of OS, based on its 'location' field, or #f
811 if it could not be determined."
812 (let ((file (and=> (operating-system-location os)
813 location-file)))
814 (and file
815 (or (and (string-prefix? "/" file) file)
816 (search-path %load-path file)))))
817
818 (define* (operating-system-with-provenance os
819 #:optional
820 (config-file
821 (operating-system-configuration-file
822 os)))
823 "Return a variant of OS that stores its own provenance information,
824 including CONFIG-FILE, if available. This is achieved by adding an instance
825 of PROVENANCE-SERVICE-TYPE to its services."
826 (operating-system
827 (inherit os)
828 (services (cons (service provenance-service-type config-file)
829 (operating-system-user-services os)))))
830
831 \f
832 ;;;
833 ;;; /etc.
834 ;;;
835
836 (define %base-firmware
837 ;; Firmware usable by default.
838 (list ath9k-htc-firmware
839 openfwwf-firmware))
840
841 (define %base-packages-artwork
842 ;; Default set of artwork packages.
843 (list guix-icons))
844
845 (define %base-packages-utils
846 ;; Default set of utilities packages.
847 (cons* procps psmisc which
848 (@ (gnu packages admin) shadow-with-man-pages) ;for 'passwd'
849
850 guile-3.0-latest
851
852 ;; The packages below are also in %FINAL-INPUTS, so take them from
853 ;; there to avoid duplication.
854 (list bash coreutils findutils grep sed
855 diffutils patch gawk tar gzip bzip2 xz lzip)))
856
857 (define %base-packages-linux
858 ;; Default set of linux specific packages.
859 (list pciutils usbutils
860 util-linux+udev
861 ;; Get 'insmod' & co. from kmod, not module-init-tools, since udev
862 ;; already depends on it anyway.
863 kmod eudev))
864
865 (define %base-packages-interactive
866 ;; Default set of common interactive packages.
867 (list less mg nano
868 nvi
869 man-db
870 info-reader ;the standalone Info reader (no Perl)
871 bash-completion
872 kbd
873 ;; The 'sudo' command is already in %SETUID-PROGRAMS, but we also
874 ;; want the other commands and the man pages (notably because
875 ;; auto-completion in Emacs shell relies on man pages.)
876 sudo
877 guile-readline guile-colorized))
878
879 (define %base-packages-networking
880 ;; Default set of networking packages.
881 (list inetutils isc-dhcp
882 iproute
883 wget
884 ;; wireless-tools is deprecated in favor of iw, but it's still what
885 ;; many people are familiar with, so keep it around.
886 iw wireless-tools))
887
888 (define %base-packages-disk-utilities
889 ;; A well-rounded set of packages for interacting with disks, partitions
890 ;; and filesystems.
891 (list parted gptfdisk ddrescue
892 ;; We used to provide fdisk from GNU fdisk, but as of version 2.0.0a
893 ;; it pulls Guile 1.8, which takes unreasonable space; furthermore
894 ;; util-linux's fdisk is already available, in %base-packages-linux.
895 cryptsetup mdadm
896 dosfstools
897 btrfs-progs
898 f2fs-tools
899 jfsutils
900 xfsprogs))
901
902 (define %base-packages
903 ;; Default set of packages globally visible. It should include anything
904 ;; required for basic administrator tasks.
905 (append (list e2fsprogs)
906 %base-packages-artwork
907 %base-packages-interactive
908 %base-packages-linux
909 %base-packages-networking
910 %base-packages-utils))
911
912 (define %default-issue
913 ;; Default contents for /etc/issue.
914 "
915 This is the GNU system. Welcome.\n")
916
917 (define (local-host-aliases host-name)
918 "Return aliases for HOST-NAME, to be used in /etc/hosts."
919 (string-append "127.0.0.1 localhost " host-name "\n"
920 "::1 localhost " host-name "\n"))
921
922 (define (default-/etc/hosts host-name)
923 "Return the default /etc/hosts file."
924 (plain-file "hosts" (local-host-aliases host-name)))
925
926 (define (validated-sudoers-file file)
927 "Return a copy of FILE, a sudoers file, after checking that it is
928 syntactically correct."
929 (computed-file "sudoers"
930 (with-imported-modules '((guix build utils))
931 #~(begin
932 (use-modules (guix build utils))
933
934 (invoke #+(file-append sudo "/sbin/visudo")
935 "--check" "--file" #$file)
936 (copy-file #$file #$output)))))
937
938 (define (os-release)
939 (plain-file "os-release"
940 "\
941 NAME=\"Guix System\"
942 ID=guix
943 PRETTY_NAME=\"Guix System\"
944 LOGO=guix-icon
945 HOME_URL=\"https://guix.gnu.org\"
946 DOCUMENTATION_URL=\"https://guix.gnu.org/en/manual\"
947 SUPPORT_URL=\"https://guix.gnu.org/en/help\"
948 BUG_REPORT_URL=\"https://lists.gnu.org/mailman/listinfo/bug-guix\"
949 "))
950
951 (define* (operating-system-etc-service os)
952 "Return a <service> that builds a directory containing the static part of
953 the /etc directory."
954 (let* ((login.defs
955 (plain-file "login.defs"
956 (string-append
957 "# Default paths for non-login shells started by su(1).\n"
958 "ENV_PATH /run/setuid-programs:"
959 "/run/current-system/profile/bin:"
960 "/run/current-system/profile/sbin\n"
961 "ENV_SUPATH /run/setuid-programs:"
962 "/run/current-system/profile/bin:"
963 "/run/current-system/profile/sbin\n"
964
965 "\n"
966 "# Allow 'chfn' to change the full name,\n"
967 "# room number, and so on.\n"
968 "CHFN_RESTRICT frwh\n")))
969
970 (hurd (operating-system-hurd os))
971 (issue (plain-file "issue" (operating-system-issue os)))
972 (nsswitch (operating-system-name-service-switch os))
973 (nsswitch (and nsswitch
974 (plain-file "nsswitch.conf"
975 (name-service-switch->string nsswitch))))
976 (sudoers (operating-system-sudoers-file os))
977
978 ;; Startup file for POSIX-compliant login shells, which set system-wide
979 ;; environment variables.
980 (profile (mixed-text-file "profile" "\
981 # Crucial variables that could be missing in the profiles' 'etc/profile'
982 # because they would require combining both profiles.
983 # FIXME: See <http://bugs.gnu.org/20255>.
984 export MANPATH=$HOME/.guix-profile/share/man:/run/current-system/profile/share/man
985 export INFOPATH=$HOME/.guix-profile/share/info:/run/current-system/profile/share/info
986 export XDG_DATA_DIRS=$HOME/.guix-profile/share:/run/current-system/profile/share
987 export XDG_CONFIG_DIRS=$HOME/.guix-profile/etc/xdg:/run/current-system/profile/etc/xdg
988
989 # Make sure libXcursor finds cursors installed into user or system profiles. See <http://bugs.gnu.org/24445>
990 export XCURSOR_PATH=$HOME/.icons:$HOME/.guix-profile/share/icons:/run/current-system/profile/share/icons
991
992 # Ignore the default value of 'PATH'.
993 unset PATH
994
995 # Load the system profile's settings.
996 GUIX_PROFILE=/run/current-system/profile ; \\
997 . /run/current-system/profile/etc/profile
998
999 # Since 'lshd' does not use pam_env, /etc/environment must be explicitly
1000 # loaded when someone logs in via SSH. See <http://bugs.gnu.org/22175>.
1001 # We need 'PATH' to be defined here, for 'cat' and 'cut'. Do this before
1002 # reading the user's 'etc/profile' to allow variables to be overridden.
1003 if [ -f /etc/environment -a -n \"$SSH_CLIENT\" \\
1004 -a -z \"$LINUX_MODULE_DIRECTORY\" ]
1005 then
1006 . /etc/environment
1007 export `cat /etc/environment | cut -d= -f1`
1008 fi
1009
1010 # Arrange so that ~/.config/guix/current comes first.
1011 for profile in \"$HOME/.guix-profile\" \"$HOME/.config/guix/current\"
1012 do
1013 if [ -f \"$profile/etc/profile\" ]
1014 then
1015 # Load the user profile's settings.
1016 GUIX_PROFILE=\"$profile\" ; \\
1017 . \"$profile/etc/profile\"
1018 else
1019 # At least define this one so that basic things just work
1020 # when the user installs their first package.
1021 export PATH=\"$profile/bin:$PATH\"
1022 fi
1023 done
1024
1025 # Prepend setuid programs.
1026 export PATH=/run/setuid-programs:$PATH
1027
1028 # Arrange so that ~/.config/guix/current/share/info comes first.
1029 export INFOPATH=\"$HOME/.config/guix/current/share/info:$INFOPATH\"
1030
1031 # Set the umask, notably for users logging in via 'lsh'.
1032 # See <http://bugs.gnu.org/22650>.
1033 umask 022
1034
1035 # Allow Hunspell-based applications (IceCat, LibreOffice, etc.) to
1036 # find dictionaries.
1037 export DICPATH=\"$HOME/.guix-profile/share/hunspell:/run/current-system/profile/share/hunspell\"
1038
1039 # Allow GStreamer-based applications to find plugins.
1040 export GST_PLUGIN_PATH=\"$HOME/.guix-profile/lib/gstreamer-1.0\"
1041
1042 if [ -n \"$BASH_VERSION\" -a -f /etc/bashrc ]
1043 then
1044 # Load Bash-specific initialization code.
1045 . /etc/bashrc
1046 fi
1047 "))
1048
1049 (bashrc (plain-file "bashrc" "\
1050 # Bash-specific initialization.
1051
1052 # The 'bash-completion' package.
1053 if [ -f /run/current-system/profile/etc/profile.d/bash_completion.sh ]
1054 then
1055 # Bash-completion sources ~/.bash_completion. It installs a dynamic
1056 # completion loader that searches its own completion files as well
1057 # as those in ~/.guix-profile and /run/current-system/profile.
1058 source /run/current-system/profile/etc/profile.d/bash_completion.sh
1059 fi\n")))
1060 (etc-service
1061 `(("os-release" ,#~#$(os-release))
1062 ("services" ,(file-append net-base "/etc/services"))
1063 ("protocols" ,(file-append net-base "/etc/protocols"))
1064 ("rpc" ,(file-append net-base "/etc/rpc"))
1065 ("login.defs" ,#~#$login.defs)
1066 ("issue" ,#~#$issue)
1067 ,@(if nsswitch `(("nsswitch.conf" ,#~#$nsswitch)) '())
1068 ("profile" ,#~#$profile)
1069 ("bashrc" ,#~#$bashrc)
1070 ("hosts" ,#~#$(or (operating-system-hosts-file os)
1071 (default-/etc/hosts (operating-system-host-name os))))
1072 ;; Write the operating-system-host-name to /etc/hostname to prevent
1073 ;; NetworkManager from changing the system's hostname when connecting
1074 ;; to certain networks. Some discussion at
1075 ;; https://lists.gnu.org/archive/html/help-guix/2017-09/msg00037.html
1076 ("hostname" ,(plain-file "hostname" (operating-system-host-name os)))
1077 ;; Some programs (e.g., GLib) look at /etc/timezone to find the
1078 ;; name of the current timezone. For details, see
1079 ;; https://lists.gnu.org/archive/html/guix-devel/2019-07/msg00166.html
1080 ;; Some programs expect a terminating newline.
1081 ("timezone" ,(plain-file "timezone"
1082 (string-append
1083 (string-trim-both
1084 (operating-system-timezone os))
1085 "\n")))
1086 ("localtime" ,(file-append tzdata "/share/zoneinfo/"
1087 (operating-system-timezone os)))
1088 ,@(if sudoers
1089 `(("sudoers" ,(validated-sudoers-file sudoers)))
1090 '())
1091 ,@(if hurd
1092 `(("login" ,(file-append hurd "/etc/login"))
1093 ("motd" ,(file-append hurd "/etc/motd"))
1094 ("ttys" ,(file-append hurd "/etc/ttys")))
1095 '())))))
1096
1097 (define %root-account
1098 ;; Default root account.
1099 (user-account
1100 (name "root")
1101 (password "")
1102 (uid 0) (group "root")
1103 (comment "System administrator")
1104 (home-directory "/root")))
1105
1106 (define (operating-system-accounts os)
1107 "Return the user accounts for OS, including an obligatory 'root' account,
1108 and excluding accounts requested by services."
1109 ;; Make sure there's a root account.
1110 (if (find (lambda (user)
1111 (and=> (user-account-uid user) zero?))
1112 (operating-system-users os))
1113 (operating-system-users os)
1114 (cons %root-account (operating-system-users os))))
1115
1116 (define (maybe-string->file file-name thing)
1117 "If THING is a string, return a <plain-file> with THING as its content.
1118 Otherwise just return THING.
1119
1120 This is for backward-compatibility of fields that used to be strings and are
1121 now file-like objects.."
1122 (match thing
1123 ((? string?)
1124 (warning (G_ "using a string for file '~a' is deprecated; \
1125 use 'plain-file' instead~%")
1126 file-name)
1127 (plain-file file-name thing))
1128 (x
1129 x)))
1130
1131 (define (maybe-file->monadic file-name thing)
1132 "If THING is a value in %STORE-MONAD, return it as is; otherwise return
1133 THING in the %STORE-MONAD.
1134
1135 This is for backward-compatibility of fields that used to be monadic values
1136 and are now file-like objects."
1137 (with-monad %store-monad
1138 (match thing
1139 ((? procedure?)
1140 (warning (G_ "using a monadic value for '~a' is deprecated; \
1141 use 'plain-file' instead~%")
1142 file-name)
1143 thing)
1144 (x
1145 (return x)))))
1146
1147 (define (operating-system-etc-directory os)
1148 "Return that static part of the /etc directory of OS."
1149 (etc-directory
1150 (fold-services (operating-system-services os)
1151 #:target-type etc-service-type)))
1152
1153 (define (operating-system-environment-variables os)
1154 "Return the environment variables of OS for
1155 @var{session-environment-service-type}, to be used in @file{/etc/environment}."
1156 `(("LANG" . ,(operating-system-locale os))
1157 ;; Note: No need to set 'TZ' since (1) we provide /etc/localtime, and (2)
1158 ;; it doesn't work for setuid binaries. See <https://bugs.gnu.org/29212>.
1159 ("TZDIR" . ,(file-append tzdata "/share/zoneinfo"))
1160 ;; Tell 'modprobe' & co. where to look for modules.
1161 ("LINUX_MODULE_DIRECTORY" . "/run/booted-system/kernel/lib/modules")
1162 ;; These variables are honored by OpenSSL (libssl) and Git.
1163 ("SSL_CERT_DIR" . "/etc/ssl/certs")
1164 ("SSL_CERT_FILE" . "/etc/ssl/certs/ca-certificates.crt")
1165 ("GIT_SSL_CAINFO" . "/etc/ssl/certs/ca-certificates.crt")
1166
1167 ;; 'GTK_DATA_PREFIX' must name one directory where GTK+ themes are
1168 ;; searched for.
1169 ("GTK_DATA_PREFIX" . "/run/current-system/profile")
1170
1171 ;; By default, applications that use D-Bus, such as Emacs, abort at startup
1172 ;; when /etc/machine-id is missing. Make sure these warnings are non-fatal.
1173 ("DBUS_FATAL_WARNINGS" . "0")
1174
1175 ;; XXX: Normally we wouldn't need to do this, but our glibc@2.23 package
1176 ;; used to look things up in 'PREFIX/lib/locale' instead of
1177 ;; '/run/current-system/locale' as was intended. Keep this hack around so
1178 ;; that people who still have glibc@2.23-using packages in their profiles
1179 ;; can use them correctly.
1180 ;; TODO: Remove when glibc@2.23 is long gone.
1181 ("GUIX_LOCPATH" . "/run/current-system/locale")))
1182
1183 ;; Ensure LST is a list of <setuid-program> records and warn otherwise.
1184 (define-with-syntax-properties (ensure-setuid-program-list (lst properties))
1185 (%ensure-setuid-program-list lst properties))
1186
1187 ;; We want to be able to use defines, so define a procedure.
1188 (define (%ensure-setuid-program-list lst properties)
1189 (define warned? #f)
1190
1191 (define (warn-once)
1192 (unless warned?
1193 (warning (source-properties->location properties)
1194 (G_ "representing setuid programs with file-like objects is \
1195 deprecated; use 'setuid-program' instead~%"))
1196 (set! warned? #t)))
1197
1198 (map (match-lambda
1199 ((? setuid-program? program)
1200 program)
1201 (program
1202 ;; PROGRAM is a file-like or a gexp like #~(string-append #$foo
1203 ;; "/bin/bar").
1204 (warn-once)
1205 (setuid-program (program program))))
1206 lst))
1207
1208 (define %setuid-programs
1209 ;; Default set of setuid-root programs.
1210 (let ((shadow (@ (gnu packages admin) shadow)))
1211 (map file-like->setuid-program
1212 (list (file-append shadow "/bin/passwd")
1213 (file-append shadow "/bin/chfn")
1214 (file-append shadow "/bin/sg")
1215 (file-append shadow "/bin/su")
1216 (file-append shadow "/bin/newgrp")
1217 (file-append shadow "/bin/newuidmap")
1218 (file-append shadow "/bin/newgidmap")
1219 (file-append inetutils "/bin/ping")
1220 (file-append inetutils "/bin/ping6")
1221 (file-append sudo "/bin/sudo")
1222 (file-append sudo "/bin/sudoedit")
1223 (file-append fuse "/bin/fusermount")
1224
1225 ;; To allow mounts with the "user" option, "mount" and "umount" must
1226 ;; be setuid-root.
1227 (file-append util-linux "/bin/mount")
1228 (file-append util-linux "/bin/umount")))))
1229
1230 (define %sudoers-specification
1231 ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel'
1232 ;; group can do anything. See
1233 ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>.
1234 ;; TODO: Add a declarative API.
1235 (plain-file "sudoers" "\
1236 root ALL=(ALL) ALL
1237 %wheel ALL=(ALL) ALL\n"))
1238
1239 (define* (operating-system-activation-script os)
1240 "Return the activation script for OS---i.e., the code that \"activates\" the
1241 stateful part of OS, including user accounts and groups, special directories,
1242 etc."
1243 (let* ((services (operating-system-services os))
1244 (activation (fold-services services
1245 #:target-type activation-service-type)))
1246 (activation-service->script activation)))
1247
1248 (define* (operating-system-boot-script os)
1249 "Return the boot script for OS---i.e., the code started by the initrd once
1250 we're running in the final root."
1251 (let* ((services (operating-system-services os))
1252 (boot (fold-services services #:target-type boot-service-type)))
1253 (service-value boot)))
1254
1255 (define (operating-system-user-accounts os)
1256 "Return the list of user accounts of OS."
1257 (let* ((services (operating-system-services os))
1258 (account (fold-services services
1259 #:target-type account-service-type)))
1260 (filter user-account?
1261 (service-value account))))
1262
1263 (define (operating-system-shepherd-service-names os)
1264 "Return the list of Shepherd service names for OS."
1265 (append-map shepherd-service-provision
1266 (shepherd-configuration-services
1267 (service-value
1268 (fold-services (operating-system-services os)
1269 #:target-type
1270 shepherd-root-service-type)))))
1271
1272 (define* (operating-system-derivation os)
1273 "Return a derivation that builds OS."
1274 (let* ((services (operating-system-services os))
1275 (system (fold-services services)))
1276 ;; SYSTEM contains the derivation as a monadic value.
1277 (service-value system)))
1278
1279 (define* (operating-system-profile os)
1280 "Return a derivation that builds the system profile of OS."
1281 (mlet* %store-monad
1282 ((services -> (operating-system-services os))
1283 (profile (fold-services services
1284 #:target-type profile-service-type)))
1285 (match profile
1286 (("profile" profile)
1287 (return profile)))))
1288
1289 (define (operating-system-root-file-system os)
1290 "Return the root file system of OS."
1291 (or (find (lambda (fs)
1292 (string=? "/" (file-system-mount-point fs)))
1293 (operating-system-file-systems os))
1294 (raise (condition
1295 (&message (message "missing root file system"))
1296 (&error-location
1297 (location (operating-system-location os)))))))
1298
1299 (define (operating-system-initrd-file os)
1300 "Return a gexp denoting the initrd file of OS."
1301 (define boot-file-systems
1302 (filter file-system-needed-for-boot?
1303 (operating-system-file-systems os)))
1304
1305 (define mapped-devices
1306 (operating-system-boot-mapped-devices os))
1307
1308 (define make-initrd
1309 (operating-system-initrd os))
1310
1311 (make-initrd boot-file-systems
1312 #:linux (operating-system-kernel os)
1313 #:linux-modules
1314 (operating-system-initrd-modules os)
1315 #:mapped-devices mapped-devices
1316 #:keyboard-layout (operating-system-keyboard-layout os)))
1317
1318 (define* (operating-system-uuid os #:optional (type 'dce))
1319 "Compute UUID object with a deterministic \"UUID\" for OS, of the given
1320 TYPE (one of 'iso9660 or 'dce). Return a UUID object."
1321 ;; Note: For this to be deterministic, we must not hash things that contains
1322 ;; (directly or indirectly) procedures, for example. That rules out
1323 ;; anything that contains gexps, thunk or delayed record fields, etc.
1324
1325 (define service-name
1326 (compose service-type-name service-kind))
1327
1328 (define (file-system-digest fs)
1329 ;; Return a hashable digest that does not contain 'dependencies' since
1330 ;; this field can contain procedures.
1331 (let ((device (file-system-device fs)))
1332 (list (file-system-mount-point fs)
1333 (file-system-type fs)
1334 (file-system-device->string device)
1335 (file-system-options fs))))
1336
1337 (if (eq? type 'iso9660)
1338 (let ((pad (compose (cut string-pad <> 2 #\0)
1339 number->string))
1340 (h (hash (map service-name (operating-system-services os))
1341 3600)))
1342 (bytevector->uuid
1343 (string->iso9660-uuid
1344 (string-append "1970-01-01-"
1345 (pad (hash (operating-system-host-name os) 24)) "-"
1346 (pad (quotient h 60)) "-"
1347 (pad (modulo h 60)) "-"
1348 (pad (hash (map file-system-digest
1349 (operating-system-file-systems os))
1350 100))))
1351 'iso9660))
1352 (bytevector->uuid
1353 (uint-list->bytevector
1354 (list (hash (map file-system-digest
1355 (operating-system-file-systems os))
1356 (- (expt 2 32) 1))
1357 (hash (operating-system-host-name os)
1358 (- (expt 2 32) 1))
1359 (hash (map service-name (operating-system-services os))
1360 (- (expt 2 32) 1))
1361 (hash (map file-system-digest (operating-system-file-systems os))
1362 (- (expt 2 32) 1)))
1363 (endianness little)
1364 4)
1365 type)))
1366
1367 (define (locale-name->definition* name)
1368 "Variant of 'locale-name->definition' that raises an error upon failure."
1369 (match (locale-name->definition name)
1370 (#f
1371 (raise (formatted-message (G_ "~a: invalid locale name") name)))
1372 (def def)))
1373
1374 (define (operating-system-locale-directory os)
1375 "Return the directory containing the locales compiled for the definitions
1376 listed in OS. The C library expects to find it under
1377 /run/current-system/locale."
1378 (define name
1379 (operating-system-locale os))
1380
1381 (define definitions
1382 ;; While we're at it, check whether NAME is defined and add it if needed.
1383 (if (member name (map locale-definition-name
1384 (operating-system-locale-definitions os)))
1385 (operating-system-locale-definitions os)
1386 (cons (locale-name->definition* name)
1387 (operating-system-locale-definitions os))))
1388
1389 (locale-directory definitions
1390 #:libcs (operating-system-locale-libcs os)))
1391
1392 (define* (kernel->boot-label kernel #:key hurd)
1393 "Return a label for the bootloader menu entry that boots KERNEL."
1394 (cond ((package? hurd)
1395 (string-append "GNU with the "
1396 (string-titlecase (package-name hurd)) " "
1397 (package-version hurd)))
1398 ((package? kernel)
1399 (string-append "GNU with "
1400 (string-titlecase (package-name kernel)) " "
1401 (package-version kernel)))
1402 ((inferior-package? kernel)
1403 (string-append "GNU with "
1404 (string-titlecase (inferior-package-name kernel)) " "
1405 (inferior-package-version kernel)))
1406 (else "GNU")))
1407
1408 (define (operating-system-default-label os)
1409 "Return the default label for OS, as it will appear in the bootloader menu
1410 entry."
1411 (kernel->boot-label (operating-system-kernel os)
1412 #:hurd (operating-system-hurd os)))
1413
1414 (define (store-file-system file-systems)
1415 "Return the file system object among FILE-SYSTEMS that contains the store."
1416 (match (filter (lambda (fs)
1417 (and (file-system-mount? fs)
1418 (not (memq 'bind-mount (file-system-flags fs)))
1419 (string-prefix? (file-system-mount-point fs)
1420 (%store-prefix))))
1421 file-systems)
1422 ((and candidates (head . tail))
1423 (reduce (lambda (fs1 fs2)
1424 (if (> (string-length (file-system-mount-point fs1))
1425 (string-length (file-system-mount-point fs2)))
1426 fs1
1427 fs2))
1428 head
1429 candidates))))
1430
1431 (define (operating-system-store-file-system os)
1432 "Return the file system that contains the store of OS."
1433 (store-file-system (operating-system-file-systems os)))
1434
1435 (define* (operating-system-bootcfg os #:optional (old-entries '()))
1436 "Return the bootloader configuration file for OS. Use OLD-ENTRIES,
1437 a list of <menu-entry>, to populate the \"old entries\" menu."
1438 (let* ((file-systems (operating-system-file-systems os))
1439 (root-fs (operating-system-root-file-system os))
1440 (root-device (file-system-device root-fs))
1441 (locale (operating-system-locale os))
1442 (crypto-devices (operating-system-bootloader-crypto-devices os))
1443 (params (operating-system-boot-parameters
1444 os root-device
1445 #:system-kernel-arguments? #t))
1446 (entry (boot-parameters->menu-entry params))
1447 (bootloader-conf (operating-system-bootloader os)))
1448
1449 (define generate-config-file
1450 (bootloader-configuration-file-generator
1451 (bootloader-configuration-bootloader bootloader-conf)))
1452
1453 (generate-config-file bootloader-conf (list entry)
1454 #:old-entries old-entries
1455 #:locale locale
1456 #:store-crypto-devices crypto-devices
1457 #:store-directory-prefix
1458 (btrfs-store-subvolume-file-name file-systems))))
1459
1460 (define (operating-system-multiboot-modules os)
1461 (if (operating-system-hurd os) (hurd-multiboot-modules os) '()))
1462
1463 (define (hurd-multiboot-modules os)
1464 (let* ((hurd (operating-system-hurd os))
1465 (root-file-system-command
1466 (list (file-append hurd "/hurd/ext2fs.static")
1467 "ext2fs"
1468 "--multiboot-command-line='${kernel-command-line}'"
1469 "--host-priv-port='${host-port}'"
1470 "--device-master-port='${device-port}'"
1471 "--exec-server-task='${exec-task}'"
1472 "--store-type=typed"
1473 "--x-xattr-translator-records"
1474 "'${root}'" "'$(task-create)'" "'$(task-resume)'"))
1475 (target (%current-target-system))
1476 (libc (if target
1477 (with-parameters ((%current-target-system #f))
1478 ;; TODO: cross-libc has extra patches for the Hurd;
1479 ;; remove in next rebuild cycle
1480 (cross-libc target))
1481 glibc))
1482 (exec-server-command
1483 (list (file-append libc "/lib/ld.so.1") "exec"
1484 (file-append hurd "/hurd/exec") "'$(exec-task=task-create)'")))
1485 (list root-file-system-command exec-server-command)))
1486
1487 (define* (operating-system-boot-parameters os root-device
1488 #:key system-kernel-arguments?)
1489 "Return a monadic <boot-parameters> record that describes the boot
1490 parameters of OS. When SYSTEM-KERNEL-ARGUMENTS? is true, add the kernel
1491 arguments 'root', 'gnu.load' and 'gnu.system' to <boot-parameters>. The
1492 SYSTEM-KERNEL-ARGUMENTS? should only be used in necessity, as the 'gnu.load'
1493 and 'gnu.system' values are self-referential (they refer to the system), thus
1494 susceptible to introduce a cyclic dependency."
1495 (let* ((initrd (and (not (operating-system-hurd os))
1496 (operating-system-initrd-file os)))
1497 (store (operating-system-store-file-system os))
1498 (file-systems (operating-system-file-systems os))
1499 (crypto-devices (operating-system-bootloader-crypto-devices os))
1500 (locale (operating-system-locale os))
1501 (bootloader (bootloader-configuration-bootloader
1502 (operating-system-bootloader os)))
1503 (bootloader-name (bootloader-name bootloader))
1504 (label (operating-system-label os))
1505 (multiboot-modules (operating-system-multiboot-modules os)))
1506 (boot-parameters
1507 (label label)
1508 (root-device root-device)
1509 (kernel (operating-system-kernel-file os))
1510 (kernel-arguments
1511 (if system-kernel-arguments?
1512 (operating-system-kernel-arguments os root-device)
1513 (operating-system-user-kernel-arguments os)))
1514 (initrd initrd)
1515 (multiboot-modules multiboot-modules)
1516 (bootloader-name bootloader-name)
1517 (bootloader-menu-entries
1518 (bootloader-configuration-menu-entries (operating-system-bootloader os)))
1519 (locale locale)
1520 (store-device (ensure-not-/dev (file-system-device store)))
1521 (store-directory-prefix (btrfs-store-subvolume-file-name file-systems))
1522 (store-crypto-devices crypto-devices)
1523 (store-mount-point (file-system-mount-point store)))))
1524
1525 (define (device->sexp device)
1526 "Serialize DEVICE as an sexp (really, as an object with a read syntax.)"
1527 (match device
1528 ((? uuid? uuid)
1529 `(uuid ,(uuid-type uuid) ,(uuid-bytevector uuid)))
1530 ((? file-system-label? label)
1531 `(file-system-label ,(file-system-label->string label)))
1532 (_
1533 device)))
1534
1535 (define* (operating-system-boot-parameters-file os)
1536 "Return a file that describes the boot parameters of OS. The primary use
1537 of this file is the reconstruction of GRUB menu entries for old
1538 configurations."
1539 (let* ((root (operating-system-root-file-system os))
1540 (device (file-system-device root))
1541 (params (operating-system-boot-parameters os device)))
1542 (scheme-file "parameters"
1543 #~(boot-parameters
1544 (version #$(boot-parameters-version params))
1545 (label #$(boot-parameters-label params))
1546 (root-device
1547 #$(device->sexp
1548 (boot-parameters-root-device params)))
1549 (kernel #$(boot-parameters-kernel params))
1550 (kernel-arguments
1551 #$(boot-parameters-kernel-arguments params))
1552 #$@(if (boot-parameters-initrd params)
1553 #~((initrd #$(boot-parameters-initrd params)))
1554 #~())
1555 #$@(if (pair? (boot-parameters-multiboot-modules params))
1556 #~((multiboot-modules
1557 #$(boot-parameters-multiboot-modules params)))
1558 #~())
1559 (bootloader-name #$(boot-parameters-bootloader-name params))
1560 (bootloader-menu-entries
1561 #$(map menu-entry->sexp
1562 (or (and=> (operating-system-bootloader os)
1563 bootloader-configuration-menu-entries)
1564 '())))
1565 (locale #$(boot-parameters-locale params))
1566 (store
1567 (device
1568 #$(device->sexp (boot-parameters-store-device params)))
1569 (mount-point #$(boot-parameters-store-mount-point
1570 params))
1571 (directory-prefix
1572 #$(boot-parameters-store-directory-prefix params))
1573 (crypto-devices
1574 #$(map device->sexp
1575 (boot-parameters-store-crypto-devices params)))))
1576 #:set-load-path? #f)))
1577
1578 (define-gexp-compiler (operating-system-compiler (os <operating-system>)
1579 system target)
1580 ((store-lift
1581 (lambda (store)
1582 ;; XXX: This is not super elegant but we can't pass SYSTEM and TARGET to
1583 ;; 'operating-system-derivation'.
1584 (run-with-store store (operating-system-derivation os)
1585 #:system system
1586 #:target target)))))
1587
1588 ;;; system.scm ends here