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