projects/guix·blob go-team · refs

1714 lines · 70.1 KiB

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