system: Add default PAM entries for xlock and xscreensaver.
[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
df5ce088 73 %setuid-programs
6f436c54 74 %base-packages))
033adfe7
LC
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))
d5b429ab
LC
89 (bootloader operating-system-bootloader) ; <grub-configuration>
90
83bcd0b8 91 (initrd operating-system-initrd ; (list fs) -> M derivation
060238ae 92 (default base-initrd))
033adfe7
LC
93
94 (host-name operating-system-host-name) ; string
95
8a6d2731 96 (file-systems operating-system-file-systems) ; list of fs
033adfe7
LC
97
98 (users operating-system-users ; list of user accounts
99 (default '()))
100 (groups operating-system-groups ; list of user groups
773e956d 101 (default %base-groups))
033adfe7 102
40281c54
LC
103 (skeletons operating-system-skeletons ; list of name/monadic value
104 (default (default-skeletons)))
548d4c13
LC
105 (issue operating-system-issue ; string
106 (default %default-issue))
40281c54 107
033adfe7 108 (packages operating-system-packages ; list of (PACKAGE OUTPUT...)
6f436c54 109 (default %base-packages)) ; or just PACKAGE
033adfe7
LC
110
111 (timezone operating-system-timezone) ; string
8a6d2731
LC
112 (locale operating-system-locale ; string
113 (default "en_US.UTF-8"))
033adfe7 114
217a5b85 115 (services operating-system-user-services ; list of monadic services
09e028f4
LC
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
69689380 121 (default %setuid-programs)) ; list of string-valued gexps
033adfe7 122
69689380
LC
123 (sudoers operating-system-sudoers ; /etc/sudoers contents
124 (default %sudoers-specification)))
033adfe7 125
2717a89a 126\f
033adfe7
LC
127;;;
128;;; Derivation.
129;;;
130
23f6056b 131(define* (file-union name files)
033adfe7
LC
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
23f6056b
LC
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)))
033adfe7 144
23f6056b 145 (gexp->derivation name builder))
033adfe7 146
40281c54
LC
147\f
148;;;
149;;; Services.
150;;;
151
023f391c
LC
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
d4c87617 163 (($ <file-system> device title target type flags opts
4e469051 164 #f check? create?)
023f391c 165 (file-system-service device target type
d4c87617 166 #:title title
023f391c 167 #:check? check?
4e469051 168 #:create-mount-point? create?
2c071ce9
LC
169 #:options opts
170 #:flags flags)))
023f391c
LC
171 file-systems)))
172
217a5b85
LC
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."
023f391c
LC
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))))
217a5b85
LC
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
40281c54
LC
194\f
195;;;
196;;; /etc.
197;;;
198
6f436c54
LC
199(define %base-packages
200 ;; Default set of packages globally visible. It should include anything
201 ;; required for basic administrator tasks.
55e70e65 202 (cons* procps psmisc which less zile nano
bdb36958 203 (@ (gnu packages admin) dmd) guix
55e70e65 204 lsof ;for Guix's 'list-runtime-roots'
9b762b8d
LC
205 util-linux inetutils isc-dhcp
206 net-tools ; XXX: remove when Inetutils suffices
03e9998f
LC
207
208 ;; Get 'insmod' & co. from kmod, not module-init-tools, since udev
209 ;; already depends on it anyway.
210 kmod udev
211
b63dbd44 212 e2fsprogs kbd
9b762b8d
LC
213
214 ;; The packages below are also in %FINAL-INPUTS, so take them from
215 ;; there to avoid duplication.
216 (map canonical-package
bdb36958 217 (list guile-2.0 bash coreutils findutils grep sed))))
6f436c54 218
548d4c13
LC
219(define %default-issue
220 ;; Default contents for /etc/issue.
221 "
222This is the GNU system. Welcome.\n")
223
033adfe7 224(define* (etc-directory #:key
3141a8bd 225 (locale "C") (timezone "Europe/Paris")
548d4c13 226 (issue "Hello!\n")
40281c54 227 (skeletons '())
033adfe7 228 (pam-services '())
b4140694 229 (profile "/run/current-system/profile")
69689380 230 (sudoers ""))
033adfe7
LC
231 "Return a derivation that builds the static part of the /etc directory."
232 (mlet* %store-monad
ab6a279a 233 ((pam.d (pam-services->directory pam-services))
69689380 234 (sudoers (text-file "sudoers" sudoers))
033adfe7 235 (login.defs (text-file "login.defs" "# Empty for now.\n"))
9038298c
LC
236 (shells (text-file "shells" ; used by xterm and others
237 "\
238/bin/sh
b4140694
LC
239/run/current-system/profile/bin/sh
240/run/current-system/profile/bin/bash\n"))
548d4c13 241 (issue (text-file "issue" issue))
033adfe7
LC
242
243 ;; TODO: Generate bashrc from packages' search-paths.
7aec3683 244 (bashrc (text-file* "bashrc" "
033adfe7 245export PS1='\\u@\\h\\$ '
3141a8bd
LC
246
247export LC_ALL=\"" locale "\"
248export TZ=\"" timezone "\"
7aec3683 249export TZDIR=\"" tzdata "/share/zoneinfo\"
3141a8bd 250
d3bbe992 251# Tell 'modprobe' & co. where to look for modules.
62f0a479 252export LINUX_MODULE_DIRECTORY=/run/booted-system/kernel/lib/modules
d3bbe992 253
585c6519
LC
254export PATH=$HOME/.guix-profile/bin:/run/current-system/profile/bin
255export PATH=/run/setuid-programs:/run/current-system/profile/sbin:$PATH
033adfe7
LC
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'
40281c54
LC
260"))
261 (skel (skeleton-directory skeletons)))
23f6056b
LC
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)
40281c54 269 ("skel" ,#~#$skel)
23f6056b
LC
270 ("shells" ,#~#$shells)
271 ("profile" ,#~#$bashrc)
272 ("localtime" ,#~(string-append #$tzdata "/share/zoneinfo/"
273 #$timezone))
69689380 274 ("sudoers" ,#~#$sudoers)))))
033adfe7 275
1aa0033b 276(define (operating-system-profile os)
29fce8b6
LC
277 "Return a derivation that builds the system profile of OS."
278 (profile-derivation (manifest (map package->manifest-entry
279 (operating-system-packages os)))))
f6a9d048 280
ab6a279a
LC
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
0b6f49ef
LC
290(define (operating-system-accounts os)
291 "Return the user accounts for OS, including an obligatory 'root' account."
ab6a279a
LC
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
217a5b85 300 (mlet %store-monad ((services (operating-system-services os)))
ab6a279a
LC
301 (return (append users
302 (append-map service-user-accounts services)))))
0b6f49ef
LC
303
304(define (operating-system-etc-directory os)
305 "Return that static part of the /etc directory of OS."
033adfe7 306 (mlet* %store-monad
217a5b85 307 ((services (operating-system-services os))
033adfe7
LC
308 (pam-services ->
309 ;; Services known to PAM.
310 (delete-duplicates
09e028f4
LC
311 (append (operating-system-pam-services os)
312 (append-map service-pam-services services))))
40281c54
LC
313 (profile-drv (operating-system-profile os))
314 (skeletons (operating-system-skeletons os)))
62f0a479 315 (etc-directory #:pam-services pam-services
40281c54 316 #:skeletons skeletons
548d4c13 317 #:issue (operating-system-issue os)
0b6f49ef
LC
318 #:locale (operating-system-locale os)
319 #:timezone (operating-system-timezone os)
69689380 320 #:sudoers (operating-system-sudoers os)
0b6f49ef 321 #:profile profile-drv)))
033adfe7 322
09e028f4
LC
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")
69689380 328 #~(string-append #$inetutils "/bin/ping")
8a07c289
LC
329 #~(string-append #$sudo "/bin/sudo")
330 #~(string-append #$fuse "/bin/fusermount"))))
69689380
LC
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")
09e028f4 339
ab6a279a
LC
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)
c8fa3426
LC
345 #$(user-group-id group)
346 #$(user-group-system? group)))
ab6a279a
LC
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
459dd9ea
LC
358 #$(user-account-password account)
359 #$(user-account-system? account)))
ab6a279a 360
484a2b3a
LC
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."
09e028f4 365 (define %modules
548f7a8f 366 '((gnu build activation)
8a9e21d1 367 (gnu build linux-boot)
e2f4b305 368 (gnu build file-systems)
548f7a8f 369 (guix build utils)))
09e028f4 370
55ccc388
LC
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
ab6a279a 377 (mlet* %store-monad ((services (operating-system-services os))
55ccc388 378 (actions (service-activations services))
ab6a279a
LC
379 (etc (operating-system-etc-directory os))
380 (modules (imported-modules %modules))
381 (compiled (compiled-modules %modules))
ab6a279a 382 (accounts (operating-system-accounts os)))
09e028f4
LC
383 (define setuid-progs
384 (operating-system-setuid-programs os))
385
ab6a279a
LC
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
4654439b 396 (gexp->file "activate"
4dfe6c58
LC
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
548f7a8f 404 (use-modules (gnu build activation))
4dfe6c58
LC
405
406 ;; Populate /etc.
407 (activate-etc #$etc)
408
ab6a279a
LC
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
09e028f4
LC
416 ;; Activate setuid programs.
417 (activate-setuid-programs (list #$@setuid-progs))
418
55ccc388
LC
419 ;; Run the services' activation snippets.
420 ;; TODO: Use 'load-compiled'.
421 (for-each primitive-load '#$actions)
422
b4140694 423 ;; Set up /run/current-system.
484a2b3a
LC
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")
b4140694 442
26a728eb
LC
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
4dfe6c58
LC
452 ;; Start dmd.
453 (execl (string-append #$dmd "/bin/dmd")
454 "dmd" "--config" #$dmd-conf)))))
2106d3fc 455
83bcd0b8
LC
456(define (operating-system-root-file-system os)
457 "Return the root file system of OS."
458 (find (match-lambda
d4c87617 459 (($ <file-system> _ _ "/") #t)
83bcd0b8
LC
460 (_ #f))
461 (operating-system-file-systems os)))
462
b4140694
LC
463(define (operating-system-initrd-file os)
464 "Return a gexp denoting the initrd file of OS."
83bcd0b8
LC
465 (define boot-file-systems
466 (filter (match-lambda
d4c87617 467 (($ <file-system> device title "/")
3c05b4bc 468 #t)
d4c87617
LC
469 (($ <file-system> device title mount-point type flags
470 options boot?)
3c05b4bc 471 boot?))
83bcd0b8
LC
472 (operating-system-file-systems os)))
473
b4140694
LC
474 (mlet %store-monad
475 ((initrd ((operating-system-initrd os) boot-file-systems)))
476 (return #~(string-append #$initrd "/initrd"))))
477
2d23e6f0
LC
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
fe6e3fe2
LC
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."
0b6f49ef 488 (mlet* %store-monad
b4140694 489 ((system (operating-system-derivation os))
83bcd0b8 490 (root-fs -> (operating-system-root-file-system os))
b4140694 491 (kernel -> (operating-system-kernel os))
033adfe7 492 (entries -> (list (menu-entry
2d23e6f0 493 (label (kernel->grub-label kernel))
033adfe7 494 (linux kernel)
f6a7b21d 495 (linux-arguments
83bcd0b8
LC
496 (list (string-append "--root="
497 (file-system-device root-fs))
b4140694
LC
498 #~(string-append "--system=" #$system)
499 #~(string-append "--load=" #$system
500 "/boot")))
501 (initrd #~(string-append #$system "/initrd"))))))
fe6e3fe2
LC
502 (grub-configuration-file (operating-system-bootloader os) entries
503 #:old-entries old-entries)))
b4140694 504
64e40dbb
LC
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
b4140694
LC
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))
64e40dbb
LC
526 (initrd (operating-system-initrd-file os))
527 (params (operating-system-parameters-file os)))
23f6056b 528 (file-union "system"
f6a7b21d 529 `(("boot" ,#~#$boot)
23f6056b 530 ("kernel" ,#~#$kernel)
64e40dbb 531 ("parameters" ,#~#$params)
b4140694 532 ("initrd" ,initrd)
23f6056b 533 ("profile" ,#~#$profile)
23f6056b 534 ("etc" ,#~#$etc)))))
033adfe7
LC
535
536;;; system.scm ends here