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