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