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