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