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