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