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