gexp: Resolve the default system at '>>=' time.
[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
b25937e3 62 operating-system-activation-script
033adfe7 63
1aa0033b 64 operating-system-derivation
83bcd0b8 65 operating-system-profile
6f436c54
LC
66 operating-system-grub.cfg
67
68 %base-packages))
033adfe7
LC
69
70;;; Commentary:
71;;;
72;;; This module supports whole-system configuration.
73;;;
74;;; Code:
75
76;; System-wide configuration.
77;; TODO: Add per-field docstrings/stexi.
78(define-record-type* <operating-system> operating-system
79 make-operating-system
80 operating-system?
81 (kernel operating-system-kernel ; package
82 (default linux-libre))
d5b429ab
LC
83 (bootloader operating-system-bootloader) ; <grub-configuration>
84
83bcd0b8 85 (initrd operating-system-initrd ; (list fs) -> M derivation
060238ae 86 (default base-initrd))
033adfe7
LC
87
88 (host-name operating-system-host-name) ; string
89
8a6d2731 90 (file-systems operating-system-file-systems) ; list of fs
033adfe7
LC
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
8a6d2731
LC
106 (locale operating-system-locale ; string
107 (default "en_US.UTF-8"))
033adfe7 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
d4c87617
LC
181 (($ <file-system> device title target type flags opts
182 #f check?)
023f391c 183 (file-system-service device target type
d4c87617 184 #:title title
023f391c
LC
185 #:check? check?
186 #:options opts)))
187 file-systems)))
188
217a5b85
LC
189(define (essential-services os)
190 "Return the list of essential services for OS. These are special services
191that implement part of what's declared in OS are responsible for low-level
192bookkeeping."
023f391c
LC
193 (mlet* %store-monad ((root-fs (root-file-system-service))
194 (other-fs (other-file-system-services os))
195 (procs (user-processes-service
196 (map (compose first service-provision)
197 other-fs)))
198 (host-name (host-name-service
199 (operating-system-host-name os))))
200 (return (cons* host-name procs root-fs other-fs))))
217a5b85
LC
201
202(define (operating-system-services os)
203 "Return all the services of OS, including \"internal\" services that do not
204explicitly appear in OS."
205 (mlet %store-monad
206 ((user (sequence %store-monad (operating-system-user-services os)))
207 (essential (essential-services os)))
208 (return (append essential user))))
209
40281c54
LC
210\f
211;;;
212;;; /etc.
213;;;
214
6f436c54
LC
215(define %base-packages
216 ;; Default set of packages globally visible. It should include anything
217 ;; required for basic administrator tasks.
9b762b8d
LC
218 (cons* procps psmisc less zile
219 guile-final (@ (gnu packages admin) dmd) guix
220 util-linux inetutils isc-dhcp
221 net-tools ; XXX: remove when Inetutils suffices
151a2c07 222 module-init-tools udev kbd
9b762b8d
LC
223
224 ;; The packages below are also in %FINAL-INPUTS, so take them from
225 ;; there to avoid duplication.
226 (map canonical-package
227 (list bash coreutils findutils grep sed))))
6f436c54 228
548d4c13
LC
229(define %default-issue
230 ;; Default contents for /etc/issue.
231 "
232This is the GNU system. Welcome.\n")
233
033adfe7 234(define* (etc-directory #:key
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 261# Tell 'modprobe' & co. where to look for modules.
62f0a479 262export LINUX_MODULE_DIRECTORY=/run/booted-system/kernel/lib/modules
d3bbe992 263
585c6519
LC
264export PATH=$HOME/.guix-profile/bin:/run/current-system/profile/bin
265export PATH=/run/setuid-programs:/run/current-system/profile/sbin:$PATH
033adfe7
LC
266export CPATH=$HOME/.guix-profile/include:" profile "/include
267export LIBRARY_PATH=$HOME/.guix-profile/lib:" profile "/lib
268alias ls='ls -p --color'
269alias ll='ls -l'
40281c54
LC
270"))
271 (skel (skeleton-directory skeletons)))
23f6056b
LC
272 (file-union "etc"
273 `(("services" ,#~(string-append #$net-base "/etc/services"))
274 ("protocols" ,#~(string-append #$net-base "/etc/protocols"))
275 ("rpc" ,#~(string-append #$net-base "/etc/rpc"))
276 ("pam.d" ,#~#$pam.d)
277 ("login.defs" ,#~#$login.defs)
278 ("issue" ,#~#$issue)
40281c54 279 ("skel" ,#~#$skel)
23f6056b
LC
280 ("shells" ,#~#$shells)
281 ("profile" ,#~#$bashrc)
282 ("localtime" ,#~(string-append #$tzdata "/share/zoneinfo/"
283 #$timezone))
69689380 284 ("sudoers" ,#~#$sudoers)))))
033adfe7 285
1aa0033b 286(define (operating-system-profile os)
f6a9d048
LC
287 "Return a derivation that builds the default profile of OS."
288 ;; TODO: Replace with a real profile with a manifest.
289 (union (operating-system-packages os)
290 #:name "default-profile"))
291
ab6a279a
LC
292(define %root-account
293 ;; Default root account.
294 (user-account
295 (name "root")
296 (password "")
297 (uid 0) (group "root")
298 (comment "System administrator")
299 (home-directory "/root")))
300
0b6f49ef
LC
301(define (operating-system-accounts os)
302 "Return the user accounts for OS, including an obligatory 'root' account."
ab6a279a
LC
303 (define users
304 ;; Make sure there's a root account.
305 (if (find (lambda (user)
306 (and=> (user-account-uid user) zero?))
307 (operating-system-users os))
308 (operating-system-users os)
309 (cons %root-account (operating-system-users os))))
310
217a5b85 311 (mlet %store-monad ((services (operating-system-services os)))
ab6a279a
LC
312 (return (append users
313 (append-map service-user-accounts services)))))
0b6f49ef
LC
314
315(define (operating-system-etc-directory os)
316 "Return that static part of the /etc directory of OS."
033adfe7 317 (mlet* %store-monad
217a5b85 318 ((services (operating-system-services os))
033adfe7
LC
319 (pam-services ->
320 ;; Services known to PAM.
321 (delete-duplicates
09e028f4
LC
322 (append (operating-system-pam-services os)
323 (append-map service-pam-services services))))
40281c54
LC
324 (profile-drv (operating-system-profile os))
325 (skeletons (operating-system-skeletons os)))
62f0a479 326 (etc-directory #:pam-services pam-services
40281c54 327 #:skeletons skeletons
548d4c13 328 #:issue (operating-system-issue os)
0b6f49ef
LC
329 #:locale (operating-system-locale os)
330 #:timezone (operating-system-timezone os)
69689380 331 #:sudoers (operating-system-sudoers os)
0b6f49ef 332 #:profile profile-drv)))
033adfe7 333
09e028f4
LC
334(define %setuid-programs
335 ;; Default set of setuid-root programs.
336 (let ((shadow (@ (gnu packages admin) shadow)))
337 (list #~(string-append #$shadow "/bin/passwd")
338 #~(string-append #$shadow "/bin/su")
69689380 339 #~(string-append #$inetutils "/bin/ping")
8a07c289
LC
340 #~(string-append #$sudo "/bin/sudo")
341 #~(string-append #$fuse "/bin/fusermount"))))
69689380
LC
342
343(define %sudoers-specification
344 ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel'
345 ;; group can do anything. See
346 ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>.
347 ;; TODO: Add a declarative API.
348 "root ALL=(ALL) ALL
349%wheel ALL=(ALL) ALL\n")
09e028f4 350
ab6a279a
LC
351(define (user-group->gexp group)
352 "Turn GROUP, a <user-group> object, into a list-valued gexp suitable for
353'active-groups'."
354 #~(list #$(user-group-name group)
355 #$(user-group-password group)
356 #$(user-group-id group)))
357
358(define (user-account->gexp account)
359 "Turn ACCOUNT, a <user-account> object, into a list-valued gexp suitable for
360'activate-users'."
361 #~`(#$(user-account-name account)
362 #$(user-account-uid account)
363 #$(user-account-group account)
364 #$(user-account-supplementary-groups account)
365 #$(user-account-comment account)
366 #$(user-account-home-directory account)
367 ,#$(user-account-shell account) ; this one is a gexp
459dd9ea
LC
368 #$(user-account-password account)
369 #$(user-account-system? account)))
ab6a279a 370
484a2b3a
LC
371(define (operating-system-activation-script os)
372 "Return the activation script for OS---i.e., the code that \"activates\" the
373stateful part of OS, including user accounts and groups, special directories,
374etc."
09e028f4
LC
375 (define %modules
376 '((guix build activation)
b4140694
LC
377 (guix build utils)
378 (guix build linux-initrd)))
09e028f4 379
55ccc388
LC
380 (define (service-activations services)
381 ;; Return the activation scripts for SERVICES.
382 (let ((gexps (filter-map service-activate services)))
383 (sequence %store-monad (map (cut gexp->file "activate-service.scm" <>)
384 gexps))))
385
ab6a279a 386 (mlet* %store-monad ((services (operating-system-services os))
55ccc388 387 (actions (service-activations services))
ab6a279a
LC
388 (etc (operating-system-etc-directory os))
389 (modules (imported-modules %modules))
390 (compiled (compiled-modules %modules))
ab6a279a 391 (accounts (operating-system-accounts os)))
09e028f4
LC
392 (define setuid-progs
393 (operating-system-setuid-programs os))
394
ab6a279a
LC
395 (define user-specs
396 (map user-account->gexp accounts))
397
398 (define groups
399 (append (operating-system-groups os)
400 (append-map service-user-groups services)))
401
402 (define group-specs
403 (map user-group->gexp groups))
404
4654439b 405 (gexp->file "activate"
4dfe6c58
LC
406 #~(begin
407 (eval-when (expand load eval)
408 ;; Make sure 'use-modules' below succeeds.
409 (set! %load-path (cons #$modules %load-path))
410 (set! %load-compiled-path
411 (cons #$compiled %load-compiled-path)))
412
413 (use-modules (guix build activation))
414
415 ;; Populate /etc.
416 (activate-etc #$etc)
417
ab6a279a
LC
418 ;; Add users and user groups.
419 (setenv "PATH"
420 (string-append #$(@ (gnu packages admin) shadow)
421 "/sbin"))
422 (activate-users+groups (list #$@user-specs)
423 (list #$@group-specs))
424
09e028f4
LC
425 ;; Activate setuid programs.
426 (activate-setuid-programs (list #$@setuid-progs))
427
55ccc388
LC
428 ;; Run the services' activation snippets.
429 ;; TODO: Use 'load-compiled'.
430 (for-each primitive-load '#$actions)
431
b4140694 432 ;; Set up /run/current-system.
484a2b3a
LC
433 (activate-current-system)))))
434
435(define (operating-system-boot-script os)
436 "Return the boot script for OS---i.e., the code started by the initrd once
437we're running in the final root."
438 (mlet* %store-monad ((services (operating-system-services os))
439 (activate (operating-system-activation-script os))
440 (dmd-conf (dmd-configuration-file services)))
441 (gexp->file "boot"
442 #~(begin
443 ;; Activate the system.
444 ;; TODO: Use 'load-compiled'.
445 (primitive-load #$activate)
446
447 ;; Keep track of the booted system.
448 (false-if-exception (delete-file "/run/booted-system"))
449 (symlink (readlink "/run/current-system")
450 "/run/booted-system")
b4140694 451
26a728eb
LC
452 ;; Close any remaining open file descriptors to be on the
453 ;; safe side. This must be the very last thing we do,
454 ;; because Guile has internal FDs such as 'sleep_pipe'
455 ;; that need to be alive.
456 (let loop ((fd 3))
457 (when (< fd 1024)
458 (false-if-exception (close-fdes fd))
459 (loop (+ 1 fd))))
460
4dfe6c58
LC
461 ;; Start dmd.
462 (execl (string-append #$dmd "/bin/dmd")
463 "dmd" "--config" #$dmd-conf)))))
2106d3fc 464
83bcd0b8
LC
465(define (operating-system-root-file-system os)
466 "Return the root file system of OS."
467 (find (match-lambda
d4c87617 468 (($ <file-system> _ _ "/") #t)
83bcd0b8
LC
469 (_ #f))
470 (operating-system-file-systems os)))
471
b4140694
LC
472(define (operating-system-initrd-file os)
473 "Return a gexp denoting the initrd file of OS."
83bcd0b8
LC
474 (define boot-file-systems
475 (filter (match-lambda
d4c87617 476 (($ <file-system> device title "/")
3c05b4bc 477 #t)
d4c87617
LC
478 (($ <file-system> device title mount-point type flags
479 options boot?)
3c05b4bc 480 boot?))
83bcd0b8
LC
481 (operating-system-file-systems os)))
482
b4140694
LC
483 (mlet %store-monad
484 ((initrd ((operating-system-initrd os) boot-file-systems)))
485 (return #~(string-append #$initrd "/initrd"))))
486
2d23e6f0
LC
487(define (kernel->grub-label kernel)
488 "Return a label for the GRUB menu entry that boots KERNEL."
489 (string-append "GNU system with "
490 (string-titlecase (package-name kernel)) " "
491 (package-version kernel)
492 " (technology preview)"))
493
fe6e3fe2
LC
494(define* (operating-system-grub.cfg os #:optional (old-entries '()))
495 "Return the GRUB configuration file for OS. Use OLD-ENTRIES to populate the
496\"old entries\" menu."
0b6f49ef 497 (mlet* %store-monad
b4140694 498 ((system (operating-system-derivation os))
83bcd0b8 499 (root-fs -> (operating-system-root-file-system os))
b4140694 500 (kernel -> (operating-system-kernel os))
033adfe7 501 (entries -> (list (menu-entry
2d23e6f0 502 (label (kernel->grub-label kernel))
033adfe7 503 (linux kernel)
f6a7b21d 504 (linux-arguments
83bcd0b8
LC
505 (list (string-append "--root="
506 (file-system-device root-fs))
b4140694
LC
507 #~(string-append "--system=" #$system)
508 #~(string-append "--load=" #$system
509 "/boot")))
510 (initrd #~(string-append #$system "/initrd"))))))
fe6e3fe2
LC
511 (grub-configuration-file (operating-system-bootloader os) entries
512 #:old-entries old-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