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