gnu: guile-wm: Fix cross-compilation.
[jackhill/guix/guix.git] / gnu / system.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019 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 ;;;
9 ;;; This file is part of GNU Guix.
10 ;;;
11 ;;; GNU Guix is free software; you can redistribute it and/or modify it
12 ;;; under the terms of the GNU General Public License as published by
13 ;;; the Free Software Foundation; either version 3 of the License, or (at
14 ;;; your option) any later version.
15 ;;;
16 ;;; GNU Guix is distributed in the hope that it will be useful, but
17 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;;; GNU General Public License for more details.
20 ;;;
21 ;;; You should have received a copy of the GNU General Public License
22 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23
24 (define-module (gnu system)
25 #:use-module (guix inferior)
26 #:use-module (guix store)
27 #:use-module (guix monads)
28 #:use-module (guix gexp)
29 #:use-module (guix records)
30 #:use-module (guix packages)
31 #:use-module (guix derivations)
32 #:use-module (guix profiles)
33 #:use-module (guix ui)
34 #:use-module (gnu packages base)
35 #:use-module (gnu packages bash)
36 #:use-module (gnu packages guile)
37 #:use-module (gnu packages guile-xyz)
38 #:use-module (gnu packages admin)
39 #:use-module (gnu packages linux)
40 #:use-module (gnu packages pciutils)
41 #:use-module (gnu packages package-management)
42 #:use-module (gnu packages less)
43 #:use-module (gnu packages zile)
44 #:use-module (gnu packages nano)
45 #:use-module (gnu packages gawk)
46 #:use-module (gnu packages man)
47 #:use-module (gnu packages texinfo)
48 #:use-module (gnu packages compression)
49 #:use-module (gnu packages firmware)
50 #:use-module (gnu services)
51 #:use-module (gnu services shepherd)
52 #:use-module (gnu services base)
53 #:use-module (gnu bootloader)
54 #:use-module (gnu system shadow)
55 #:use-module (gnu system nss)
56 #:use-module (gnu system locale)
57 #:use-module (gnu system pam)
58 #:use-module (gnu system linux-initrd)
59 #:use-module (gnu system uuid)
60 #:use-module (gnu system file-systems)
61 #:use-module (gnu system mapped-devices)
62 #:use-module (ice-9 match)
63 #:use-module (srfi srfi-1)
64 #:use-module (srfi srfi-26)
65 #:use-module (srfi srfi-34)
66 #:use-module (srfi srfi-35)
67 #:use-module (rnrs bytevectors)
68 #:export (operating-system
69 operating-system?
70 this-operating-system
71
72 operating-system-bootloader
73 operating-system-services
74 operating-system-essential-services
75 operating-system-default-essential-services
76 operating-system-user-services
77 operating-system-packages
78 operating-system-host-name
79 operating-system-hosts-file
80 operating-system-kernel
81 operating-system-kernel-file
82 operating-system-kernel-arguments
83 operating-system-label
84 operating-system-default-label
85 operating-system-initrd-modules
86 operating-system-initrd
87 operating-system-users
88 operating-system-groups
89 operating-system-issue
90 operating-system-timezone
91 operating-system-locale
92 operating-system-locale-definitions
93 operating-system-locale-libcs
94 operating-system-mapped-devices
95 operating-system-file-systems
96 operating-system-store-file-system
97 operating-system-user-mapped-devices
98 operating-system-boot-mapped-devices
99 operating-system-activation-script
100 operating-system-user-accounts
101 operating-system-shepherd-service-names
102 operating-system-user-kernel-arguments
103
104 operating-system-derivation
105 operating-system-profile
106 operating-system-bootcfg
107 operating-system-etc-directory
108 operating-system-locale-directory
109 operating-system-boot-script
110
111 system-linux-image-file-name
112 operating-system-with-gc-roots
113
114 boot-parameters
115 boot-parameters?
116 boot-parameters-label
117 boot-parameters-root-device
118 boot-parameters-bootloader-name
119 boot-parameters-bootloader-menu-entries
120 boot-parameters-store-device
121 boot-parameters-store-mount-point
122 boot-parameters-kernel
123 boot-parameters-kernel-arguments
124 boot-parameters-initrd
125 read-boot-parameters
126 read-boot-parameters-file
127 boot-parameters->menu-entry
128
129 local-host-aliases
130 %root-account
131 %setuid-programs
132 %base-packages
133 %base-firmware))
134
135 ;;; Commentary:
136 ;;;
137 ;;; This module supports whole-system configuration.
138 ;;;
139 ;;; Code:
140
141 (define (bootable-kernel-arguments system root-device)
142 "Return a list of kernel arguments (gexps) to boot SYSTEM from ROOT-DEVICE."
143 (list (string-append "--root="
144 (cond ((uuid? root-device)
145
146 ;; Note: Always use the DCE format because that's
147 ;; what (gnu build linux-boot) expects for the
148 ;; '--root' kernel command-line option.
149 (uuid->string (uuid-bytevector root-device)
150 'dce))
151 ((file-system-label? root-device)
152 (file-system-label->string root-device))
153 (else root-device)))
154 #~(string-append "--system=" #$system)
155 #~(string-append "--load=" #$system "/boot")))
156
157 ;; System-wide configuration.
158 ;; TODO: Add per-field docstrings/stexi.
159 (define-record-type* <operating-system> operating-system
160 make-operating-system
161 operating-system?
162 this-operating-system
163
164 (kernel operating-system-kernel ; package
165 (default linux-libre))
166 (kernel-arguments operating-system-user-kernel-arguments
167 (default '("quiet"))) ; list of gexps/strings
168 (bootloader operating-system-bootloader) ; <bootloader-configuration>
169 (label operating-system-label ; string
170 (thunked)
171 (default (operating-system-default-label this-operating-system)))
172
173 (keyboard-layout operating-system-keyboard-layout ;#f | <keyboard-layout>
174 (default #f))
175 (initrd operating-system-initrd ; (list fs) -> file-like
176 (default base-initrd))
177 (initrd-modules operating-system-initrd-modules ; list of strings
178 (thunked) ; it's system-dependent
179 (default %base-initrd-modules))
180
181 (firmware operating-system-firmware ; list of packages
182 (default %base-firmware))
183
184 (host-name operating-system-host-name) ; string
185 (hosts-file operating-system-hosts-file ; file-like | #f
186 (default #f))
187
188 (mapped-devices operating-system-mapped-devices ; list of <mapped-device>
189 (default '()))
190 (file-systems operating-system-file-systems) ; list of fs
191 (swap-devices operating-system-swap-devices ; list of strings
192 (default '()))
193
194 (users operating-system-users ; list of user accounts
195 (default %base-user-accounts))
196 (groups operating-system-groups ; list of user groups
197 (default %base-groups))
198
199 (skeletons operating-system-skeletons ; list of name/file-like value
200 (default (default-skeletons)))
201 (issue operating-system-issue ; string
202 (default %default-issue))
203
204 (packages operating-system-packages ; list of (PACKAGE OUTPUT...)
205 (default %base-packages)) ; or just PACKAGE
206
207 (timezone operating-system-timezone) ; string
208 (locale operating-system-locale ; string
209 (default "en_US.utf8"))
210 (locale-definitions operating-system-locale-definitions ; list of <locale-definition>
211 (default %default-locale-definitions))
212 (locale-libcs operating-system-locale-libcs ; list of <packages>
213 (default %default-locale-libcs))
214 (name-service-switch operating-system-name-service-switch ; <name-service-switch>
215 (default %default-nss))
216
217 (essential-services operating-system-essential-services ; list of services
218 (thunked)
219 (default (operating-system-default-essential-services
220 this-operating-system)))
221 (services operating-system-user-services ; list of services
222 (default %base-services))
223
224 (pam-services operating-system-pam-services ; list of PAM services
225 (default (base-pam-services)))
226 (setuid-programs operating-system-setuid-programs
227 (default %setuid-programs)) ; list of string-valued gexps
228
229 (sudoers-file operating-system-sudoers-file ; file-like
230 (default %sudoers-specification)))
231
232 (define (operating-system-kernel-arguments os root-device)
233 "Return all the kernel arguments, including the ones not specified
234 directly by the user."
235 (append (bootable-kernel-arguments os root-device)
236 (operating-system-user-kernel-arguments os)))
237
238 \f
239 ;;;
240 ;;; Boot parameters
241 ;;;
242
243 (define-record-type* <boot-parameters>
244 boot-parameters make-boot-parameters boot-parameters?
245 (label boot-parameters-label)
246 ;; Because we will use the 'store-device' to create the GRUB search command,
247 ;; the 'store-device' has slightly different semantics than 'root-device'.
248 ;; The 'store-device' can be a file system uuid, a file system label, or #f,
249 ;; but it cannot be a device path such as "/dev/sda3", since GRUB would not
250 ;; understand that. The 'root-device', on the other hand, corresponds
251 ;; exactly to the device field of the <file-system> object representing the
252 ;; OS's root file system, so it might be a device path like "/dev/sda3".
253 (root-device boot-parameters-root-device)
254 (bootloader-name boot-parameters-bootloader-name)
255 (bootloader-menu-entries ;list of <menu-entry>
256 boot-parameters-bootloader-menu-entries)
257 (store-device boot-parameters-store-device)
258 (store-mount-point boot-parameters-store-mount-point)
259 (kernel boot-parameters-kernel)
260 (kernel-arguments boot-parameters-kernel-arguments)
261 (initrd boot-parameters-initrd))
262
263 (define (ensure-not-/dev device)
264 "If DEVICE starts with a slash, return #f. This is meant to filter out
265 Linux device names such as /dev/sda, and to preserve GRUB device names and
266 file system labels."
267 (if (and (string? device) (string-prefix? "/" device))
268 #f
269 device))
270
271 (define (read-boot-parameters port)
272 "Read boot parameters from PORT and return the corresponding
273 <boot-parameters> object or #f if the format is unrecognized."
274 (define device-sexp->device
275 (match-lambda
276 (('uuid (? symbol? type) (? bytevector? bv))
277 (bytevector->uuid bv type))
278 (('file-system-label (? string? label))
279 (file-system-label label))
280 ((? bytevector? bv) ;old format
281 (bytevector->uuid bv 'dce))
282 ((? string? device)
283 ;; It used to be that we would not distinguish between labels and
284 ;; device names. Try to infer the right thing here.
285 (if (string-prefix? "/dev/" device)
286 device
287 (file-system-label device)))))
288
289 (match (read port)
290 (('boot-parameters ('version 0)
291 ('label label) ('root-device root)
292 ('kernel linux)
293 rest ...)
294 (boot-parameters
295 (label label)
296 (root-device (device-sexp->device root))
297
298 (bootloader-name
299 (match (assq 'bootloader-name rest)
300 ((_ args) args)
301 (#f 'grub))) ; for compatibility reasons.
302
303 (bootloader-menu-entries
304 (match (assq 'bootloader-menu-entries rest)
305 ((_ entries) (map sexp->menu-entry entries))
306 (#f '())))
307
308 ;; In the past, we would store the directory name of the kernel instead
309 ;; of the absolute file name of its image. Detect that and correct it.
310 (kernel (if (string=? linux (direct-store-path linux))
311 (string-append linux "/"
312 (system-linux-image-file-name))
313 linux))
314
315 (kernel-arguments
316 (match (assq 'kernel-arguments rest)
317 ((_ args) args)
318 (#f '()))) ;the old format
319
320 (initrd
321 (match (assq 'initrd rest)
322 (('initrd ('string-append directory file)) ;the old format
323 (string-append directory file))
324 (('initrd (? string? file))
325 file)))
326
327 (store-device
328 ;; Linux device names like "/dev/sda1" are not suitable GRUB device
329 ;; identifiers, so we just filter them out.
330 (ensure-not-/dev
331 (match (assq 'store rest)
332 (('store ('device #f) _ ...)
333 root-device)
334 (('store ('device device) _ ...)
335 (device-sexp->device device))
336 (_ ;the old format
337 root-device))))
338
339 (store-mount-point
340 (match (assq 'store rest)
341 (('store ('device _) ('mount-point mount-point) _ ...)
342 mount-point)
343 (_ ;the old format
344 "/")))))
345 (x ;unsupported format
346 (warning (G_ "unrecognized boot parameters at '~a'~%")
347 (port-filename port))
348 #f)))
349
350 (define (read-boot-parameters-file system)
351 "Read boot parameters from SYSTEM's (system or generation) \"parameters\"
352 file and returns the corresponding <boot-parameters> object or #f if the
353 format is unrecognized.
354 The object has its kernel-arguments extended in order to make it bootable."
355 (let* ((file (string-append system "/parameters"))
356 (params (call-with-input-file file read-boot-parameters))
357 (root (boot-parameters-root-device params)))
358 (boot-parameters
359 (inherit params)
360 (kernel-arguments (append (bootable-kernel-arguments system root)
361 (boot-parameters-kernel-arguments params))))))
362
363 (define (boot-parameters->menu-entry conf)
364 (menu-entry
365 (label (boot-parameters-label conf))
366 (device (boot-parameters-store-device conf))
367 (device-mount-point (boot-parameters-store-mount-point conf))
368 (linux (boot-parameters-kernel conf))
369 (linux-arguments (boot-parameters-kernel-arguments conf))
370 (initrd (boot-parameters-initrd conf))))
371
372
373 \f
374 ;;;
375 ;;; Services.
376 ;;;
377
378 (define (non-boot-file-system-service os)
379 "Return the file system service for the file systems of OS that are not
380 marked as 'needed-for-boot'."
381 (define file-systems
382 (remove file-system-needed-for-boot?
383 (operating-system-file-systems os)))
384
385 (define mapped-devices-for-boot
386 (operating-system-boot-mapped-devices os))
387
388 (define (device-mappings fs)
389 (let ((device (file-system-device fs)))
390 (if (string? device) ;title is 'device
391 (filter (lambda (md)
392 (string=? (string-append "/dev/mapper/"
393 (mapped-device-target md))
394 device))
395 (operating-system-mapped-devices os))
396 '())))
397
398 (define (add-dependencies fs)
399 ;; Add the dependencies due to device mappings to FS.
400 (file-system
401 (inherit fs)
402 (dependencies
403 (delete-duplicates
404 (remove (cut member <> mapped-devices-for-boot)
405 (append (device-mappings fs)
406 (file-system-dependencies fs)))
407 eq?))))
408
409 (service file-system-service-type
410 (map add-dependencies file-systems)))
411
412 (define (mapped-device-users device file-systems)
413 "Return the subset of FILE-SYSTEMS that use DEVICE."
414 (let ((target (string-append "/dev/mapper/" (mapped-device-target device))))
415 (filter (lambda (fs)
416 (or (member device (file-system-dependencies fs))
417 (and (string? (file-system-device fs))
418 (string=? (file-system-device fs) target))))
419 file-systems)))
420
421 (define (operating-system-user-mapped-devices os)
422 "Return the subset of mapped devices that can be installed in
423 user-land--i.e., those not needed during boot."
424 (let ((devices (operating-system-mapped-devices os))
425 (file-systems (operating-system-file-systems os)))
426 (filter (lambda (md)
427 (let ((users (mapped-device-users md file-systems)))
428 (not (any file-system-needed-for-boot? users))))
429 devices)))
430
431 (define (operating-system-boot-mapped-devices os)
432 "Return the subset of mapped devices that must be installed during boot,
433 from the initrd."
434 (let ((devices (operating-system-mapped-devices os))
435 (file-systems (operating-system-file-systems os)))
436 (filter (lambda (md)
437 (let ((users (mapped-device-users md file-systems)))
438 (any file-system-needed-for-boot? users)))
439 devices)))
440
441 (define (device-mapping-services os)
442 "Return the list of device-mapping services for OS as a list."
443 (map device-mapping-service
444 (operating-system-user-mapped-devices os)))
445
446 (define (swap-services os)
447 "Return the list of swap services for OS."
448 (map swap-service (operating-system-swap-devices os)))
449
450 (define* (system-linux-image-file-name #:optional (system (%current-system)))
451 "Return the basename of the kernel image file for SYSTEM."
452 ;; FIXME: Evaluate the conditional based on the actual current system.
453 (cond
454 ((string-prefix? "arm" (%current-system)) "zImage")
455 ((string-prefix? "mips" (%current-system)) "vmlinuz")
456 ((string-prefix? "aarch64" (%current-system)) "Image")
457 (else "bzImage")))
458
459 (define (operating-system-kernel-file os)
460 "Return an object representing the absolute file name of the kernel image of
461 OS."
462 (file-append (operating-system-kernel os)
463 "/" (system-linux-image-file-name os)))
464
465 (define* (operating-system-directory-base-entries os)
466 "Return the basic entries of the 'system' directory of OS for use as the
467 value of the SYSTEM-SERVICE-TYPE service."
468 (let ((locale (operating-system-locale-directory os)))
469 (mlet %store-monad ((kernel -> (operating-system-kernel os))
470 (initrd -> (operating-system-initrd-file os))
471 (params (operating-system-boot-parameters-file os)))
472 (return `(("kernel" ,kernel)
473 ("parameters" ,params)
474 ("initrd" ,initrd)
475 ("locale" ,locale)))))) ;used by libc
476
477 (define (operating-system-default-essential-services os)
478 "Return the list of essential services for OS. These are special services
479 that implement part of what's declared in OS are responsible for low-level
480 bookkeeping."
481 (define known-fs
482 (map file-system-mount-point (operating-system-file-systems os)))
483
484 (let* ((mappings (device-mapping-services os))
485 (root-fs (root-file-system-service))
486 (other-fs (non-boot-file-system-service os))
487 (swaps (swap-services os))
488 (procs (service user-processes-service-type))
489 (host-name (host-name-service (operating-system-host-name os)))
490 (entries (operating-system-directory-base-entries os)))
491 (cons* (service system-service-type entries)
492 %boot-service
493
494 ;; %SHEPHERD-ROOT-SERVICE must come last so that the gexp that
495 ;; execs shepherd comes last in the boot script (XXX). Likewise,
496 ;; the cleanup service must come first so that its gexp runs before
497 ;; activation code.
498 (service cleanup-service-type #f)
499 %activation-service
500 %shepherd-root-service
501
502 (pam-root-service (operating-system-pam-services os))
503 (account-service (append (operating-system-accounts os)
504 (operating-system-groups os))
505 (operating-system-skeletons os))
506 (operating-system-etc-service os)
507 (service fstab-service-type
508 (filter file-system-needed-for-boot?
509 (operating-system-file-systems os)))
510 (session-environment-service
511 (operating-system-environment-variables os))
512 host-name procs root-fs
513 (service setuid-program-service-type
514 (operating-system-setuid-programs os))
515 (service profile-service-type
516 (operating-system-packages os))
517 other-fs
518 (append mappings swaps
519
520 ;; Add the firmware service.
521 (list %linux-bare-metal-service
522 (service firmware-service-type
523 (operating-system-firmware os)))))))
524
525 (define* (operating-system-services os)
526 "Return all the services of OS, including \"essential\" services."
527 (instantiate-missing-services
528 (append (operating-system-user-services os)
529 (operating-system-essential-services os))))
530
531 (define (operating-system-with-gc-roots os roots)
532 "Return a variant of OS where ROOTS are registered as GC roots."
533 (operating-system
534 (inherit os)
535
536 ;; We use this procedure for the installation OS, which already defines GC
537 ;; roots. Add ROOTS to those.
538 (services (cons (simple-service 'extra-root
539 gc-root-service-type roots)
540 (operating-system-user-services os)))))
541
542 \f
543 ;;;
544 ;;; /etc.
545 ;;;
546
547 (define %base-firmware
548 ;; Firmware usable by default.
549 (list ath9k-htc-firmware
550 openfwwf-firmware))
551
552 (define %base-packages
553 ;; Default set of packages globally visible. It should include anything
554 ;; required for basic administrator tasks.
555 (cons* procps psmisc which less zile nano
556 pciutils usbutils
557 util-linux
558 inetutils isc-dhcp
559 (@ (gnu packages admin) shadow) ;for 'passwd'
560
561 ;; wireless-tools is deprecated in favor of iw, but it's still what
562 ;; many people are familiar with, so keep it around.
563 iw wireless-tools
564
565 iproute
566 net-tools ; XXX: remove when Inetutils suffices
567 man-db
568 info-reader ;the standalone Info reader (no Perl)
569
570 ;; The 'sudo' command is already in %SETUID-PROGRAMS, but we also
571 ;; want the other commands and the man pages (notably because
572 ;; auto-completion in Emacs shell relies on man pages.)
573 sudo
574
575 ;; Get 'insmod' & co. from kmod, not module-init-tools, since udev
576 ;; already depends on it anyway.
577 kmod eudev
578
579 e2fsprogs kbd
580
581 bash-completion
582
583 ;; XXX: We don't use (canonical-package guile-2.2) here because that
584 ;; would create a collision in the global profile between the GMP
585 ;; variant propagated by 'guile-final' and the GMP variant propagated
586 ;; by 'gnutls', itself propagated by 'guix'.
587 guile-2.2
588 guile-readline guile-colorized
589
590 ;; The packages below are also in %FINAL-INPUTS, so take them from
591 ;; there to avoid duplication.
592 (map canonical-package
593 (list bash coreutils findutils grep sed
594 diffutils patch gawk tar gzip bzip2 xz lzip))))
595
596 (define %default-issue
597 ;; Default contents for /etc/issue.
598 "
599 This is the GNU system. Welcome.\n")
600
601 (define (local-host-aliases host-name)
602 "Return aliases for HOST-NAME, to be used in /etc/hosts."
603 (string-append "127.0.0.1 localhost " host-name "\n"
604 "::1 localhost " host-name "\n"))
605
606 (define (default-/etc/hosts host-name)
607 "Return the default /etc/hosts file."
608 (plain-file "hosts" (local-host-aliases host-name)))
609
610 (define* (operating-system-etc-service os)
611 "Return a <service> that builds containing the static part of the /etc
612 directory."
613 (let ((login.defs
614 (plain-file "login.defs"
615 (string-append
616 "# Default paths for non-login shells started by su(1).\n"
617 "ENV_PATH /run/setuid-programs:"
618 "/run/current-system/profile/bin:"
619 "/run/current-system/profile/sbin\n"
620 "ENV_SUPATH /run/setuid-programs:"
621 "/run/current-system/profile/bin:"
622 "/run/current-system/profile/sbin\n")))
623
624 (issue (plain-file "issue" (operating-system-issue os)))
625 (nsswitch (plain-file "nsswitch.conf"
626 (name-service-switch->string
627 (operating-system-name-service-switch os))))
628
629 ;; Startup file for POSIX-compliant login shells, which set system-wide
630 ;; environment variables.
631 (profile (mixed-text-file "profile" "\
632 # Crucial variables that could be missing in the profiles' 'etc/profile'
633 # because they would require combining both profiles.
634 # FIXME: See <http://bugs.gnu.org/20255>.
635 export MANPATH=$HOME/.guix-profile/share/man:/run/current-system/profile/share/man
636 export INFOPATH=$HOME/.guix-profile/share/info:/run/current-system/profile/share/info
637 export XDG_DATA_DIRS=$HOME/.guix-profile/share:/run/current-system/profile/share
638 export XDG_CONFIG_DIRS=$HOME/.guix-profile/etc/xdg:/run/current-system/profile/etc/xdg
639
640 # Make sure libXcursor finds cursors installed into user or system profiles. See <http://bugs.gnu.org/24445>
641 export XCURSOR_PATH=$HOME/.icons:$HOME/.guix-profile/share/icons:/run/current-system/profile/share/icons
642
643 # Ignore the default value of 'PATH'.
644 unset PATH
645
646 # Load the system profile's settings.
647 GUIX_PROFILE=/run/current-system/profile ; \\
648 . /run/current-system/profile/etc/profile
649
650 # Since 'lshd' does not use pam_env, /etc/environment must be explicitly
651 # loaded when someone logs in via SSH. See <http://bugs.gnu.org/22175>.
652 # We need 'PATH' to be defined here, for 'cat' and 'cut'. Do this before
653 # reading the user's 'etc/profile' to allow variables to be overridden.
654 if [ -f /etc/environment -a -n \"$SSH_CLIENT\" \\
655 -a -z \"$LINUX_MODULE_DIRECTORY\" ]
656 then
657 . /etc/environment
658 export `cat /etc/environment | cut -d= -f1`
659 fi
660
661 # Arrange so that ~/.config/guix/current comes first.
662 for profile in \"$HOME/.guix-profile\" \"$HOME/.config/guix/current\"
663 do
664 if [ -f \"$profile/etc/profile\" ]
665 then
666 # Load the user profile's settings.
667 GUIX_PROFILE=\"$profile\" ; \\
668 . \"$profile/etc/profile\"
669 else
670 # At least define this one so that basic things just work
671 # when the user installs their first package.
672 export PATH=\"$profile/bin:$PATH\"
673 fi
674 done
675
676 # Prepend setuid programs.
677 export PATH=/run/setuid-programs:$PATH
678
679 # Arrange so that ~/.config/guix/current/share/info comes first.
680 export INFOPATH=\"$HOME/.config/guix/current/share/info:$INFOPATH\"
681
682 # Set the umask, notably for users logging in via 'lsh'.
683 # See <http://bugs.gnu.org/22650>.
684 umask 022
685
686 # Allow Hunspell-based applications (IceCat, LibreOffice, etc.) to
687 # find dictionaries.
688 export DICPATH=\"$HOME/.guix-profile/share/hunspell:/run/current-system/profile/share/hunspell\"
689
690 # Allow GStreamer-based applications to find plugins.
691 export GST_PLUGIN_PATH=\"$HOME/.guix-profile/lib/gstreamer-1.0\"
692
693 if [ -n \"$BASH_VERSION\" -a -f /etc/bashrc ]
694 then
695 # Load Bash-specific initialization code.
696 . /etc/bashrc
697 fi
698 "))
699
700 (bashrc (plain-file "bashrc" "\
701 # Bash-specific initialization.
702
703 # The 'bash-completion' package.
704 if [ -f /run/current-system/profile/etc/profile.d/bash_completion.sh ]
705 then
706 # Bash-completion sources ~/.bash_completion. It installs a dynamic
707 # completion loader that searches its own completion files as well
708 # as those in ~/.guix-profile and /run/current-system/profile.
709 source /run/current-system/profile/etc/profile.d/bash_completion.sh
710 fi\n")))
711 (etc-service
712 `(("services" ,(file-append net-base "/etc/services"))
713 ("protocols" ,(file-append net-base "/etc/protocols"))
714 ("rpc" ,(file-append net-base "/etc/rpc"))
715 ("login.defs" ,#~#$login.defs)
716 ("issue" ,#~#$issue)
717 ("nsswitch.conf" ,#~#$nsswitch)
718 ("profile" ,#~#$profile)
719 ("bashrc" ,#~#$bashrc)
720 ("hosts" ,#~#$(or (operating-system-hosts-file os)
721 (default-/etc/hosts (operating-system-host-name os))))
722 ;; Write the operating-system-host-name to /etc/hostname to prevent
723 ;; NetworkManager from changing the system's hostname when connecting
724 ;; to certain networks. Some discussion at
725 ;; https://lists.gnu.org/archive/html/help-guix/2017-09/msg00037.html
726 ("hostname" ,(plain-file "hostname" (operating-system-host-name os)))
727 ("localtime" ,(file-append tzdata "/share/zoneinfo/"
728 (operating-system-timezone os)))
729 ("sudoers" ,(operating-system-sudoers-file os))))))
730
731 (define %root-account
732 ;; Default root account.
733 (user-account
734 (name "root")
735 (password "")
736 (uid 0) (group "root")
737 (comment "System administrator")
738 (home-directory "/root")))
739
740 (define (operating-system-accounts os)
741 "Return the user accounts for OS, including an obligatory 'root' account,
742 and excluding accounts requested by services."
743 ;; Make sure there's a root account.
744 (if (find (lambda (user)
745 (and=> (user-account-uid user) zero?))
746 (operating-system-users os))
747 (operating-system-users os)
748 (cons %root-account (operating-system-users os))))
749
750 (define (maybe-string->file file-name thing)
751 "If THING is a string, return a <plain-file> with THING as its content.
752 Otherwise just return THING.
753
754 This is for backward-compatibility of fields that used to be strings and are
755 now file-like objects.."
756 (match thing
757 ((? string?)
758 (warning (G_ "using a string for file '~a' is deprecated; \
759 use 'plain-file' instead~%")
760 file-name)
761 (plain-file file-name thing))
762 (x
763 x)))
764
765 (define (maybe-file->monadic file-name thing)
766 "If THING is a value in %STORE-MONAD, return it as is; otherwise return
767 THING in the %STORE-MONAD.
768
769 This is for backward-compatibility of fields that used to be monadic values
770 and are now file-like objects."
771 (with-monad %store-monad
772 (match thing
773 ((? procedure?)
774 (warning (G_ "using a monadic value for '~a' is deprecated; \
775 use 'plain-file' instead~%")
776 file-name)
777 thing)
778 (x
779 (return x)))))
780
781 (define (operating-system-etc-directory os)
782 "Return that static part of the /etc directory of OS."
783 (etc-directory
784 (fold-services (operating-system-services os)
785 #:target-type etc-service-type)))
786
787 (define (operating-system-environment-variables os)
788 "Return the environment variables of OS for
789 @var{session-environment-service-type}, to be used in @file{/etc/environment}."
790 `(("LANG" . ,(operating-system-locale os))
791 ;; Note: No need to set 'TZ' since (1) we provide /etc/localtime, and (2)
792 ;; it doesn't work for setuid binaries. See <https://bugs.gnu.org/29212>.
793 ("TZDIR" . ,(file-append tzdata "/share/zoneinfo"))
794 ;; Tell 'modprobe' & co. where to look for modules.
795 ("LINUX_MODULE_DIRECTORY" . "/run/booted-system/kernel/lib/modules")
796 ;; These variables are honored by OpenSSL (libssl) and Git.
797 ("SSL_CERT_DIR" . "/etc/ssl/certs")
798 ("SSL_CERT_FILE" . "/etc/ssl/certs/ca-certificates.crt")
799 ("GIT_SSL_CAINFO" . "/etc/ssl/certs/ca-certificates.crt")
800
801 ;; 'GTK_DATA_PREFIX' must name one directory where GTK+ themes are
802 ;; searched for.
803 ("GTK_DATA_PREFIX" . "/run/current-system/profile")
804
805 ;; By default, applications that use D-Bus, such as Emacs, abort at startup
806 ;; when /etc/machine-id is missing. Make sure these warnings are non-fatal.
807 ("DBUS_FATAL_WARNINGS" . "0")
808
809 ;; XXX: Normally we wouldn't need to do this, but our glibc@2.23 package
810 ;; used to look things up in 'PREFIX/lib/locale' instead of
811 ;; '/run/current-system/locale' as was intended. Keep this hack around so
812 ;; that people who still have glibc@2.23-using packages in their profiles
813 ;; can use them correctly.
814 ;; TODO: Remove when glibc@2.23 is long gone.
815 ("GUIX_LOCPATH" . "/run/current-system/locale")))
816
817 (define %setuid-programs
818 ;; Default set of setuid-root programs.
819 (let ((shadow (@ (gnu packages admin) shadow)))
820 (list (file-append shadow "/bin/passwd")
821 (file-append shadow "/bin/su")
822 (file-append shadow "/bin/newuidmap")
823 (file-append shadow "/bin/newgidmap")
824 (file-append inetutils "/bin/ping")
825 (file-append inetutils "/bin/ping6")
826 (file-append sudo "/bin/sudo")
827 (file-append sudo "/bin/sudoedit")
828 (file-append fuse "/bin/fusermount"))))
829
830 (define %sudoers-specification
831 ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel'
832 ;; group can do anything. See
833 ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>.
834 ;; TODO: Add a declarative API.
835 (plain-file "sudoers" "\
836 root ALL=(ALL) ALL
837 %wheel ALL=(ALL) ALL\n"))
838
839 (define* (operating-system-activation-script os)
840 "Return the activation script for OS---i.e., the code that \"activates\" the
841 stateful part of OS, including user accounts and groups, special directories,
842 etc."
843 (let* ((services (operating-system-services os))
844 (activation (fold-services services
845 #:target-type activation-service-type)))
846 (activation-service->script activation)))
847
848 (define* (operating-system-boot-script os)
849 "Return the boot script for OS---i.e., the code started by the initrd once
850 we're running in the final root."
851 (let* ((services (operating-system-services os))
852 (boot (fold-services services #:target-type boot-service-type)))
853 (service-value boot)))
854
855 (define (operating-system-user-accounts os)
856 "Return the list of user accounts of OS."
857 (let* ((services (operating-system-services os))
858 (account (fold-services services
859 #:target-type account-service-type)))
860 (filter user-account?
861 (service-value account))))
862
863 (define (operating-system-shepherd-service-names os)
864 "Return the list of Shepherd service names for OS."
865 (append-map shepherd-service-provision
866 (service-value
867 (fold-services (operating-system-services os)
868 #:target-type
869 shepherd-root-service-type))))
870
871 (define* (operating-system-derivation os)
872 "Return a derivation that builds OS."
873 (let* ((services (operating-system-services os))
874 (system (fold-services services)))
875 ;; SYSTEM contains the derivation as a monadic value.
876 (service-value system)))
877
878 (define* (operating-system-profile os)
879 "Return a derivation that builds the system profile of OS."
880 (mlet* %store-monad
881 ((services -> (operating-system-services os))
882 (profile (fold-services services
883 #:target-type profile-service-type)))
884 (match profile
885 (("profile" profile)
886 (return profile)))))
887
888 (define (operating-system-root-file-system os)
889 "Return the root file system of OS."
890 (find (lambda (fs)
891 (string=? "/" (file-system-mount-point fs)))
892 (operating-system-file-systems os)))
893
894 (define (operating-system-initrd-file os)
895 "Return a gexp denoting the initrd file of OS."
896 (define boot-file-systems
897 (filter file-system-needed-for-boot?
898 (operating-system-file-systems os)))
899
900 (define mapped-devices
901 (operating-system-boot-mapped-devices os))
902
903 (define make-initrd
904 (operating-system-initrd os))
905
906 (make-initrd boot-file-systems
907 #:linux (operating-system-kernel os)
908 #:linux-modules
909 (operating-system-initrd-modules os)
910 #:mapped-devices mapped-devices
911 #:keyboard-layout (operating-system-keyboard-layout os)))
912
913 (define (locale-name->definition* name)
914 "Variant of 'locale-name->definition' that raises an error upon failure."
915 (match (locale-name->definition name)
916 (#f
917 (raise (condition
918 (&message
919 (message (format #f (G_ "~a: invalid locale name") name))))))
920 (def def)))
921
922 (define (operating-system-locale-directory os)
923 "Return the directory containing the locales compiled for the definitions
924 listed in OS. The C library expects to find it under
925 /run/current-system/locale."
926 (define name
927 (operating-system-locale os))
928
929 (define definitions
930 ;; While we're at it, check whether NAME is defined and add it if needed.
931 (if (member name (map locale-definition-name
932 (operating-system-locale-definitions os)))
933 (operating-system-locale-definitions os)
934 (cons (locale-name->definition* name)
935 (operating-system-locale-definitions os))))
936
937 (locale-directory definitions
938 #:libcs (operating-system-locale-libcs os)))
939
940 (define (kernel->boot-label kernel)
941 "Return a label for the bootloader menu entry that boots KERNEL."
942 (cond ((package? kernel)
943 (string-append "GNU with "
944 (string-titlecase (package-name kernel)) " "
945 (package-version kernel)))
946 ((inferior-package? kernel)
947 (string-append "GNU with "
948 (string-titlecase (inferior-package-name kernel)) " "
949 (inferior-package-version kernel)))
950 (else "GNU")))
951
952 (define (operating-system-default-label os)
953 "Return the default label for OS, as it will appear in the bootloader menu
954 entry."
955 (kernel->boot-label (operating-system-kernel os)))
956
957 (define (store-file-system file-systems)
958 "Return the file system object among FILE-SYSTEMS that contains the store."
959 (match (filter (lambda (fs)
960 (and (file-system-mount? fs)
961 (not (memq 'bind-mount (file-system-flags fs)))
962 (string-prefix? (file-system-mount-point fs)
963 (%store-prefix))))
964 file-systems)
965 ((and candidates (head . tail))
966 (reduce (lambda (fs1 fs2)
967 (if (> (string-length (file-system-mount-point fs1))
968 (string-length (file-system-mount-point fs2)))
969 fs1
970 fs2))
971 head
972 candidates))))
973
974 (define (operating-system-store-file-system os)
975 "Return the file system that contains the store of OS."
976 (store-file-system (operating-system-file-systems os)))
977
978 (define* (operating-system-bootcfg os #:optional (old-entries '()))
979 "Return the bootloader configuration file for OS. Use OLD-ENTRIES,
980 a list of <menu-entry>, to populate the \"old entries\" menu."
981 (let* ((root-fs (operating-system-root-file-system os))
982 (root-device (file-system-device root-fs))
983 (params (operating-system-boot-parameters
984 os root-device
985 #:system-kernel-arguments? #t))
986 (entry (boot-parameters->menu-entry params))
987 (bootloader-conf (operating-system-bootloader os)))
988 (define generate-config-file
989 (bootloader-configuration-file-generator
990 (bootloader-configuration-bootloader bootloader-conf)))
991
992 (generate-config-file bootloader-conf (list entry)
993 #:old-entries old-entries)))
994
995 (define* (operating-system-boot-parameters os root-device
996 #:key system-kernel-arguments?)
997 "Return a monadic <boot-parameters> record that describes the boot
998 parameters of OS. When SYSTEM-KERNEL-ARGUMENTS? is true, add kernel arguments
999 such as '--root' and '--load' to <boot-parameters>."
1000 (let* ((initrd (operating-system-initrd-file os))
1001 (store (operating-system-store-file-system os))
1002 (bootloader (bootloader-configuration-bootloader
1003 (operating-system-bootloader os)))
1004 (bootloader-name (bootloader-name bootloader))
1005 (label (operating-system-label os)))
1006 (boot-parameters
1007 (label label)
1008 (root-device root-device)
1009 (kernel (operating-system-kernel-file os))
1010 (kernel-arguments
1011 (if system-kernel-arguments?
1012 (operating-system-kernel-arguments os root-device)
1013 (operating-system-user-kernel-arguments os)))
1014 (initrd initrd)
1015 (bootloader-name bootloader-name)
1016 (bootloader-menu-entries
1017 (bootloader-configuration-menu-entries (operating-system-bootloader os)))
1018 (store-device (ensure-not-/dev (file-system-device store)))
1019 (store-mount-point (file-system-mount-point store)))))
1020
1021 (define (device->sexp device)
1022 "Serialize DEVICE as an sexp (really, as an object with a read syntax.)"
1023 (match device
1024 ((? uuid? uuid)
1025 `(uuid ,(uuid-type uuid) ,(uuid-bytevector uuid)))
1026 ((? file-system-label? label)
1027 `(file-system-label ,(file-system-label->string label)))
1028 (_
1029 device)))
1030
1031 (define* (operating-system-boot-parameters-file os
1032 #:key system-kernel-arguments?)
1033 "Return a file that describes the boot parameters of OS. The primary use of
1034 this file is the reconstruction of GRUB menu entries for old configurations.
1035
1036 When SYSTEM-KERNEL-ARGUMENTS? is true, add kernel arguments such as '--root'
1037 and '--load' to the returned file (since the returned file is then usually
1038 stored into the content-addressed \"system\" directory, it's usually not a
1039 good idea to give it because the content hash would change by the content hash
1040 being stored into the \"parameters\" file)."
1041 (let* ((root (operating-system-root-file-system os))
1042 (device (file-system-device root))
1043 (params (operating-system-boot-parameters
1044 os device
1045 #:system-kernel-arguments?
1046 system-kernel-arguments?)))
1047 (gexp->file "parameters"
1048 #~(boot-parameters
1049 (version 0)
1050 (label #$(boot-parameters-label params))
1051 (root-device
1052 #$(device->sexp
1053 (boot-parameters-root-device params)))
1054 (kernel #$(boot-parameters-kernel params))
1055 (kernel-arguments
1056 #$(boot-parameters-kernel-arguments params))
1057 (initrd #$(boot-parameters-initrd params))
1058 (bootloader-name #$(boot-parameters-bootloader-name params))
1059 (bootloader-menu-entries
1060 #$(map menu-entry->sexp
1061 (or (and=> (operating-system-bootloader os)
1062 bootloader-configuration-menu-entries)
1063 '())))
1064 (store
1065 (device
1066 #$(device->sexp (boot-parameters-store-device params)))
1067 (mount-point #$(boot-parameters-store-mount-point params))))
1068 #:set-load-path? #f)))
1069
1070 (define-gexp-compiler (operating-system-compiler (os <operating-system>)
1071 system target)
1072 ((store-lift
1073 (lambda (store)
1074 ;; XXX: This is not super elegant but we can't pass SYSTEM and TARGET to
1075 ;; 'operating-system-derivation'.
1076 (run-with-store store (operating-system-derivation os)
1077 #:system system
1078 #:target target)))))
1079
1080 ;;; system.scm ends here