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