file-systems: Add helpers for parsing the options string into an alist.
[jackhill/guix/guix.git] / gnu / system.scm
CommitLineData
033adfe7 1;;; GNU Guix --- Functional package management for GNU
cda75110 2;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
5e738ac2 3;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
5dccaffe 4;;; Copyright © 2015, 2016 Alex Kost <alezost@gmail.com>
1ef8b72a 5;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
c76b3046 6;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
5144df2c 7;;; Copyright © 2019 Meiyo Peng <meiyo.peng@gmail.com>
5c79f238 8;;; Copyright © 2020 Danny Milosavljevic <dannym@scratchpost.org>
93664fee 9;;; Copyright © 2020 Brice Waegeneire <brice@waegenei.re>
e06664da 10;;; Copyright © 2020 Florian Pelz <pelzflorian@pelzflorian.de>
033adfe7
LC
11;;;
12;;; This file is part of GNU Guix.
13;;;
14;;; GNU Guix is free software; you can redistribute it and/or modify it
15;;; under the terms of the GNU General Public License as published by
16;;; the Free Software Foundation; either version 3 of the License, or (at
17;;; your option) any later version.
18;;;
19;;; GNU Guix is distributed in the hope that it will be useful, but
20;;; WITHOUT ANY WARRANTY; without even the implied warranty of
21;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22;;; GNU General Public License for more details.
23;;;
24;;; You should have received a copy of the GNU General Public License
25;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
26
27(define-module (gnu system)
bdc61ff9 28 #:use-module (guix inferior)
033adfe7
LC
29 #:use-module (guix store)
30 #:use-module (guix monads)
02100028 31 #:use-module (guix gexp)
033adfe7
LC
32 #:use-module (guix records)
33 #:use-module (guix packages)
34 #:use-module (guix derivations)
29fce8b6 35 #:use-module (guix profiles)
84765839 36 #:use-module (guix ui)
f91ad0b1 37 #:use-module (guix utils)
033adfe7
LC
38 #:use-module (gnu packages base)
39 #:use-module (gnu packages bash)
bdb36958 40 #:use-module (gnu packages guile)
01ac0e6b 41 #:use-module (gnu packages guile-xyz)
9de46ffb 42 #:use-module (gnu packages admin)
8a07c289 43 #:use-module (gnu packages linux)
a96a82d7 44 #:use-module (gnu packages pciutils)
033adfe7 45 #:use-module (gnu packages package-management)
6f436c54
LC
46 #:use-module (gnu packages less)
47 #:use-module (gnu packages zile)
55e70e65 48 #:use-module (gnu packages nano)
a68c6967 49 #:use-module (gnu packages gawk)
18316d86 50 #:use-module (gnu packages man)
939c5c31 51 #:use-module (gnu packages texinfo)
a68c6967 52 #:use-module (gnu packages compression)
f34c56be 53 #:use-module (gnu packages firmware)
db4fdc04 54 #:use-module (gnu services)
0190c1c0 55 #:use-module (gnu services shepherd)
db4fdc04 56 #:use-module (gnu services base)
b09a8da4 57 #:use-module (gnu bootloader)
033adfe7 58 #:use-module (gnu system shadow)
996ed739 59 #:use-module (gnu system nss)
598e19dc 60 #:use-module (gnu system locale)
6e828634 61 #:use-module (gnu system pam)
735c6dd7 62 #:use-module (gnu system linux-initrd)
9b336338 63 #:use-module (gnu system uuid)
c5df1839 64 #:use-module (gnu system file-systems)
060d62a7 65 #:use-module (gnu system mapped-devices)
033adfe7
LC
66 #:use-module (ice-9 match)
67 #:use-module (srfi srfi-1)
68 #:use-module (srfi srfi-26)
598e19dc
LC
69 #:use-module (srfi srfi-34)
70 #:use-module (srfi srfi-35)
3382bfe9 71 #:use-module (rnrs bytevectors)
033adfe7
LC
72 #:export (operating-system
73 operating-system?
d8bead6c 74 this-operating-system
d5b429ab
LC
75
76 operating-system-bootloader
033adfe7 77 operating-system-services
69cae3d3 78 operating-system-essential-services
f8885eca 79 operating-system-default-essential-services
217a5b85 80 operating-system-user-services
033adfe7 81 operating-system-packages
fd3bfc44 82 operating-system-host-name
c65e1834 83 operating-system-hosts-file
fd3bfc44 84 operating-system-kernel
44d5f54e 85 operating-system-kernel-file
33f0aa88 86 operating-system-kernel-arguments
3f03a198
LC
87 operating-system-label
88 operating-system-default-label
bc499b11 89 operating-system-initrd-modules
fd3bfc44
LC
90 operating-system-initrd
91 operating-system-users
92 operating-system-groups
548d4c13 93 operating-system-issue
fd3bfc44
LC
94 operating-system-timezone
95 operating-system-locale
598e19dc 96 operating-system-locale-definitions
34760ae7 97 operating-system-locale-libcs
5dae0186 98 operating-system-mapped-devices
83bcd0b8 99 operating-system-file-systems
6b779207 100 operating-system-store-file-system
2bdd7ac1
LC
101 operating-system-user-mapped-devices
102 operating-system-boot-mapped-devices
b25937e3 103 operating-system-activation-script
b2fef041
LC
104 operating-system-user-accounts
105 operating-system-shepherd-service-names
20abb8c4 106 operating-system-user-kernel-arguments
9ef37e81
BW
107 operating-system-firmware
108 operating-system-keyboard-layout
109 operating-system-name-service-switch
110 operating-system-pam-services
111 operating-system-setuid-programs
112 operating-system-skeletons
113 operating-system-sudoers-file
114 operating-system-swap-devices
a9f2c210 115 operating-system-kernel-loadable-modules
033adfe7 116
1aa0033b 117 operating-system-derivation
83bcd0b8 118 operating-system-profile
c76b3046 119 operating-system-bootcfg
239db054
DT
120 operating-system-etc-directory
121 operating-system-locale-directory
122 operating-system-boot-script
78fbf2bd 123 operating-system-uuid
239db054 124
43fe431c 125 system-linux-image-file-name
9fcfe30d 126 operating-system-with-gc-roots
33b7cb7a 127 operating-system-with-provenance
43fe431c 128
b8300494
AK
129 boot-parameters
130 boot-parameters?
131 boot-parameters-label
132 boot-parameters-root-device
f96752e3 133 boot-parameters-bootloader-name
a28cfee8 134 boot-parameters-bootloader-menu-entries
1ef8b72a
CM
135 boot-parameters-store-device
136 boot-parameters-store-mount-point
b8300494
AK
137 boot-parameters-kernel
138 boot-parameters-kernel-arguments
d7b342d8 139 boot-parameters-initrd
4e4e0185 140 read-boot-parameters
9530e73b 141 read-boot-parameters-file
1975c754 142 boot-parameters->menu-entry
b8300494 143
568841d4 144 local-host-aliases
d0f3a672 145 %root-account
df5ce088 146 %setuid-programs
f6b95031 147 %sudoers-specification
5dae0186 148 %base-packages
93664fee
BW
149 %base-packages-interactive
150 %base-packages-linux
151 %base-packages-networking
152 %base-packages-utils
e06664da
FP
153 %base-firmware
154 %default-kernel-arguments))
033adfe7
LC
155
156;;; Commentary:
157;;;
158;;; This module supports whole-system configuration.
159;;;
160;;; Code:
161
a7ef45d9
LC
162(define (bootable-kernel-arguments system root-device)
163 "Return a list of kernel arguments (gexps) to boot SYSTEM from ROOT-DEVICE."
164 (list (string-append "--root="
99e676db
MC
165 ;; Note: Always use the DCE format because that's what
166 ;; (gnu build linux-boot) expects for the '--root'
167 ;; kernel command-line option.
168 (file-system-device->string root-device
169 #:uuid-type 'dce))
a7ef45d9
LC
170 #~(string-append "--system=" #$system)
171 #~(string-append "--load=" #$system "/boot")))
33f0aa88 172
033adfe7
LC
173;; System-wide configuration.
174;; TODO: Add per-field docstrings/stexi.
175(define-record-type* <operating-system> operating-system
176 make-operating-system
177 operating-system?
d8bead6c
LC
178 this-operating-system
179
033adfe7 180 (kernel operating-system-kernel ; package
57082417 181 (default linux-libre))
5c79f238
DM
182 (kernel-loadable-modules operating-system-kernel-loadable-modules
183 (default '())) ; list of packages
af98d25a 184 (kernel-arguments operating-system-user-kernel-arguments
e06664da 185 (default %default-kernel-arguments)) ; list of gexps/strings
b09a8da4 186 (bootloader operating-system-bootloader) ; <bootloader-configuration>
3f03a198
LC
187 (label operating-system-label ; string
188 (thunked)
189 (default (operating-system-default-label this-operating-system)))
d5b429ab 190
ae7a316b
LC
191 (keyboard-layout operating-system-keyboard-layout ;#f | <keyboard-layout>
192 (default #f))
e34ae75d 193 (initrd operating-system-initrd ; (list fs) -> file-like
060238ae 194 (default base-initrd))
bc499b11
LC
195 (initrd-modules operating-system-initrd-modules ; list of strings
196 (thunked) ; it's system-dependent
197 (default %base-initrd-modules))
198
f34c56be
LC
199 (firmware operating-system-firmware ; list of packages
200 (default %base-firmware))
033adfe7
LC
201
202 (host-name operating-system-host-name) ; string
24e02c28 203 (hosts-file operating-system-hosts-file ; file-like | #f
c65e1834 204 (default #f))
033adfe7 205
5dae0186
LC
206 (mapped-devices operating-system-mapped-devices ; list of <mapped-device>
207 (default '()))
8a6d2731 208 (file-systems operating-system-file-systems) ; list of fs
2a13d05e
LC
209 (swap-devices operating-system-swap-devices ; list of strings
210 (default '()))
033adfe7
LC
211
212 (users operating-system-users ; list of user accounts
bf87f38a 213 (default %base-user-accounts))
033adfe7 214 (groups operating-system-groups ; list of user groups
773e956d 215 (default %base-groups))
033adfe7 216
ac3c14fb 217 (skeletons operating-system-skeletons ; list of name/file-like value
40281c54 218 (default (default-skeletons)))
548d4c13
LC
219 (issue operating-system-issue ; string
220 (default %default-issue))
40281c54 221
033adfe7 222 (packages operating-system-packages ; list of (PACKAGE OUTPUT...)
6f436c54 223 (default %base-packages)) ; or just PACKAGE
033adfe7
LC
224
225 (timezone operating-system-timezone) ; string
8a6d2731 226 (locale operating-system-locale ; string
598e19dc
LC
227 (default "en_US.utf8"))
228 (locale-definitions operating-system-locale-definitions ; list of <locale-definition>
229 (default %default-locale-definitions))
34760ae7
LC
230 (locale-libcs operating-system-locale-libcs ; list of <packages>
231 (default %default-locale-libcs))
996ed739
LC
232 (name-service-switch operating-system-name-service-switch ; <name-service-switch>
233 (default %default-nss))
033adfe7 234
69cae3d3
LC
235 (essential-services operating-system-essential-services ; list of services
236 (thunked)
f8885eca
LC
237 (default (operating-system-default-essential-services
238 this-operating-system)))
ac3c14fb 239 (services operating-system-user-services ; list of services
09e028f4
LC
240 (default %base-services))
241
242 (pam-services operating-system-pam-services ; list of PAM services
243 (default (base-pam-services)))
244 (setuid-programs operating-system-setuid-programs
69689380 245 (default %setuid-programs)) ; list of string-valued gexps
033adfe7 246
f5a9ffa0
AK
247 (sudoers-file operating-system-sudoers-file ; file-like
248 (default %sudoers-specification)))
033adfe7 249
a7ef45d9 250(define (operating-system-kernel-arguments os root-device)
33f0aa88
DM
251 "Return all the kernel arguments, including the ones not specified
252directly by the user."
a7ef45d9
LC
253 (append (bootable-kernel-arguments os root-device)
254 (operating-system-user-kernel-arguments os)))
33f0aa88 255
2717a89a 256\f
8e815c5b
LC
257;;;
258;;; Boot parameters
259;;;
260
261(define-record-type* <boot-parameters>
262 boot-parameters make-boot-parameters boot-parameters?
263 (label boot-parameters-label)
264 ;; Because we will use the 'store-device' to create the GRUB search command,
265 ;; the 'store-device' has slightly different semantics than 'root-device'.
266 ;; The 'store-device' can be a file system uuid, a file system label, or #f,
267 ;; but it cannot be a device path such as "/dev/sda3", since GRUB would not
268 ;; understand that. The 'root-device', on the other hand, corresponds
269 ;; exactly to the device field of the <file-system> object representing the
270 ;; OS's root file system, so it might be a device path like "/dev/sda3".
271 (root-device boot-parameters-root-device)
f96752e3 272 (bootloader-name boot-parameters-bootloader-name)
a28cfee8
LC
273 (bootloader-menu-entries ;list of <menu-entry>
274 boot-parameters-bootloader-menu-entries)
8e815c5b
LC
275 (store-device boot-parameters-store-device)
276 (store-mount-point boot-parameters-store-mount-point)
277 (kernel boot-parameters-kernel)
278 (kernel-arguments boot-parameters-kernel-arguments)
279 (initrd boot-parameters-initrd))
280
90d23ed9
LC
281(define (ensure-not-/dev device)
282 "If DEVICE starts with a slash, return #f. This is meant to filter out
283Linux device names such as /dev/sda, and to preserve GRUB device names and
284file system labels."
285 (if (and (string? device) (string-prefix? "/" device))
286 #f
287 device))
288
8e815c5b
LC
289(define (read-boot-parameters port)
290 "Read boot parameters from PORT and return the corresponding
291<boot-parameters> object or #f if the format is unrecognized."
075681d3
LC
292 (define device-sexp->device
293 (match-lambda
294 (('uuid (? symbol? type) (? bytevector? bv))
295 (bytevector->uuid bv type))
a5acc17a
LC
296 (('file-system-label (? string? label))
297 (file-system-label label))
075681d3
LC
298 ((? bytevector? bv) ;old format
299 (bytevector->uuid bv 'dce))
300 ((? string? device)
a5acc17a
LC
301 ;; It used to be that we would not distinguish between labels and
302 ;; device names. Try to infer the right thing here.
303 (if (string-prefix? "/dev/" device)
304 device
305 (file-system-label device)))))
075681d3 306
8e815c5b
LC
307 (match (read port)
308 (('boot-parameters ('version 0)
309 ('label label) ('root-device root)
310 ('kernel linux)
311 rest ...)
312 (boot-parameters
313 (label label)
7940188e 314 (root-device (device-sexp->device root))
8e815c5b 315
f96752e3
MO
316 (bootloader-name
317 (match (assq 'bootloader-name rest)
bcaf67c4
MO
318 ((_ args) args)
319 (#f 'grub))) ; for compatibility reasons.
320
a28cfee8
LC
321 (bootloader-menu-entries
322 (match (assq 'bootloader-menu-entries rest)
323 ((_ entries) (map sexp->menu-entry entries))
324 (#f '())))
325
8e815c5b
LC
326 ;; In the past, we would store the directory name of the kernel instead
327 ;; of the absolute file name of its image. Detect that and correct it.
328 (kernel (if (string=? linux (direct-store-path linux))
329 (string-append linux "/"
330 (system-linux-image-file-name))
331 linux))
332
333 (kernel-arguments
334 (match (assq 'kernel-arguments rest)
335 ((_ args) args)
336 (#f '()))) ;the old format
337
338 (initrd
339 (match (assq 'initrd rest)
340 (('initrd ('string-append directory file)) ;the old format
341 (string-append directory file))
342 (('initrd (? string? file))
343 file)))
344
345 (store-device
db4e8fd5
LC
346 ;; Linux device names like "/dev/sda1" are not suitable GRUB device
347 ;; identifiers, so we just filter them out.
348 (ensure-not-/dev
349 (match (assq 'store rest)
350 (('store ('device #f) _ ...)
351 root-device)
352 (('store ('device device) _ ...)
353 (device-sexp->device device))
354 (_ ;the old format
355 root-device))))
8e815c5b
LC
356
357 (store-mount-point
358 (match (assq 'store rest)
359 (('store ('device _) ('mount-point mount-point) _ ...)
360 mount-point)
361 (_ ;the old format
362 "/")))))
363 (x ;unsupported format
b1059b38
LC
364 (warning (G_ "unrecognized boot parameters at '~a'~%")
365 (port-filename port))
8e815c5b
LC
366 #f)))
367
368(define (read-boot-parameters-file system)
369 "Read boot parameters from SYSTEM's (system or generation) \"parameters\"
370file and returns the corresponding <boot-parameters> object or #f if the
371format is unrecognized.
372The object has its kernel-arguments extended in order to make it bootable."
373 (let* ((file (string-append system "/parameters"))
374 (params (call-with-input-file file read-boot-parameters))
a7ef45d9
LC
375 (root (boot-parameters-root-device params)))
376 (boot-parameters
377 (inherit params)
378 (kernel-arguments (append (bootable-kernel-arguments system root)
379 (boot-parameters-kernel-arguments params))))))
8b22107e 380
1975c754
DM
381(define (boot-parameters->menu-entry conf)
382 (menu-entry
383 (label (boot-parameters-label conf))
384 (device (boot-parameters-store-device conf))
385 (device-mount-point (boot-parameters-store-mount-point conf))
386 (linux (boot-parameters-kernel conf))
387 (linux-arguments (boot-parameters-kernel-arguments conf))
388 (initrd (boot-parameters-initrd conf))))
389
8b22107e 390
8e815c5b 391\f
40281c54
LC
392;;;
393;;; Services.
394;;;
395
aa1145df
LC
396(define (non-boot-file-system-service os)
397 "Return the file system service for the file systems of OS that are not
398marked as 'needed-for-boot'."
023f391c 399 (define file-systems
4d6b879c 400 (remove file-system-needed-for-boot?
023f391c
LC
401 (operating-system-file-systems os)))
402
68a58775
LC
403 (define mapped-devices-for-boot
404 (operating-system-boot-mapped-devices os))
405
5dae0186 406 (define (device-mappings fs)
ab64483f 407 (let ((device (file-system-device fs)))
29824d80 408 (if (string? device) ;title is 'device
ab64483f
LC
409 (filter (lambda (md)
410 (string=? (string-append "/dev/mapper/"
411 (mapped-device-target md))
412 device))
413 (operating-system-mapped-devices os))
414 '())))
5dae0186 415
e502bf89
LC
416 (define (add-dependencies fs)
417 ;; Add the dependencies due to device mappings to FS.
418 (file-system
419 (inherit fs)
420 (dependencies
68a58775
LC
421 (delete-duplicates
422 (remove (cut member <> mapped-devices-for-boot)
423 (append (device-mappings fs)
424 (file-system-dependencies fs)))
425 eq?))))
e502bf89 426
aa1145df
LC
427 (service file-system-service-type
428 (map add-dependencies file-systems)))
023f391c 429
68a58775
LC
430(define (mapped-device-users device file-systems)
431 "Return the subset of FILE-SYSTEMS that use DEVICE."
de1c158f 432 (let ((target (string-append "/dev/mapper/" (mapped-device-target device))))
68a58775
LC
433 (filter (lambda (fs)
434 (or (member device (file-system-dependencies fs))
435 (and (string? (file-system-device fs))
436 (string=? (file-system-device fs) target))))
437 file-systems)))
de1c158f
LC
438
439(define (operating-system-user-mapped-devices os)
440 "Return the subset of mapped devices that can be installed in
441user-land--i.e., those not needed during boot."
9cb426b8
LC
442 (let ((devices (operating-system-mapped-devices os))
443 (file-systems (operating-system-file-systems os)))
444 (filter (lambda (md)
68a58775
LC
445 (let ((users (mapped-device-users md file-systems)))
446 (not (any file-system-needed-for-boot? users))))
9cb426b8 447 devices)))
de1c158f
LC
448
449(define (operating-system-boot-mapped-devices os)
450 "Return the subset of mapped devices that must be installed during boot,
451from the initrd."
9cb426b8
LC
452 (let ((devices (operating-system-mapped-devices os))
453 (file-systems (operating-system-file-systems os)))
454 (filter (lambda (md)
68a58775
LC
455 (let ((users (mapped-device-users md file-systems)))
456 (any file-system-needed-for-boot? users)))
9cb426b8 457 devices)))
de1c158f 458
5dae0186 459(define (device-mapping-services os)
be1c2c54 460 "Return the list of device-mapping services for OS as a list."
4da8c19e 461 (map device-mapping-service
be1c2c54 462 (operating-system-user-mapped-devices os)))
5dae0186 463
2a13d05e 464(define (swap-services os)
be1c2c54
LC
465 "Return the list of swap services for OS."
466 (map swap-service (operating-system-swap-devices os)))
2a13d05e 467
d4ddf22d 468(define* (system-linux-image-file-name)
44d5f54e
LC
469 "Return the basename of the kernel image file for SYSTEM."
470 ;; FIXME: Evaluate the conditional based on the actual current system.
d4ddf22d
MO
471 (let ((target (or (%current-target-system) (%current-system))))
472 (cond
473 ((string-prefix? "arm" target) "zImage")
474 ((string-prefix? "mips" target) "vmlinuz")
475 ((string-prefix? "aarch64" target) "Image")
476 (else "bzImage"))))
44d5f54e
LC
477
478(define (operating-system-kernel-file os)
479 "Return an object representing the absolute file name of the kernel image of
480OS."
481 (file-append (operating-system-kernel os)
d4ddf22d 482 "/" (system-linux-image-file-name)))
44d5f54e 483
f91ad0b1
DM
484(define (package-for-kernel target-kernel module-package)
485 "Return a package like MODULE-PACKAGE, adapted for TARGET-KERNEL, if
486possible (that is if there's a LINUX keyword argument in the build system)."
487 (package
488 (inherit module-package)
489 (arguments
490 (substitute-keyword-arguments (package-arguments module-package)
491 ((#:linux kernel #f)
492 target-kernel)))))
493
e06664da
FP
494(define %default-modprobe-blacklist
495 ;; List of kernel modules to blacklist by default.
fd31731b
FP
496 '("usbmouse" ;races with bcm5974, see <https://bugs.gnu.org/35574>
497 "usbkbd")) ;races with usbhid, see <https://issues.guix.gnu.org/35574#18>
e06664da
FP
498
499(define %default-kernel-arguments
500 ;; Default arguments passed to the kernel.
501 (list (string-append "modprobe.blacklist="
502 (string-join %default-modprobe-blacklist ","))
503 "quiet"))
504
69cae3d3 505(define* (operating-system-directory-base-entries os)
d62e201c
LC
506 "Return the basic entries of the 'system' directory of OS for use as the
507value of the SYSTEM-SERVICE-TYPE service."
cda75110
LC
508 (let* ((locale (operating-system-locale-directory os))
509 (kernel (operating-system-kernel os))
510 (modules (operating-system-kernel-loadable-modules os))
511 (kernel (profile
512 (content (packages->manifest
513 (cons kernel
514 (map (lambda (module)
515 (if (package? module)
516 (package-for-kernel kernel
517 module)
518 module))
519 modules))))
520 (hooks (list linux-module-database))))
521 (initrd (operating-system-initrd-file os))
522 (params (operating-system-boot-parameters-file os)))
0e5c2d5e
LC
523 `(("kernel" ,kernel)
524 ("parameters" ,params)
525 ("initrd" ,initrd)
526 ("locale" ,locale)))) ;used by libc
69cae3d3 527
f8885eca 528(define (operating-system-default-essential-services os)
217a5b85
LC
529 "Return the list of essential services for OS. These are special services
530that implement part of what's declared in OS are responsible for low-level
69cae3d3 531bookkeeping."
d6e2a622
LC
532 (define known-fs
533 (map file-system-mount-point (operating-system-file-systems os)))
534
be1c2c54
LC
535 (let* ((mappings (device-mapping-services os))
536 (root-fs (root-file-system-service))
aa1145df 537 (other-fs (non-boot-file-system-service os))
be1c2c54 538 (swaps (swap-services os))
206a28d8 539 (procs (service user-processes-service-type))
d62e201c 540 (host-name (host-name-service (operating-system-host-name os)))
69cae3d3 541 (entries (operating-system-directory-base-entries os)))
d62e201c
LC
542 (cons* (service system-service-type entries)
543 %boot-service
0adfe95a 544
661c237b 545 ;; %SHEPHERD-ROOT-SERVICE must come last so that the gexp that
d4053c71 546 ;; execs shepherd comes last in the boot script (XXX). Likewise,
661c237b 547 ;; the cleanup service must come first so that its gexp runs before
d4053c71 548 ;; activation code.
be7be9e8 549 (service cleanup-service-type #f)
661c237b
LC
550 %activation-service
551 %shepherd-root-service
0adfe95a
LC
552
553 (pam-root-service (operating-system-pam-services os))
554 (account-service (append (operating-system-accounts os)
555 (operating-system-groups os))
556 (operating-system-skeletons os))
557 (operating-system-etc-service os)
aea1a42e
LC
558 (service fstab-service-type
559 (filter file-system-needed-for-boot?
560 (operating-system-file-systems os)))
6bad7524
SB
561 (session-environment-service
562 (operating-system-environment-variables os))
6c445817 563 host-name procs root-fs
0adfe95a
LC
564 (service setuid-program-service-type
565 (operating-system-setuid-programs os))
af4c3fd5
LC
566 (service profile-service-type
567 (operating-system-packages os))
aa1145df
LC
568 other-fs
569 (append mappings swaps
0adfe95a 570
69cae3d3
LC
571 ;; Add the firmware service.
572 (list %linux-bare-metal-service
573 (service firmware-service-type
574 (operating-system-firmware os)))))))
575
576(define* (operating-system-services os)
577 "Return all the services of OS, including \"essential\" services."
d466b1fc
LC
578 (instantiate-missing-services
579 (append (operating-system-user-services os)
69cae3d3 580 (operating-system-essential-services os))))
217a5b85 581
9fcfe30d
LC
582(define (operating-system-with-gc-roots os roots)
583 "Return a variant of OS where ROOTS are registered as GC roots."
584 (operating-system
585 (inherit os)
586
587 ;; We use this procedure for the installation OS, which already defines GC
588 ;; roots. Add ROOTS to those.
589 (services (cons (simple-service 'extra-root
590 gc-root-service-type roots)
591 (operating-system-user-services os)))))
592
33b7cb7a
LC
593(define* (operating-system-with-provenance os #:optional config-file)
594 "Return a variant of OS that stores its own provenance information,
595including CONFIG-FILE, if available. This is achieved by adding an instance
596of PROVENANCE-SERVICE-TYPE to its services."
597 (operating-system
598 (inherit os)
599 (services (cons (service provenance-service-type config-file)
600 (operating-system-user-services os)))))
601
40281c54
LC
602\f
603;;;
604;;; /etc.
605;;;
606
f34c56be
LC
607(define %base-firmware
608 ;; Firmware usable by default.
52db41af
EB
609 (list ath9k-htc-firmware
610 openfwwf-firmware))
f34c56be 611
93664fee
BW
612(define %base-packages-utils
613 ;; Default set of utilities packages.
614 (cons* procps psmisc which
615 (@ (gnu packages admin) shadow) ;for 'passwd'
616
030f6f48 617 guile-3.0
93664fee
BW
618
619 ;; The packages below are also in %FINAL-INPUTS, so take them from
620 ;; there to avoid duplication.
030f6f48
MB
621 (list bash coreutils findutils grep sed
622 diffutils patch gawk tar gzip bzip2 xz lzip)))
93664fee
BW
623
624(define %base-packages-linux
625 ;; Default set of linux specific packages.
626 (list pciutils usbutils
627 util-linux+udev
628 ;; Get 'insmod' & co. from kmod, not module-init-tools, since udev
629 ;; already depends on it anyway.
630 kmod eudev))
631
632(define %base-packages-interactive
633 ;; Default set of common interactive packages.
634 (list less zile nano
635 man-db
636 info-reader ;the standalone Info reader (no Perl)
637 bash-completion
638 kbd
639 ;; The 'sudo' command is already in %SETUID-PROGRAMS, but we also
640 ;; want the other commands and the man pages (notably because
641 ;; auto-completion in Emacs shell relies on man pages.)
642 sudo
643 guile-readline guile-colorized))
644
645(define %base-packages-networking
646 ;; Default set of networking packages.
647 (list inetutils isc-dhcp
648 iproute
649 ;; wireless-tools is deprecated in favor of iw, but it's still what
650 ;; many people are familiar with, so keep it around.
651 iw wireless-tools))
652
6f436c54
LC
653(define %base-packages
654 ;; Default set of packages globally visible. It should include anything
655 ;; required for basic administrator tasks.
93664fee
BW
656 (append (list e2fsprogs)
657 %base-packages-interactive
658 %base-packages-linux
659 %base-packages-networking
660 %base-packages-utils))
6f436c54 661
548d4c13
LC
662(define %default-issue
663 ;; Default contents for /etc/issue.
664 "
665This is the GNU system. Welcome.\n")
666
568841d4
LC
667(define (local-host-aliases host-name)
668 "Return aliases for HOST-NAME, to be used in /etc/hosts."
669 (string-append "127.0.0.1 localhost " host-name "\n"
670 "::1 localhost " host-name "\n"))
671
c65e1834
LC
672(define (default-/etc/hosts host-name)
673 "Return the default /etc/hosts file."
24e02c28 674 (plain-file "hosts" (local-host-aliases host-name)))
c65e1834 675
0adfe95a
LC
676(define* (operating-system-etc-service os)
677 "Return a <service> that builds containing the static part of the /etc
678directory."
e453da13
LF
679 (let ((login.defs
680 (plain-file "login.defs"
681 (string-append
682 "# Default paths for non-login shells started by su(1).\n"
683 "ENV_PATH /run/setuid-programs:"
684 "/run/current-system/profile/bin:"
685 "/run/current-system/profile/sbin\n"
686 "ENV_SUPATH /run/setuid-programs:"
687 "/run/current-system/profile/bin:"
688 "/run/current-system/profile/sbin\n")))
0adfe95a 689
0adfe95a
LC
690 (issue (plain-file "issue" (operating-system-issue os)))
691 (nsswitch (plain-file "nsswitch.conf"
692 (name-service-switch->string
693 (operating-system-name-service-switch os))))
694
695 ;; Startup file for POSIX-compliant login shells, which set system-wide
696 ;; environment variables.
697 (profile (mixed-text-file "profile" "\
c05c4321 698# Crucial variables that could be missing in the profiles' 'etc/profile'
d9959421
LC
699# because they would require combining both profiles.
700# FIXME: See <http://bugs.gnu.org/20255>.
97ab2c0f 701export MANPATH=$HOME/.guix-profile/share/man:/run/current-system/profile/share/man
8d09bfe2 702export INFOPATH=$HOME/.guix-profile/share/info:/run/current-system/profile/share/info
00239d05
SB
703export XDG_DATA_DIRS=$HOME/.guix-profile/share:/run/current-system/profile/share
704export XDG_CONFIG_DIRS=$HOME/.guix-profile/etc/xdg:/run/current-system/profile/etc/xdg
705
ce380150
TD
706# Make sure libXcursor finds cursors installed into user or system profiles. See <http://bugs.gnu.org/24445>
707export XCURSOR_PATH=$HOME/.icons:$HOME/.guix-profile/share/icons:/run/current-system/profile/share/icons
708
d9959421
LC
709# Ignore the default value of 'PATH'.
710unset PATH
711
712# Load the system profile's settings.
bd7e136d 713GUIX_PROFILE=/run/current-system/profile ; \\
669786da 714. /run/current-system/profile/etc/profile
d9959421 715
cad7e6ab
CSLL
716# Since 'lshd' does not use pam_env, /etc/environment must be explicitly
717# loaded when someone logs in via SSH. See <http://bugs.gnu.org/22175>.
718# We need 'PATH' to be defined here, for 'cat' and 'cut'. Do this before
719# reading the user's 'etc/profile' to allow variables to be overridden.
720if [ -f /etc/environment -a -n \"$SSH_CLIENT\" \\
721 -a -z \"$LINUX_MODULE_DIRECTORY\" ]
722then
723 . /etc/environment
724 export `cat /etc/environment | cut -d= -f1`
725fi
726
8d09bfe2
LC
727# Arrange so that ~/.config/guix/current comes first.
728for profile in \"$HOME/.guix-profile\" \"$HOME/.config/guix/current\"
cdc5b932
LC
729do
730 if [ -f \"$profile/etc/profile\" ]
731 then
732 # Load the user profile's settings.
733 GUIX_PROFILE=\"$profile\" ; \\
734 . \"$profile/etc/profile\"
735 else
736 # At least define this one so that basic things just work
737 # when the user installs their first package.
738 export PATH=\"$profile/bin:$PATH\"
739 fi
740done
d9959421 741
a854525a
LC
742# Prepend setuid programs.
743export PATH=/run/setuid-programs:$PATH
744
8d09bfe2
LC
745# Arrange so that ~/.config/guix/current/share/info comes first.
746export INFOPATH=\"$HOME/.config/guix/current/share/info:$INFOPATH\"
747
11202482
LC
748# Set the umask, notably for users logging in via 'lsh'.
749# See <http://bugs.gnu.org/22650>.
750umask 022
2a5f0db4 751
c08da2ee
LC
752# Allow Hunspell-based applications (IceCat, LibreOffice, etc.) to
753# find dictionaries.
754export DICPATH=\"$HOME/.guix-profile/share/hunspell:/run/current-system/profile/share/hunspell\"
755
4af7c83b
LC
756# Allow GStreamer-based applications to find plugins.
757export GST_PLUGIN_PATH=\"$HOME/.guix-profile/lib/gstreamer-1.0\"
758
1d167b6e
LC
759if [ -n \"$BASH_VERSION\" -a -f /etc/bashrc ]
760then
761 # Load Bash-specific initialization code.
669786da 762 . /etc/bashrc
1d167b6e 763fi
40281c54 764"))
1d167b6e 765
0adfe95a 766 (bashrc (plain-file "bashrc" "\
1d167b6e
LC
767# Bash-specific initialization.
768
769# The 'bash-completion' package.
770if [ -f /run/current-system/profile/etc/profile.d/bash_completion.sh ]
771then
772 # Bash-completion sources ~/.bash_completion. It installs a dynamic
773 # completion loader that searches its own completion files as well
774 # as those in ~/.guix-profile and /run/current-system/profile.
775 source /run/current-system/profile/etc/profile.d/bash_completion.sh
0adfe95a
LC
776fi\n")))
777 (etc-service
9e41130b
LC
778 `(("services" ,(file-append net-base "/etc/services"))
779 ("protocols" ,(file-append net-base "/etc/protocols"))
780 ("rpc" ,(file-append net-base "/etc/rpc"))
0adfe95a
LC
781 ("login.defs" ,#~#$login.defs)
782 ("issue" ,#~#$issue)
783 ("nsswitch.conf" ,#~#$nsswitch)
0adfe95a
LC
784 ("profile" ,#~#$profile)
785 ("bashrc" ,#~#$bashrc)
786 ("hosts" ,#~#$(or (operating-system-hosts-file os)
787 (default-/etc/hosts (operating-system-host-name os))))
c694520b
TD
788 ;; Write the operating-system-host-name to /etc/hostname to prevent
789 ;; NetworkManager from changing the system's hostname when connecting
790 ;; to certain networks. Some discussion at
791 ;; https://lists.gnu.org/archive/html/help-guix/2017-09/msg00037.html
792 ("hostname" ,(plain-file "hostname" (operating-system-host-name os)))
2a80d9e5
TS
793 ;; Some programs (e.g., GLib) look at /etc/timezone to find the
794 ;; name of the current timezone. For details, see
795 ;; https://lists.gnu.org/archive/html/guix-devel/2019-07/msg00166.html
796 ("timezone" ,(plain-file "timezone" (operating-system-timezone os)))
9e41130b
LC
797 ("localtime" ,(file-append tzdata "/share/zoneinfo/"
798 (operating-system-timezone os)))
0adfe95a 799 ("sudoers" ,(operating-system-sudoers-file os))))))
033adfe7 800
ab6a279a
LC
801(define %root-account
802 ;; Default root account.
803 (user-account
804 (name "root")
805 (password "")
806 (uid 0) (group "root")
807 (comment "System administrator")
808 (home-directory "/root")))
809
0b6f49ef 810(define (operating-system-accounts os)
0adfe95a
LC
811 "Return the user accounts for OS, including an obligatory 'root' account,
812and excluding accounts requested by services."
813 ;; Make sure there's a root account.
814 (if (find (lambda (user)
815 (and=> (user-account-uid user) zero?))
816 (operating-system-users os))
817 (operating-system-users os)
818 (cons %root-account (operating-system-users os))))
0b6f49ef 819
84765839
LC
820(define (maybe-string->file file-name thing)
821 "If THING is a string, return a <plain-file> with THING as its content.
822Otherwise just return THING.
823
824This is for backward-compatibility of fields that used to be strings and are
825now file-like objects.."
826 (match thing
827 ((? string?)
69daee23 828 (warning (G_ "using a string for file '~a' is deprecated; \
84765839
LC
829use 'plain-file' instead~%")
830 file-name)
831 (plain-file file-name thing))
832 (x
833 x)))
834
24e02c28
LC
835(define (maybe-file->monadic file-name thing)
836 "If THING is a value in %STORE-MONAD, return it as is; otherwise return
837THING in the %STORE-MONAD.
838
839This is for backward-compatibility of fields that used to be monadic values
840and are now file-like objects."
841 (with-monad %store-monad
842 (match thing
843 ((? procedure?)
69daee23 844 (warning (G_ "using a monadic value for '~a' is deprecated; \
24e02c28
LC
845use 'plain-file' instead~%")
846 file-name)
847 thing)
848 (x
849 (return x)))))
850
0b6f49ef
LC
851(define (operating-system-etc-directory os)
852 "Return that static part of the /etc directory of OS."
0adfe95a
LC
853 (etc-directory
854 (fold-services (operating-system-services os)
855 #:target-type etc-service-type)))
033adfe7 856
6bad7524
SB
857(define (operating-system-environment-variables os)
858 "Return the environment variables of OS for
859@var{session-environment-service-type}, to be used in @file{/etc/environment}."
860 `(("LANG" . ,(operating-system-locale os))
54757499
LC
861 ;; Note: No need to set 'TZ' since (1) we provide /etc/localtime, and (2)
862 ;; it doesn't work for setuid binaries. See <https://bugs.gnu.org/29212>.
9e41130b 863 ("TZDIR" . ,(file-append tzdata "/share/zoneinfo"))
6bad7524
SB
864 ;; Tell 'modprobe' & co. where to look for modules.
865 ("LINUX_MODULE_DIRECTORY" . "/run/booted-system/kernel/lib/modules")
866 ;; These variables are honored by OpenSSL (libssl) and Git.
867 ("SSL_CERT_DIR" . "/etc/ssl/certs")
868 ("SSL_CERT_FILE" . "/etc/ssl/certs/ca-certificates.crt")
869 ("GIT_SSL_CAINFO" . "/etc/ssl/certs/ca-certificates.crt")
ae05e366
LC
870
871 ;; 'GTK_DATA_PREFIX' must name one directory where GTK+ themes are
872 ;; searched for.
873 ("GTK_DATA_PREFIX" . "/run/current-system/profile")
874
6bad7524
SB
875 ;; By default, applications that use D-Bus, such as Emacs, abort at startup
876 ;; when /etc/machine-id is missing. Make sure these warnings are non-fatal.
946465bb
LC
877 ("DBUS_FATAL_WARNINGS" . "0")
878
879 ;; XXX: Normally we wouldn't need to do this, but our glibc@2.23 package
880 ;; used to look things up in 'PREFIX/lib/locale' instead of
881 ;; '/run/current-system/locale' as was intended. Keep this hack around so
882 ;; that people who still have glibc@2.23-using packages in their profiles
883 ;; can use them correctly.
884 ;; TODO: Remove when glibc@2.23 is long gone.
885 ("GUIX_LOCPATH" . "/run/current-system/locale")))
6bad7524 886
09e028f4
LC
887(define %setuid-programs
888 ;; Default set of setuid-root programs.
3b65abac 889 (let ((shadow (@ (gnu packages admin) shadow)))
9e41130b
LC
890 (list (file-append shadow "/bin/passwd")
891 (file-append shadow "/bin/su")
852241eb
SB
892 (file-append shadow "/bin/newuidmap")
893 (file-append shadow "/bin/newgidmap")
9e41130b
LC
894 (file-append inetutils "/bin/ping")
895 (file-append inetutils "/bin/ping6")
896 (file-append sudo "/bin/sudo")
5144df2c 897 (file-append sudo "/bin/sudoedit")
19944227
LC
898 (file-append fuse "/bin/fusermount")
899
900 ;; To allow mounts with the "user" option, "mount" and "umount" must
901 ;; be setuid-root.
902 (file-append util-linux "/bin/mount")
903 (file-append util-linux "/bin/umount"))))
69689380
LC
904
905(define %sudoers-specification
906 ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel'
907 ;; group can do anything. See
908 ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>.
909 ;; TODO: Add a declarative API.
84765839
LC
910 (plain-file "sudoers" "\
911root ALL=(ALL) ALL
912%wheel ALL=(ALL) ALL\n"))
09e028f4 913
69cae3d3 914(define* (operating-system-activation-script os)
484a2b3a
LC
915 "Return the activation script for OS---i.e., the code that \"activates\" the
916stateful part of OS, including user accounts and groups, special directories,
917etc."
69cae3d3 918 (let* ((services (operating-system-services os))
0adfe95a
LC
919 (activation (fold-services services
920 #:target-type activation-service-type)))
921 (activation-service->script activation)))
484a2b3a 922
69cae3d3 923(define* (operating-system-boot-script os)
484a2b3a 924 "Return the boot script for OS---i.e., the code started by the initrd once
69cae3d3
LC
925we're running in the final root."
926 (let* ((services (operating-system-services os))
d62e201c 927 (boot (fold-services services #:target-type boot-service-type)))
efe7d19a 928 (service-value boot)))
2106d3fc 929
b2fef041
LC
930(define (operating-system-user-accounts os)
931 "Return the list of user accounts of OS."
932 (let* ((services (operating-system-services os))
933 (account (fold-services services
934 #:target-type account-service-type)))
935 (filter user-account?
efe7d19a 936 (service-value account))))
b2fef041
LC
937
938(define (operating-system-shepherd-service-names os)
939 "Return the list of Shepherd service names for OS."
940 (append-map shepherd-service-provision
efe7d19a 941 (service-value
b2fef041
LC
942 (fold-services (operating-system-services os)
943 #:target-type
944 shepherd-root-service-type))))
945
69cae3d3 946(define* (operating-system-derivation os)
d62e201c 947 "Return a derivation that builds OS."
69cae3d3 948 (let* ((services (operating-system-services os))
d62e201c
LC
949 (system (fold-services services)))
950 ;; SYSTEM contains the derivation as a monadic value.
efe7d19a 951 (service-value system)))
d62e201c 952
69cae3d3 953(define* (operating-system-profile os)
af4c3fd5
LC
954 "Return a derivation that builds the system profile of OS."
955 (mlet* %store-monad
69cae3d3 956 ((services -> (operating-system-services os))
af4c3fd5
LC
957 (profile (fold-services services
958 #:target-type profile-service-type)))
959 (match profile
960 (("profile" profile)
961 (return profile)))))
962
83bcd0b8
LC
963(define (operating-system-root-file-system os)
964 "Return the root file system of OS."
a48d3450
LC
965 (find (lambda (fs)
966 (string=? "/" (file-system-mount-point fs)))
83bcd0b8
LC
967 (operating-system-file-systems os)))
968
b4140694
LC
969(define (operating-system-initrd-file os)
970 "Return a gexp denoting the initrd file of OS."
83bcd0b8 971 (define boot-file-systems
4d6b879c 972 (filter file-system-needed-for-boot?
83bcd0b8
LC
973 (operating-system-file-systems os)))
974
de1c158f
LC
975 (define mapped-devices
976 (operating-system-boot-mapped-devices os))
977
978 (define make-initrd
979 (operating-system-initrd os))
980
d422cbb3
LC
981 (make-initrd boot-file-systems
982 #:linux (operating-system-kernel os)
983 #:linux-modules
984 (operating-system-initrd-modules os)
ae7a316b
LC
985 #:mapped-devices mapped-devices
986 #:keyboard-layout (operating-system-keyboard-layout os)))
b4140694 987
78fbf2bd
MO
988(define* (operating-system-uuid os #:optional (type 'dce))
989 "Compute UUID object with a deterministic \"UUID\" for OS, of the given
990TYPE (one of 'iso9660 or 'dce). Return a UUID object."
991 ;; Note: For this to be deterministic, we must not hash things that contains
992 ;; (directly or indirectly) procedures, for example. That rules out
993 ;; anything that contains gexps, thunk or delayed record fields, etc.
994
995 (define service-name
996 (compose service-type-name service-kind))
997
998 (define (file-system-digest fs)
999 ;; Return a hashable digest that does not contain 'dependencies' since
1000 ;; this field can contain procedures.
1001 (let ((device (file-system-device fs)))
1002 (list (file-system-mount-point fs)
1003 (file-system-type fs)
1004 (file-system-device->string device)
1005 (file-system-options fs))))
1006
1007 (if (eq? type 'iso9660)
1008 (let ((pad (compose (cut string-pad <> 2 #\0)
1009 number->string))
1010 (h (hash (map service-name (operating-system-services os))
1011 3600)))
1012 (bytevector->uuid
1013 (string->iso9660-uuid
1014 (string-append "1970-01-01-"
1015 (pad (hash (operating-system-host-name os) 24)) "-"
1016 (pad (quotient h 60)) "-"
1017 (pad (modulo h 60)) "-"
1018 (pad (hash (map file-system-digest
1019 (operating-system-file-systems os))
1020 100))))
1021 'iso9660))
1022 (bytevector->uuid
1023 (uint-list->bytevector
1024 (list (hash (map file-system-digest
1025 (operating-system-file-systems os))
1026 (- (expt 2 32) 1))
1027 (hash (operating-system-host-name os)
1028 (- (expt 2 32) 1))
1029 (hash (map service-name (operating-system-services os))
1030 (- (expt 2 32) 1))
1031 (hash (map file-system-digest (operating-system-file-systems os))
1032 (- (expt 2 32) 1)))
1033 (endianness little)
1034 4)
1035 type)))
1036
f5582b2c
LC
1037(define (locale-name->definition* name)
1038 "Variant of 'locale-name->definition' that raises an error upon failure."
1039 (match (locale-name->definition name)
1040 (#f
1041 (raise (condition
1042 (&message
69daee23 1043 (message (format #f (G_ "~a: invalid locale name") name))))))
f5582b2c
LC
1044 (def def)))
1045
598e19dc
LC
1046(define (operating-system-locale-directory os)
1047 "Return the directory containing the locales compiled for the definitions
1048listed in OS. The C library expects to find it under
1049/run/current-system/locale."
f5582b2c
LC
1050 (define name
1051 (operating-system-locale os))
1052
1053 (define definitions
1054 ;; While we're at it, check whether NAME is defined and add it if needed.
1055 (if (member name (map locale-definition-name
1056 (operating-system-locale-definitions os)))
1057 (operating-system-locale-definitions os)
1058 (cons (locale-name->definition* name)
1059 (operating-system-locale-definitions os))))
1060
1061 (locale-directory definitions
34760ae7 1062 #:libcs (operating-system-locale-libcs os)))
598e19dc 1063
c2e9942b
MO
1064(define (kernel->boot-label kernel)
1065 "Return a label for the bootloader menu entry that boots KERNEL."
bdc61ff9
P
1066 (cond ((package? kernel)
1067 (string-append "GNU with "
1068 (string-titlecase (package-name kernel)) " "
4ce3a326 1069 (package-version kernel)))
bdc61ff9
P
1070 ((inferior-package? kernel)
1071 (string-append "GNU with "
b12f8720 1072 (string-titlecase (inferior-package-name kernel)) " "
4ce3a326 1073 (inferior-package-version kernel)))
bdc61ff9 1074 (else "GNU")))
2d23e6f0 1075
3f03a198
LC
1076(define (operating-system-default-label os)
1077 "Return the default label for OS, as it will appear in the bootloader menu
1078entry."
1079 (kernel->boot-label (operating-system-kernel os)))
1080
6b779207
LC
1081(define (store-file-system file-systems)
1082 "Return the file system object among FILE-SYSTEMS that contains the store."
1083 (match (filter (lambda (fs)
1084 (and (file-system-mount? fs)
1085 (not (memq 'bind-mount (file-system-flags fs)))
1086 (string-prefix? (file-system-mount-point fs)
1087 (%store-prefix))))
1088 file-systems)
1089 ((and candidates (head . tail))
1090 (reduce (lambda (fs1 fs2)
1091 (if (> (string-length (file-system-mount-point fs1))
1092 (string-length (file-system-mount-point fs2)))
1093 fs1
1094 fs2))
1095 head
1096 candidates))))
1097
1098(define (operating-system-store-file-system os)
1099 "Return the file system that contains the store of OS."
1100 (store-file-system (operating-system-file-systems os)))
1101
c76b3046 1102(define* (operating-system-bootcfg os #:optional (old-entries '()))
5ece56dc
LC
1103 "Return the bootloader configuration file for OS. Use OLD-ENTRIES,
1104a list of <menu-entry>, to populate the \"old entries\" menu."
9782c822
LC
1105 (let* ((root-fs (operating-system-root-file-system os))
1106 (root-device (file-system-device root-fs))
1107 (params (operating-system-boot-parameters
1108 os root-device
1109 #:system-kernel-arguments? #t))
1110 (entry (boot-parameters->menu-entry params))
1111 (bootloader-conf (operating-system-bootloader os)))
46c296dc
LC
1112 (define generate-config-file
1113 (bootloader-configuration-file-generator
1114 (bootloader-configuration-bootloader bootloader-conf)))
1115
9782c822
LC
1116 (generate-config-file bootloader-conf (list entry)
1117 #:old-entries old-entries)))
b4140694 1118
a7ef45d9
LC
1119(define* (operating-system-boot-parameters os root-device
1120 #:key system-kernel-arguments?)
1121 "Return a monadic <boot-parameters> record that describes the boot
1122parameters of OS. When SYSTEM-KERNEL-ARGUMENTS? is true, add kernel arguments
1123such as '--root' and '--load' to <boot-parameters>."
35b44681
LC
1124 (let* ((initrd (operating-system-initrd-file os))
1125 (store (operating-system-store-file-system os))
1126 (bootloader (bootloader-configuration-bootloader
1127 (operating-system-bootloader os)))
1128 (bootloader-name (bootloader-name bootloader))
3f03a198 1129 (label (operating-system-label os)))
35b44681
LC
1130 (boot-parameters
1131 (label label)
1132 (root-device root-device)
1133 (kernel (operating-system-kernel-file os))
1134 (kernel-arguments
1135 (if system-kernel-arguments?
1136 (operating-system-kernel-arguments os root-device)
1137 (operating-system-user-kernel-arguments os)))
1138 (initrd initrd)
1139 (bootloader-name bootloader-name)
a28cfee8
LC
1140 (bootloader-menu-entries
1141 (bootloader-configuration-menu-entries (operating-system-bootloader os)))
35b44681
LC
1142 (store-device (ensure-not-/dev (file-system-device store)))
1143 (store-mount-point (file-system-mount-point store)))))
40fad1c2 1144
9b336338
LC
1145(define (device->sexp device)
1146 "Serialize DEVICE as an sexp (really, as an object with a read syntax.)"
1147 (match device
1148 ((? uuid? uuid)
075681d3 1149 `(uuid ,(uuid-type uuid) ,(uuid-bytevector uuid)))
a5acc17a
LC
1150 ((? file-system-label? label)
1151 `(file-system-label ,(file-system-label->string label)))
9b336338
LC
1152 (_
1153 device)))
1154
a7ef45d9
LC
1155(define* (operating-system-boot-parameters-file os
1156 #:key system-kernel-arguments?)
40fad1c2
DM
1157 "Return a file that describes the boot parameters of OS. The primary use of
1158this file is the reconstruction of GRUB menu entries for old configurations.
a7ef45d9
LC
1159
1160When SYSTEM-KERNEL-ARGUMENTS? is true, add kernel arguments such as '--root'
1161and '--load' to the returned file (since the returned file is then usually
1162stored into the content-addressed \"system\" directory, it's usually not a
1163good idea to give it because the content hash would change by the content hash
40fad1c2 1164being stored into the \"parameters\" file)."
35b44681
LC
1165 (let* ((root (operating-system-root-file-system os))
1166 (device (file-system-device root))
1167 (params (operating-system-boot-parameters
1168 os device
1169 #:system-kernel-arguments?
1170 system-kernel-arguments?)))
2e37d158
LC
1171 (scheme-file "parameters"
1172 #~(boot-parameters
1173 (version 0)
1174 (label #$(boot-parameters-label params))
1175 (root-device
1176 #$(device->sexp
1177 (boot-parameters-root-device params)))
1178 (kernel #$(boot-parameters-kernel params))
1179 (kernel-arguments
1180 #$(boot-parameters-kernel-arguments params))
1181 (initrd #$(boot-parameters-initrd params))
1182 (bootloader-name #$(boot-parameters-bootloader-name params))
1183 (bootloader-menu-entries
1184 #$(map menu-entry->sexp
1185 (or (and=> (operating-system-bootloader os)
1186 bootloader-configuration-menu-entries)
1187 '())))
1188 (store
1189 (device
1190 #$(device->sexp (boot-parameters-store-device params)))
1191 (mount-point #$(boot-parameters-store-mount-point
1192 params))))
1193 #:set-load-path? #f)))
64e40dbb 1194
96da5d62
LC
1195(define-gexp-compiler (operating-system-compiler (os <operating-system>)
1196 system target)
1197 ((store-lift
1198 (lambda (store)
1199 ;; XXX: This is not super elegant but we can't pass SYSTEM and TARGET to
1200 ;; 'operating-system-derivation'.
1201 (run-with-store store (operating-system-derivation os)
1202 #:system system
1203 #:target target)))))
1204
033adfe7 1205;;; system.scm ends here