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