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