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