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