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