system: Make service procedures non-monadic.
[jackhill/guix/guix.git] / gnu / system.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
4 ;;; Copyright © 2015 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 compression)
44 #:use-module (gnu packages firmware)
45 #:autoload (gnu packages cryptsetup) (cryptsetup)
46 #:use-module (gnu services)
47 #:use-module (gnu services dmd)
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 linux)
54 #:use-module (gnu system linux-initrd)
55 #:use-module (gnu system file-systems)
56 #:use-module (ice-9 match)
57 #:use-module (srfi srfi-1)
58 #:use-module (srfi srfi-26)
59 #:use-module (srfi srfi-34)
60 #:use-module (srfi srfi-35)
61 #:export (operating-system
62 operating-system?
63
64 operating-system-bootloader
65 operating-system-services
66 operating-system-user-services
67 operating-system-packages
68 operating-system-host-name
69 operating-system-hosts-file
70 operating-system-kernel
71 operating-system-kernel-arguments
72 operating-system-initrd
73 operating-system-users
74 operating-system-groups
75 operating-system-issue
76 operating-system-timezone
77 operating-system-locale
78 operating-system-locale-definitions
79 operating-system-mapped-devices
80 operating-system-file-systems
81 operating-system-activation-script
82
83 operating-system-derivation
84 operating-system-profile
85 operating-system-grub.cfg
86 operating-system-etc-directory
87 operating-system-locale-directory
88 operating-system-boot-script
89
90 file-union
91
92 local-host-aliases
93 %setuid-programs
94 %base-packages
95 %base-firmware
96
97 luks-device-mapping))
98
99 ;;; Commentary:
100 ;;;
101 ;;; This module supports whole-system configuration.
102 ;;;
103 ;;; Code:
104
105 ;; System-wide configuration.
106 ;; TODO: Add per-field docstrings/stexi.
107 (define-record-type* <operating-system> operating-system
108 make-operating-system
109 operating-system?
110 (kernel operating-system-kernel ; package
111 (default linux-libre))
112 (kernel-arguments operating-system-kernel-arguments
113 (default '())) ; list of gexps/strings
114 (bootloader operating-system-bootloader) ; <grub-configuration>
115
116 (initrd operating-system-initrd ; (list fs) -> M derivation
117 (default base-initrd))
118 (firmware operating-system-firmware ; list of packages
119 (default %base-firmware))
120
121 (host-name operating-system-host-name) ; string
122 (hosts-file operating-system-hosts-file ; file-like | #f
123 (default #f))
124
125 (mapped-devices operating-system-mapped-devices ; list of <mapped-device>
126 (default '()))
127 (file-systems operating-system-file-systems) ; list of fs
128 (swap-devices operating-system-swap-devices ; list of strings
129 (default '()))
130
131 (users operating-system-users ; list of user accounts
132 (default %base-user-accounts))
133 (groups operating-system-groups ; list of user groups
134 (default %base-groups))
135
136 (skeletons operating-system-skeletons ; list of name/monadic value
137 (default (default-skeletons)))
138 (issue operating-system-issue ; string
139 (default %default-issue))
140
141 (packages operating-system-packages ; list of (PACKAGE OUTPUT...)
142 (default %base-packages)) ; or just PACKAGE
143
144 (timezone operating-system-timezone) ; string
145 (locale operating-system-locale ; string
146 (default "en_US.utf8"))
147 (locale-definitions operating-system-locale-definitions ; list of <locale-definition>
148 (default %default-locale-definitions))
149 (name-service-switch operating-system-name-service-switch ; <name-service-switch>
150 (default %default-nss))
151
152 (services operating-system-user-services ; list of monadic services
153 (default %base-services))
154
155 (pam-services operating-system-pam-services ; list of PAM services
156 (default (base-pam-services)))
157 (setuid-programs operating-system-setuid-programs
158 (default %setuid-programs)) ; list of string-valued gexps
159
160 (sudoers-file operating-system-sudoers-file ; file-like
161 (default %sudoers-specification)))
162
163 \f
164 ;;;
165 ;;; Derivation.
166 ;;;
167
168 (define* (file-union name files)
169 "Return a derivation that builds a directory containing all of FILES. Each
170 item in FILES must be a list where the first element is the file name to use
171 in the new directory, and the second element is a gexp denoting the target
172 file."
173 (define builder
174 #~(begin
175 (mkdir #$output)
176 (chdir #$output)
177 #$@(map (match-lambda
178 ((target source)
179 #~(symlink #$source #$target)))
180 files)))
181
182 (gexp->derivation name builder))
183
184 (define (directory-union name things)
185 "Return a directory that is the union of THINGS."
186 (match things
187 ((one)
188 ;; Only one thing; return it.
189 (with-monad %store-monad (return one)))
190 (_
191 (gexp->derivation name
192 #~(begin
193 (use-modules (guix build union))
194 (union-build #$output '#$things))
195 #:modules '((guix build union))
196 #:local-build? #t))))
197
198 \f
199 ;;;
200 ;;; Services.
201 ;;;
202
203 (define (open-luks-device source target)
204 "Return a gexp that maps SOURCE to TARGET as a LUKS device, using
205 'cryptsetup'."
206 #~(zero? (system* (string-append #$cryptsetup "/sbin/cryptsetup")
207 "open" "--type" "luks"
208 #$source #$target)))
209
210 (define (close-luks-device source target)
211 "Return a gexp that closes TARGET, a LUKS device."
212 #~(zero? (system* (string-append #$cryptsetup "/sbin/cryptsetup")
213 "close" #$target)))
214
215 (define luks-device-mapping
216 ;; The type of LUKS mapped devices.
217 (mapped-device-kind
218 (open open-luks-device)
219 (close close-luks-device)))
220
221 (define (other-file-system-services os)
222 "Return file system services for the file systems of OS that are not marked
223 as 'needed-for-boot'."
224 (define file-systems
225 (remove file-system-needed-for-boot?
226 (operating-system-file-systems os)))
227
228 (define (device-mappings fs)
229 (filter (lambda (md)
230 (string=? (string-append "/dev/mapper/"
231 (mapped-device-target md))
232 (file-system-device fs)))
233 (operating-system-mapped-devices os)))
234
235 (define (requirements fs)
236 ;; XXX: Fiddling with dmd service names is not nice.
237 (append (map (lambda (fs)
238 (symbol-append 'file-system-
239 (string->symbol
240 (file-system-mount-point fs))))
241 (file-system-dependencies fs))
242 (map (lambda (md)
243 (symbol-append 'device-mapping-
244 (string->symbol (mapped-device-target md))))
245 (device-mappings fs))))
246
247 (map (lambda (fs)
248 (match fs
249 (($ <file-system> device title target type flags opts
250 #f check? create?)
251 (file-system-service device target type
252 #:title title
253 #:requirements (requirements fs)
254 #:check? check?
255 #:create-mount-point? create?
256 #:options opts
257 #:flags flags))))
258 file-systems))
259
260 (define (mapped-device-user device file-systems)
261 "Return a file system among FILE-SYSTEMS that uses DEVICE, or #f."
262 (let ((target (string-append "/dev/mapper/" (mapped-device-target device))))
263 (find (lambda (fs)
264 (string=? (file-system-device fs) target))
265 file-systems)))
266
267 (define (operating-system-user-mapped-devices os)
268 "Return the subset of mapped devices that can be installed in
269 user-land--i.e., those not needed during boot."
270 (let ((devices (operating-system-mapped-devices os))
271 (file-systems (operating-system-file-systems os)))
272 (filter (lambda (md)
273 (let ((user (mapped-device-user md file-systems)))
274 (or (not user)
275 (not (file-system-needed-for-boot? user)))))
276 devices)))
277
278 (define (operating-system-boot-mapped-devices os)
279 "Return the subset of mapped devices that must be installed during boot,
280 from the initrd."
281 (let ((devices (operating-system-mapped-devices os))
282 (file-systems (operating-system-file-systems os)))
283 (filter (lambda (md)
284 (let ((user (mapped-device-user md file-systems)))
285 (and user (file-system-needed-for-boot? user))))
286 devices)))
287
288 (define (device-mapping-services os)
289 "Return the list of device-mapping services for OS as a list."
290 (map (lambda (md)
291 (let* ((source (mapped-device-source md))
292 (target (mapped-device-target md))
293 (type (mapped-device-type md))
294 (open (mapped-device-kind-open type))
295 (close (mapped-device-kind-close type)))
296 (device-mapping-service target
297 (open source target)
298 (close source target))))
299 (operating-system-user-mapped-devices os)))
300
301 (define (swap-services os)
302 "Return the list of swap services for OS."
303 (map swap-service (operating-system-swap-devices os)))
304
305 (define (essential-services os)
306 "Return the list of essential services for OS. These are special services
307 that implement part of what's declared in OS are responsible for low-level
308 bookkeeping."
309 (define known-fs
310 (map file-system-mount-point (operating-system-file-systems os)))
311
312 (let* ((mappings (device-mapping-services os))
313 (root-fs (root-file-system-service))
314 (other-fs (other-file-system-services os))
315 (unmount (user-unmount-service known-fs))
316 (swaps (swap-services os))
317 (procs (user-processes-service
318 (map (compose first service-provision)
319 other-fs)))
320 (host-name (host-name-service (operating-system-host-name os))))
321 (cons* host-name procs root-fs unmount
322 (append other-fs mappings swaps))))
323
324 (define (operating-system-services os)
325 "Return all the services of OS, including \"internal\" services that do not
326 explicitly appear in OS."
327 (append (operating-system-user-services os)
328 (essential-services os)))
329
330 \f
331 ;;;
332 ;;; /etc.
333 ;;;
334
335 (define %base-firmware
336 ;; Firmware usable by default.
337 (list ath9k-htc-firmware))
338
339 (define %base-packages
340 ;; Default set of packages globally visible. It should include anything
341 ;; required for basic administrator tasks.
342 (cons* procps psmisc which less zile nano
343 (@ (gnu packages admin) dmd) guix
344 lsof ;for Guix's 'list-runtime-roots'
345 pciutils usbutils
346 util-linux inetutils isc-dhcp
347
348 ;; wireless-tools is deprecated in favor of iw, but it's still what
349 ;; many people are familiar with, so keep it around.
350 iw wireless-tools
351
352 net-tools ; XXX: remove when Inetutils suffices
353 man-db
354
355 ;; The 'sudo' command is already in %SETUID-PROGRAMS, but we also
356 ;; want the other commands and the man pages (notably because
357 ;; auto-completion in Emacs shell relies on man pages.)
358 sudo
359
360 ;; Get 'insmod' & co. from kmod, not module-init-tools, since udev
361 ;; already depends on it anyway.
362 kmod eudev
363
364 e2fsprogs kbd
365
366 bash-completion
367
368 ;; The packages below are also in %FINAL-INPUTS, so take them from
369 ;; there to avoid duplication.
370 (map canonical-package
371 (list guile-2.0 bash coreutils findutils grep sed
372 diffutils patch gawk tar gzip bzip2 xz lzip))))
373
374 (define %default-issue
375 ;; Default contents for /etc/issue.
376 "
377 This is the GNU system. Welcome.\n")
378
379 (define (local-host-aliases host-name)
380 "Return aliases for HOST-NAME, to be used in /etc/hosts."
381 (string-append "127.0.0.1 localhost " host-name "\n"
382 "::1 localhost " host-name "\n"))
383
384 (define (default-/etc/hosts host-name)
385 "Return the default /etc/hosts file."
386 (plain-file "hosts" (local-host-aliases host-name)))
387
388 (define (emacs-site-file)
389 "Return the Emacs 'site-start.el' file. That file contains the necessary
390 settings for 'guix.el' to work out-of-the-box."
391 (gexp->file "site-start.el"
392 #~(progn
393 ;; Add the "normal" elisp directory to the search path;
394 ;; guix.el may be there.
395 (add-to-list
396 'load-path
397 "/run/current-system/profile/share/emacs/site-lisp")
398
399 ;; Attempt to load guix.el.
400 (require 'guix-init nil t)
401
402 ;; Attempt to load geiser.
403 (require 'geiser-install nil t))))
404
405 (define (emacs-site-directory)
406 "Return the Emacs site directory, aka. /etc/emacs."
407 (mlet %store-monad ((file (emacs-site-file)))
408 (gexp->derivation "emacs"
409 #~(begin
410 (mkdir #$output)
411 (chdir #$output)
412 (symlink #$file "site-start.el")))))
413
414 (define (user-shells os)
415 "Return the list of all the shells used by the accounts of OS. These may be
416 gexps or strings."
417 (map user-account-shell (operating-system-accounts os)))
418
419 (define (shells-file shells)
420 "Return a derivation that builds a shell list for use as /etc/shells based
421 on SHELLS. /etc/shells is used by xterm, polkit, and other programs."
422 (gexp->derivation "shells"
423 #~(begin
424 (use-modules (srfi srfi-1))
425
426 (define shells
427 (delete-duplicates (list #$@shells)))
428
429 (call-with-output-file #$output
430 (lambda (port)
431 (display "\
432 /bin/sh
433 /run/current-system/profile/bin/sh
434 /run/current-system/profile/bin/bash\n" port)
435 (for-each (lambda (shell)
436 (display shell port)
437 (newline port))
438 shells))))))
439
440 (define* (etc-directory #:key
441 (locale "C") (timezone "Europe/Paris")
442 (issue "Hello!\n")
443 (skeletons '())
444 (pam-services '())
445 (profile "/run/current-system/profile")
446 hosts-file nss (shells '())
447 (sudoers-file (plain-file "sudoers" "")))
448 "Return a derivation that builds the static part of the /etc directory."
449 (mlet* %store-monad
450 ((pam.d (pam-services->directory pam-services))
451 (login.defs (text-file "login.defs" "# Empty for now.\n"))
452 (shells (shells-file shells))
453 (emacs (emacs-site-directory))
454 (issue (text-file "issue" issue))
455 (nsswitch (text-file "nsswitch.conf"
456 (name-service-switch->string nss)))
457
458 ;; Startup file for POSIX-compliant login shells, which set system-wide
459 ;; environment variables.
460 (profile (text-file* "profile" "\
461 export LANG=\"" locale "\"
462 export TZ=\"" timezone "\"
463 export TZDIR=\"" tzdata "/share/zoneinfo\"
464
465 # Tell 'modprobe' & co. where to look for modules.
466 export LINUX_MODULE_DIRECTORY=/run/booted-system/kernel/lib/modules
467
468 # These variables are honored by OpenSSL (libssl) and Git.
469 export SSL_CERT_DIR=/etc/ssl/certs
470 export SSL_CERT_FILE=\"$SSL_CERT_DIR/ca-certificates.crt\"
471 export GIT_SSL_CAINFO=\"$SSL_CERT_FILE\"
472
473 # Crucial variables that could be missing in the profiles' 'etc/profile'
474 # because they would require combining both profiles.
475 # FIXME: See <http://bugs.gnu.org/20255>.
476 export MANPATH=$HOME/.guix-profile/share/man:/run/current-system/profile/share/man
477 export INFOPATH=$HOME/.guix-profile/share/info:/run/current-system/profile/share/info
478 export XDG_DATA_DIRS=$HOME/.guix-profile/share:/run/current-system/profile/share
479 export XDG_CONFIG_DIRS=$HOME/.guix-profile/etc/xdg:/run/current-system/profile/etc/xdg
480
481 # Ignore the default value of 'PATH'.
482 unset PATH
483
484 # Load the system profile's settings.
485 GUIX_PROFILE=/run/current-system/profile \\
486 . /run/current-system/profile/etc/profile
487
488 # Prepend setuid programs.
489 export PATH=/run/setuid-programs:$PATH
490
491 if [ -f \"$HOME/.guix-profile/etc/profile\" ]
492 then
493 # Load the user profile's settings.
494 GUIX_PROFILE=\"$HOME/.guix-profile\" \\
495 . \"$HOME/.guix-profile/etc/profile\"
496 else
497 # At least define this one so that basic things just work
498 # when the user installs their first package.
499 export PATH=\"$HOME/.guix-profile/bin:$PATH\"
500 fi
501
502 # Append the directory of 'site-start.el' to the search path.
503 export EMACSLOADPATH=:/etc/emacs
504
505 # By default, applications that use D-Bus, such as Emacs, abort at startup
506 # when /etc/machine-id is missing. Make sure these warnings are non-fatal.
507 export DBUS_FATAL_WARNINGS=0
508
509 # Allow Aspell to find dictionaries installed in the user profile.
510 export ASPELL_CONF=\"dict-dir $HOME/.guix-profile/lib/aspell\"
511
512 if [ -n \"$BASH_VERSION\" -a -f /etc/bashrc ]
513 then
514 # Load Bash-specific initialization code.
515 . /etc/bashrc
516 fi
517 "))
518
519 (bashrc (text-file "bashrc" "\
520 # Bash-specific initialization.
521
522 # The 'bash-completion' package.
523 if [ -f /run/current-system/profile/etc/profile.d/bash_completion.sh ]
524 then
525 # Bash-completion sources ~/.bash_completion. It installs a dynamic
526 # completion loader that searches its own completion files as well
527 # as those in ~/.guix-profile and /run/current-system/profile.
528 source /run/current-system/profile/etc/profile.d/bash_completion.sh
529 fi\n"))
530 (skel (skeleton-directory skeletons)))
531 (file-union "etc"
532 `(("services" ,#~(string-append #$net-base "/etc/services"))
533 ("protocols" ,#~(string-append #$net-base "/etc/protocols"))
534 ("rpc" ,#~(string-append #$net-base "/etc/rpc"))
535 ("emacs" ,#~#$emacs)
536 ("pam.d" ,#~#$pam.d)
537 ("login.defs" ,#~#$login.defs)
538 ("issue" ,#~#$issue)
539 ("nsswitch.conf" ,#~#$nsswitch)
540 ("skel" ,#~#$skel)
541 ("shells" ,#~#$shells)
542 ("profile" ,#~#$profile)
543 ("bashrc" ,#~#$bashrc)
544 ("hosts" ,#~#$hosts-file)
545 ("localtime" ,#~(string-append #$tzdata "/share/zoneinfo/"
546 #$timezone))
547 ("sudoers" ,sudoers-file)))))
548
549 (define (operating-system-profile os)
550 "Return a derivation that builds the system profile of OS."
551 (profile-derivation (manifest (map package->manifest-entry
552 (operating-system-packages os)))))
553
554 (define %root-account
555 ;; Default root account.
556 (user-account
557 (name "root")
558 (password "")
559 (uid 0) (group "root")
560 (comment "System administrator")
561 (home-directory "/root")))
562
563 (define (operating-system-accounts os)
564 "Return the user accounts for OS, including an obligatory 'root' account."
565 (define users
566 ;; Make sure there's a root account.
567 (if (find (lambda (user)
568 (and=> (user-account-uid user) zero?))
569 (operating-system-users os))
570 (operating-system-users os)
571 (cons %root-account (operating-system-users os))))
572
573 (append users
574 (append-map service-user-accounts
575 (operating-system-services os))))
576
577 (define (maybe-string->file file-name thing)
578 "If THING is a string, return a <plain-file> with THING as its content.
579 Otherwise just return THING.
580
581 This is for backward-compatibility of fields that used to be strings and are
582 now file-like objects.."
583 (match thing
584 ((? string?)
585 (warning (_ "using a string for file '~a' is deprecated; \
586 use 'plain-file' instead~%")
587 file-name)
588 (plain-file file-name thing))
589 (x
590 x)))
591
592 (define (maybe-file->monadic file-name thing)
593 "If THING is a value in %STORE-MONAD, return it as is; otherwise return
594 THING in the %STORE-MONAD.
595
596 This is for backward-compatibility of fields that used to be monadic values
597 and are now file-like objects."
598 (with-monad %store-monad
599 (match thing
600 ((? procedure?)
601 (warning (_ "using a monadic value for '~a' is deprecated; \
602 use 'plain-file' instead~%")
603 file-name)
604 thing)
605 (x
606 (return x)))))
607
608 (define (operating-system-etc-directory os)
609 "Return that static part of the /etc directory of OS."
610 (mlet* %store-monad
611 ((services -> (operating-system-services os))
612 (pam-services ->
613 ;; Services known to PAM.
614 (append (operating-system-pam-services os)
615 (append-map service-pam-services services)))
616 (profile-drv (operating-system-profile os))
617 (skeletons (operating-system-skeletons os))
618 (/etc/hosts (maybe-file->monadic
619 "hosts"
620 (or (operating-system-hosts-file os)
621 (default-/etc/hosts (operating-system-host-name os)))))
622 (shells -> (user-shells os)))
623 (etc-directory #:pam-services pam-services
624 #:skeletons skeletons
625 #:issue (operating-system-issue os)
626 #:locale (operating-system-locale os)
627 #:nss (operating-system-name-service-switch os)
628 #:timezone (operating-system-timezone os)
629 #:hosts-file /etc/hosts
630 #:shells shells
631 #:sudoers-file (maybe-string->file
632 "sudoers"
633 (operating-system-sudoers-file os))
634 #:profile profile-drv)))
635
636 (define %setuid-programs
637 ;; Default set of setuid-root programs.
638 (let ((shadow (@ (gnu packages admin) shadow)))
639 (list #~(string-append #$shadow "/bin/passwd")
640 #~(string-append #$shadow "/bin/su")
641 #~(string-append #$inetutils "/bin/ping")
642 #~(string-append #$inetutils "/bin/ping6")
643 #~(string-append #$sudo "/bin/sudo")
644 #~(string-append #$fuse "/bin/fusermount"))))
645
646 (define %sudoers-specification
647 ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel'
648 ;; group can do anything. See
649 ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>.
650 ;; TODO: Add a declarative API.
651 (plain-file "sudoers" "\
652 root ALL=(ALL) ALL
653 %wheel ALL=(ALL) ALL\n"))
654
655 (define (user-group->gexp group)
656 "Turn GROUP, a <user-group> object, into a list-valued gexp suitable for
657 'active-groups'."
658 #~(list #$(user-group-name group)
659 #$(user-group-password group)
660 #$(user-group-id group)
661 #$(user-group-system? group)))
662
663 (define (user-account->gexp account)
664 "Turn ACCOUNT, a <user-account> object, into a list-valued gexp suitable for
665 'activate-users'."
666 #~`(#$(user-account-name account)
667 #$(user-account-uid account)
668 #$(user-account-group account)
669 #$(user-account-supplementary-groups account)
670 #$(user-account-comment account)
671 #$(user-account-home-directory account)
672 ,#$(user-account-shell account) ; this one is a gexp
673 #$(user-account-password account)
674 #$(user-account-system? account)))
675
676 (define (modprobe-wrapper)
677 "Return a wrapper for the 'modprobe' command that knows where modules live.
678
679 This wrapper is typically invoked by the Linux kernel ('call_modprobe', in
680 kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY' environment
681 variable is not set---hence the need for this wrapper."
682 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
683 (gexp->script "modprobe"
684 #~(begin
685 (setenv "LINUX_MODULE_DIRECTORY"
686 "/run/booted-system/kernel/lib/modules")
687 (apply execl #$modprobe
688 (cons #$modprobe (cdr (command-line))))))))
689
690 (define* (operating-system-activation-script os #:key container?)
691 "Return the activation script for OS---i.e., the code that \"activates\" the
692 stateful part of OS, including user accounts and groups, special directories,
693 etc."
694 (define %modules
695 '((gnu build activation)
696 (gnu build linux-boot)
697 (gnu build linux-modules)
698 (gnu build file-systems)
699 (guix build utils)
700 (guix build syscalls)
701 (guix elf)))
702
703 (define (service-activations services)
704 ;; Return the activation scripts for SERVICES.
705 (let ((gexps (filter-map service-activate services)))
706 (sequence %store-monad (map (cut gexp->file "activate-service.scm" <>)
707 gexps))))
708
709 (mlet* %store-monad ((services -> (operating-system-services os))
710 (actions (service-activations services))
711 (etc (operating-system-etc-directory os))
712 (modules (imported-modules %modules))
713 (compiled (compiled-modules %modules))
714 (modprobe (modprobe-wrapper))
715 (firmware (directory-union
716 "firmware" (operating-system-firmware os)))
717 (accounts -> (operating-system-accounts os)))
718 (define setuid-progs
719 (operating-system-setuid-programs os))
720
721 (define user-specs
722 (map user-account->gexp accounts))
723
724 (define groups
725 (append (operating-system-groups os)
726 (append-map service-user-groups services)))
727
728 (define group-specs
729 (map user-group->gexp groups))
730
731 (assert-valid-users/groups accounts groups)
732
733 (gexp->file "activate"
734 #~(begin
735 (eval-when (expand load eval)
736 ;; Make sure 'use-modules' below succeeds.
737 (set! %load-path (cons #$modules %load-path))
738 (set! %load-compiled-path
739 (cons #$compiled %load-compiled-path)))
740
741 (use-modules (gnu build activation))
742
743 ;; Make sure /bin/sh is valid and current.
744 (activate-/bin/sh
745 (string-append #$(canonical-package bash)
746 "/bin/sh"))
747
748 ;; Populate /etc.
749 (activate-etc #$etc)
750
751 ;; Add users and user groups.
752 (setenv "PATH"
753 (string-append #$(@ (gnu packages admin) shadow)
754 "/sbin"))
755 (activate-users+groups (list #$@user-specs)
756 (list #$@group-specs))
757
758 ;; Activate setuid programs.
759 (activate-setuid-programs (list #$@setuid-progs))
760
761 ;; Tell the kernel to use our 'modprobe' command.
762 (activate-modprobe #$modprobe)
763
764 ;; Tell the kernel where firmware is, unless we are
765 ;; activating a container.
766 #$@(if container?
767 #~()
768 ;; Tell the kernel where firmware is.
769 #~((activate-firmware
770 (string-append #$firmware "/lib/firmware"))
771 ;; Let users debug their own processes!
772 (activate-ptrace-attach)))
773
774 ;; Run the services' activation snippets.
775 ;; TODO: Use 'load-compiled'.
776 (for-each primitive-load '#$actions)
777
778 ;; Set up /run/current-system.
779 (activate-current-system)))))
780
781 (define* (operating-system-boot-script os #:key container?)
782 "Return the boot script for OS---i.e., the code started by the initrd once
783 we're running in the final root. When CONTAINER? is true, skip all
784 hardware-related operations as necessary when booting a Linux container."
785 (mlet* %store-monad ((services -> (operating-system-services os))
786 (activate (operating-system-activation-script os))
787 (dmd-conf (dmd-configuration-file services)))
788 (gexp->file "boot"
789 #~(begin
790 (use-modules (guix build utils))
791
792 ;; Clean out /tmp and /var/run.
793 ;;
794 ;; XXX This needs to happen before service activations, so
795 ;; it has to be here, but this also implicitly assumes
796 ;; that /tmp and /var/run are on the root partition.
797 (false-if-exception (delete-file-recursively "/tmp"))
798 (false-if-exception (delete-file-recursively "/var/run"))
799 (false-if-exception (mkdir "/tmp"))
800 (false-if-exception (chmod "/tmp" #o1777))
801 (false-if-exception (mkdir "/var/run"))
802 (false-if-exception (chmod "/var/run" #o755))
803
804 ;; Activate the system.
805 ;; TODO: Use 'load-compiled'.
806 (primitive-load #$activate)
807
808 ;; Keep track of the booted system.
809 (false-if-exception (delete-file "/run/booted-system"))
810 (symlink (readlink "/run/current-system")
811 "/run/booted-system")
812
813 ;; Close any remaining open file descriptors to be on the
814 ;; safe side. This must be the very last thing we do,
815 ;; because Guile has internal FDs such as 'sleep_pipe'
816 ;; that need to be alive.
817 (let loop ((fd 3))
818 (when (< fd 1024)
819 (false-if-exception (close-fdes fd))
820 (loop (+ 1 fd))))
821
822 ;; Start dmd.
823 (execl (string-append #$dmd "/bin/dmd")
824 "dmd" "--config" #$dmd-conf)))))
825
826 (define (operating-system-root-file-system os)
827 "Return the root file system of OS."
828 (find (match-lambda
829 (($ <file-system> _ _ "/") #t)
830 (_ #f))
831 (operating-system-file-systems os)))
832
833 (define (operating-system-initrd-file os)
834 "Return a gexp denoting the initrd file of OS."
835 (define boot-file-systems
836 (filter file-system-needed-for-boot?
837 (operating-system-file-systems os)))
838
839 (define mapped-devices
840 (operating-system-boot-mapped-devices os))
841
842 (define make-initrd
843 (operating-system-initrd os))
844
845 (mlet %store-monad ((initrd (make-initrd boot-file-systems
846 #:linux (operating-system-kernel os)
847 #:mapped-devices mapped-devices)))
848 (return #~(string-append #$initrd "/initrd"))))
849
850 (define (operating-system-locale-directory os)
851 "Return the directory containing the locales compiled for the definitions
852 listed in OS. The C library expects to find it under
853 /run/current-system/locale."
854 ;; While we're at it, check whether the locale of OS is defined.
855 (unless (member (operating-system-locale os)
856 (map locale-definition-name
857 (operating-system-locale-definitions os)))
858 (raise (condition
859 (&message (message "system locale lacks a definition")))))
860
861 (locale-directory (operating-system-locale-definitions os)))
862
863 (define (kernel->grub-label kernel)
864 "Return a label for the GRUB menu entry that boots KERNEL."
865 (string-append "GNU with "
866 (string-titlecase (package-name kernel)) " "
867 (package-version kernel)
868 " (alpha)"))
869
870 (define* (operating-system-grub.cfg os #:optional (old-entries '()))
871 "Return the GRUB configuration file for OS. Use OLD-ENTRIES to populate the
872 \"old entries\" menu."
873 (mlet* %store-monad
874 ((system (operating-system-derivation os))
875 (root-fs -> (operating-system-root-file-system os))
876 (kernel -> (operating-system-kernel os))
877 (entries -> (list (menu-entry
878 (label (kernel->grub-label kernel))
879 (linux kernel)
880 (linux-arguments
881 (cons* (string-append "--root="
882 (file-system-device root-fs))
883 #~(string-append "--system=" #$system)
884 #~(string-append "--load=" #$system
885 "/boot")
886 (operating-system-kernel-arguments os)))
887 (initrd #~(string-append #$system "/initrd"))))))
888 (grub-configuration-file (operating-system-bootloader os) entries
889 #:old-entries old-entries)))
890
891 (define (operating-system-parameters-file os)
892 "Return a file that describes the boot parameters of OS. The primary use of
893 this file is the reconstruction of GRUB menu entries for old configurations."
894 (mlet %store-monad ((initrd (operating-system-initrd-file os))
895 (root -> (operating-system-root-file-system os))
896 (label -> (kernel->grub-label
897 (operating-system-kernel os))))
898 (gexp->file "parameters"
899 #~(boot-parameters (version 0)
900 (label #$label)
901 (root-device #$(file-system-device root))
902 (kernel #$(operating-system-kernel os))
903 (kernel-arguments
904 #$(operating-system-kernel-arguments os))
905 (initrd #$initrd)))))
906
907 (define (operating-system-derivation os)
908 "Return a derivation that builds OS."
909 (mlet* %store-monad
910 ((profile (operating-system-profile os))
911 (etc (operating-system-etc-directory os))
912 (boot (operating-system-boot-script os))
913 (kernel -> (operating-system-kernel os))
914 (initrd (operating-system-initrd-file os))
915 (locale (operating-system-locale-directory os))
916 (params (operating-system-parameters-file os)))
917 (file-union "system"
918 `(("boot" ,#~#$boot)
919 ("kernel" ,#~#$kernel)
920 ("parameters" ,#~#$params)
921 ("initrd" ,initrd)
922 ("profile" ,#~#$profile)
923 ("locale" ,#~#$locale) ;used by libc
924 ("etc" ,#~#$etc)))))
925
926 ;;; system.scm ends here