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