system: Export 'local-host-aliases'.
[jackhill/guix/guix.git] / gnu / system.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
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)
22 #:use-module (guix gexp)
23 #:use-module (guix records)
24 #:use-module (guix packages)
25 #:use-module (guix derivations)
26 #:use-module (guix profiles)
27 #:use-module (gnu packages base)
28 #:use-module (gnu packages bash)
29 #:use-module (gnu packages guile)
30 #:use-module (gnu packages which)
31 #:use-module (gnu packages admin)
32 #:use-module (gnu packages linux)
33 #:use-module (gnu packages package-management)
34 #:use-module (gnu packages which)
35 #:use-module (gnu packages less)
36 #:use-module (gnu packages zile)
37 #:use-module (gnu packages nano)
38 #:use-module (gnu packages lsof)
39 #:use-module (gnu packages gawk)
40 #:use-module (gnu packages compression)
41 #:autoload (gnu packages cryptsetup) (cryptsetup)
42 #:use-module (gnu services)
43 #:use-module (gnu services dmd)
44 #:use-module (gnu services base)
45 #:use-module (gnu system grub)
46 #:use-module (gnu system shadow)
47 #:use-module (gnu system linux)
48 #:use-module (gnu system linux-initrd)
49 #:use-module (gnu system file-systems)
50 #:use-module (ice-9 match)
51 #:use-module (srfi srfi-1)
52 #:use-module (srfi srfi-26)
53 #:export (operating-system
54 operating-system?
55
56 operating-system-bootloader
57 operating-system-services
58 operating-system-user-services
59 operating-system-packages
60 operating-system-host-name
61 operating-system-hosts-file
62 operating-system-kernel
63 operating-system-initrd
64 operating-system-users
65 operating-system-groups
66 operating-system-issue
67 operating-system-packages
68 operating-system-timezone
69 operating-system-locale
70 operating-system-mapped-devices
71 operating-system-file-systems
72 operating-system-activation-script
73
74 operating-system-derivation
75 operating-system-profile
76 operating-system-grub.cfg
77
78 local-host-aliases
79 %setuid-programs
80 %base-packages
81
82 luks-device-mapping))
83
84 ;;; Commentary:
85 ;;;
86 ;;; This module supports whole-system configuration.
87 ;;;
88 ;;; Code:
89
90 ;; System-wide configuration.
91 ;; TODO: Add per-field docstrings/stexi.
92 (define-record-type* <operating-system> operating-system
93 make-operating-system
94 operating-system?
95 (kernel operating-system-kernel ; package
96 (default linux-libre))
97 (bootloader operating-system-bootloader) ; <grub-configuration>
98
99 (initrd operating-system-initrd ; (list fs) -> M derivation
100 (default base-initrd))
101
102 (host-name operating-system-host-name) ; string
103 (hosts-file operating-system-hosts-file ; M item | #f
104 (default #f))
105
106 (mapped-devices operating-system-mapped-devices ; list of <mapped-device>
107 (default '()))
108 (file-systems operating-system-file-systems) ; list of fs
109 (swap-devices operating-system-swap-devices ; list of strings
110 (default '()))
111
112 (users operating-system-users ; list of user accounts
113 (default '()))
114 (groups operating-system-groups ; list of user groups
115 (default %base-groups))
116
117 (skeletons operating-system-skeletons ; list of name/monadic value
118 (default (default-skeletons)))
119 (issue operating-system-issue ; string
120 (default %default-issue))
121
122 (packages operating-system-packages ; list of (PACKAGE OUTPUT...)
123 (default %base-packages)) ; or just PACKAGE
124
125 (timezone operating-system-timezone) ; string
126 (locale operating-system-locale ; string
127 (default "en_US.UTF-8"))
128
129 (services operating-system-user-services ; list of monadic services
130 (default %base-services))
131
132 (pam-services operating-system-pam-services ; list of PAM services
133 (default (base-pam-services)))
134 (setuid-programs operating-system-setuid-programs
135 (default %setuid-programs)) ; list of string-valued gexps
136
137 (sudoers operating-system-sudoers ; /etc/sudoers contents
138 (default %sudoers-specification)))
139
140 \f
141 ;;;
142 ;;; Derivation.
143 ;;;
144
145 (define* (file-union name files)
146 "Return a derivation that builds a directory containing all of FILES. Each
147 item in FILES must be a list where the first element is the file name to use
148 in the new directory, and the second element is a gexp denoting the target
149 file."
150 (define builder
151 #~(begin
152 (mkdir #$output)
153 (chdir #$output)
154 #$@(map (match-lambda
155 ((target source)
156 #~(symlink #$source #$target)))
157 files)))
158
159 (gexp->derivation name builder))
160
161 \f
162 ;;;
163 ;;; Services.
164 ;;;
165
166 (define (open-luks-device source target)
167 "Return a gexp that maps SOURCE to TARGET as a LUKS device, using
168 'cryptsetup'."
169 #~(zero? (system* (string-append #$cryptsetup "/sbin/cryptsetup")
170 "open" "--type" "luks"
171 #$source #$target)))
172
173 (define (close-luks-device source target)
174 "Return a gexp that closes TARGET, a LUKS device."
175 #~(zero? (system* (string-append #$cryptsetup "/sbin/cryptsetup")
176 "close" #$target)))
177
178 (define luks-device-mapping
179 ;; The type of LUKS mapped devices.
180 (mapped-device-kind
181 (open open-luks-device)
182 (close close-luks-device)))
183
184 (define (other-file-system-services os)
185 "Return file system services for the file systems of OS that are not marked
186 as 'needed-for-boot'."
187 (define file-systems
188 (remove (lambda (fs)
189 (or (file-system-needed-for-boot? fs)
190 (string=? "/" (file-system-mount-point fs))))
191 (operating-system-file-systems os)))
192
193 (define (device-mappings fs)
194 (filter (lambda (md)
195 (string=? (string-append "/dev/mapper/"
196 (mapped-device-target md))
197 (file-system-device fs)))
198 (operating-system-mapped-devices os)))
199
200 (define (requirements fs)
201 (map (lambda (md)
202 (symbol-append 'device-mapping-
203 (string->symbol (mapped-device-target md))))
204 (device-mappings fs)))
205
206 (sequence %store-monad
207 (map (lambda (fs)
208 (match fs
209 (($ <file-system> device title target type flags opts
210 #f check? create?)
211 (file-system-service device target type
212 #:title title
213 #:requirements (requirements fs)
214 #:check? check?
215 #:create-mount-point? create?
216 #:options opts
217 #:flags flags))))
218 file-systems)))
219
220 (define (mapped-device-user device file-systems)
221 "Return a file system among FILE-SYSTEMS that uses DEVICE, or #f."
222 (let ((target (string-append "/dev/mapper/" (mapped-device-target device))))
223 (find (lambda (fs)
224 (string=? (file-system-device fs) target))
225 file-systems)))
226
227 (define (operating-system-user-mapped-devices os)
228 "Return the subset of mapped devices that can be installed in
229 user-land--i.e., those not needed during boot."
230 (let ((devices (operating-system-mapped-devices os))
231 (file-systems (operating-system-file-systems os)))
232 (filter (lambda (md)
233 (let ((user (mapped-device-user md file-systems)))
234 (or (not user)
235 (not (file-system-needed-for-boot? user)))))
236 devices)))
237
238 (define (operating-system-boot-mapped-devices os)
239 "Return the subset of mapped devices that must be installed during boot,
240 from the initrd."
241 (let ((devices (operating-system-mapped-devices os))
242 (file-systems (operating-system-file-systems os)))
243 (filter (lambda (md)
244 (let ((user (mapped-device-user md file-systems)))
245 (and user (file-system-needed-for-boot? user))))
246 devices)))
247
248 (define (device-mapping-services os)
249 "Return the list of device-mapping services for OS as a monadic list."
250 (sequence %store-monad
251 (map (lambda (md)
252 (let* ((source (mapped-device-source md))
253 (target (mapped-device-target md))
254 (type (mapped-device-type md))
255 (open (mapped-device-kind-open type))
256 (close (mapped-device-kind-close type)))
257 (device-mapping-service target
258 (open source target)
259 (close source target))))
260 (operating-system-user-mapped-devices os))))
261
262 (define (swap-services os)
263 "Return the list of swap services for OS as a monadic list."
264 (sequence %store-monad
265 (map swap-service (operating-system-swap-devices os))))
266
267 (define (essential-services os)
268 "Return the list of essential services for OS. These are special services
269 that implement part of what's declared in OS are responsible for low-level
270 bookkeeping."
271 (mlet* %store-monad ((mappings (device-mapping-services os))
272 (root-fs (root-file-system-service))
273 (other-fs (other-file-system-services os))
274 (swaps (swap-services os))
275 (procs (user-processes-service
276 (map (compose first service-provision)
277 other-fs)))
278 (host-name (host-name-service
279 (operating-system-host-name os))))
280 (return (cons* host-name procs root-fs
281 (append other-fs mappings swaps)))))
282
283 (define (operating-system-services os)
284 "Return all the services of OS, including \"internal\" services that do not
285 explicitly appear in OS."
286 (mlet %store-monad
287 ((user (sequence %store-monad (operating-system-user-services os)))
288 (essential (essential-services os)))
289 (return (append essential user))))
290
291 \f
292 ;;;
293 ;;; /etc.
294 ;;;
295
296 (define %base-packages
297 ;; Default set of packages globally visible. It should include anything
298 ;; required for basic administrator tasks.
299 (cons* procps psmisc which less zile nano
300 (@ (gnu packages admin) dmd) guix
301 lsof ;for Guix's 'list-runtime-roots'
302 util-linux inetutils isc-dhcp wireless-tools
303 net-tools ; XXX: remove when Inetutils suffices
304
305 ;; Get 'insmod' & co. from kmod, not module-init-tools, since udev
306 ;; already depends on it anyway.
307 kmod eudev
308
309 e2fsprogs kbd
310
311 ;; The packages below are also in %FINAL-INPUTS, so take them from
312 ;; there to avoid duplication.
313 (map canonical-package
314 (list guile-2.0 bash coreutils findutils grep sed
315 diffutils patch gawk tar gzip bzip2 xz lzip))))
316
317 (define %default-issue
318 ;; Default contents for /etc/issue.
319 "
320 This is the GNU system. Welcome.\n")
321
322 (define (local-host-aliases host-name)
323 "Return aliases for HOST-NAME, to be used in /etc/hosts."
324 (string-append "127.0.0.1 localhost " host-name "\n"
325 "::1 localhost " host-name "\n"))
326
327 (define (default-/etc/hosts host-name)
328 "Return the default /etc/hosts file."
329 (text-file "hosts" (local-host-aliases host-name)))
330
331 (define* (etc-directory #:key
332 (locale "C") (timezone "Europe/Paris")
333 (issue "Hello!\n")
334 (skeletons '())
335 (pam-services '())
336 (profile "/run/current-system/profile")
337 hosts-file
338 (sudoers ""))
339 "Return a derivation that builds the static part of the /etc directory."
340 (mlet* %store-monad
341 ((pam.d (pam-services->directory pam-services))
342 (sudoers (text-file "sudoers" sudoers))
343 (login.defs (text-file "login.defs" "# Empty for now.\n"))
344 (shells (text-file "shells" ; used by xterm and others
345 "\
346 /bin/sh
347 /run/current-system/profile/bin/sh
348 /run/current-system/profile/bin/bash\n"))
349 (issue (text-file "issue" issue))
350
351 ;; For now, generate a basic config so that /etc/hosts is honored.
352 (nsswitch (text-file "nsswitch.conf"
353 "hosts: files dns\n"))
354
355 ;; TODO: Generate bashrc from packages' search-paths.
356 (bashrc (text-file* "bashrc" "
357 export PS1='\\u@\\h \\w\\$ '
358
359 export LC_ALL=\"" locale "\"
360 export TZ=\"" timezone "\"
361 export TZDIR=\"" tzdata "/share/zoneinfo\"
362
363 # Tell 'modprobe' & co. where to look for modules.
364 export LINUX_MODULE_DIRECTORY=/run/booted-system/kernel/lib/modules
365
366 export PATH=$HOME/.guix-profile/bin:/run/current-system/profile/bin
367 export PATH=/run/setuid-programs:/run/current-system/profile/sbin:$PATH
368 export CPATH=$HOME/.guix-profile/include:" profile "/include
369 export LIBRARY_PATH=$HOME/.guix-profile/lib:" profile "/lib
370 alias ls='ls -p --color'
371 alias ll='ls -l'
372 "))
373 (skel (skeleton-directory skeletons)))
374 (file-union "etc"
375 `(("services" ,#~(string-append #$net-base "/etc/services"))
376 ("protocols" ,#~(string-append #$net-base "/etc/protocols"))
377 ("rpc" ,#~(string-append #$net-base "/etc/rpc"))
378 ("pam.d" ,#~#$pam.d)
379 ("login.defs" ,#~#$login.defs)
380 ("issue" ,#~#$issue)
381 ("nsswitch.conf" ,#~#$nsswitch)
382 ("skel" ,#~#$skel)
383 ("shells" ,#~#$shells)
384 ("profile" ,#~#$bashrc)
385 ("hosts" ,#~#$hosts-file)
386 ("localtime" ,#~(string-append #$tzdata "/share/zoneinfo/"
387 #$timezone))
388 ("sudoers" ,#~#$sudoers)))))
389
390 (define (operating-system-profile os)
391 "Return a derivation that builds the system profile of OS."
392 (profile-derivation (manifest (map package->manifest-entry
393 (operating-system-packages os)))))
394
395 (define %root-account
396 ;; Default root account.
397 (user-account
398 (name "root")
399 (password "")
400 (uid 0) (group "root")
401 (comment "System administrator")
402 (home-directory "/root")))
403
404 (define (operating-system-accounts os)
405 "Return the user accounts for OS, including an obligatory 'root' account."
406 (define users
407 ;; Make sure there's a root account.
408 (if (find (lambda (user)
409 (and=> (user-account-uid user) zero?))
410 (operating-system-users os))
411 (operating-system-users os)
412 (cons %root-account (operating-system-users os))))
413
414 (mlet %store-monad ((services (operating-system-services os)))
415 (return (append users
416 (append-map service-user-accounts services)))))
417
418 (define (operating-system-etc-directory os)
419 "Return that static part of the /etc directory of OS."
420 (mlet* %store-monad
421 ((services (operating-system-services os))
422 (pam-services ->
423 ;; Services known to PAM.
424 (delete-duplicates
425 (append (operating-system-pam-services os)
426 (append-map service-pam-services services))))
427 (profile-drv (operating-system-profile os))
428 (skeletons (operating-system-skeletons os))
429 (/etc/hosts (or (operating-system-hosts-file os)
430 (default-/etc/hosts (operating-system-host-name os)))))
431 (etc-directory #:pam-services pam-services
432 #:skeletons skeletons
433 #:issue (operating-system-issue os)
434 #:locale (operating-system-locale os)
435 #:timezone (operating-system-timezone os)
436 #:hosts-file /etc/hosts
437 #:sudoers (operating-system-sudoers os)
438 #:profile profile-drv)))
439
440 (define %setuid-programs
441 ;; Default set of setuid-root programs.
442 (let ((shadow (@ (gnu packages admin) shadow)))
443 (list #~(string-append #$shadow "/bin/passwd")
444 #~(string-append #$shadow "/bin/su")
445 #~(string-append #$inetutils "/bin/ping")
446 #~(string-append #$sudo "/bin/sudo")
447 #~(string-append #$fuse "/bin/fusermount"))))
448
449 (define %sudoers-specification
450 ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel'
451 ;; group can do anything. See
452 ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>.
453 ;; TODO: Add a declarative API.
454 "root ALL=(ALL) ALL
455 %wheel ALL=(ALL) ALL\n")
456
457 (define (user-group->gexp group)
458 "Turn GROUP, a <user-group> object, into a list-valued gexp suitable for
459 'active-groups'."
460 #~(list #$(user-group-name group)
461 #$(user-group-password group)
462 #$(user-group-id group)
463 #$(user-group-system? group)))
464
465 (define (user-account->gexp account)
466 "Turn ACCOUNT, a <user-account> object, into a list-valued gexp suitable for
467 'activate-users'."
468 #~`(#$(user-account-name account)
469 #$(user-account-uid account)
470 #$(user-account-group account)
471 #$(user-account-supplementary-groups account)
472 #$(user-account-comment account)
473 #$(user-account-home-directory account)
474 ,#$(user-account-shell account) ; this one is a gexp
475 #$(user-account-password account)
476 #$(user-account-system? account)))
477
478 (define (operating-system-activation-script os)
479 "Return the activation script for OS---i.e., the code that \"activates\" the
480 stateful part of OS, including user accounts and groups, special directories,
481 etc."
482 (define %modules
483 '((gnu build activation)
484 (gnu build linux-boot)
485 (gnu build file-systems)
486 (guix build utils)))
487
488 (define (service-activations services)
489 ;; Return the activation scripts for SERVICES.
490 (let ((gexps (filter-map service-activate services)))
491 (sequence %store-monad (map (cut gexp->file "activate-service.scm" <>)
492 gexps))))
493
494 (mlet* %store-monad ((services (operating-system-services os))
495 (actions (service-activations services))
496 (etc (operating-system-etc-directory os))
497 (modules (imported-modules %modules))
498 (compiled (compiled-modules %modules))
499 (accounts (operating-system-accounts os)))
500 (define setuid-progs
501 (operating-system-setuid-programs os))
502
503 (define user-specs
504 (map user-account->gexp accounts))
505
506 (define groups
507 (append (operating-system-groups os)
508 (append-map service-user-groups services)))
509
510 (define group-specs
511 (map user-group->gexp groups))
512
513 (gexp->file "activate"
514 #~(begin
515 (eval-when (expand load eval)
516 ;; Make sure 'use-modules' below succeeds.
517 (set! %load-path (cons #$modules %load-path))
518 (set! %load-compiled-path
519 (cons #$compiled %load-compiled-path)))
520
521 (use-modules (gnu build activation))
522
523 ;; Make sure /bin/sh is valid and current.
524 (activate-/bin/sh
525 (string-append #$(canonical-package bash)
526 "/bin/sh"))
527
528 ;; Populate /etc.
529 (activate-etc #$etc)
530
531 ;; Add users and user groups.
532 (setenv "PATH"
533 (string-append #$(@ (gnu packages admin) shadow)
534 "/sbin"))
535 (activate-users+groups (list #$@user-specs)
536 (list #$@group-specs))
537
538 ;; Activate setuid programs.
539 (activate-setuid-programs (list #$@setuid-progs))
540
541 ;; Run the services' activation snippets.
542 ;; TODO: Use 'load-compiled'.
543 (for-each primitive-load '#$actions)
544
545 ;; Set up /run/current-system.
546 (activate-current-system)))))
547
548 (define (operating-system-boot-script os)
549 "Return the boot script for OS---i.e., the code started by the initrd once
550 we're running in the final root."
551 (mlet* %store-monad ((services (operating-system-services os))
552 (activate (operating-system-activation-script os))
553 (dmd-conf (dmd-configuration-file services)))
554 (gexp->file "boot"
555 #~(begin
556 ;; Activate the system.
557 ;; TODO: Use 'load-compiled'.
558 (primitive-load #$activate)
559
560 ;; Keep track of the booted system.
561 (false-if-exception (delete-file "/run/booted-system"))
562 (symlink (readlink "/run/current-system")
563 "/run/booted-system")
564
565 ;; Close any remaining open file descriptors to be on the
566 ;; safe side. This must be the very last thing we do,
567 ;; because Guile has internal FDs such as 'sleep_pipe'
568 ;; that need to be alive.
569 (let loop ((fd 3))
570 (when (< fd 1024)
571 (false-if-exception (close-fdes fd))
572 (loop (+ 1 fd))))
573
574 ;; Start dmd.
575 (execl (string-append #$dmd "/bin/dmd")
576 "dmd" "--config" #$dmd-conf)))))
577
578 (define (operating-system-root-file-system os)
579 "Return the root file system of OS."
580 (find (match-lambda
581 (($ <file-system> _ _ "/") #t)
582 (_ #f))
583 (operating-system-file-systems os)))
584
585 (define (operating-system-initrd-file os)
586 "Return a gexp denoting the initrd file of OS."
587 (define boot-file-systems
588 (filter (match-lambda
589 (($ <file-system> device title "/")
590 #t)
591 (($ <file-system> device title mount-point type flags
592 options boot?)
593 boot?))
594 (operating-system-file-systems os)))
595
596 (define mapped-devices
597 (operating-system-boot-mapped-devices os))
598
599 (define make-initrd
600 (operating-system-initrd os))
601
602 (mlet %store-monad ((initrd (make-initrd boot-file-systems
603 #:mapped-devices mapped-devices)))
604 (return #~(string-append #$initrd "/initrd"))))
605
606 (define (kernel->grub-label kernel)
607 "Return a label for the GRUB menu entry that boots KERNEL."
608 (string-append "GNU system with "
609 (string-titlecase (package-name kernel)) " "
610 (package-version kernel)
611 " (technology preview)"))
612
613 (define* (operating-system-grub.cfg os #:optional (old-entries '()))
614 "Return the GRUB configuration file for OS. Use OLD-ENTRIES to populate the
615 \"old entries\" menu."
616 (mlet* %store-monad
617 ((system (operating-system-derivation os))
618 (root-fs -> (operating-system-root-file-system os))
619 (kernel -> (operating-system-kernel os))
620 (entries -> (list (menu-entry
621 (label (kernel->grub-label kernel))
622 (linux kernel)
623 (linux-arguments
624 (list (string-append "--root="
625 (file-system-device root-fs))
626 #~(string-append "--system=" #$system)
627 #~(string-append "--load=" #$system
628 "/boot")))
629 (initrd #~(string-append #$system "/initrd"))))))
630 (grub-configuration-file (operating-system-bootloader os) entries
631 #:old-entries old-entries)))
632
633 (define (operating-system-parameters-file os)
634 "Return a file that describes the boot parameters of OS. The primary use of
635 this file is the reconstruction of GRUB menu entries for old configurations."
636 (mlet %store-monad ((initrd (operating-system-initrd-file os))
637 (root -> (operating-system-root-file-system os))
638 (label -> (kernel->grub-label
639 (operating-system-kernel os))))
640 (gexp->file "parameters"
641 #~(boot-parameters (version 0)
642 (label #$label)
643 (root-device #$(file-system-device root))
644 (kernel #$(operating-system-kernel os))
645 (initrd #$initrd)))))
646
647 (define (operating-system-derivation os)
648 "Return a derivation that builds OS."
649 (mlet* %store-monad
650 ((profile (operating-system-profile os))
651 (etc (operating-system-etc-directory os))
652 (boot (operating-system-boot-script os))
653 (kernel -> (operating-system-kernel os))
654 (initrd (operating-system-initrd-file os))
655 (params (operating-system-parameters-file os)))
656 (file-union "system"
657 `(("boot" ,#~#$boot)
658 ("kernel" ,#~#$kernel)
659 ("parameters" ,#~#$params)
660 ("initrd" ,initrd)
661 ("profile" ,#~#$profile)
662 ("etc" ,#~#$etc)))))
663
664 ;;; system.scm ends here