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