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