system: Rename 'sudoers' into 'sudoers-file'.
[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
LC
70 operating-system-kernel
71 operating-system-initrd
72 operating-system-users
73 operating-system-groups
548d4c13 74 operating-system-issue
fd3bfc44
LC
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
24e02c28 114 (hosts-file operating-system-hosts-file ; file-like | #f
c65e1834 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
bf87f38a 124 (default %base-user-accounts))
033adfe7 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
f5a9ffa0
AK
152 (sudoers-file operating-system-sudoers-file ; file-like
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 357
1d167b6e
LC
358 bash-completion
359
9b762b8d
LC
360 ;; The packages below are also in %FINAL-INPUTS, so take them from
361 ;; there to avoid duplication.
362 (map canonical-package
a68c6967 363 (list guile-2.0 bash coreutils findutils grep sed
4b164c45 364 diffutils patch gawk tar gzip bzip2 xz lzip))))
6f436c54 365
548d4c13
LC
366(define %default-issue
367 ;; Default contents for /etc/issue.
368 "
369This is the GNU system. Welcome.\n")
370
568841d4
LC
371(define (local-host-aliases host-name)
372 "Return aliases for HOST-NAME, to be used in /etc/hosts."
373 (string-append "127.0.0.1 localhost " host-name "\n"
374 "::1 localhost " host-name "\n"))
375
c65e1834
LC
376(define (default-/etc/hosts host-name)
377 "Return the default /etc/hosts file."
24e02c28 378 (plain-file "hosts" (local-host-aliases host-name)))
c65e1834 379
0a051769
LC
380(define (emacs-site-file)
381 "Return the Emacs 'site-start.el' file. That file contains the necessary
382settings for 'guix.el' to work out-of-the-box."
383 (gexp->file "site-start.el"
384 #~(progn
385 ;; Add the "normal" elisp directory to the search path;
386 ;; guix.el may be there.
387 (add-to-list
388 'load-path
389 "/run/current-system/profile/share/emacs/site-lisp")
390
391 ;; Attempt to load guix.el.
392 (require 'guix-init nil t)
393
394 (when (require 'geiser-guile nil t)
395 ;; Make sure Geiser's Scheme modules are in Guile's search
396 ;; path.
397 (add-to-list
398 'geiser-guile-load-path
399 "/run/current-system/profile/share/geiser/guile")))))
400
401(define (emacs-site-directory)
402 "Return the Emacs site directory, aka. /etc/emacs."
403 (mlet %store-monad ((file (emacs-site-file)))
404 (gexp->derivation "emacs"
405 #~(begin
406 (mkdir #$output)
407 (chdir #$output)
408 (symlink #$file "site-start.el")))))
409
8e974b9b
LC
410(define (user-shells os)
411 "Return the list of all the shells used by the accounts of OS. These may be
412gexps or strings."
413 (mlet %store-monad ((accounts (operating-system-accounts os)))
414 (return (map user-account-shell accounts))))
415
416(define (shells-file shells)
417 "Return a derivation that builds a shell list for use as /etc/shells based
418on SHELLS. /etc/shells is used by xterm, polkit, and other programs."
419 (gexp->derivation "shells"
420 #~(begin
421 (use-modules (srfi srfi-1))
422
423 (define shells
424 (delete-duplicates (list #$@shells)))
425
426 (call-with-output-file #$output
427 (lambda (port)
428 (display "\
429/bin/sh
430/run/current-system/profile/bin/sh
431/run/current-system/profile/bin/bash\n" port)
432 (for-each (lambda (shell)
433 (display shell port)
434 (newline port))
435 shells))))))
436
033adfe7 437(define* (etc-directory #:key
3141a8bd 438 (locale "C") (timezone "Europe/Paris")
548d4c13 439 (issue "Hello!\n")
40281c54 440 (skeletons '())
033adfe7 441 (pam-services '())
b4140694 442 (profile "/run/current-system/profile")
8e974b9b 443 hosts-file nss (shells '())
f5a9ffa0 444 (sudoers-file (plain-file "sudoers" "")))
033adfe7
LC
445 "Return a derivation that builds the static part of the /etc directory."
446 (mlet* %store-monad
ab6a279a 447 ((pam.d (pam-services->directory pam-services))
033adfe7 448 (login.defs (text-file "login.defs" "# Empty for now.\n"))
8e974b9b 449 (shells (shells-file shells))
0a051769 450 (emacs (emacs-site-directory))
548d4c13 451 (issue (text-file "issue" issue))
07b08343 452 (nsswitch (text-file "nsswitch.conf"
996ed739 453 (name-service-switch->string nss)))
07b08343 454
4e2a21d3
SB
455 ;; Startup file for POSIX-compliant login shells, which set system-wide
456 ;; environment variables.
457 (profile (text-file* "profile" "\
458export LANG=\"" locale "\"
3141a8bd 459export TZ=\"" timezone "\"
7aec3683 460export TZDIR=\"" tzdata "/share/zoneinfo\"
3141a8bd 461
d3bbe992 462# Tell 'modprobe' & co. where to look for modules.
62f0a479 463export LINUX_MODULE_DIRECTORY=/run/booted-system/kernel/lib/modules
d3bbe992 464
d9959421
LC
465# These variables are honored by OpenSSL (libssl) and Git.
466export SSL_CERT_DIR=/etc/ssl/certs
467export SSL_CERT_FILE=\"$SSL_CERT_DIR/ca-certificates.crt\"
468export GIT_SSL_CAINFO=\"$SSL_CERT_FILE\"
469
470# Crucial variables that could be missing the the profiles' 'etc/profile'
471# because they would require combining both profiles.
472# FIXME: See <http://bugs.gnu.org/20255>.
97ab2c0f 473export MANPATH=$HOME/.guix-profile/share/man:/run/current-system/profile/share/man
95f92d4e 474export INFOPATH=$HOME/.guix-profile/share/info:/run/current-system/profile/share/info
00239d05
SB
475export XDG_DATA_DIRS=$HOME/.guix-profile/share:/run/current-system/profile/share
476export XDG_CONFIG_DIRS=$HOME/.guix-profile/etc/xdg:/run/current-system/profile/etc/xdg
477
d9959421
LC
478# Ignore the default value of 'PATH'.
479unset PATH
480
481# Load the system profile's settings.
482GUIX_PROFILE=/run/current-system/profile \\
669786da 483. /run/current-system/profile/etc/profile
d9959421
LC
484
485# Prepend setuid programs.
486export PATH=/run/setuid-programs:$PATH
487
507c71d6 488if [ -f \"$HOME/.guix-profile/etc/profile\" ]
d9959421
LC
489then
490 # Load the user profile's settings.
491 GUIX_PROFILE=\"$HOME/.guix-profile\" \\
669786da 492 . \"$HOME/.guix-profile/etc/profile\"
d9959421
LC
493else
494 # At least define this one so that basic things just work
495 # when the user installs their first package.
496 export PATH=\"$HOME/.guix-profile/bin:$PATH\"
497fi
498
0a051769
LC
499# Append the directory of 'site-start.el' to the search path.
500export EMACSLOADPATH=:/etc/emacs
8c9267a4
LC
501
502# By default, applications that use D-Bus, such as Emacs, abort at startup
503# when /etc/machine-id is missing. Make sure these warnings are non-fatal.
504export DBUS_FATAL_WARNINGS=0
3761792c
LC
505
506# Allow Aspell to find dictionaries installed in the user profile.
507export ASPELL_CONF=\"dict-dir $HOME/.guix-profile/lib/aspell\"
1d167b6e
LC
508
509if [ -n \"$BASH_VERSION\" -a -f /etc/bashrc ]
510then
511 # Load Bash-specific initialization code.
669786da 512 . /etc/bashrc
1d167b6e 513fi
40281c54 514"))
1d167b6e
LC
515
516 (bashrc (text-file "bashrc" "\
517# Bash-specific initialization.
518
519# The 'bash-completion' package.
520if [ -f /run/current-system/profile/etc/profile.d/bash_completion.sh ]
521then
522 # Bash-completion sources ~/.bash_completion. It installs a dynamic
523 # completion loader that searches its own completion files as well
524 # as those in ~/.guix-profile and /run/current-system/profile.
525 source /run/current-system/profile/etc/profile.d/bash_completion.sh
526fi\n"))
40281c54 527 (skel (skeleton-directory skeletons)))
23f6056b
LC
528 (file-union "etc"
529 `(("services" ,#~(string-append #$net-base "/etc/services"))
530 ("protocols" ,#~(string-append #$net-base "/etc/protocols"))
531 ("rpc" ,#~(string-append #$net-base "/etc/rpc"))
0a051769 532 ("emacs" ,#~#$emacs)
23f6056b
LC
533 ("pam.d" ,#~#$pam.d)
534 ("login.defs" ,#~#$login.defs)
535 ("issue" ,#~#$issue)
07b08343 536 ("nsswitch.conf" ,#~#$nsswitch)
40281c54 537 ("skel" ,#~#$skel)
23f6056b 538 ("shells" ,#~#$shells)
4e2a21d3 539 ("profile" ,#~#$profile)
1d167b6e 540 ("bashrc" ,#~#$bashrc)
c65e1834 541 ("hosts" ,#~#$hosts-file)
23f6056b
LC
542 ("localtime" ,#~(string-append #$tzdata "/share/zoneinfo/"
543 #$timezone))
f5a9ffa0 544 ("sudoers" ,sudoers-file)))))
033adfe7 545
1aa0033b 546(define (operating-system-profile os)
29fce8b6
LC
547 "Return a derivation that builds the system profile of OS."
548 (profile-derivation (manifest (map package->manifest-entry
549 (operating-system-packages os)))))
f6a9d048 550
ab6a279a
LC
551(define %root-account
552 ;; Default root account.
553 (user-account
554 (name "root")
555 (password "")
556 (uid 0) (group "root")
557 (comment "System administrator")
558 (home-directory "/root")))
559
0b6f49ef
LC
560(define (operating-system-accounts os)
561 "Return the user accounts for OS, including an obligatory 'root' account."
ab6a279a
LC
562 (define users
563 ;; Make sure there's a root account.
564 (if (find (lambda (user)
565 (and=> (user-account-uid user) zero?))
566 (operating-system-users os))
567 (operating-system-users os)
568 (cons %root-account (operating-system-users os))))
569
217a5b85 570 (mlet %store-monad ((services (operating-system-services os)))
ab6a279a
LC
571 (return (append users
572 (append-map service-user-accounts services)))))
0b6f49ef 573
84765839
LC
574(define (maybe-string->file file-name thing)
575 "If THING is a string, return a <plain-file> with THING as its content.
576Otherwise just return THING.
577
578This is for backward-compatibility of fields that used to be strings and are
579now file-like objects.."
580 (match thing
581 ((? string?)
582 (warning (_ "using a string for file '~a' is deprecated; \
583use 'plain-file' instead~%")
584 file-name)
585 (plain-file file-name thing))
586 (x
587 x)))
588
24e02c28
LC
589(define (maybe-file->monadic file-name thing)
590 "If THING is a value in %STORE-MONAD, return it as is; otherwise return
591THING in the %STORE-MONAD.
592
593This is for backward-compatibility of fields that used to be monadic values
594and are now file-like objects."
595 (with-monad %store-monad
596 (match thing
597 ((? procedure?)
598 (warning (_ "using a monadic value for '~a' is deprecated; \
599use 'plain-file' instead~%")
600 file-name)
601 thing)
602 (x
603 (return x)))))
604
0b6f49ef
LC
605(define (operating-system-etc-directory os)
606 "Return that static part of the /etc directory of OS."
033adfe7 607 (mlet* %store-monad
217a5b85 608 ((services (operating-system-services os))
033adfe7
LC
609 (pam-services ->
610 ;; Services known to PAM.
11dddd8a
LC
611 (append (operating-system-pam-services os)
612 (append-map service-pam-services services)))
40281c54 613 (profile-drv (operating-system-profile os))
c65e1834 614 (skeletons (operating-system-skeletons os))
24e02c28
LC
615 (/etc/hosts (maybe-file->monadic
616 "hosts"
617 (or (operating-system-hosts-file os)
618 (default-/etc/hosts (operating-system-host-name os)))))
8e974b9b 619 (shells (user-shells os)))
62f0a479 620 (etc-directory #:pam-services pam-services
40281c54 621 #:skeletons skeletons
548d4c13 622 #:issue (operating-system-issue os)
0b6f49ef 623 #:locale (operating-system-locale os)
996ed739 624 #:nss (operating-system-name-service-switch os)
0b6f49ef 625 #:timezone (operating-system-timezone os)
c65e1834 626 #:hosts-file /etc/hosts
8e974b9b 627 #:shells shells
f5a9ffa0
AK
628 #:sudoers-file (maybe-string->file
629 "sudoers"
630 (operating-system-sudoers-file os))
0b6f49ef 631 #:profile profile-drv)))
033adfe7 632
09e028f4
LC
633(define %setuid-programs
634 ;; Default set of setuid-root programs.
635 (let ((shadow (@ (gnu packages admin) shadow)))
636 (list #~(string-append #$shadow "/bin/passwd")
637 #~(string-append #$shadow "/bin/su")
69689380 638 #~(string-append #$inetutils "/bin/ping")
8a07c289
LC
639 #~(string-append #$sudo "/bin/sudo")
640 #~(string-append #$fuse "/bin/fusermount"))))
69689380
LC
641
642(define %sudoers-specification
643 ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel'
644 ;; group can do anything. See
645 ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>.
646 ;; TODO: Add a declarative API.
84765839
LC
647 (plain-file "sudoers" "\
648root ALL=(ALL) ALL
649%wheel ALL=(ALL) ALL\n"))
09e028f4 650
ab6a279a
LC
651(define (user-group->gexp group)
652 "Turn GROUP, a <user-group> object, into a list-valued gexp suitable for
653'active-groups'."
654 #~(list #$(user-group-name group)
655 #$(user-group-password group)
c8fa3426
LC
656 #$(user-group-id group)
657 #$(user-group-system? group)))
ab6a279a
LC
658
659(define (user-account->gexp account)
660 "Turn ACCOUNT, a <user-account> object, into a list-valued gexp suitable for
661'activate-users'."
662 #~`(#$(user-account-name account)
663 #$(user-account-uid account)
664 #$(user-account-group account)
665 #$(user-account-supplementary-groups account)
666 #$(user-account-comment account)
667 #$(user-account-home-directory account)
668 ,#$(user-account-shell account) ; this one is a gexp
459dd9ea
LC
669 #$(user-account-password account)
670 #$(user-account-system? account)))
ab6a279a 671
d460204f
LC
672(define (modprobe-wrapper)
673 "Return a wrapper for the 'modprobe' command that knows where modules live.
674
675This wrapper is typically invoked by the Linux kernel ('call_modprobe', in
676kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY' environment
677variable is not set---hence the need for this wrapper."
678 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
679 (gexp->script "modprobe"
680 #~(begin
681 (setenv "LINUX_MODULE_DIRECTORY"
682 "/run/booted-system/kernel/lib/modules")
683 (apply execl #$modprobe
684 (cons #$modprobe (cdr (command-line))))))))
685
484a2b3a
LC
686(define (operating-system-activation-script os)
687 "Return the activation script for OS---i.e., the code that \"activates\" the
688stateful part of OS, including user accounts and groups, special directories,
689etc."
09e028f4 690 (define %modules
548f7a8f 691 '((gnu build activation)
8a9e21d1 692 (gnu build linux-boot)
0e704a2d 693 (gnu build linux-modules)
e2f4b305 694 (gnu build file-systems)
0e704a2d
LC
695 (guix build utils)
696 (guix elf)))
09e028f4 697
55ccc388
LC
698 (define (service-activations services)
699 ;; Return the activation scripts for SERVICES.
700 (let ((gexps (filter-map service-activate services)))
701 (sequence %store-monad (map (cut gexp->file "activate-service.scm" <>)
702 gexps))))
703
ab6a279a 704 (mlet* %store-monad ((services (operating-system-services os))
55ccc388 705 (actions (service-activations services))
ab6a279a
LC
706 (etc (operating-system-etc-directory os))
707 (modules (imported-modules %modules))
708 (compiled (compiled-modules %modules))
d460204f 709 (modprobe (modprobe-wrapper))
f34c56be
LC
710 (firmware (directory-union
711 "firmware" (operating-system-firmware os)))
ab6a279a 712 (accounts (operating-system-accounts os)))
09e028f4
LC
713 (define setuid-progs
714 (operating-system-setuid-programs os))
715
ab6a279a
LC
716 (define user-specs
717 (map user-account->gexp accounts))
718
719 (define groups
720 (append (operating-system-groups os)
721 (append-map service-user-groups services)))
722
723 (define group-specs
724 (map user-group->gexp groups))
725
0c09a306
LC
726 (assert-valid-users/groups accounts groups)
727
4654439b 728 (gexp->file "activate"
4dfe6c58
LC
729 #~(begin
730 (eval-when (expand load eval)
731 ;; Make sure 'use-modules' below succeeds.
732 (set! %load-path (cons #$modules %load-path))
733 (set! %load-compiled-path
734 (cons #$compiled %load-compiled-path)))
735
548f7a8f 736 (use-modules (gnu build activation))
4dfe6c58 737
ee248b6a
LC
738 ;; Make sure /bin/sh is valid and current.
739 (activate-/bin/sh
740 (string-append #$(canonical-package bash)
741 "/bin/sh"))
742
4dfe6c58
LC
743 ;; Populate /etc.
744 (activate-etc #$etc)
745
ab6a279a
LC
746 ;; Add users and user groups.
747 (setenv "PATH"
748 (string-append #$(@ (gnu packages admin) shadow)
749 "/sbin"))
750 (activate-users+groups (list #$@user-specs)
751 (list #$@group-specs))
752
09e028f4
LC
753 ;; Activate setuid programs.
754 (activate-setuid-programs (list #$@setuid-progs))
755
d460204f
LC
756 ;; Tell the kernel to use our 'modprobe' command.
757 (activate-modprobe #$modprobe)
758
f34c56be
LC
759 ;; Tell the kernel where firmware is.
760 (activate-firmware
761 (string-append #$firmware "/lib/firmware"))
762
b158f1d7
LC
763 ;; Let users debug their own processes!
764 (activate-ptrace-attach)
765
55ccc388
LC
766 ;; Run the services' activation snippets.
767 ;; TODO: Use 'load-compiled'.
768 (for-each primitive-load '#$actions)
769
b4140694 770 ;; Set up /run/current-system.
484a2b3a
LC
771 (activate-current-system)))))
772
773(define (operating-system-boot-script os)
774 "Return the boot script for OS---i.e., the code started by the initrd once
775we're running in the final root."
776 (mlet* %store-monad ((services (operating-system-services os))
777 (activate (operating-system-activation-script os))
778 (dmd-conf (dmd-configuration-file services)))
779 (gexp->file "boot"
780 #~(begin
5e738ac2
MW
781 (use-modules (guix build utils))
782
783 ;; Clean out /tmp and /var/run.
784 ;;
785 ;; XXX This needs to happen before service activations, so
786 ;; it has to be here, but this also implicitly assumes
787 ;; that /tmp and /var/run are on the root partition.
788 (false-if-exception (delete-file-recursively "/tmp"))
789 (false-if-exception (delete-file-recursively "/var/run"))
790 (false-if-exception (mkdir "/tmp"))
791 (false-if-exception (chmod "/tmp" #o1777))
792 (false-if-exception (mkdir "/var/run"))
793 (false-if-exception (chmod "/var/run" #o755))
794
484a2b3a
LC
795 ;; Activate the system.
796 ;; TODO: Use 'load-compiled'.
797 (primitive-load #$activate)
798
799 ;; Keep track of the booted system.
800 (false-if-exception (delete-file "/run/booted-system"))
801 (symlink (readlink "/run/current-system")
802 "/run/booted-system")
b4140694 803
26a728eb
LC
804 ;; Close any remaining open file descriptors to be on the
805 ;; safe side. This must be the very last thing we do,
806 ;; because Guile has internal FDs such as 'sleep_pipe'
807 ;; that need to be alive.
808 (let loop ((fd 3))
809 (when (< fd 1024)
810 (false-if-exception (close-fdes fd))
811 (loop (+ 1 fd))))
812
4dfe6c58
LC
813 ;; Start dmd.
814 (execl (string-append #$dmd "/bin/dmd")
815 "dmd" "--config" #$dmd-conf)))))
2106d3fc 816
83bcd0b8
LC
817(define (operating-system-root-file-system os)
818 "Return the root file system of OS."
819 (find (match-lambda
d4c87617 820 (($ <file-system> _ _ "/") #t)
83bcd0b8
LC
821 (_ #f))
822 (operating-system-file-systems os)))
823
b4140694
LC
824(define (operating-system-initrd-file os)
825 "Return a gexp denoting the initrd file of OS."
83bcd0b8 826 (define boot-file-systems
4d6b879c 827 (filter file-system-needed-for-boot?
83bcd0b8
LC
828 (operating-system-file-systems os)))
829
de1c158f
LC
830 (define mapped-devices
831 (operating-system-boot-mapped-devices os))
832
833 (define make-initrd
834 (operating-system-initrd os))
835
836 (mlet %store-monad ((initrd (make-initrd boot-file-systems
0d275f4a 837 #:linux (operating-system-kernel os)
de1c158f 838 #:mapped-devices mapped-devices)))
b4140694
LC
839 (return #~(string-append #$initrd "/initrd"))))
840
598e19dc
LC
841(define (operating-system-locale-directory os)
842 "Return the directory containing the locales compiled for the definitions
843listed in OS. The C library expects to find it under
844/run/current-system/locale."
845 ;; While we're at it, check whether the locale of OS is defined.
846 (unless (member (operating-system-locale os)
847 (map locale-definition-name
848 (operating-system-locale-definitions os)))
849 (raise (condition
850 (&message (message "system locale lacks a definition")))))
851
852 (locale-directory (operating-system-locale-definitions os)))
853
2d23e6f0
LC
854(define (kernel->grub-label kernel)
855 "Return a label for the GRUB menu entry that boots KERNEL."
42de9608 856 (string-append "GNU with "
2d23e6f0
LC
857 (string-titlecase (package-name kernel)) " "
858 (package-version kernel)
42de9608 859 " (alpha)"))
2d23e6f0 860
fe6e3fe2
LC
861(define* (operating-system-grub.cfg os #:optional (old-entries '()))
862 "Return the GRUB configuration file for OS. Use OLD-ENTRIES to populate the
863\"old entries\" menu."
0b6f49ef 864 (mlet* %store-monad
b4140694 865 ((system (operating-system-derivation os))
83bcd0b8 866 (root-fs -> (operating-system-root-file-system os))
b4140694 867 (kernel -> (operating-system-kernel os))
033adfe7 868 (entries -> (list (menu-entry
2d23e6f0 869 (label (kernel->grub-label kernel))
033adfe7 870 (linux kernel)
f6a7b21d 871 (linux-arguments
83bcd0b8
LC
872 (list (string-append "--root="
873 (file-system-device root-fs))
b4140694
LC
874 #~(string-append "--system=" #$system)
875 #~(string-append "--load=" #$system
876 "/boot")))
877 (initrd #~(string-append #$system "/initrd"))))))
fe6e3fe2
LC
878 (grub-configuration-file (operating-system-bootloader os) entries
879 #:old-entries old-entries)))
b4140694 880
64e40dbb
LC
881(define (operating-system-parameters-file os)
882 "Return a file that describes the boot parameters of OS. The primary use of
883this file is the reconstruction of GRUB menu entries for old configurations."
884 (mlet %store-monad ((initrd (operating-system-initrd-file os))
885 (root -> (operating-system-root-file-system os))
886 (label -> (kernel->grub-label
887 (operating-system-kernel os))))
888 (gexp->file "parameters"
889 #~(boot-parameters (version 0)
890 (label #$label)
891 (root-device #$(file-system-device root))
892 (kernel #$(operating-system-kernel os))
893 (initrd #$initrd)))))
894
b4140694
LC
895(define (operating-system-derivation os)
896 "Return a derivation that builds OS."
897 (mlet* %store-monad
898 ((profile (operating-system-profile os))
899 (etc (operating-system-etc-directory os))
900 (boot (operating-system-boot-script os))
901 (kernel -> (operating-system-kernel os))
64e40dbb 902 (initrd (operating-system-initrd-file os))
598e19dc 903 (locale (operating-system-locale-directory os))
64e40dbb 904 (params (operating-system-parameters-file os)))
23f6056b 905 (file-union "system"
f6a7b21d 906 `(("boot" ,#~#$boot)
23f6056b 907 ("kernel" ,#~#$kernel)
64e40dbb 908 ("parameters" ,#~#$params)
b4140694 909 ("initrd" ,initrd)
23f6056b 910 ("profile" ,#~#$profile)
598e19dc 911 ("locale" ,#~#$locale) ;used by libc
23f6056b 912 ("etc" ,#~#$etc)))))
033adfe7
LC
913
914;;; system.scm ends here