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