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