1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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 (gnu packages base)
27 #:use-module (gnu packages bash)
28 #:use-module (gnu packages admin)
29 #:use-module (gnu packages linux)
30 #:use-module (gnu packages package-management)
31 #:use-module (gnu packages which)
32 #:use-module (gnu packages less)
33 #:use-module (gnu packages zile)
34 #:use-module (gnu services)
35 #:use-module (gnu services dmd)
36 #:use-module (gnu services base)
37 #:use-module (gnu system grub)
38 #:use-module (gnu system shadow)
39 #:use-module (gnu system linux)
40 #:use-module (gnu system linux-initrd)
41 #:use-module (gnu system file-systems)
42 #:use-module (ice-9 match)
43 #:use-module (srfi srfi-1)
44 #:use-module (srfi srfi-26)
45 #:export (operating-system
48 operating-system-bootloader
49 operating-system-services
50 operating-system-user-services
51 operating-system-packages
52 operating-system-host-name
53 operating-system-kernel
54 operating-system-initrd
55 operating-system-users
56 operating-system-groups
57 operating-system-issue
58 operating-system-packages
59 operating-system-timezone
60 operating-system-locale
61 operating-system-file-systems
63 operating-system-derivation
64 operating-system-profile
65 operating-system-grub.cfg
71 ;;; This module supports whole-system configuration.
75 ;; System-wide configuration.
76 ;; TODO: Add per-field docstrings/stexi.
77 (define-record-type* <operating-system> operating-system
80 (kernel operating-system-kernel ; package
81 (default linux-libre))
82 (bootloader operating-system-bootloader) ; <grub-configuration>
84 (initrd operating-system-initrd ; (list fs) -> M derivation
85 (default qemu-initrd))
87 (host-name operating-system-host-name) ; string
89 (file-systems operating-system-file-systems ; list of fs
92 (users operating-system-users ; list of user accounts
94 (groups operating-system-groups ; list of user groups
95 (default (list (user-group
99 (skeletons operating-system-skeletons ; list of name/monadic value
100 (default (default-skeletons)))
101 (issue operating-system-issue ; string
102 (default %default-issue))
104 (packages operating-system-packages ; list of (PACKAGE OUTPUT...)
105 (default %base-packages)) ; or just PACKAGE
107 (timezone operating-system-timezone) ; string
108 (locale operating-system-locale) ; string
110 (services operating-system-user-services ; list of monadic services
111 (default %base-services))
113 (pam-services operating-system-pam-services ; list of PAM services
114 (default (base-pam-services)))
115 (setuid-programs operating-system-setuid-programs
116 (default %setuid-programs)) ; list of string-valued gexps
118 (sudoers operating-system-sudoers ; /etc/sudoers contents
119 (default %sudoers-specification)))
126 (define* (union inputs
127 #:key (guile (%guile-for-build)) (system (%current-system))
129 "Return a derivation that builds the union of INPUTS. INPUTS is a list of
133 (use-modules (guix build union))
135 (define inputs '#$inputs)
137 (setvbuf (current-output-port) _IOLBF)
138 (setvbuf (current-error-port) _IOLBF)
140 (format #t "building union `~a' with ~a packages...~%"
141 #$output (length inputs))
142 (union-build #$output inputs)))
144 (gexp->derivation name builder
146 #:modules '((guix build union))
147 #:guile-for-build guile
150 (define* (file-union name files)
151 "Return a derivation that builds a directory containing all of FILES. Each
152 item in FILES must be a list where the first element is the file name to use
153 in the new directory, and the second element is a gexp denoting the target
159 #$@(map (match-lambda
161 #~(symlink #$source #$target)))
164 (gexp->derivation name builder))
171 (define (other-file-system-services os)
172 "Return file system services for the file systems of OS that are not marked
173 as 'needed-for-boot'."
176 (or (file-system-needed-for-boot? fs)
177 (string=? "/" (file-system-mount-point fs))))
178 (operating-system-file-systems os)))
180 (sequence %store-monad
182 (($ <file-system> device title target type flags opts
184 (file-system-service device target type
190 (define (essential-services os)
191 "Return the list of essential services for OS. These are special services
192 that implement part of what's declared in OS are responsible for low-level
194 (mlet* %store-monad ((root-fs (root-file-system-service))
195 (other-fs (other-file-system-services os))
196 (procs (user-processes-service
197 (map (compose first service-provision)
199 (host-name (host-name-service
200 (operating-system-host-name os))))
201 (return (cons* host-name procs root-fs other-fs))))
203 (define (operating-system-services os)
204 "Return all the services of OS, including \"internal\" services that do not
205 explicitly appear in OS."
207 ((user (sequence %store-monad (operating-system-user-services os)))
208 (essential (essential-services os)))
209 (return (append essential user))))
216 (define %base-packages
217 ;; Default set of packages globally visible. It should include anything
218 ;; required for basic administrator tasks.
219 (list bash coreutils findutils grep sed
220 procps psmisc less zile
221 guile-final (@ (gnu packages admin) dmd) guix
222 util-linux inetutils isc-dhcp
223 net-tools ; XXX: remove when Inetutils suffices
224 module-init-tools kbd))
226 (define %default-issue
227 ;; Default contents for /etc/issue.
229 This is the GNU system. Welcome.\n")
231 (define* (etc-directory #:key
232 (locale "C") (timezone "Europe/Paris")
236 (profile "/run/current-system/profile")
238 "Return a derivation that builds the static part of the /etc directory."
240 ((pam.d (pam-services->directory pam-services))
241 (sudoers (text-file "sudoers" sudoers))
242 (login.defs (text-file "login.defs" "# Empty for now.\n"))
243 (shells (text-file "shells" ; used by xterm and others
246 /run/current-system/profile/bin/sh
247 /run/current-system/profile/bin/bash\n"))
248 (issue (text-file "issue" issue))
250 ;; TODO: Generate bashrc from packages' search-paths.
251 (bashrc (text-file* "bashrc" "
252 export PS1='\\u@\\h\\$ '
254 export LC_ALL=\"" locale "\"
255 export TZ=\"" timezone "\"
256 export TZDIR=\"" tzdata "/share/zoneinfo\"
258 export PATH=$HOME/.guix-profile/bin:/run/current-system/profile/bin
259 export PATH=/run/setuid-programs:/run/current-system/profile/sbin:$PATH
260 export CPATH=$HOME/.guix-profile/include:" profile "/include
261 export LIBRARY_PATH=$HOME/.guix-profile/lib:" profile "/lib
262 alias ls='ls -p --color'
265 (skel (skeleton-directory skeletons)))
267 `(("services" ,#~(string-append #$net-base "/etc/services"))
268 ("protocols" ,#~(string-append #$net-base "/etc/protocols"))
269 ("rpc" ,#~(string-append #$net-base "/etc/rpc"))
271 ("login.defs" ,#~#$login.defs)
274 ("shells" ,#~#$shells)
275 ("profile" ,#~#$bashrc)
276 ("localtime" ,#~(string-append #$tzdata "/share/zoneinfo/"
278 ("sudoers" ,#~#$sudoers)))))
280 (define (operating-system-profile os)
281 "Return a derivation that builds the default profile of OS."
282 ;; TODO: Replace with a real profile with a manifest.
283 (union (operating-system-packages os)
284 #:name "default-profile"))
286 (define %root-account
287 ;; Default root account.
291 (uid 0) (group "root")
292 (comment "System administrator")
293 (home-directory "/root")))
295 (define (operating-system-accounts os)
296 "Return the user accounts for OS, including an obligatory 'root' account."
298 ;; Make sure there's a root account.
299 (if (find (lambda (user)
300 (and=> (user-account-uid user) zero?))
301 (operating-system-users os))
302 (operating-system-users os)
303 (cons %root-account (operating-system-users os))))
305 (mlet %store-monad ((services (operating-system-services os)))
306 (return (append users
307 (append-map service-user-accounts services)))))
309 (define (operating-system-etc-directory os)
310 "Return that static part of the /etc directory of OS."
312 ((services (operating-system-services os))
314 ;; Services known to PAM.
316 (append (operating-system-pam-services os)
317 (append-map service-pam-services services))))
318 (profile-drv (operating-system-profile os))
319 (skeletons (operating-system-skeletons os)))
320 (etc-directory #:pam-services pam-services
321 #:skeletons skeletons
322 #:issue (operating-system-issue os)
323 #:locale (operating-system-locale os)
324 #:timezone (operating-system-timezone os)
325 #:sudoers (operating-system-sudoers os)
326 #:profile profile-drv)))
328 (define %setuid-programs
329 ;; Default set of setuid-root programs.
330 (let ((shadow (@ (gnu packages admin) shadow)))
331 (list #~(string-append #$shadow "/bin/passwd")
332 #~(string-append #$shadow "/bin/su")
333 #~(string-append #$inetutils "/bin/ping")
334 #~(string-append #$sudo "/bin/sudo")
335 #~(string-append #$fuse "/bin/fusermount"))))
337 (define %sudoers-specification
338 ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel'
339 ;; group can do anything. See
340 ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>.
341 ;; TODO: Add a declarative API.
343 %wheel ALL=(ALL) ALL\n")
345 (define (user-group->gexp group)
346 "Turn GROUP, a <user-group> object, into a list-valued gexp suitable for
348 #~(list #$(user-group-name group)
349 #$(user-group-password group)
350 #$(user-group-id group)))
352 (define (user-account->gexp account)
353 "Turn ACCOUNT, a <user-account> object, into a list-valued gexp suitable for
355 #~`(#$(user-account-name account)
356 #$(user-account-uid account)
357 #$(user-account-group account)
358 #$(user-account-supplementary-groups account)
359 #$(user-account-comment account)
360 #$(user-account-home-directory account)
361 ,#$(user-account-shell account) ; this one is a gexp
362 #$(user-account-password account)))
364 (define (operating-system-activation-script os)
365 "Return the activation script for OS---i.e., the code that \"activates\" the
366 stateful part of OS, including user accounts and groups, special directories,
369 '((guix build activation)
371 (guix build linux-initrd)))
373 (define (service-activations services)
374 ;; Return the activation scripts for SERVICES.
375 (let ((gexps (filter-map service-activate services)))
376 (sequence %store-monad (map (cut gexp->file "activate-service.scm" <>)
379 (mlet* %store-monad ((services (operating-system-services os))
380 (actions (service-activations services))
381 (etc (operating-system-etc-directory os))
382 (modules (imported-modules %modules))
383 (compiled (compiled-modules %modules))
384 (accounts (operating-system-accounts os)))
386 (operating-system-setuid-programs os))
389 (map user-account->gexp accounts))
392 (append (operating-system-groups os)
393 (append-map service-user-groups services)))
396 (map user-group->gexp groups))
400 (eval-when (expand load eval)
401 ;; Make sure 'use-modules' below succeeds.
402 (set! %load-path (cons #$modules %load-path))
403 (set! %load-compiled-path
404 (cons #$compiled %load-compiled-path)))
406 (use-modules (guix build activation))
411 ;; Add users and user groups.
413 (string-append #$(@ (gnu packages admin) shadow)
415 (activate-users+groups (list #$@user-specs)
416 (list #$@group-specs))
418 ;; Activate setuid programs.
419 (activate-setuid-programs (list #$@setuid-progs))
421 ;; Run the services' activation snippets.
422 ;; TODO: Use 'load-compiled'.
423 (for-each primitive-load '#$actions)
425 ;; Set up /run/current-system.
426 (activate-current-system)))))
428 (define (operating-system-boot-script os)
429 "Return the boot script for OS---i.e., the code started by the initrd once
430 we're running in the final root."
431 (mlet* %store-monad ((services (operating-system-services os))
432 (activate (operating-system-activation-script os))
433 (dmd-conf (dmd-configuration-file services)))
436 ;; Activate the system.
437 ;; TODO: Use 'load-compiled'.
438 (primitive-load #$activate)
440 ;; Keep track of the booted system.
441 (false-if-exception (delete-file "/run/booted-system"))
442 (symlink (readlink "/run/current-system")
443 "/run/booted-system")
445 ;; Close any remaining open file descriptors to be on the
446 ;; safe side. This must be the very last thing we do,
447 ;; because Guile has internal FDs such as 'sleep_pipe'
448 ;; that need to be alive.
451 (false-if-exception (close-fdes fd))
455 (execl (string-append #$dmd "/bin/dmd")
456 "dmd" "--config" #$dmd-conf)))))
458 (define (operating-system-root-file-system os)
459 "Return the root file system of OS."
461 (($ <file-system> _ _ "/") #t)
463 (operating-system-file-systems os)))
465 (define (operating-system-initrd-file os)
466 "Return a gexp denoting the initrd file of OS."
467 (define boot-file-systems
468 (filter (match-lambda
469 (($ <file-system> device title "/")
471 (($ <file-system> device title mount-point type flags
474 (operating-system-file-systems os)))
477 ((initrd ((operating-system-initrd os) boot-file-systems)))
478 (return #~(string-append #$initrd "/initrd"))))
480 (define (operating-system-grub.cfg os)
481 "Return the GRUB configuration file for OS."
483 ((system (operating-system-derivation os))
484 (root-fs -> (operating-system-root-file-system os))
485 (kernel -> (operating-system-kernel os))
486 (entries -> (list (menu-entry
487 (label (string-append
489 (package-full-name kernel)
490 " (technology preview)"))
493 (list (string-append "--root="
494 (file-system-device root-fs))
495 #~(string-append "--system=" #$system)
496 #~(string-append "--load=" #$system
498 (initrd #~(string-append #$system "/initrd"))))))
499 (grub-configuration-file (operating-system-bootloader os) entries)))
501 (define (operating-system-derivation os)
502 "Return a derivation that builds OS."
504 ((profile (operating-system-profile os))
505 (etc (operating-system-etc-directory os))
506 (boot (operating-system-boot-script os))
507 (kernel -> (operating-system-kernel os))
508 (initrd (operating-system-initrd-file os)))
511 ("kernel" ,#~#$kernel)
513 ("profile" ,#~#$profile)
516 ;;; system.scm ends here