gnu: wireless-tools: Describe it as deprecated.
[jackhill/guix/guix.git] / gnu / system.scm
CommitLineData
033adfe7 1;;; GNU Guix --- Functional package management for GNU
6ce206cb 2;;; Copyright © 2013, 2014 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
a68c6967 335 util-linux inetutils isc-dhcp wireless-tools
9b762b8d 336 net-tools ; XXX: remove when Inetutils suffices
18316d86 337 man-db
03e9998f 338
a8a086e3
LC
339 ;; The 'sudo' command is already in %SETUID-PROGRAMS, but we also
340 ;; want the other commands and the man pages (notably because
341 ;; auto-completion in Emacs shell relies on man pages.)
342 sudo
343
03e9998f
LC
344 ;; Get 'insmod' & co. from kmod, not module-init-tools, since udev
345 ;; already depends on it anyway.
8a7330fd 346 kmod eudev
03e9998f 347
b63dbd44 348 e2fsprogs kbd
9b762b8d
LC
349
350 ;; The packages below are also in %FINAL-INPUTS, so take them from
351 ;; there to avoid duplication.
352 (map canonical-package
a68c6967 353 (list guile-2.0 bash coreutils findutils grep sed
4b164c45 354 diffutils patch gawk tar gzip bzip2 xz lzip))))
6f436c54 355
548d4c13
LC
356(define %default-issue
357 ;; Default contents for /etc/issue.
358 "
359This is the GNU system. Welcome.\n")
360
568841d4
LC
361(define (local-host-aliases host-name)
362 "Return aliases for HOST-NAME, to be used in /etc/hosts."
363 (string-append "127.0.0.1 localhost " host-name "\n"
364 "::1 localhost " host-name "\n"))
365
c65e1834
LC
366(define (default-/etc/hosts host-name)
367 "Return the default /etc/hosts file."
568841d4 368 (text-file "hosts" (local-host-aliases host-name)))
c65e1834 369
033adfe7 370(define* (etc-directory #:key
3141a8bd 371 (locale "C") (timezone "Europe/Paris")
548d4c13 372 (issue "Hello!\n")
40281c54 373 (skeletons '())
033adfe7 374 (pam-services '())
b4140694 375 (profile "/run/current-system/profile")
c65e1834 376 hosts-file
69689380 377 (sudoers ""))
033adfe7
LC
378 "Return a derivation that builds the static part of the /etc directory."
379 (mlet* %store-monad
ab6a279a 380 ((pam.d (pam-services->directory pam-services))
69689380 381 (sudoers (text-file "sudoers" sudoers))
033adfe7 382 (login.defs (text-file "login.defs" "# Empty for now.\n"))
9038298c
LC
383 (shells (text-file "shells" ; used by xterm and others
384 "\
385/bin/sh
b4140694
LC
386/run/current-system/profile/bin/sh
387/run/current-system/profile/bin/bash\n"))
548d4c13 388 (issue (text-file "issue" issue))
033adfe7 389
07b08343
LC
390 ;; For now, generate a basic config so that /etc/hosts is honored.
391 (nsswitch (text-file "nsswitch.conf"
392 "hosts: files dns\n"))
393
4e2a21d3
SB
394 ;; Startup file for POSIX-compliant login shells, which set system-wide
395 ;; environment variables.
396 (profile (text-file* "profile" "\
397export LANG=\"" locale "\"
3141a8bd 398export TZ=\"" timezone "\"
7aec3683 399export TZDIR=\"" tzdata "/share/zoneinfo\"
3141a8bd 400
d3bbe992 401# Tell 'modprobe' & co. where to look for modules.
62f0a479 402export LINUX_MODULE_DIRECTORY=/run/booted-system/kernel/lib/modules
d3bbe992 403
585c6519
LC
404export PATH=$HOME/.guix-profile/bin:/run/current-system/profile/bin
405export PATH=/run/setuid-programs:/run/current-system/profile/sbin:$PATH
97ab2c0f 406export MANPATH=$HOME/.guix-profile/share/man:/run/current-system/profile/share/man
95f92d4e 407export INFOPATH=$HOME/.guix-profile/share/info:/run/current-system/profile/share/info
40281c54
LC
408"))
409 (skel (skeleton-directory skeletons)))
23f6056b
LC
410 (file-union "etc"
411 `(("services" ,#~(string-append #$net-base "/etc/services"))
412 ("protocols" ,#~(string-append #$net-base "/etc/protocols"))
413 ("rpc" ,#~(string-append #$net-base "/etc/rpc"))
414 ("pam.d" ,#~#$pam.d)
415 ("login.defs" ,#~#$login.defs)
416 ("issue" ,#~#$issue)
07b08343 417 ("nsswitch.conf" ,#~#$nsswitch)
40281c54 418 ("skel" ,#~#$skel)
23f6056b 419 ("shells" ,#~#$shells)
4e2a21d3 420 ("profile" ,#~#$profile)
c65e1834 421 ("hosts" ,#~#$hosts-file)
23f6056b
LC
422 ("localtime" ,#~(string-append #$tzdata "/share/zoneinfo/"
423 #$timezone))
69689380 424 ("sudoers" ,#~#$sudoers)))))
033adfe7 425
1aa0033b 426(define (operating-system-profile os)
29fce8b6
LC
427 "Return a derivation that builds the system profile of OS."
428 (profile-derivation (manifest (map package->manifest-entry
429 (operating-system-packages os)))))
f6a9d048 430
ab6a279a
LC
431(define %root-account
432 ;; Default root account.
433 (user-account
434 (name "root")
435 (password "")
436 (uid 0) (group "root")
437 (comment "System administrator")
438 (home-directory "/root")))
439
0b6f49ef
LC
440(define (operating-system-accounts os)
441 "Return the user accounts for OS, including an obligatory 'root' account."
ab6a279a
LC
442 (define users
443 ;; Make sure there's a root account.
444 (if (find (lambda (user)
445 (and=> (user-account-uid user) zero?))
446 (operating-system-users os))
447 (operating-system-users os)
448 (cons %root-account (operating-system-users os))))
449
217a5b85 450 (mlet %store-monad ((services (operating-system-services os)))
ab6a279a
LC
451 (return (append users
452 (append-map service-user-accounts services)))))
0b6f49ef
LC
453
454(define (operating-system-etc-directory os)
455 "Return that static part of the /etc directory of OS."
033adfe7 456 (mlet* %store-monad
217a5b85 457 ((services (operating-system-services os))
033adfe7
LC
458 (pam-services ->
459 ;; Services known to PAM.
460 (delete-duplicates
09e028f4
LC
461 (append (operating-system-pam-services os)
462 (append-map service-pam-services services))))
40281c54 463 (profile-drv (operating-system-profile os))
c65e1834
LC
464 (skeletons (operating-system-skeletons os))
465 (/etc/hosts (or (operating-system-hosts-file os)
466 (default-/etc/hosts (operating-system-host-name os)))))
62f0a479 467 (etc-directory #:pam-services pam-services
40281c54 468 #:skeletons skeletons
548d4c13 469 #:issue (operating-system-issue os)
0b6f49ef
LC
470 #:locale (operating-system-locale os)
471 #:timezone (operating-system-timezone os)
c65e1834 472 #:hosts-file /etc/hosts
69689380 473 #:sudoers (operating-system-sudoers os)
0b6f49ef 474 #:profile profile-drv)))
033adfe7 475
09e028f4
LC
476(define %setuid-programs
477 ;; Default set of setuid-root programs.
478 (let ((shadow (@ (gnu packages admin) shadow)))
479 (list #~(string-append #$shadow "/bin/passwd")
480 #~(string-append #$shadow "/bin/su")
69689380 481 #~(string-append #$inetutils "/bin/ping")
8a07c289
LC
482 #~(string-append #$sudo "/bin/sudo")
483 #~(string-append #$fuse "/bin/fusermount"))))
69689380
LC
484
485(define %sudoers-specification
486 ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel'
487 ;; group can do anything. See
488 ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>.
489 ;; TODO: Add a declarative API.
490 "root ALL=(ALL) ALL
491%wheel ALL=(ALL) ALL\n")
09e028f4 492
ab6a279a
LC
493(define (user-group->gexp group)
494 "Turn GROUP, a <user-group> object, into a list-valued gexp suitable for
495'active-groups'."
496 #~(list #$(user-group-name group)
497 #$(user-group-password group)
c8fa3426
LC
498 #$(user-group-id group)
499 #$(user-group-system? group)))
ab6a279a
LC
500
501(define (user-account->gexp account)
502 "Turn ACCOUNT, a <user-account> object, into a list-valued gexp suitable for
503'activate-users'."
504 #~`(#$(user-account-name account)
505 #$(user-account-uid account)
506 #$(user-account-group account)
507 #$(user-account-supplementary-groups account)
508 #$(user-account-comment account)
509 #$(user-account-home-directory account)
510 ,#$(user-account-shell account) ; this one is a gexp
459dd9ea
LC
511 #$(user-account-password account)
512 #$(user-account-system? account)))
ab6a279a 513
d460204f
LC
514(define (modprobe-wrapper)
515 "Return a wrapper for the 'modprobe' command that knows where modules live.
516
517This wrapper is typically invoked by the Linux kernel ('call_modprobe', in
518kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY' environment
519variable is not set---hence the need for this wrapper."
520 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
521 (gexp->script "modprobe"
522 #~(begin
523 (setenv "LINUX_MODULE_DIRECTORY"
524 "/run/booted-system/kernel/lib/modules")
525 (apply execl #$modprobe
526 (cons #$modprobe (cdr (command-line))))))))
527
484a2b3a
LC
528(define (operating-system-activation-script os)
529 "Return the activation script for OS---i.e., the code that \"activates\" the
530stateful part of OS, including user accounts and groups, special directories,
531etc."
09e028f4 532 (define %modules
548f7a8f 533 '((gnu build activation)
8a9e21d1 534 (gnu build linux-boot)
0e704a2d 535 (gnu build linux-modules)
e2f4b305 536 (gnu build file-systems)
0e704a2d
LC
537 (guix build utils)
538 (guix elf)))
09e028f4 539
55ccc388
LC
540 (define (service-activations services)
541 ;; Return the activation scripts for SERVICES.
542 (let ((gexps (filter-map service-activate services)))
543 (sequence %store-monad (map (cut gexp->file "activate-service.scm" <>)
544 gexps))))
545
ab6a279a 546 (mlet* %store-monad ((services (operating-system-services os))
55ccc388 547 (actions (service-activations services))
ab6a279a
LC
548 (etc (operating-system-etc-directory os))
549 (modules (imported-modules %modules))
550 (compiled (compiled-modules %modules))
d460204f 551 (modprobe (modprobe-wrapper))
f34c56be
LC
552 (firmware (directory-union
553 "firmware" (operating-system-firmware os)))
ab6a279a 554 (accounts (operating-system-accounts os)))
09e028f4
LC
555 (define setuid-progs
556 (operating-system-setuid-programs os))
557
ab6a279a
LC
558 (define user-specs
559 (map user-account->gexp accounts))
560
561 (define groups
562 (append (operating-system-groups os)
563 (append-map service-user-groups services)))
564
565 (define group-specs
566 (map user-group->gexp groups))
567
4654439b 568 (gexp->file "activate"
4dfe6c58
LC
569 #~(begin
570 (eval-when (expand load eval)
571 ;; Make sure 'use-modules' below succeeds.
572 (set! %load-path (cons #$modules %load-path))
573 (set! %load-compiled-path
574 (cons #$compiled %load-compiled-path)))
575
548f7a8f 576 (use-modules (gnu build activation))
4dfe6c58 577
ee248b6a
LC
578 ;; Make sure /bin/sh is valid and current.
579 (activate-/bin/sh
580 (string-append #$(canonical-package bash)
581 "/bin/sh"))
582
4dfe6c58
LC
583 ;; Populate /etc.
584 (activate-etc #$etc)
585
ab6a279a
LC
586 ;; Add users and user groups.
587 (setenv "PATH"
588 (string-append #$(@ (gnu packages admin) shadow)
589 "/sbin"))
590 (activate-users+groups (list #$@user-specs)
591 (list #$@group-specs))
592
09e028f4
LC
593 ;; Activate setuid programs.
594 (activate-setuid-programs (list #$@setuid-progs))
595
d460204f
LC
596 ;; Tell the kernel to use our 'modprobe' command.
597 (activate-modprobe #$modprobe)
598
f34c56be
LC
599 ;; Tell the kernel where firmware is.
600 (activate-firmware
601 (string-append #$firmware "/lib/firmware"))
602
55ccc388
LC
603 ;; Run the services' activation snippets.
604 ;; TODO: Use 'load-compiled'.
605 (for-each primitive-load '#$actions)
606
b4140694 607 ;; Set up /run/current-system.
484a2b3a
LC
608 (activate-current-system)))))
609
610(define (operating-system-boot-script os)
611 "Return the boot script for OS---i.e., the code started by the initrd once
612we're running in the final root."
613 (mlet* %store-monad ((services (operating-system-services os))
614 (activate (operating-system-activation-script os))
615 (dmd-conf (dmd-configuration-file services)))
616 (gexp->file "boot"
617 #~(begin
618 ;; Activate the system.
619 ;; TODO: Use 'load-compiled'.
620 (primitive-load #$activate)
621
622 ;; Keep track of the booted system.
623 (false-if-exception (delete-file "/run/booted-system"))
624 (symlink (readlink "/run/current-system")
625 "/run/booted-system")
b4140694 626
26a728eb
LC
627 ;; Close any remaining open file descriptors to be on the
628 ;; safe side. This must be the very last thing we do,
629 ;; because Guile has internal FDs such as 'sleep_pipe'
630 ;; that need to be alive.
631 (let loop ((fd 3))
632 (when (< fd 1024)
633 (false-if-exception (close-fdes fd))
634 (loop (+ 1 fd))))
635
4dfe6c58
LC
636 ;; Start dmd.
637 (execl (string-append #$dmd "/bin/dmd")
638 "dmd" "--config" #$dmd-conf)))))
2106d3fc 639
83bcd0b8
LC
640(define (operating-system-root-file-system os)
641 "Return the root file system of OS."
642 (find (match-lambda
d4c87617 643 (($ <file-system> _ _ "/") #t)
83bcd0b8
LC
644 (_ #f))
645 (operating-system-file-systems os)))
646
b4140694
LC
647(define (operating-system-initrd-file os)
648 "Return a gexp denoting the initrd file of OS."
83bcd0b8 649 (define boot-file-systems
4d6b879c 650 (filter file-system-needed-for-boot?
83bcd0b8
LC
651 (operating-system-file-systems os)))
652
de1c158f
LC
653 (define mapped-devices
654 (operating-system-boot-mapped-devices os))
655
656 (define make-initrd
657 (operating-system-initrd os))
658
659 (mlet %store-monad ((initrd (make-initrd boot-file-systems
660 #:mapped-devices mapped-devices)))
b4140694
LC
661 (return #~(string-append #$initrd "/initrd"))))
662
598e19dc
LC
663(define (operating-system-locale-directory os)
664 "Return the directory containing the locales compiled for the definitions
665listed in OS. The C library expects to find it under
666/run/current-system/locale."
667 ;; While we're at it, check whether the locale of OS is defined.
668 (unless (member (operating-system-locale os)
669 (map locale-definition-name
670 (operating-system-locale-definitions os)))
671 (raise (condition
672 (&message (message "system locale lacks a definition")))))
673
674 (locale-directory (operating-system-locale-definitions os)))
675
2d23e6f0
LC
676(define (kernel->grub-label kernel)
677 "Return a label for the GRUB menu entry that boots KERNEL."
42de9608 678 (string-append "GNU with "
2d23e6f0
LC
679 (string-titlecase (package-name kernel)) " "
680 (package-version kernel)
42de9608 681 " (alpha)"))
2d23e6f0 682
fe6e3fe2
LC
683(define* (operating-system-grub.cfg os #:optional (old-entries '()))
684 "Return the GRUB configuration file for OS. Use OLD-ENTRIES to populate the
685\"old entries\" menu."
0b6f49ef 686 (mlet* %store-monad
b4140694 687 ((system (operating-system-derivation os))
83bcd0b8 688 (root-fs -> (operating-system-root-file-system os))
b4140694 689 (kernel -> (operating-system-kernel os))
033adfe7 690 (entries -> (list (menu-entry
2d23e6f0 691 (label (kernel->grub-label kernel))
033adfe7 692 (linux kernel)
f6a7b21d 693 (linux-arguments
83bcd0b8
LC
694 (list (string-append "--root="
695 (file-system-device root-fs))
b4140694
LC
696 #~(string-append "--system=" #$system)
697 #~(string-append "--load=" #$system
698 "/boot")))
699 (initrd #~(string-append #$system "/initrd"))))))
fe6e3fe2
LC
700 (grub-configuration-file (operating-system-bootloader os) entries
701 #:old-entries old-entries)))
b4140694 702
64e40dbb
LC
703(define (operating-system-parameters-file os)
704 "Return a file that describes the boot parameters of OS. The primary use of
705this file is the reconstruction of GRUB menu entries for old configurations."
706 (mlet %store-monad ((initrd (operating-system-initrd-file os))
707 (root -> (operating-system-root-file-system os))
708 (label -> (kernel->grub-label
709 (operating-system-kernel os))))
710 (gexp->file "parameters"
711 #~(boot-parameters (version 0)
712 (label #$label)
713 (root-device #$(file-system-device root))
714 (kernel #$(operating-system-kernel os))
715 (initrd #$initrd)))))
716
b4140694
LC
717(define (operating-system-derivation os)
718 "Return a derivation that builds OS."
719 (mlet* %store-monad
720 ((profile (operating-system-profile os))
721 (etc (operating-system-etc-directory os))
722 (boot (operating-system-boot-script os))
723 (kernel -> (operating-system-kernel os))
64e40dbb 724 (initrd (operating-system-initrd-file os))
598e19dc 725 (locale (operating-system-locale-directory os))
64e40dbb 726 (params (operating-system-parameters-file os)))
23f6056b 727 (file-union "system"
f6a7b21d 728 `(("boot" ,#~#$boot)
23f6056b 729 ("kernel" ,#~#$kernel)
64e40dbb 730 ("parameters" ,#~#$params)
b4140694 731 ("initrd" ,initrd)
23f6056b 732 ("profile" ,#~#$profile)
598e19dc 733 ("locale" ,#~#$locale) ;used by libc
23f6056b 734 ("etc" ,#~#$etc)))))
033adfe7
LC
735
736;;; system.scm ends here