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