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