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