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