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