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