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