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