doc: Improve wording and example in "Using the Configuration System".
[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
LC
85 (initrd operating-system-initrd ; (list fs) -> M derivation
86 (default qemu-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
d3bbe992 235 kernel
3141a8bd 236 (locale "C") (timezone "Europe/Paris")
548d4c13 237 (issue "Hello!\n")
40281c54 238 (skeletons '())
033adfe7 239 (pam-services '())
b4140694 240 (profile "/run/current-system/profile")
69689380 241 (sudoers ""))
033adfe7
LC
242 "Return a derivation that builds the static part of the /etc directory."
243 (mlet* %store-monad
ab6a279a 244 ((pam.d (pam-services->directory pam-services))
69689380 245 (sudoers (text-file "sudoers" sudoers))
033adfe7 246 (login.defs (text-file "login.defs" "# Empty for now.\n"))
9038298c
LC
247 (shells (text-file "shells" ; used by xterm and others
248 "\
249/bin/sh
b4140694
LC
250/run/current-system/profile/bin/sh
251/run/current-system/profile/bin/bash\n"))
548d4c13 252 (issue (text-file "issue" issue))
033adfe7
LC
253
254 ;; TODO: Generate bashrc from packages' search-paths.
7aec3683 255 (bashrc (text-file* "bashrc" "
033adfe7 256export PS1='\\u@\\h\\$ '
3141a8bd
LC
257
258export LC_ALL=\"" locale "\"
259export TZ=\"" timezone "\"
7aec3683 260export TZDIR=\"" tzdata "/share/zoneinfo\"
3141a8bd 261
d3bbe992
LC
262# Tell 'modprobe' & co. where to look for modules.
263# XXX: The downside of doing it here is that when switching to a new config
264# without rebooting, this variable possibly becomes invalid.
265export LINUX_MODULE_DIRECTORY=" kernel "/lib/modules
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)))
d3bbe992
LC
329 (etc-directory #:kernel (operating-system-kernel os)
330 #:pam-services pam-services
40281c54 331 #:skeletons skeletons
548d4c13 332 #:issue (operating-system-issue os)
0b6f49ef
LC
333 #:locale (operating-system-locale os)
334 #:timezone (operating-system-timezone os)
69689380 335 #:sudoers (operating-system-sudoers os)
0b6f49ef 336 #:profile profile-drv)))
033adfe7 337
09e028f4
LC
338(define %setuid-programs
339 ;; Default set of setuid-root programs.
340 (let ((shadow (@ (gnu packages admin) shadow)))
341 (list #~(string-append #$shadow "/bin/passwd")
342 #~(string-append #$shadow "/bin/su")
69689380 343 #~(string-append #$inetutils "/bin/ping")
8a07c289
LC
344 #~(string-append #$sudo "/bin/sudo")
345 #~(string-append #$fuse "/bin/fusermount"))))
69689380
LC
346
347(define %sudoers-specification
348 ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel'
349 ;; group can do anything. See
350 ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>.
351 ;; TODO: Add a declarative API.
352 "root ALL=(ALL) ALL
353%wheel ALL=(ALL) ALL\n")
09e028f4 354
ab6a279a
LC
355(define (user-group->gexp group)
356 "Turn GROUP, a <user-group> object, into a list-valued gexp suitable for
357'active-groups'."
358 #~(list #$(user-group-name group)
359 #$(user-group-password group)
360 #$(user-group-id group)))
361
362(define (user-account->gexp account)
363 "Turn ACCOUNT, a <user-account> object, into a list-valued gexp suitable for
364'activate-users'."
365 #~`(#$(user-account-name account)
366 #$(user-account-uid account)
367 #$(user-account-group account)
368 #$(user-account-supplementary-groups account)
369 #$(user-account-comment account)
370 #$(user-account-home-directory account)
371 ,#$(user-account-shell account) ; this one is a gexp
459dd9ea
LC
372 #$(user-account-password account)
373 #$(user-account-system? account)))
ab6a279a 374
484a2b3a
LC
375(define (operating-system-activation-script os)
376 "Return the activation script for OS---i.e., the code that \"activates\" the
377stateful part of OS, including user accounts and groups, special directories,
378etc."
09e028f4
LC
379 (define %modules
380 '((guix build activation)
b4140694
LC
381 (guix build utils)
382 (guix build linux-initrd)))
09e028f4 383
55ccc388
LC
384 (define (service-activations services)
385 ;; Return the activation scripts for SERVICES.
386 (let ((gexps (filter-map service-activate services)))
387 (sequence %store-monad (map (cut gexp->file "activate-service.scm" <>)
388 gexps))))
389
ab6a279a 390 (mlet* %store-monad ((services (operating-system-services os))
55ccc388 391 (actions (service-activations services))
ab6a279a
LC
392 (etc (operating-system-etc-directory os))
393 (modules (imported-modules %modules))
394 (compiled (compiled-modules %modules))
ab6a279a 395 (accounts (operating-system-accounts os)))
09e028f4
LC
396 (define setuid-progs
397 (operating-system-setuid-programs os))
398
ab6a279a
LC
399 (define user-specs
400 (map user-account->gexp accounts))
401
402 (define groups
403 (append (operating-system-groups os)
404 (append-map service-user-groups services)))
405
406 (define group-specs
407 (map user-group->gexp groups))
408
4654439b 409 (gexp->file "activate"
4dfe6c58
LC
410 #~(begin
411 (eval-when (expand load eval)
412 ;; Make sure 'use-modules' below succeeds.
413 (set! %load-path (cons #$modules %load-path))
414 (set! %load-compiled-path
415 (cons #$compiled %load-compiled-path)))
416
417 (use-modules (guix build activation))
418
419 ;; Populate /etc.
420 (activate-etc #$etc)
421
ab6a279a
LC
422 ;; Add users and user groups.
423 (setenv "PATH"
424 (string-append #$(@ (gnu packages admin) shadow)
425 "/sbin"))
426 (activate-users+groups (list #$@user-specs)
427 (list #$@group-specs))
428
09e028f4
LC
429 ;; Activate setuid programs.
430 (activate-setuid-programs (list #$@setuid-progs))
431
55ccc388
LC
432 ;; Run the services' activation snippets.
433 ;; TODO: Use 'load-compiled'.
434 (for-each primitive-load '#$actions)
435
b4140694 436 ;; Set up /run/current-system.
484a2b3a
LC
437 (activate-current-system)))))
438
439(define (operating-system-boot-script os)
440 "Return the boot script for OS---i.e., the code started by the initrd once
441we're running in the final root."
442 (mlet* %store-monad ((services (operating-system-services os))
443 (activate (operating-system-activation-script os))
444 (dmd-conf (dmd-configuration-file services)))
445 (gexp->file "boot"
446 #~(begin
447 ;; Activate the system.
448 ;; TODO: Use 'load-compiled'.
449 (primitive-load #$activate)
450
451 ;; Keep track of the booted system.
452 (false-if-exception (delete-file "/run/booted-system"))
453 (symlink (readlink "/run/current-system")
454 "/run/booted-system")
b4140694 455
26a728eb
LC
456 ;; Close any remaining open file descriptors to be on the
457 ;; safe side. This must be the very last thing we do,
458 ;; because Guile has internal FDs such as 'sleep_pipe'
459 ;; that need to be alive.
460 (let loop ((fd 3))
461 (when (< fd 1024)
462 (false-if-exception (close-fdes fd))
463 (loop (+ 1 fd))))
464
4dfe6c58
LC
465 ;; Start dmd.
466 (execl (string-append #$dmd "/bin/dmd")
467 "dmd" "--config" #$dmd-conf)))))
2106d3fc 468
83bcd0b8
LC
469(define (operating-system-root-file-system os)
470 "Return the root file system of OS."
471 (find (match-lambda
d4c87617 472 (($ <file-system> _ _ "/") #t)
83bcd0b8
LC
473 (_ #f))
474 (operating-system-file-systems os)))
475
b4140694
LC
476(define (operating-system-initrd-file os)
477 "Return a gexp denoting the initrd file of OS."
83bcd0b8
LC
478 (define boot-file-systems
479 (filter (match-lambda
d4c87617 480 (($ <file-system> device title "/")
3c05b4bc 481 #t)
d4c87617
LC
482 (($ <file-system> device title mount-point type flags
483 options boot?)
3c05b4bc 484 boot?))
83bcd0b8
LC
485 (operating-system-file-systems os)))
486
b4140694
LC
487 (mlet %store-monad
488 ((initrd ((operating-system-initrd os) boot-file-systems)))
489 (return #~(string-append #$initrd "/initrd"))))
490
2d23e6f0
LC
491(define (kernel->grub-label kernel)
492 "Return a label for the GRUB menu entry that boots KERNEL."
493 (string-append "GNU system with "
494 (string-titlecase (package-name kernel)) " "
495 (package-version kernel)
496 " (technology preview)"))
497
fe6e3fe2
LC
498(define* (operating-system-grub.cfg os #:optional (old-entries '()))
499 "Return the GRUB configuration file for OS. Use OLD-ENTRIES to populate the
500\"old entries\" menu."
0b6f49ef 501 (mlet* %store-monad
b4140694 502 ((system (operating-system-derivation os))
83bcd0b8 503 (root-fs -> (operating-system-root-file-system os))
b4140694 504 (kernel -> (operating-system-kernel os))
033adfe7 505 (entries -> (list (menu-entry
2d23e6f0 506 (label (kernel->grub-label kernel))
033adfe7 507 (linux kernel)
f6a7b21d 508 (linux-arguments
83bcd0b8
LC
509 (list (string-append "--root="
510 (file-system-device root-fs))
b4140694
LC
511 #~(string-append "--system=" #$system)
512 #~(string-append "--load=" #$system
513 "/boot")))
514 (initrd #~(string-append #$system "/initrd"))))))
fe6e3fe2
LC
515 (grub-configuration-file (operating-system-bootloader os) entries
516 #:old-entries old-entries)))
b4140694 517
64e40dbb
LC
518(define (operating-system-parameters-file os)
519 "Return a file that describes the boot parameters of OS. The primary use of
520this file is the reconstruction of GRUB menu entries for old configurations."
521 (mlet %store-monad ((initrd (operating-system-initrd-file os))
522 (root -> (operating-system-root-file-system os))
523 (label -> (kernel->grub-label
524 (operating-system-kernel os))))
525 (gexp->file "parameters"
526 #~(boot-parameters (version 0)
527 (label #$label)
528 (root-device #$(file-system-device root))
529 (kernel #$(operating-system-kernel os))
530 (initrd #$initrd)))))
531
b4140694
LC
532(define (operating-system-derivation os)
533 "Return a derivation that builds OS."
534 (mlet* %store-monad
535 ((profile (operating-system-profile os))
536 (etc (operating-system-etc-directory os))
537 (boot (operating-system-boot-script os))
538 (kernel -> (operating-system-kernel os))
64e40dbb
LC
539 (initrd (operating-system-initrd-file os))
540 (params (operating-system-parameters-file os)))
23f6056b 541 (file-union "system"
f6a7b21d 542 `(("boot" ,#~#$boot)
23f6056b 543 ("kernel" ,#~#$kernel)
64e40dbb 544 ("parameters" ,#~#$params)
b4140694 545 ("initrd" ,initrd)
23f6056b 546 ("profile" ,#~#$profile)
23f6056b 547 ("etc" ,#~#$etc)))))
033adfe7
LC
548
549;;; system.scm ends here