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