gnu: guix-devel: Propagate Geiser.
[jackhill/guix/guix.git] / gnu / system.scm
CommitLineData
033adfe7 1;;; GNU Guix --- Functional package management for GNU
be681773 2;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
033adfe7
LC
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)
02100028 22 #:use-module (guix gexp)
033adfe7
LC
23 #:use-module (guix records)
24 #:use-module (guix packages)
25 #:use-module (guix derivations)
29fce8b6 26 #:use-module (guix profiles)
033adfe7
LC
27 #:use-module (gnu packages base)
28 #:use-module (gnu packages bash)
bdb36958 29 #:use-module (gnu packages guile)
89a0d00a 30 #:use-module (gnu packages which)
9de46ffb 31 #:use-module (gnu packages admin)
8a07c289 32 #:use-module (gnu packages linux)
a96a82d7 33 #:use-module (gnu packages pciutils)
033adfe7 34 #:use-module (gnu packages package-management)
6f436c54
LC
35 #:use-module (gnu packages which)
36 #:use-module (gnu packages less)
37 #:use-module (gnu packages zile)
55e70e65
LC
38 #:use-module (gnu packages nano)
39 #:use-module (gnu packages lsof)
a68c6967 40 #:use-module (gnu packages gawk)
18316d86 41 #:use-module (gnu packages man)
a68c6967 42 #:use-module (gnu packages compression)
f34c56be 43 #:use-module (gnu packages firmware)
a68c6967 44 #:autoload (gnu packages cryptsetup) (cryptsetup)
db4fdc04
LC
45 #:use-module (gnu services)
46 #:use-module (gnu services dmd)
47 #:use-module (gnu services base)
033adfe7
LC
48 #:use-module (gnu system grub)
49 #:use-module (gnu system shadow)
598e19dc 50 #:use-module (gnu system locale)
033adfe7 51 #:use-module (gnu system linux)
735c6dd7 52 #:use-module (gnu system linux-initrd)
c5df1839 53 #:use-module (gnu system file-systems)
033adfe7
LC
54 #:use-module (ice-9 match)
55 #:use-module (srfi srfi-1)
56 #:use-module (srfi srfi-26)
598e19dc
LC
57 #:use-module (srfi srfi-34)
58 #:use-module (srfi srfi-35)
033adfe7
LC
59 #:export (operating-system
60 operating-system?
d5b429ab
LC
61
62 operating-system-bootloader
033adfe7 63 operating-system-services
217a5b85 64 operating-system-user-services
033adfe7 65 operating-system-packages
fd3bfc44 66 operating-system-host-name
c65e1834 67 operating-system-hosts-file
fd3bfc44
LC
68 operating-system-kernel
69 operating-system-initrd
70 operating-system-users
71 operating-system-groups
548d4c13 72 operating-system-issue
fd3bfc44
LC
73 operating-system-packages
74 operating-system-timezone
75 operating-system-locale
598e19dc 76 operating-system-locale-definitions
5dae0186 77 operating-system-mapped-devices
83bcd0b8 78 operating-system-file-systems
b25937e3 79 operating-system-activation-script
033adfe7 80
1aa0033b 81 operating-system-derivation
83bcd0b8 82 operating-system-profile
6f436c54
LC
83 operating-system-grub.cfg
84
568841d4 85 local-host-aliases
df5ce088 86 %setuid-programs
5dae0186 87 %base-packages
f34c56be 88 %base-firmware
5dae0186
LC
89
90 luks-device-mapping))
033adfe7
LC
91
92;;; Commentary:
93;;;
94;;; This module supports whole-system configuration.
95;;;
96;;; Code:
97
98;; System-wide configuration.
99;; TODO: Add per-field docstrings/stexi.
100(define-record-type* <operating-system> operating-system
101 make-operating-system
102 operating-system?
103 (kernel operating-system-kernel ; package
104 (default linux-libre))
d5b429ab
LC
105 (bootloader operating-system-bootloader) ; <grub-configuration>
106
83bcd0b8 107 (initrd operating-system-initrd ; (list fs) -> M derivation
060238ae 108 (default base-initrd))
f34c56be
LC
109 (firmware operating-system-firmware ; list of packages
110 (default %base-firmware))
033adfe7
LC
111
112 (host-name operating-system-host-name) ; string
c65e1834
LC
113 (hosts-file operating-system-hosts-file ; M item | #f
114 (default #f))
033adfe7 115
5dae0186
LC
116 (mapped-devices operating-system-mapped-devices ; list of <mapped-device>
117 (default '()))
8a6d2731 118 (file-systems operating-system-file-systems) ; list of fs
2a13d05e
LC
119 (swap-devices operating-system-swap-devices ; list of strings
120 (default '()))
033adfe7
LC
121
122 (users operating-system-users ; list of user accounts
123 (default '()))
124 (groups operating-system-groups ; list of user groups
773e956d 125 (default %base-groups))
033adfe7 126
40281c54
LC
127 (skeletons operating-system-skeletons ; list of name/monadic value
128 (default (default-skeletons)))
548d4c13
LC
129 (issue operating-system-issue ; string
130 (default %default-issue))
40281c54 131
033adfe7 132 (packages operating-system-packages ; list of (PACKAGE OUTPUT...)
6f436c54 133 (default %base-packages)) ; or just PACKAGE
033adfe7
LC
134
135 (timezone operating-system-timezone) ; string
8a6d2731 136 (locale operating-system-locale ; string
598e19dc
LC
137 (default "en_US.utf8"))
138 (locale-definitions operating-system-locale-definitions ; list of <locale-definition>
139 (default %default-locale-definitions))
033adfe7 140
217a5b85 141 (services operating-system-user-services ; list of monadic services
09e028f4
LC
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
69689380 147 (default %setuid-programs)) ; list of string-valued gexps
033adfe7 148
69689380
LC
149 (sudoers operating-system-sudoers ; /etc/sudoers contents
150 (default %sudoers-specification)))
033adfe7 151
2717a89a 152\f
033adfe7
LC
153;;;
154;;; Derivation.
155;;;
156
23f6056b 157(define* (file-union name files)
033adfe7
LC
158 "Return a derivation that builds a directory containing all of FILES. Each
159item in FILES must be a list where the first element is the file name to use
23f6056b
LC
160in the new directory, and the second element is a gexp denoting the target
161file."
162 (define builder
163 #~(begin
164 (mkdir #$output)
165 (chdir #$output)
166 #$@(map (match-lambda
167 ((target source)
168 #~(symlink #$source #$target)))
169 files)))
033adfe7 170
23f6056b 171 (gexp->derivation name builder))
033adfe7 172
f34c56be
LC
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
40281c54
LC
187\f
188;;;
189;;; Services.
190;;;
191
722554a3 192(define (open-luks-device source target)
5dae0186
LC
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
722554a3
LC
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
023f391c
LC
210(define (other-file-system-services os)
211 "Return file system services for the file systems of OS that are not marked
212as 'needed-for-boot'."
213 (define file-systems
4d6b879c 214 (remove file-system-needed-for-boot?
023f391c
LC
215 (operating-system-file-systems os)))
216
5dae0186
LC
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
023f391c 230 (sequence %store-monad
5dae0186
LC
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))))
023f391c
LC
242 file-systems)))
243
de1c158f
LC
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
253user-land--i.e., those not needed during boot."
9cb426b8
LC
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)))
de1c158f
LC
261
262(define (operating-system-boot-mapped-devices os)
263 "Return the subset of mapped devices that must be installed during boot,
264from the initrd."
9cb426b8
LC
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)))
de1c158f 271
5dae0186
LC
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)
722554a3
LC
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)))
5dae0186 281 (device-mapping-service target
722554a3
LC
282 (open source target)
283 (close source target))))
de1c158f 284 (operating-system-user-mapped-devices os))))
5dae0186 285
2a13d05e
LC
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
217a5b85
LC
291(define (essential-services os)
292 "Return the list of essential services for OS. These are special services
293that implement part of what's declared in OS are responsible for low-level
294bookkeeping."
d6e2a622
LC
295 (define known-fs
296 (map file-system-mount-point (operating-system-file-systems os)))
297
5dae0186
LC
298 (mlet* %store-monad ((mappings (device-mapping-services os))
299 (root-fs (root-file-system-service))
023f391c 300 (other-fs (other-file-system-services os))
d6e2a622 301 (unmount (user-unmount-service known-fs))
2a13d05e 302 (swaps (swap-services os))
023f391c
LC
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))))
d6e2a622 308 (return (cons* host-name procs root-fs unmount
2a13d05e 309 (append other-fs mappings swaps)))))
217a5b85
LC
310
311(define (operating-system-services os)
312 "Return all the services of OS, including \"internal\" services that do not
313explicitly 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
40281c54
LC
319\f
320;;;
321;;; /etc.
322;;;
323
f34c56be
LC
324(define %base-firmware
325 ;; Firmware usable by default.
326 (list ath9k-htc-firmware))
327
6f436c54
LC
328(define %base-packages
329 ;; Default set of packages globally visible. It should include anything
330 ;; required for basic administrator tasks.
55e70e65 331 (cons* procps psmisc which less zile nano
bdb36958 332 (@ (gnu packages admin) dmd) guix
55e70e65 333 lsof ;for Guix's 'list-runtime-roots'
a96a82d7 334 pciutils usbutils
be681773
LC
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
9b762b8d 341 net-tools ; XXX: remove when Inetutils suffices
18316d86 342 man-db
03e9998f 343
a8a086e3
LC
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
03e9998f
LC
349 ;; Get 'insmod' & co. from kmod, not module-init-tools, since udev
350 ;; already depends on it anyway.
8a7330fd 351 kmod eudev
03e9998f 352
b63dbd44 353 e2fsprogs kbd
9b762b8d
LC
354
355 ;; The packages below are also in %FINAL-INPUTS, so take them from
356 ;; there to avoid duplication.
357 (map canonical-package
a68c6967 358 (list guile-2.0 bash coreutils findutils grep sed
4b164c45 359 diffutils patch gawk tar gzip bzip2 xz lzip))))
6f436c54 360
548d4c13
LC
361(define %default-issue
362 ;; Default contents for /etc/issue.
363 "
364This is the GNU system. Welcome.\n")
365
568841d4
LC
366(define (local-host-aliases host-name)
367 "Return aliases for HOST-NAME, to be used in /etc/hosts."
368 (string-append "127.0.0.1 localhost " host-name "\n"
369 "::1 localhost " host-name "\n"))
370
c65e1834
LC
371(define (default-/etc/hosts host-name)
372 "Return the default /etc/hosts file."
568841d4 373 (text-file "hosts" (local-host-aliases host-name)))
c65e1834 374
033adfe7 375(define* (etc-directory #:key
3141a8bd 376 (locale "C") (timezone "Europe/Paris")
548d4c13 377 (issue "Hello!\n")
40281c54 378 (skeletons '())
033adfe7 379 (pam-services '())
b4140694 380 (profile "/run/current-system/profile")
c65e1834 381 hosts-file
69689380 382 (sudoers ""))
033adfe7
LC
383 "Return a derivation that builds the static part of the /etc directory."
384 (mlet* %store-monad
ab6a279a 385 ((pam.d (pam-services->directory pam-services))
69689380 386 (sudoers (text-file "sudoers" sudoers))
033adfe7 387 (login.defs (text-file "login.defs" "# Empty for now.\n"))
9038298c
LC
388 (shells (text-file "shells" ; used by xterm and others
389 "\
390/bin/sh
b4140694
LC
391/run/current-system/profile/bin/sh
392/run/current-system/profile/bin/bash\n"))
548d4c13 393 (issue (text-file "issue" issue))
033adfe7 394
07b08343
LC
395 ;; For now, generate a basic config so that /etc/hosts is honored.
396 (nsswitch (text-file "nsswitch.conf"
397 "hosts: files dns\n"))
398
4e2a21d3
SB
399 ;; Startup file for POSIX-compliant login shells, which set system-wide
400 ;; environment variables.
401 (profile (text-file* "profile" "\
402export LANG=\"" locale "\"
3141a8bd 403export TZ=\"" timezone "\"
7aec3683 404export TZDIR=\"" tzdata "/share/zoneinfo\"
3141a8bd 405
d3bbe992 406# Tell 'modprobe' & co. where to look for modules.
62f0a479 407export LINUX_MODULE_DIRECTORY=/run/booted-system/kernel/lib/modules
d3bbe992 408
585c6519
LC
409export PATH=$HOME/.guix-profile/bin:/run/current-system/profile/bin
410export PATH=/run/setuid-programs:/run/current-system/profile/sbin:$PATH
97ab2c0f 411export MANPATH=$HOME/.guix-profile/share/man:/run/current-system/profile/share/man
95f92d4e 412export INFOPATH=$HOME/.guix-profile/share/info:/run/current-system/profile/share/info
40281c54
LC
413"))
414 (skel (skeleton-directory skeletons)))
23f6056b
LC
415 (file-union "etc"
416 `(("services" ,#~(string-append #$net-base "/etc/services"))
417 ("protocols" ,#~(string-append #$net-base "/etc/protocols"))
418 ("rpc" ,#~(string-append #$net-base "/etc/rpc"))
419 ("pam.d" ,#~#$pam.d)
420 ("login.defs" ,#~#$login.defs)
421 ("issue" ,#~#$issue)
07b08343 422 ("nsswitch.conf" ,#~#$nsswitch)
40281c54 423 ("skel" ,#~#$skel)
23f6056b 424 ("shells" ,#~#$shells)
4e2a21d3 425 ("profile" ,#~#$profile)
c65e1834 426 ("hosts" ,#~#$hosts-file)
23f6056b
LC
427 ("localtime" ,#~(string-append #$tzdata "/share/zoneinfo/"
428 #$timezone))
69689380 429 ("sudoers" ,#~#$sudoers)))))
033adfe7 430
1aa0033b 431(define (operating-system-profile os)
29fce8b6
LC
432 "Return a derivation that builds the system profile of OS."
433 (profile-derivation (manifest (map package->manifest-entry
434 (operating-system-packages os)))))
f6a9d048 435
ab6a279a
LC
436(define %root-account
437 ;; Default root account.
438 (user-account
439 (name "root")
440 (password "")
441 (uid 0) (group "root")
442 (comment "System administrator")
443 (home-directory "/root")))
444
0b6f49ef
LC
445(define (operating-system-accounts os)
446 "Return the user accounts for OS, including an obligatory 'root' account."
ab6a279a
LC
447 (define users
448 ;; Make sure there's a root account.
449 (if (find (lambda (user)
450 (and=> (user-account-uid user) zero?))
451 (operating-system-users os))
452 (operating-system-users os)
453 (cons %root-account (operating-system-users os))))
454
217a5b85 455 (mlet %store-monad ((services (operating-system-services os)))
ab6a279a
LC
456 (return (append users
457 (append-map service-user-accounts services)))))
0b6f49ef
LC
458
459(define (operating-system-etc-directory os)
460 "Return that static part of the /etc directory of OS."
033adfe7 461 (mlet* %store-monad
217a5b85 462 ((services (operating-system-services os))
033adfe7
LC
463 (pam-services ->
464 ;; Services known to PAM.
465 (delete-duplicates
09e028f4
LC
466 (append (operating-system-pam-services os)
467 (append-map service-pam-services services))))
40281c54 468 (profile-drv (operating-system-profile os))
c65e1834
LC
469 (skeletons (operating-system-skeletons os))
470 (/etc/hosts (or (operating-system-hosts-file os)
471 (default-/etc/hosts (operating-system-host-name os)))))
62f0a479 472 (etc-directory #:pam-services pam-services
40281c54 473 #:skeletons skeletons
548d4c13 474 #:issue (operating-system-issue os)
0b6f49ef
LC
475 #:locale (operating-system-locale os)
476 #:timezone (operating-system-timezone os)
c65e1834 477 #:hosts-file /etc/hosts
69689380 478 #:sudoers (operating-system-sudoers os)
0b6f49ef 479 #:profile profile-drv)))
033adfe7 480
09e028f4
LC
481(define %setuid-programs
482 ;; Default set of setuid-root programs.
483 (let ((shadow (@ (gnu packages admin) shadow)))
484 (list #~(string-append #$shadow "/bin/passwd")
485 #~(string-append #$shadow "/bin/su")
69689380 486 #~(string-append #$inetutils "/bin/ping")
8a07c289
LC
487 #~(string-append #$sudo "/bin/sudo")
488 #~(string-append #$fuse "/bin/fusermount"))))
69689380
LC
489
490(define %sudoers-specification
491 ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel'
492 ;; group can do anything. See
493 ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>.
494 ;; TODO: Add a declarative API.
495 "root ALL=(ALL) ALL
496%wheel ALL=(ALL) ALL\n")
09e028f4 497
ab6a279a
LC
498(define (user-group->gexp group)
499 "Turn GROUP, a <user-group> object, into a list-valued gexp suitable for
500'active-groups'."
501 #~(list #$(user-group-name group)
502 #$(user-group-password group)
c8fa3426
LC
503 #$(user-group-id group)
504 #$(user-group-system? group)))
ab6a279a
LC
505
506(define (user-account->gexp account)
507 "Turn ACCOUNT, a <user-account> object, into a list-valued gexp suitable for
508'activate-users'."
509 #~`(#$(user-account-name account)
510 #$(user-account-uid account)
511 #$(user-account-group account)
512 #$(user-account-supplementary-groups account)
513 #$(user-account-comment account)
514 #$(user-account-home-directory account)
515 ,#$(user-account-shell account) ; this one is a gexp
459dd9ea
LC
516 #$(user-account-password account)
517 #$(user-account-system? account)))
ab6a279a 518
d460204f
LC
519(define (modprobe-wrapper)
520 "Return a wrapper for the 'modprobe' command that knows where modules live.
521
522This wrapper is typically invoked by the Linux kernel ('call_modprobe', in
523kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY' environment
524variable is not set---hence the need for this wrapper."
525 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
526 (gexp->script "modprobe"
527 #~(begin
528 (setenv "LINUX_MODULE_DIRECTORY"
529 "/run/booted-system/kernel/lib/modules")
530 (apply execl #$modprobe
531 (cons #$modprobe (cdr (command-line))))))))
532
484a2b3a
LC
533(define (operating-system-activation-script os)
534 "Return the activation script for OS---i.e., the code that \"activates\" the
535stateful part of OS, including user accounts and groups, special directories,
536etc."
09e028f4 537 (define %modules
548f7a8f 538 '((gnu build activation)
8a9e21d1 539 (gnu build linux-boot)
0e704a2d 540 (gnu build linux-modules)
e2f4b305 541 (gnu build file-systems)
0e704a2d
LC
542 (guix build utils)
543 (guix elf)))
09e028f4 544
55ccc388
LC
545 (define (service-activations services)
546 ;; Return the activation scripts for SERVICES.
547 (let ((gexps (filter-map service-activate services)))
548 (sequence %store-monad (map (cut gexp->file "activate-service.scm" <>)
549 gexps))))
550
ab6a279a 551 (mlet* %store-monad ((services (operating-system-services os))
55ccc388 552 (actions (service-activations services))
ab6a279a
LC
553 (etc (operating-system-etc-directory os))
554 (modules (imported-modules %modules))
555 (compiled (compiled-modules %modules))
d460204f 556 (modprobe (modprobe-wrapper))
f34c56be
LC
557 (firmware (directory-union
558 "firmware" (operating-system-firmware os)))
ab6a279a 559 (accounts (operating-system-accounts os)))
09e028f4
LC
560 (define setuid-progs
561 (operating-system-setuid-programs os))
562
ab6a279a
LC
563 (define user-specs
564 (map user-account->gexp accounts))
565
566 (define groups
567 (append (operating-system-groups os)
568 (append-map service-user-groups services)))
569
570 (define group-specs
571 (map user-group->gexp groups))
572
4654439b 573 (gexp->file "activate"
4dfe6c58
LC
574 #~(begin
575 (eval-when (expand load eval)
576 ;; Make sure 'use-modules' below succeeds.
577 (set! %load-path (cons #$modules %load-path))
578 (set! %load-compiled-path
579 (cons #$compiled %load-compiled-path)))
580
548f7a8f 581 (use-modules (gnu build activation))
4dfe6c58 582
ee248b6a
LC
583 ;; Make sure /bin/sh is valid and current.
584 (activate-/bin/sh
585 (string-append #$(canonical-package bash)
586 "/bin/sh"))
587
4dfe6c58
LC
588 ;; Populate /etc.
589 (activate-etc #$etc)
590
ab6a279a
LC
591 ;; Add users and user groups.
592 (setenv "PATH"
593 (string-append #$(@ (gnu packages admin) shadow)
594 "/sbin"))
595 (activate-users+groups (list #$@user-specs)
596 (list #$@group-specs))
597
09e028f4
LC
598 ;; Activate setuid programs.
599 (activate-setuid-programs (list #$@setuid-progs))
600
d460204f
LC
601 ;; Tell the kernel to use our 'modprobe' command.
602 (activate-modprobe #$modprobe)
603
f34c56be
LC
604 ;; Tell the kernel where firmware is.
605 (activate-firmware
606 (string-append #$firmware "/lib/firmware"))
607
55ccc388
LC
608 ;; Run the services' activation snippets.
609 ;; TODO: Use 'load-compiled'.
610 (for-each primitive-load '#$actions)
611
b4140694 612 ;; Set up /run/current-system.
484a2b3a
LC
613 (activate-current-system)))))
614
615(define (operating-system-boot-script os)
616 "Return the boot script for OS---i.e., the code started by the initrd once
617we're running in the final root."
618 (mlet* %store-monad ((services (operating-system-services os))
619 (activate (operating-system-activation-script os))
620 (dmd-conf (dmd-configuration-file services)))
621 (gexp->file "boot"
622 #~(begin
623 ;; Activate the system.
624 ;; TODO: Use 'load-compiled'.
625 (primitive-load #$activate)
626
627 ;; Keep track of the booted system.
628 (false-if-exception (delete-file "/run/booted-system"))
629 (symlink (readlink "/run/current-system")
630 "/run/booted-system")
b4140694 631
26a728eb
LC
632 ;; Close any remaining open file descriptors to be on the
633 ;; safe side. This must be the very last thing we do,
634 ;; because Guile has internal FDs such as 'sleep_pipe'
635 ;; that need to be alive.
636 (let loop ((fd 3))
637 (when (< fd 1024)
638 (false-if-exception (close-fdes fd))
639 (loop (+ 1 fd))))
640
4dfe6c58
LC
641 ;; Start dmd.
642 (execl (string-append #$dmd "/bin/dmd")
643 "dmd" "--config" #$dmd-conf)))))
2106d3fc 644
83bcd0b8
LC
645(define (operating-system-root-file-system os)
646 "Return the root file system of OS."
647 (find (match-lambda
d4c87617 648 (($ <file-system> _ _ "/") #t)
83bcd0b8
LC
649 (_ #f))
650 (operating-system-file-systems os)))
651
b4140694
LC
652(define (operating-system-initrd-file os)
653 "Return a gexp denoting the initrd file of OS."
83bcd0b8 654 (define boot-file-systems
4d6b879c 655 (filter file-system-needed-for-boot?
83bcd0b8
LC
656 (operating-system-file-systems os)))
657
de1c158f
LC
658 (define mapped-devices
659 (operating-system-boot-mapped-devices os))
660
661 (define make-initrd
662 (operating-system-initrd os))
663
664 (mlet %store-monad ((initrd (make-initrd boot-file-systems
665 #:mapped-devices mapped-devices)))
b4140694
LC
666 (return #~(string-append #$initrd "/initrd"))))
667
598e19dc
LC
668(define (operating-system-locale-directory os)
669 "Return the directory containing the locales compiled for the definitions
670listed in OS. The C library expects to find it under
671/run/current-system/locale."
672 ;; While we're at it, check whether the locale of OS is defined.
673 (unless (member (operating-system-locale os)
674 (map locale-definition-name
675 (operating-system-locale-definitions os)))
676 (raise (condition
677 (&message (message "system locale lacks a definition")))))
678
679 (locale-directory (operating-system-locale-definitions os)))
680
2d23e6f0
LC
681(define (kernel->grub-label kernel)
682 "Return a label for the GRUB menu entry that boots KERNEL."
42de9608 683 (string-append "GNU with "
2d23e6f0
LC
684 (string-titlecase (package-name kernel)) " "
685 (package-version kernel)
42de9608 686 " (alpha)"))
2d23e6f0 687
fe6e3fe2
LC
688(define* (operating-system-grub.cfg os #:optional (old-entries '()))
689 "Return the GRUB configuration file for OS. Use OLD-ENTRIES to populate the
690\"old entries\" menu."
0b6f49ef 691 (mlet* %store-monad
b4140694 692 ((system (operating-system-derivation os))
83bcd0b8 693 (root-fs -> (operating-system-root-file-system os))
b4140694 694 (kernel -> (operating-system-kernel os))
033adfe7 695 (entries -> (list (menu-entry
2d23e6f0 696 (label (kernel->grub-label kernel))
033adfe7 697 (linux kernel)
f6a7b21d 698 (linux-arguments
83bcd0b8
LC
699 (list (string-append "--root="
700 (file-system-device root-fs))
b4140694
LC
701 #~(string-append "--system=" #$system)
702 #~(string-append "--load=" #$system
703 "/boot")))
704 (initrd #~(string-append #$system "/initrd"))))))
fe6e3fe2
LC
705 (grub-configuration-file (operating-system-bootloader os) entries
706 #:old-entries old-entries)))
b4140694 707
64e40dbb
LC
708(define (operating-system-parameters-file os)
709 "Return a file that describes the boot parameters of OS. The primary use of
710this file is the reconstruction of GRUB menu entries for old configurations."
711 (mlet %store-monad ((initrd (operating-system-initrd-file os))
712 (root -> (operating-system-root-file-system os))
713 (label -> (kernel->grub-label
714 (operating-system-kernel os))))
715 (gexp->file "parameters"
716 #~(boot-parameters (version 0)
717 (label #$label)
718 (root-device #$(file-system-device root))
719 (kernel #$(operating-system-kernel os))
720 (initrd #$initrd)))))
721
b4140694
LC
722(define (operating-system-derivation os)
723 "Return a derivation that builds OS."
724 (mlet* %store-monad
725 ((profile (operating-system-profile os))
726 (etc (operating-system-etc-directory os))
727 (boot (operating-system-boot-script os))
728 (kernel -> (operating-system-kernel os))
64e40dbb 729 (initrd (operating-system-initrd-file os))
598e19dc 730 (locale (operating-system-locale-directory os))
64e40dbb 731 (params (operating-system-parameters-file os)))
23f6056b 732 (file-union "system"
f6a7b21d 733 `(("boot" ,#~#$boot)
23f6056b 734 ("kernel" ,#~#$kernel)
64e40dbb 735 ("parameters" ,#~#$params)
b4140694 736 ("initrd" ,initrd)
23f6056b 737 ("profile" ,#~#$profile)
598e19dc 738 ("locale" ,#~#$locale) ;used by libc
23f6056b 739 ("etc" ,#~#$etc)))))
033adfe7
LC
740
741;;; system.scm ends here