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