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