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