system: Store boot parameters in the system derivation.
[jackhill/guix/guix.git] / gnu / system.scm
CommitLineData
033adfe7 1;;; GNU Guix --- Functional package management for GNU
6ce206cb 2;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
033adfe7
LC
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (gnu system)
20 #:use-module (guix store)
21 #:use-module (guix monads)
02100028 22 #:use-module (guix gexp)
033adfe7
LC
23 #:use-module (guix records)
24 #:use-module (guix packages)
25 #:use-module (guix derivations)
033adfe7
LC
26 #:use-module (gnu packages base)
27 #:use-module (gnu packages bash)
9de46ffb 28 #:use-module (gnu packages admin)
8a07c289 29 #:use-module (gnu packages linux)
033adfe7 30 #:use-module (gnu packages package-management)
6f436c54
LC
31 #:use-module (gnu packages which)
32 #:use-module (gnu packages less)
33 #:use-module (gnu packages zile)
db4fdc04
LC
34 #:use-module (gnu services)
35 #:use-module (gnu services dmd)
36 #:use-module (gnu services base)
033adfe7
LC
37 #:use-module (gnu system grub)
38 #:use-module (gnu system shadow)
39 #:use-module (gnu system linux)
735c6dd7 40 #:use-module (gnu system linux-initrd)
c5df1839 41 #:use-module (gnu system file-systems)
033adfe7
LC
42 #:use-module (ice-9 match)
43 #:use-module (srfi srfi-1)
44 #:use-module (srfi srfi-26)
45 #:export (operating-system
46 operating-system?
d5b429ab
LC
47
48 operating-system-bootloader
033adfe7 49 operating-system-services
217a5b85 50 operating-system-user-services
033adfe7 51 operating-system-packages
fd3bfc44
LC
52 operating-system-host-name
53 operating-system-kernel
54 operating-system-initrd
55 operating-system-users
56 operating-system-groups
548d4c13 57 operating-system-issue
fd3bfc44
LC
58 operating-system-packages
59 operating-system-timezone
60 operating-system-locale
83bcd0b8 61 operating-system-file-systems
033adfe7 62
1aa0033b 63 operating-system-derivation
83bcd0b8 64 operating-system-profile
6f436c54
LC
65 operating-system-grub.cfg
66
67 %base-packages))
033adfe7
LC
68
69;;; Commentary:
70;;;
71;;; This module supports whole-system configuration.
72;;;
73;;; Code:
74
75;; System-wide configuration.
76;; TODO: Add per-field docstrings/stexi.
77(define-record-type* <operating-system> operating-system
78 make-operating-system
79 operating-system?
80 (kernel operating-system-kernel ; package
81 (default linux-libre))
d5b429ab
LC
82 (bootloader operating-system-bootloader) ; <grub-configuration>
83
83bcd0b8
LC
84 (initrd operating-system-initrd ; (list fs) -> M derivation
85 (default qemu-initrd))
033adfe7
LC
86
87 (host-name operating-system-host-name) ; string
88
89 (file-systems operating-system-file-systems ; list of fs
90 (default '()))
91
92 (users operating-system-users ; list of user accounts
93 (default '()))
94 (groups operating-system-groups ; list of user groups
773e956d 95 (default %base-groups))
033adfe7 96
40281c54
LC
97 (skeletons operating-system-skeletons ; list of name/monadic value
98 (default (default-skeletons)))
548d4c13
LC
99 (issue operating-system-issue ; string
100 (default %default-issue))
40281c54 101
033adfe7 102 (packages operating-system-packages ; list of (PACKAGE OUTPUT...)
6f436c54 103 (default %base-packages)) ; or just PACKAGE
033adfe7
LC
104
105 (timezone operating-system-timezone) ; string
106 (locale operating-system-locale) ; string
107
217a5b85 108 (services operating-system-user-services ; list of monadic services
09e028f4
LC
109 (default %base-services))
110
111 (pam-services operating-system-pam-services ; list of PAM services
112 (default (base-pam-services)))
113 (setuid-programs operating-system-setuid-programs
69689380 114 (default %setuid-programs)) ; list of string-valued gexps
033adfe7 115
69689380
LC
116 (sudoers operating-system-sudoers ; /etc/sudoers contents
117 (default %sudoers-specification)))
033adfe7 118
2717a89a 119\f
033adfe7
LC
120;;;
121;;; Derivation.
122;;;
123
124(define* (union inputs
125 #:key (guile (%guile-for-build)) (system (%current-system))
126 (name "union"))
127 "Return a derivation that builds the union of INPUTS. INPUTS is a list of
128input tuples."
129 (define builder
8c35bfb6
LC
130 #~(begin
131 (use-modules (guix build union))
132
133 (define inputs '#$inputs)
134
135 (setvbuf (current-output-port) _IOLBF)
136 (setvbuf (current-error-port) _IOLBF)
137
138 (format #t "building union `~a' with ~a packages...~%"
139 #$output (length inputs))
140 (union-build #$output inputs)))
141
142 (gexp->derivation name builder
143 #:system system
144 #:modules '((guix build union))
145 #:guile-for-build guile
146 #:local-build? #t))
033adfe7 147
23f6056b 148(define* (file-union name files)
033adfe7
LC
149 "Return a derivation that builds a directory containing all of FILES. Each
150item in FILES must be a list where the first element is the file name to use
23f6056b
LC
151in the new directory, and the second element is a gexp denoting the target
152file."
153 (define builder
154 #~(begin
155 (mkdir #$output)
156 (chdir #$output)
157 #$@(map (match-lambda
158 ((target source)
159 #~(symlink #$source #$target)))
160 files)))
033adfe7 161
23f6056b 162 (gexp->derivation name builder))
033adfe7 163
40281c54
LC
164\f
165;;;
166;;; Services.
167;;;
168
023f391c
LC
169(define (other-file-system-services os)
170 "Return file system services for the file systems of OS that are not marked
171as 'needed-for-boot'."
172 (define file-systems
173 (remove (lambda (fs)
174 (or (file-system-needed-for-boot? fs)
175 (string=? "/" (file-system-mount-point fs))))
176 (operating-system-file-systems os)))
177
178 (sequence %store-monad
179 (map (match-lambda
d4c87617
LC
180 (($ <file-system> device title target type flags opts
181 #f check?)
023f391c 182 (file-system-service device target type
d4c87617 183 #:title title
023f391c
LC
184 #:check? check?
185 #:options opts)))
186 file-systems)))
187
217a5b85
LC
188(define (essential-services os)
189 "Return the list of essential services for OS. These are special services
190that implement part of what's declared in OS are responsible for low-level
191bookkeeping."
023f391c
LC
192 (mlet* %store-monad ((root-fs (root-file-system-service))
193 (other-fs (other-file-system-services os))
194 (procs (user-processes-service
195 (map (compose first service-provision)
196 other-fs)))
197 (host-name (host-name-service
198 (operating-system-host-name os))))
199 (return (cons* host-name procs root-fs other-fs))))
217a5b85
LC
200
201(define (operating-system-services os)
202 "Return all the services of OS, including \"internal\" services that do not
203explicitly appear in OS."
204 (mlet %store-monad
205 ((user (sequence %store-monad (operating-system-user-services os)))
206 (essential (essential-services os)))
207 (return (append essential user))))
208
40281c54
LC
209\f
210;;;
211;;; /etc.
212;;;
213
6f436c54
LC
214(define %base-packages
215 ;; Default set of packages globally visible. It should include anything
216 ;; required for basic administrator tasks.
9b762b8d
LC
217 (cons* procps psmisc less zile
218 guile-final (@ (gnu packages admin) dmd) guix
219 util-linux inetutils isc-dhcp
220 net-tools ; XXX: remove when Inetutils suffices
151a2c07 221 module-init-tools udev kbd
9b762b8d
LC
222
223 ;; The packages below are also in %FINAL-INPUTS, so take them from
224 ;; there to avoid duplication.
225 (map canonical-package
226 (list bash coreutils findutils grep sed))))
6f436c54 227
548d4c13
LC
228(define %default-issue
229 ;; Default contents for /etc/issue.
230 "
231This is the GNU system. Welcome.\n")
232
033adfe7 233(define* (etc-directory #:key
d3bbe992 234 kernel
3141a8bd 235 (locale "C") (timezone "Europe/Paris")
548d4c13 236 (issue "Hello!\n")
40281c54 237 (skeletons '())
033adfe7 238 (pam-services '())
b4140694 239 (profile "/run/current-system/profile")
69689380 240 (sudoers ""))
033adfe7
LC
241 "Return a derivation that builds the static part of the /etc directory."
242 (mlet* %store-monad
ab6a279a 243 ((pam.d (pam-services->directory pam-services))
69689380 244 (sudoers (text-file "sudoers" sudoers))
033adfe7 245 (login.defs (text-file "login.defs" "# Empty for now.\n"))
9038298c
LC
246 (shells (text-file "shells" ; used by xterm and others
247 "\
248/bin/sh
b4140694
LC
249/run/current-system/profile/bin/sh
250/run/current-system/profile/bin/bash\n"))
548d4c13 251 (issue (text-file "issue" issue))
033adfe7
LC
252
253 ;; TODO: Generate bashrc from packages' search-paths.
7aec3683 254 (bashrc (text-file* "bashrc" "
033adfe7 255export PS1='\\u@\\h\\$ '
3141a8bd
LC
256
257export LC_ALL=\"" locale "\"
258export TZ=\"" timezone "\"
7aec3683 259export TZDIR=\"" tzdata "/share/zoneinfo\"
3141a8bd 260
d3bbe992
LC
261# Tell 'modprobe' & co. where to look for modules.
262# XXX: The downside of doing it here is that when switching to a new config
263# without rebooting, this variable possibly becomes invalid.
264export LINUX_MODULE_DIRECTORY=" kernel "/lib/modules
265
585c6519
LC
266export PATH=$HOME/.guix-profile/bin:/run/current-system/profile/bin
267export PATH=/run/setuid-programs:/run/current-system/profile/sbin:$PATH
033adfe7
LC
268export CPATH=$HOME/.guix-profile/include:" profile "/include
269export LIBRARY_PATH=$HOME/.guix-profile/lib:" profile "/lib
270alias ls='ls -p --color'
271alias ll='ls -l'
40281c54
LC
272"))
273 (skel (skeleton-directory skeletons)))
23f6056b
LC
274 (file-union "etc"
275 `(("services" ,#~(string-append #$net-base "/etc/services"))
276 ("protocols" ,#~(string-append #$net-base "/etc/protocols"))
277 ("rpc" ,#~(string-append #$net-base "/etc/rpc"))
278 ("pam.d" ,#~#$pam.d)
279 ("login.defs" ,#~#$login.defs)
280 ("issue" ,#~#$issue)
40281c54 281 ("skel" ,#~#$skel)
23f6056b
LC
282 ("shells" ,#~#$shells)
283 ("profile" ,#~#$bashrc)
284 ("localtime" ,#~(string-append #$tzdata "/share/zoneinfo/"
285 #$timezone))
69689380 286 ("sudoers" ,#~#$sudoers)))))
033adfe7 287
1aa0033b 288(define (operating-system-profile os)
f6a9d048
LC
289 "Return a derivation that builds the default profile of OS."
290 ;; TODO: Replace with a real profile with a manifest.
291 (union (operating-system-packages os)
292 #:name "default-profile"))
293
ab6a279a
LC
294(define %root-account
295 ;; Default root account.
296 (user-account
297 (name "root")
298 (password "")
299 (uid 0) (group "root")
300 (comment "System administrator")
301 (home-directory "/root")))
302
0b6f49ef
LC
303(define (operating-system-accounts os)
304 "Return the user accounts for OS, including an obligatory 'root' account."
ab6a279a
LC
305 (define users
306 ;; Make sure there's a root account.
307 (if (find (lambda (user)
308 (and=> (user-account-uid user) zero?))
309 (operating-system-users os))
310 (operating-system-users os)
311 (cons %root-account (operating-system-users os))))
312
217a5b85 313 (mlet %store-monad ((services (operating-system-services os)))
ab6a279a
LC
314 (return (append users
315 (append-map service-user-accounts services)))))
0b6f49ef
LC
316
317(define (operating-system-etc-directory os)
318 "Return that static part of the /etc directory of OS."
033adfe7 319 (mlet* %store-monad
217a5b85 320 ((services (operating-system-services os))
033adfe7
LC
321 (pam-services ->
322 ;; Services known to PAM.
323 (delete-duplicates
09e028f4
LC
324 (append (operating-system-pam-services os)
325 (append-map service-pam-services services))))
40281c54
LC
326 (profile-drv (operating-system-profile os))
327 (skeletons (operating-system-skeletons os)))
d3bbe992
LC
328 (etc-directory #:kernel (operating-system-kernel os)
329 #:pam-services pam-services
40281c54 330 #:skeletons skeletons
548d4c13 331 #:issue (operating-system-issue os)
0b6f49ef
LC
332 #:locale (operating-system-locale os)
333 #:timezone (operating-system-timezone os)
69689380 334 #:sudoers (operating-system-sudoers os)
0b6f49ef 335 #:profile profile-drv)))
033adfe7 336
09e028f4
LC
337(define %setuid-programs
338 ;; Default set of setuid-root programs.
339 (let ((shadow (@ (gnu packages admin) shadow)))
340 (list #~(string-append #$shadow "/bin/passwd")
341 #~(string-append #$shadow "/bin/su")
69689380 342 #~(string-append #$inetutils "/bin/ping")
8a07c289
LC
343 #~(string-append #$sudo "/bin/sudo")
344 #~(string-append #$fuse "/bin/fusermount"))))
69689380
LC
345
346(define %sudoers-specification
347 ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel'
348 ;; group can do anything. See
349 ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>.
350 ;; TODO: Add a declarative API.
351 "root ALL=(ALL) ALL
352%wheel ALL=(ALL) ALL\n")
09e028f4 353
ab6a279a
LC
354(define (user-group->gexp group)
355 "Turn GROUP, a <user-group> object, into a list-valued gexp suitable for
356'active-groups'."
357 #~(list #$(user-group-name group)
358 #$(user-group-password group)
359 #$(user-group-id group)))
360
361(define (user-account->gexp account)
362 "Turn ACCOUNT, a <user-account> object, into a list-valued gexp suitable for
363'activate-users'."
364 #~`(#$(user-account-name account)
365 #$(user-account-uid account)
366 #$(user-account-group account)
367 #$(user-account-supplementary-groups account)
368 #$(user-account-comment account)
369 #$(user-account-home-directory account)
370 ,#$(user-account-shell account) ; this one is a gexp
371 #$(user-account-password account)))
372
484a2b3a
LC
373(define (operating-system-activation-script os)
374 "Return the activation script for OS---i.e., the code that \"activates\" the
375stateful part of OS, including user accounts and groups, special directories,
376etc."
09e028f4
LC
377 (define %modules
378 '((guix build activation)
b4140694
LC
379 (guix build utils)
380 (guix build linux-initrd)))
09e028f4 381
55ccc388
LC
382 (define (service-activations services)
383 ;; Return the activation scripts for SERVICES.
384 (let ((gexps (filter-map service-activate services)))
385 (sequence %store-monad (map (cut gexp->file "activate-service.scm" <>)
386 gexps))))
387
ab6a279a 388 (mlet* %store-monad ((services (operating-system-services os))
55ccc388 389 (actions (service-activations services))
ab6a279a
LC
390 (etc (operating-system-etc-directory os))
391 (modules (imported-modules %modules))
392 (compiled (compiled-modules %modules))
ab6a279a 393 (accounts (operating-system-accounts os)))
09e028f4
LC
394 (define setuid-progs
395 (operating-system-setuid-programs os))
396
ab6a279a
LC
397 (define user-specs
398 (map user-account->gexp accounts))
399
400 (define groups
401 (append (operating-system-groups os)
402 (append-map service-user-groups services)))
403
404 (define group-specs
405 (map user-group->gexp groups))
406
4654439b 407 (gexp->file "activate"
4dfe6c58
LC
408 #~(begin
409 (eval-when (expand load eval)
410 ;; Make sure 'use-modules' below succeeds.
411 (set! %load-path (cons #$modules %load-path))
412 (set! %load-compiled-path
413 (cons #$compiled %load-compiled-path)))
414
415 (use-modules (guix build activation))
416
417 ;; Populate /etc.
418 (activate-etc #$etc)
419
ab6a279a
LC
420 ;; Add users and user groups.
421 (setenv "PATH"
422 (string-append #$(@ (gnu packages admin) shadow)
423 "/sbin"))
424 (activate-users+groups (list #$@user-specs)
425 (list #$@group-specs))
426
09e028f4
LC
427 ;; Activate setuid programs.
428 (activate-setuid-programs (list #$@setuid-progs))
429
55ccc388
LC
430 ;; Run the services' activation snippets.
431 ;; TODO: Use 'load-compiled'.
432 (for-each primitive-load '#$actions)
433
b4140694 434 ;; Set up /run/current-system.
484a2b3a
LC
435 (activate-current-system)))))
436
437(define (operating-system-boot-script os)
438 "Return the boot script for OS---i.e., the code started by the initrd once
439we're running in the final root."
440 (mlet* %store-monad ((services (operating-system-services os))
441 (activate (operating-system-activation-script os))
442 (dmd-conf (dmd-configuration-file services)))
443 (gexp->file "boot"
444 #~(begin
445 ;; Activate the system.
446 ;; TODO: Use 'load-compiled'.
447 (primitive-load #$activate)
448
449 ;; Keep track of the booted system.
450 (false-if-exception (delete-file "/run/booted-system"))
451 (symlink (readlink "/run/current-system")
452 "/run/booted-system")
b4140694 453
26a728eb
LC
454 ;; Close any remaining open file descriptors to be on the
455 ;; safe side. This must be the very last thing we do,
456 ;; because Guile has internal FDs such as 'sleep_pipe'
457 ;; that need to be alive.
458 (let loop ((fd 3))
459 (when (< fd 1024)
460 (false-if-exception (close-fdes fd))
461 (loop (+ 1 fd))))
462
4dfe6c58
LC
463 ;; Start dmd.
464 (execl (string-append #$dmd "/bin/dmd")
465 "dmd" "--config" #$dmd-conf)))))
2106d3fc 466
83bcd0b8
LC
467(define (operating-system-root-file-system os)
468 "Return the root file system of OS."
469 (find (match-lambda
d4c87617 470 (($ <file-system> _ _ "/") #t)
83bcd0b8
LC
471 (_ #f))
472 (operating-system-file-systems os)))
473
b4140694
LC
474(define (operating-system-initrd-file os)
475 "Return a gexp denoting the initrd file of OS."
83bcd0b8
LC
476 (define boot-file-systems
477 (filter (match-lambda
d4c87617 478 (($ <file-system> device title "/")
3c05b4bc 479 #t)
d4c87617
LC
480 (($ <file-system> device title mount-point type flags
481 options boot?)
3c05b4bc 482 boot?))
83bcd0b8
LC
483 (operating-system-file-systems os)))
484
b4140694
LC
485 (mlet %store-monad
486 ((initrd ((operating-system-initrd os) boot-file-systems)))
487 (return #~(string-append #$initrd "/initrd"))))
488
2d23e6f0
LC
489(define (kernel->grub-label kernel)
490 "Return a label for the GRUB menu entry that boots KERNEL."
491 (string-append "GNU system with "
492 (string-titlecase (package-name kernel)) " "
493 (package-version kernel)
494 " (technology preview)"))
495
b4140694
LC
496(define (operating-system-grub.cfg os)
497 "Return the GRUB configuration file for OS."
0b6f49ef 498 (mlet* %store-monad
b4140694 499 ((system (operating-system-derivation os))
83bcd0b8 500 (root-fs -> (operating-system-root-file-system os))
b4140694 501 (kernel -> (operating-system-kernel os))
033adfe7 502 (entries -> (list (menu-entry
2d23e6f0 503 (label (kernel->grub-label kernel))
033adfe7 504 (linux kernel)
f6a7b21d 505 (linux-arguments
83bcd0b8
LC
506 (list (string-append "--root="
507 (file-system-device root-fs))
b4140694
LC
508 #~(string-append "--system=" #$system)
509 #~(string-append "--load=" #$system
510 "/boot")))
511 (initrd #~(string-append #$system "/initrd"))))))
d5b429ab 512 (grub-configuration-file (operating-system-bootloader os) entries)))
b4140694 513
64e40dbb
LC
514(define (operating-system-parameters-file os)
515 "Return a file that describes the boot parameters of OS. The primary use of
516this file is the reconstruction of GRUB menu entries for old configurations."
517 (mlet %store-monad ((initrd (operating-system-initrd-file os))
518 (root -> (operating-system-root-file-system os))
519 (label -> (kernel->grub-label
520 (operating-system-kernel os))))
521 (gexp->file "parameters"
522 #~(boot-parameters (version 0)
523 (label #$label)
524 (root-device #$(file-system-device root))
525 (kernel #$(operating-system-kernel os))
526 (initrd #$initrd)))))
527
b4140694
LC
528(define (operating-system-derivation os)
529 "Return a derivation that builds OS."
530 (mlet* %store-monad
531 ((profile (operating-system-profile os))
532 (etc (operating-system-etc-directory os))
533 (boot (operating-system-boot-script os))
534 (kernel -> (operating-system-kernel os))
64e40dbb
LC
535 (initrd (operating-system-initrd-file os))
536 (params (operating-system-parameters-file os)))
23f6056b 537 (file-union "system"
f6a7b21d 538 `(("boot" ,#~#$boot)
23f6056b 539 ("kernel" ,#~#$kernel)
64e40dbb 540 ("parameters" ,#~#$params)
b4140694 541 ("initrd" ,initrd)
23f6056b 542 ("profile" ,#~#$profile)
23f6056b 543 ("etc" ,#~#$etc)))))
033adfe7
LC
544
545;;; system.scm ends here