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