gexp: Build 'gexp->file' derivations locally.
[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)
89a0d00a 28 #:use-module (gnu packages which)
9de46ffb 29 #:use-module (gnu packages admin)
8a07c289 30 #:use-module (gnu packages linux)
033adfe7 31 #:use-module (gnu packages package-management)
6f436c54
LC
32 #:use-module (gnu packages which)
33 #:use-module (gnu packages less)
34 #:use-module (gnu packages zile)
55e70e65
LC
35 #:use-module (gnu packages nano)
36 #:use-module (gnu packages lsof)
db4fdc04
LC
37 #:use-module (gnu services)
38 #:use-module (gnu services dmd)
39 #:use-module (gnu services base)
033adfe7
LC
40 #:use-module (gnu system grub)
41 #:use-module (gnu system shadow)
42 #:use-module (gnu system linux)
735c6dd7 43 #:use-module (gnu system linux-initrd)
c5df1839 44 #:use-module (gnu system file-systems)
033adfe7
LC
45 #:use-module (ice-9 match)
46 #:use-module (srfi srfi-1)
47 #:use-module (srfi srfi-26)
48 #:export (operating-system
49 operating-system?
d5b429ab
LC
50
51 operating-system-bootloader
033adfe7 52 operating-system-services
217a5b85 53 operating-system-user-services
033adfe7 54 operating-system-packages
fd3bfc44
LC
55 operating-system-host-name
56 operating-system-kernel
57 operating-system-initrd
58 operating-system-users
59 operating-system-groups
548d4c13 60 operating-system-issue
fd3bfc44
LC
61 operating-system-packages
62 operating-system-timezone
63 operating-system-locale
83bcd0b8 64 operating-system-file-systems
b25937e3 65 operating-system-activation-script
033adfe7 66
1aa0033b 67 operating-system-derivation
83bcd0b8 68 operating-system-profile
6f436c54
LC
69 operating-system-grub.cfg
70
71 %base-packages))
033adfe7
LC
72
73;;; Commentary:
74;;;
75;;; This module supports whole-system configuration.
76;;;
77;;; Code:
78
79;; System-wide configuration.
80;; TODO: Add per-field docstrings/stexi.
81(define-record-type* <operating-system> operating-system
82 make-operating-system
83 operating-system?
84 (kernel operating-system-kernel ; package
85 (default linux-libre))
d5b429ab
LC
86 (bootloader operating-system-bootloader) ; <grub-configuration>
87
83bcd0b8 88 (initrd operating-system-initrd ; (list fs) -> M derivation
060238ae 89 (default base-initrd))
033adfe7
LC
90
91 (host-name operating-system-host-name) ; string
92
8a6d2731 93 (file-systems operating-system-file-systems) ; list of fs
033adfe7
LC
94
95 (users operating-system-users ; list of user accounts
96 (default '()))
97 (groups operating-system-groups ; list of user groups
773e956d 98 (default %base-groups))
033adfe7 99
40281c54
LC
100 (skeletons operating-system-skeletons ; list of name/monadic value
101 (default (default-skeletons)))
548d4c13
LC
102 (issue operating-system-issue ; string
103 (default %default-issue))
40281c54 104
033adfe7 105 (packages operating-system-packages ; list of (PACKAGE OUTPUT...)
6f436c54 106 (default %base-packages)) ; or just PACKAGE
033adfe7
LC
107
108 (timezone operating-system-timezone) ; string
8a6d2731
LC
109 (locale operating-system-locale ; string
110 (default "en_US.UTF-8"))
033adfe7 111
217a5b85 112 (services operating-system-user-services ; list of monadic services
09e028f4
LC
113 (default %base-services))
114
115 (pam-services operating-system-pam-services ; list of PAM services
116 (default (base-pam-services)))
117 (setuid-programs operating-system-setuid-programs
69689380 118 (default %setuid-programs)) ; list of string-valued gexps
033adfe7 119
69689380
LC
120 (sudoers operating-system-sudoers ; /etc/sudoers contents
121 (default %sudoers-specification)))
033adfe7 122
2717a89a 123\f
033adfe7
LC
124;;;
125;;; Derivation.
126;;;
127
128(define* (union inputs
f111b327 129 #:key (guile (%guile-for-build))
033adfe7
LC
130 (name "union"))
131 "Return a derivation that builds the union of INPUTS. INPUTS is a list of
132input tuples."
133 (define builder
8c35bfb6
LC
134 #~(begin
135 (use-modules (guix build union))
136
137 (define inputs '#$inputs)
138
139 (setvbuf (current-output-port) _IOLBF)
140 (setvbuf (current-error-port) _IOLBF)
141
142 (format #t "building union `~a' with ~a packages...~%"
143 #$output (length inputs))
144 (union-build #$output inputs)))
145
146 (gexp->derivation name builder
8c35bfb6
LC
147 #:modules '((guix build union))
148 #:guile-for-build guile
149 #:local-build? #t))
033adfe7 150
23f6056b 151(define* (file-union name files)
033adfe7
LC
152 "Return a derivation that builds a directory containing all of FILES. Each
153item in FILES must be a list where the first element is the file name to use
23f6056b
LC
154in the new directory, and the second element is a gexp denoting the target
155file."
156 (define builder
157 #~(begin
158 (mkdir #$output)
159 (chdir #$output)
160 #$@(map (match-lambda
161 ((target source)
162 #~(symlink #$source #$target)))
163 files)))
033adfe7 164
23f6056b 165 (gexp->derivation name builder))
033adfe7 166
40281c54
LC
167\f
168;;;
169;;; Services.
170;;;
171
023f391c
LC
172(define (other-file-system-services os)
173 "Return file system services for the file systems of OS that are not marked
174as 'needed-for-boot'."
175 (define file-systems
176 (remove (lambda (fs)
177 (or (file-system-needed-for-boot? fs)
178 (string=? "/" (file-system-mount-point fs))))
179 (operating-system-file-systems os)))
180
181 (sequence %store-monad
182 (map (match-lambda
d4c87617
LC
183 (($ <file-system> device title target type flags opts
184 #f check?)
023f391c 185 (file-system-service device target type
d4c87617 186 #:title title
023f391c
LC
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
6f436c54
LC
217(define %base-packages
218 ;; Default set of packages globally visible. It should include anything
219 ;; required for basic administrator tasks.
55e70e65 220 (cons* procps psmisc which less zile nano
9b762b8d 221 guile-final (@ (gnu packages admin) dmd) guix
55e70e65 222 lsof ;for Guix's 'list-runtime-roots'
9b762b8d
LC
223 util-linux inetutils isc-dhcp
224 net-tools ; XXX: remove when Inetutils suffices
151a2c07 225 module-init-tools udev kbd
9b762b8d
LC
226
227 ;; The packages below are also in %FINAL-INPUTS, so take them from
228 ;; there to avoid duplication.
229 (map canonical-package
230 (list bash coreutils findutils grep sed))))
6f436c54 231
548d4c13
LC
232(define %default-issue
233 ;; Default contents for /etc/issue.
234 "
235This is the GNU system. Welcome.\n")
236
033adfe7 237(define* (etc-directory #:key
3141a8bd 238 (locale "C") (timezone "Europe/Paris")
548d4c13 239 (issue "Hello!\n")
40281c54 240 (skeletons '())
033adfe7 241 (pam-services '())
b4140694 242 (profile "/run/current-system/profile")
69689380 243 (sudoers ""))
033adfe7
LC
244 "Return a derivation that builds the static part of the /etc directory."
245 (mlet* %store-monad
ab6a279a 246 ((pam.d (pam-services->directory pam-services))
69689380 247 (sudoers (text-file "sudoers" sudoers))
033adfe7 248 (login.defs (text-file "login.defs" "# Empty for now.\n"))
9038298c
LC
249 (shells (text-file "shells" ; used by xterm and others
250 "\
251/bin/sh
b4140694
LC
252/run/current-system/profile/bin/sh
253/run/current-system/profile/bin/bash\n"))
548d4c13 254 (issue (text-file "issue" issue))
033adfe7
LC
255
256 ;; TODO: Generate bashrc from packages' search-paths.
7aec3683 257 (bashrc (text-file* "bashrc" "
033adfe7 258export PS1='\\u@\\h\\$ '
3141a8bd
LC
259
260export LC_ALL=\"" locale "\"
261export TZ=\"" timezone "\"
7aec3683 262export TZDIR=\"" tzdata "/share/zoneinfo\"
3141a8bd 263
d3bbe992 264# Tell 'modprobe' & co. where to look for modules.
62f0a479 265export LINUX_MODULE_DIRECTORY=/run/booted-system/kernel/lib/modules
d3bbe992 266
585c6519
LC
267export PATH=$HOME/.guix-profile/bin:/run/current-system/profile/bin
268export PATH=/run/setuid-programs:/run/current-system/profile/sbin:$PATH
033adfe7
LC
269export CPATH=$HOME/.guix-profile/include:" profile "/include
270export LIBRARY_PATH=$HOME/.guix-profile/lib:" profile "/lib
271alias ls='ls -p --color'
272alias ll='ls -l'
40281c54
LC
273"))
274 (skel (skeleton-directory skeletons)))
23f6056b
LC
275 (file-union "etc"
276 `(("services" ,#~(string-append #$net-base "/etc/services"))
277 ("protocols" ,#~(string-append #$net-base "/etc/protocols"))
278 ("rpc" ,#~(string-append #$net-base "/etc/rpc"))
279 ("pam.d" ,#~#$pam.d)
280 ("login.defs" ,#~#$login.defs)
281 ("issue" ,#~#$issue)
40281c54 282 ("skel" ,#~#$skel)
23f6056b
LC
283 ("shells" ,#~#$shells)
284 ("profile" ,#~#$bashrc)
285 ("localtime" ,#~(string-append #$tzdata "/share/zoneinfo/"
286 #$timezone))
69689380 287 ("sudoers" ,#~#$sudoers)))))
033adfe7 288
1aa0033b 289(define (operating-system-profile os)
f6a9d048
LC
290 "Return a derivation that builds the default profile of OS."
291 ;; TODO: Replace with a real profile with a manifest.
292 (union (operating-system-packages os)
293 #:name "default-profile"))
294
ab6a279a
LC
295(define %root-account
296 ;; Default root account.
297 (user-account
298 (name "root")
299 (password "")
300 (uid 0) (group "root")
301 (comment "System administrator")
302 (home-directory "/root")))
303
0b6f49ef
LC
304(define (operating-system-accounts os)
305 "Return the user accounts for OS, including an obligatory 'root' account."
ab6a279a
LC
306 (define users
307 ;; Make sure there's a root account.
308 (if (find (lambda (user)
309 (and=> (user-account-uid user) zero?))
310 (operating-system-users os))
311 (operating-system-users os)
312 (cons %root-account (operating-system-users os))))
313
217a5b85 314 (mlet %store-monad ((services (operating-system-services os)))
ab6a279a
LC
315 (return (append users
316 (append-map service-user-accounts services)))))
0b6f49ef
LC
317
318(define (operating-system-etc-directory os)
319 "Return that static part of the /etc directory of OS."
033adfe7 320 (mlet* %store-monad
217a5b85 321 ((services (operating-system-services os))
033adfe7
LC
322 (pam-services ->
323 ;; Services known to PAM.
324 (delete-duplicates
09e028f4
LC
325 (append (operating-system-pam-services os)
326 (append-map service-pam-services services))))
40281c54
LC
327 (profile-drv (operating-system-profile os))
328 (skeletons (operating-system-skeletons os)))
62f0a479 329 (etc-directory #: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
459dd9ea
LC
371 #$(user-account-password account)
372 #$(user-account-system? account)))
ab6a279a 373
484a2b3a
LC
374(define (operating-system-activation-script os)
375 "Return the activation script for OS---i.e., the code that \"activates\" the
376stateful part of OS, including user accounts and groups, special directories,
377etc."
09e028f4
LC
378 (define %modules
379 '((guix build activation)
b4140694
LC
380 (guix build utils)
381 (guix build linux-initrd)))
09e028f4 382
55ccc388
LC
383 (define (service-activations services)
384 ;; Return the activation scripts for SERVICES.
385 (let ((gexps (filter-map service-activate services)))
386 (sequence %store-monad (map (cut gexp->file "activate-service.scm" <>)
387 gexps))))
388
ab6a279a 389 (mlet* %store-monad ((services (operating-system-services os))
55ccc388 390 (actions (service-activations services))
ab6a279a
LC
391 (etc (operating-system-etc-directory os))
392 (modules (imported-modules %modules))
393 (compiled (compiled-modules %modules))
ab6a279a 394 (accounts (operating-system-accounts os)))
09e028f4
LC
395 (define setuid-progs
396 (operating-system-setuid-programs os))
397
ab6a279a
LC
398 (define user-specs
399 (map user-account->gexp accounts))
400
401 (define groups
402 (append (operating-system-groups os)
403 (append-map service-user-groups services)))
404
405 (define group-specs
406 (map user-group->gexp groups))
407
4654439b 408 (gexp->file "activate"
4dfe6c58
LC
409 #~(begin
410 (eval-when (expand load eval)
411 ;; Make sure 'use-modules' below succeeds.
412 (set! %load-path (cons #$modules %load-path))
413 (set! %load-compiled-path
414 (cons #$compiled %load-compiled-path)))
415
416 (use-modules (guix build activation))
417
418 ;; Populate /etc.
419 (activate-etc #$etc)
420
ab6a279a
LC
421 ;; Add users and user groups.
422 (setenv "PATH"
423 (string-append #$(@ (gnu packages admin) shadow)
424 "/sbin"))
425 (activate-users+groups (list #$@user-specs)
426 (list #$@group-specs))
427
09e028f4
LC
428 ;; Activate setuid programs.
429 (activate-setuid-programs (list #$@setuid-progs))
430
55ccc388
LC
431 ;; Run the services' activation snippets.
432 ;; TODO: Use 'load-compiled'.
433 (for-each primitive-load '#$actions)
434
b4140694 435 ;; Set up /run/current-system.
484a2b3a
LC
436 (activate-current-system)))))
437
438(define (operating-system-boot-script os)
439 "Return the boot script for OS---i.e., the code started by the initrd once
440we're running in the final root."
441 (mlet* %store-monad ((services (operating-system-services os))
442 (activate (operating-system-activation-script os))
443 (dmd-conf (dmd-configuration-file services)))
444 (gexp->file "boot"
445 #~(begin
446 ;; Activate the system.
447 ;; TODO: Use 'load-compiled'.
448 (primitive-load #$activate)
449
450 ;; Keep track of the booted system.
451 (false-if-exception (delete-file "/run/booted-system"))
452 (symlink (readlink "/run/current-system")
453 "/run/booted-system")
b4140694 454
26a728eb
LC
455 ;; Close any remaining open file descriptors to be on the
456 ;; safe side. This must be the very last thing we do,
457 ;; because Guile has internal FDs such as 'sleep_pipe'
458 ;; that need to be alive.
459 (let loop ((fd 3))
460 (when (< fd 1024)
461 (false-if-exception (close-fdes fd))
462 (loop (+ 1 fd))))
463
4dfe6c58
LC
464 ;; Start dmd.
465 (execl (string-append #$dmd "/bin/dmd")
466 "dmd" "--config" #$dmd-conf)))))
2106d3fc 467
83bcd0b8
LC
468(define (operating-system-root-file-system os)
469 "Return the root file system of OS."
470 (find (match-lambda
d4c87617 471 (($ <file-system> _ _ "/") #t)
83bcd0b8
LC
472 (_ #f))
473 (operating-system-file-systems os)))
474
b4140694
LC
475(define (operating-system-initrd-file os)
476 "Return a gexp denoting the initrd file of OS."
83bcd0b8
LC
477 (define boot-file-systems
478 (filter (match-lambda
d4c87617 479 (($ <file-system> device title "/")
3c05b4bc 480 #t)
d4c87617
LC
481 (($ <file-system> device title mount-point type flags
482 options boot?)
3c05b4bc 483 boot?))
83bcd0b8
LC
484 (operating-system-file-systems os)))
485
b4140694
LC
486 (mlet %store-monad
487 ((initrd ((operating-system-initrd os) boot-file-systems)))
488 (return #~(string-append #$initrd "/initrd"))))
489
2d23e6f0
LC
490(define (kernel->grub-label kernel)
491 "Return a label for the GRUB menu entry that boots KERNEL."
492 (string-append "GNU system with "
493 (string-titlecase (package-name kernel)) " "
494 (package-version kernel)
495 " (technology preview)"))
496
fe6e3fe2
LC
497(define* (operating-system-grub.cfg os #:optional (old-entries '()))
498 "Return the GRUB configuration file for OS. Use OLD-ENTRIES to populate the
499\"old entries\" menu."
0b6f49ef 500 (mlet* %store-monad
b4140694 501 ((system (operating-system-derivation os))
83bcd0b8 502 (root-fs -> (operating-system-root-file-system os))
b4140694 503 (kernel -> (operating-system-kernel os))
033adfe7 504 (entries -> (list (menu-entry
2d23e6f0 505 (label (kernel->grub-label kernel))
033adfe7 506 (linux kernel)
f6a7b21d 507 (linux-arguments
83bcd0b8
LC
508 (list (string-append "--root="
509 (file-system-device root-fs))
b4140694
LC
510 #~(string-append "--system=" #$system)
511 #~(string-append "--load=" #$system
512 "/boot")))
513 (initrd #~(string-append #$system "/initrd"))))))
fe6e3fe2
LC
514 (grub-configuration-file (operating-system-bootloader os) entries
515 #:old-entries old-entries)))
b4140694 516
64e40dbb
LC
517(define (operating-system-parameters-file os)
518 "Return a file that describes the boot parameters of OS. The primary use of
519this file is the reconstruction of GRUB menu entries for old configurations."
520 (mlet %store-monad ((initrd (operating-system-initrd-file os))
521 (root -> (operating-system-root-file-system os))
522 (label -> (kernel->grub-label
523 (operating-system-kernel os))))
524 (gexp->file "parameters"
525 #~(boot-parameters (version 0)
526 (label #$label)
527 (root-device #$(file-system-device root))
528 (kernel #$(operating-system-kernel os))
529 (initrd #$initrd)))))
530
b4140694
LC
531(define (operating-system-derivation os)
532 "Return a derivation that builds OS."
533 (mlet* %store-monad
534 ((profile (operating-system-profile os))
535 (etc (operating-system-etc-directory os))
536 (boot (operating-system-boot-script os))
537 (kernel -> (operating-system-kernel os))
64e40dbb
LC
538 (initrd (operating-system-initrd-file os))
539 (params (operating-system-parameters-file os)))
23f6056b 540 (file-union "system"
f6a7b21d 541 `(("boot" ,#~#$boot)
23f6056b 542 ("kernel" ,#~#$kernel)
64e40dbb 543 ("parameters" ,#~#$params)
b4140694 544 ("initrd" ,initrd)
23f6056b 545 ("profile" ,#~#$profile)
23f6056b 546 ("etc" ,#~#$etc)))))
033adfe7
LC
547
548;;; system.scm ends here