gnu: libxslt: Replace with 1.1.29 [fixes CVE-2016-{1683,1684}].
[jackhill/guix/guix.git] / gnu / services.scm
CommitLineData
db4fdc04 1;;; GNU Guix --- Functional package management for GNU
94af9daa 2;;; Copyright © 2015, 2016 Ludovic Courtès <ludo@gnu.org>
4d343a14 3;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
db4fdc04
LC
4;;;
5;;; This file is part of GNU Guix.
6;;;
7;;; GNU Guix is free software; you can redistribute it and/or modify it
8;;; under the terms of the GNU General Public License as published by
9;;; the Free Software Foundation; either version 3 of the License, or (at
10;;; your option) any later version.
11;;;
12;;; GNU Guix is distributed in the hope that it will be useful, but
13;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;;; GNU General Public License for more details.
16;;;
17;;; You should have received a copy of the GNU General Public License
18;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20(define-module (gnu services)
bebc8681 21 #:use-module (guix gexp)
0adfe95a
LC
22 #:use-module (guix monads)
23 #:use-module (guix store)
db4fdc04 24 #:use-module (guix records)
af4c3fd5 25 #:use-module (guix profiles)
0adfe95a
LC
26 #:use-module (guix sets)
27 #:use-module (guix ui)
28 #:use-module (gnu packages base)
29 #:use-module (gnu packages bash)
30 #:use-module (srfi srfi-1)
31 #:use-module (srfi srfi-9)
32 #:use-module (srfi srfi-9 gnu)
33 #:use-module (srfi srfi-26)
34 #:use-module (srfi srfi-34)
35 #:use-module (srfi srfi-35)
36 #:use-module (ice-9 vlist)
37 #:use-module (ice-9 match)
38 #:export (service-extension
39 service-extension?
40
41 service-type
42 service-type?
5152d13b
LC
43 service-type-name
44 service-type-extensions
45 service-type-compose
46 service-type-extend
0adfe95a 47
db4fdc04 48 service
0adfe95a
LC
49 service?
50 service-kind
51 service-parameters
52
cd6f6c22 53 modify-services
5152d13b 54 service-back-edges
0adfe95a
LC
55 fold-services
56
57 service-error?
58 missing-target-service-error?
59 missing-target-service-error-service
60 missing-target-service-error-target-type
61 ambiguous-target-service-error?
62 ambiguous-target-service-error-service
63 ambiguous-target-service-error-target-type
64
d62e201c 65 system-service-type
0adfe95a 66 boot-service-type
be7be9e8 67 cleanup-service-type
0adfe95a
LC
68 activation-service-type
69 activation-service->script
a241a7ac 70 %linux-bare-metal-service
0adfe95a
LC
71 etc-service-type
72 etc-directory
73 setuid-program-service-type
af4c3fd5 74 profile-service-type
0adfe95a
LC
75 firmware-service-type
76
77 %boot-service
78 %activation-service
79 etc-service
80
81 file-union)) ;XXX: for lack of a better place
82
83;;; Comment:
84;;;
85;;; This module defines a broad notion of "service types" and "services."
db4fdc04 86;;;
0adfe95a
LC
87;;; A service type describe how its instances extend instances of other
88;;; service types. For instance, some services extend the instance of
89;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create;
d4053c71
AK
90;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of
91;;; <shepherd-service>.
0adfe95a
LC
92;;;
93;;; When applicable, the service type defines how it can itself be extended,
94;;; by providing one procedure to compose extensions, and one procedure to
95;;; extend itself.
96;;;
d62e201c
LC
97;;; A notable service type is SYSTEM-SERVICE-TYPE, which has a single
98;;; instance, which is the root of the service DAG. Its value is the
99;;; derivation that produces the 'system' directory as returned by
100;;; 'operating-system-derivation'.
0adfe95a
LC
101;;;
102;;; The 'fold-services' procedure can be passed a list of procedures, which it
103;;; "folds" by propagating extensions down the graph; it returns the root
104;;; service after the applying all its extensions.
db4fdc04
LC
105;;;
106;;; Code:
107
0adfe95a
LC
108(define-record-type <service-extension>
109 (service-extension target compute)
110 service-extension?
111 (target service-extension-target) ;<service-type>
112 (compute service-extension-compute)) ;params -> params
113
114(define-record-type* <service-type> service-type make-service-type
115 service-type?
116 (name service-type-name) ;symbol (for debugging)
117
118 ;; Things extended by services of this type.
119 (extensions service-type-extensions) ;list of <service-extensions>
120
121 ;; Given a list of extensions, "compose" them.
122 (compose service-type-compose ;list of Any -> Any
123 (default #f))
124
125 ;; Extend the services' own parameters with the extension composition.
126 (extend service-type-extend ;list of Any -> parameters
127 (default #f)))
128
129(define (write-service-type type port)
130 (format port "#<service-type ~a ~a>"
131 (service-type-name type)
132 (number->string (object-address type) 16)))
133
134(set-record-type-printer! <service-type> write-service-type)
135
136;; Services of a given type.
137(define-record-type <service>
138 (service type parameters)
db4fdc04 139 service?
0adfe95a
LC
140 (type service-kind)
141 (parameters service-parameters))
142
143
cd6f6c22
LC
144(define-syntax %modify-service
145 (syntax-rules (=>)
146 ((_ service)
147 service)
148 ((_ svc (kind param => exp ...) clauses ...)
149 (if (eq? (service-kind svc) kind)
150 (let ((param (service-parameters svc)))
151 (service (service-kind svc)
152 (begin exp ...)))
153 (%modify-service svc clauses ...)))))
154
155(define-syntax modify-services
156 (syntax-rules ()
4d343a14
CM
157 "Modify the services listed in SERVICES according to CLAUSES and return
158the resulting list of services. Each clause must have the form:
cd6f6c22
LC
159
160 (TYPE VARIABLE => BODY)
161
162where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
163identifier that is bound within BODY to the value of the service of that
164TYPE. Consider this example:
165
166 (modify-services %base-services
167 (guix-service-type config =>
168 (guix-configuration
169 (inherit config)
170 (use-substitutes? #f)
171 (extra-options '(\"--gc-keep-derivations\"))))
172 (mingetty-service-type config =>
173 (mingetty-configuration
174 (inherit config)
175 (motd (plain-file \"motd\" \"Hi there!\")))))
176
177It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
178all the MINGETTY-SERVICE-TYPE instances.
179
180This is a shorthand for (map (lambda (svc) ...) %base-services)."
181 ((_ services clauses ...)
182 (map (lambda (service)
183 (%modify-service service clauses ...))
184 services))))
0adfe95a
LC
185
186\f
187;;;
188;;; Core services.
189;;;
190
d62e201c
LC
191(define (system-derivation mentries mextensions)
192 "Return as a monadic value the derivation of the 'system' directory
193containing the given entries."
194 (mlet %store-monad ((entries mentries)
195 (extensions (sequence %store-monad mextensions)))
196 (lower-object
197 (file-union "system"
198 (append entries (concatenate extensions))))))
199
200(define system-service-type
201 ;; This is the ultimate service type, the root of the service DAG. The
202 ;; service of this type is extended by monadic name/item pairs. These items
203 ;; end up in the "system directory" as returned by
204 ;; 'operating-system-derivation'.
205 (service-type (name 'system)
206 (extensions '())
207 (compose identity)
208 (extend system-derivation)))
209
3a391e68 210(define (compute-boot-script _ mexps)
be7be9e8 211 (mlet %store-monad ((gexps (sequence %store-monad mexps)))
0adfe95a 212 (gexp->file "boot"
d4053c71 213 ;; Clean up and activate the system, then spawn shepherd.
be7be9e8 214 #~(begin #$@gexps))))
0adfe95a 215
d62e201c
LC
216(define (boot-script-entry mboot)
217 "Return, as a monadic value, an entry for the boot script in the system
218directory."
219 (mlet %store-monad ((boot mboot))
220 (return `(("boot" ,boot)))))
221
0adfe95a
LC
222(define boot-service-type
223 ;; The service of this type is extended by being passed gexps as monadic
224 ;; values. It aggregates them in a single script, as a monadic value, which
225 ;; becomes its 'parameters'. It is the only service that extends nothing.
226 (service-type (name 'boot)
d62e201c
LC
227 (extensions
228 (list (service-extension system-service-type
229 boot-script-entry)))
3a391e68
LC
230 (compose append)
231 (extend compute-boot-script)))
0adfe95a
LC
232
233(define %boot-service
d62e201c 234 ;; The service that produces the boot script.
0adfe95a 235 (service boot-service-type #t))
be7be9e8
LC
236
237(define (cleanup-gexp _)
238 "Return as a monadic value a gexp to clean up /tmp and similar places upon
239boot."
240 (define %modules
241 '((guix build utils)))
242
243 (mlet %store-monad ((modules (imported-modules %modules))
244 (compiled (compiled-modules %modules)))
245 (return #~(begin
246 (eval-when (expand load eval)
247 ;; Make sure 'use-modules' below succeeds.
248 (set! %load-path (cons #$modules %load-path))
249 (set! %load-compiled-path
250 (cons #$compiled %load-compiled-path)))
251
252 (use-modules (guix build utils))
253
254 ;; Clean out /tmp and /var/run.
255 ;;
256 ;; XXX This needs to happen before service activations, so it
257 ;; has to be here, but this also implicitly assumes that /tmp
258 ;; and /var/run are on the root partition.
3c4c8c3e
LC
259 (letrec-syntax ((fail-safe (syntax-rules ()
260 ((_ exp rest ...)
261 (begin
262 (catch 'system-error
263 (lambda () exp)
264 (const #f))
265 (fail-safe rest ...)))
266 ((_)
267 #t))))
268 ;; Ignore I/O errors so the system can boot.
269 (fail-safe
270 (delete-file-recursively "/tmp")
271 (delete-file-recursively "/var/run")
272 (mkdir "/tmp")
273 (chmod "/tmp" #o1777)
274 (mkdir "/var/run")
275 (chmod "/var/run" #o755)))))))
be7be9e8
LC
276
277(define cleanup-service-type
278 ;; Service that cleans things up in /tmp and similar.
279 (service-type (name 'cleanup)
280 (extensions
281 (list (service-extension boot-service-type
282 cleanup-gexp)))))
0adfe95a
LC
283
284(define* (file-union name files) ;FIXME: Factorize.
285 "Return a <computed-file> that builds a directory containing all of FILES.
286Each item in FILES must be a list where the first element is the file name to
287use in the new directory, and the second element is a gexp denoting the target
288file."
289 (computed-file name
290 #~(begin
291 (mkdir #$output)
292 (chdir #$output)
293 #$@(map (match-lambda
294 ((target source)
37dd1e6a
LC
295 #~(begin
296 ;; Stat the source to abort early if it
297 ;; does not exist.
298 (stat #$source)
299
300 (symlink #$source #$target))))
0adfe95a
LC
301 files))))
302
303(define (directory-union name things)
304 "Return a directory that is the union of THINGS."
305 (match things
306 ((one)
307 ;; Only one thing; return it.
308 one)
309 (_
310 (computed-file name
311 #~(begin
312 (use-modules (guix build union))
313 (union-build #$output '#$things))
314 #:modules '((guix build union))))))
315
0adfe95a
LC
316(define* (activation-service->script service)
317 "Return as a monadic value the activation script for SERVICE, a service of
318ACTIVATION-SCRIPT-TYPE."
319 (activation-script (service-parameters service)))
320
321(define (activation-script gexps)
322 "Return the system's activation script, which evaluates GEXPS."
323 (define %modules
324 '((gnu build activation)
325 (gnu build linux-boot)
326 (gnu build linux-modules)
327 (gnu build file-systems)
328 (guix build utils)
329 (guix build syscalls)
6eb43907 330 (guix build bournish)
0adfe95a
LC
331 (guix elf)))
332
333 (define (service-activations)
334 ;; Return the activation scripts for SERVICES.
335 (mapm %store-monad
336 (cut gexp->file "activate-service" <>)
337 gexps))
338
339 (mlet* %store-monad ((actions (service-activations))
340 (modules (imported-modules %modules))
a241a7ac 341 (compiled (compiled-modules %modules)))
0adfe95a
LC
342 (gexp->file "activate"
343 #~(begin
344 (eval-when (expand load eval)
345 ;; Make sure 'use-modules' below succeeds.
346 (set! %load-path (cons #$modules %load-path))
347 (set! %load-compiled-path
348 (cons #$compiled %load-compiled-path)))
349
350 (use-modules (gnu build activation))
351
352 ;; Make sure /bin/sh is valid and current.
353 (activate-/bin/sh
354 (string-append #$(canonical-package bash) "/bin/sh"))
355
0adfe95a
LC
356 ;; Run the services' activation snippets.
357 ;; TODO: Use 'load-compiled'.
358 (for-each primitive-load '#$actions)
359
360 ;; Set up /run/current-system.
361 (activate-current-system)))))
362
363(define (gexps->activation-gexp gexps)
364 "Return a gexp that runs the activation script containing GEXPS."
365 (mlet %store-monad ((script (activation-script gexps)))
366 (return #~(primitive-load #$script))))
367
3a391e68
LC
368(define (second-argument a b) b)
369
0adfe95a
LC
370(define activation-service-type
371 (service-type (name 'activate)
372 (extensions
373 (list (service-extension boot-service-type
374 gexps->activation-gexp)))
375 (compose append)
376 (extend second-argument)))
377
378(define %activation-service
379 ;; The activation service produces the activation script from the gexps it
380 ;; receives.
381 (service activation-service-type #t))
382
a241a7ac
LC
383(define %modprobe-wrapper
384 ;; Wrapper for the 'modprobe' command that knows where modules live.
385 ;;
386 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
387 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
388 ;; environment variable is not set---hence the need for this wrapper.
389 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
390 (program-file "modprobe"
391 #~(begin
392 (setenv "LINUX_MODULE_DIRECTORY"
393 "/run/booted-system/kernel/lib/modules")
394 (apply execl #$modprobe
395 (cons #$modprobe (cdr (command-line))))))))
396
397(define %linux-kernel-activation
398 ;; Activation of the Linux kernel running on the bare metal (as opposed to
399 ;; running in a container.)
400 #~(begin
401 ;; Tell the kernel to use our 'modprobe' command.
402 (activate-modprobe #$%modprobe-wrapper)
403
404 ;; Let users debug their own processes!
405 (activate-ptrace-attach)))
406
407(define linux-bare-metal-service-type
408 (service-type (name 'linux-bare-metal)
409 (extensions
410 (list (service-extension activation-service-type
411 (const %linux-kernel-activation))))))
412
413(define %linux-bare-metal-service
414 ;; The service that does things that are needed on the "bare metal", but not
415 ;; necessary or impossible in a container.
416 (service linux-bare-metal-service-type #f))
417
0adfe95a
LC
418(define (etc-directory service)
419 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
420 (files->etc-directory (service-parameters service)))
421
422(define (files->etc-directory files)
423 (file-union "etc" files))
424
d62e201c
LC
425(define (etc-entry files)
426 "Return an entry for the /etc directory consisting of FILES in the system
427directory."
428 (with-monad %store-monad
429 (return `(("etc" ,(files->etc-directory files))))))
430
0adfe95a
LC
431(define etc-service-type
432 (service-type (name 'etc)
433 (extensions
434 (list
435 (service-extension activation-service-type
436 (lambda (files)
437 (let ((etc
438 (files->etc-directory files)))
d62e201c
LC
439 #~(activate-etc #$etc))))
440 (service-extension system-service-type etc-entry)))
0adfe95a
LC
441 (compose concatenate)
442 (extend append)))
443
444(define (etc-service files)
445 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
446FILES must be a list of name/file-like object pairs."
447 (service etc-service-type files))
448
449(define setuid-program-service-type
450 (service-type (name 'setuid-program)
451 (extensions
452 (list (service-extension activation-service-type
453 (lambda (programs)
454 #~(activate-setuid-programs
455 (list #$@programs))))))
456 (compose concatenate)
457 (extend append)))
458
af4c3fd5
LC
459(define (packages->profile-entry packages)
460 "Return a system entry for the profile containing PACKAGES."
461 (mlet %store-monad ((profile (profile-derivation
60a0886d
SB
462 (packages->manifest
463 (delete-duplicates packages eq?)))))
af4c3fd5
LC
464 (return `(("profile" ,profile)))))
465
466(define profile-service-type
467 ;; The service that populates the system's profile---i.e.,
468 ;; /run/current-system/profile. It is extended by package lists.
469 (service-type (name 'profile)
470 (extensions
471 (list (service-extension system-service-type
472 packages->profile-entry)))
473 (compose concatenate)
474 (extend append)))
475
0adfe95a
LC
476(define (firmware->activation-gexp firmware)
477 "Return a gexp to make the packages listed in FIRMWARE loadable by the
478kernel."
479 (let ((directory (directory-union "firmware" firmware)))
480 ;; Tell the kernel where firmware is.
481 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
482
483(define firmware-service-type
484 ;; The service that collects firmware.
485 (service-type (name 'firmware)
486 (extensions
487 (list (service-extension activation-service-type
488 firmware->activation-gexp)))
489 (compose concatenate)
490 (extend append)))
491
492\f
493;;;
494;;; Service folding.
495;;;
496
497(define-condition-type &service-error &error
498 service-error?)
499
500(define-condition-type &missing-target-service-error &service-error
501 missing-target-service-error?
502 (service missing-target-service-error-service)
503 (target-type missing-target-service-error-target-type))
504
505(define-condition-type &ambiguous-target-service-error &service-error
506 ambiguous-target-service-error?
507 (service ambiguous-target-service-error-service)
508 (target-type ambiguous-target-service-error-target-type))
509
510(define (service-back-edges services)
511 "Return a procedure that, when passed a <service>, returns the list of
512<service> objects that depend on it."
513 (define (add-edges service edges)
514 (define (add-edge extension edges)
515 (let ((target-type (service-extension-target extension)))
516 (match (filter (lambda (service)
517 (eq? (service-kind service) target-type))
518 services)
519 ((target)
520 (vhash-consq target service edges))
521 (()
522 (raise
523 (condition (&missing-target-service-error
524 (service service)
525 (target-type target-type))
526 (&message
527 (message
528 (format #f (_ "no target of type '~a' for service ~s")
529 (service-type-name target-type)
530 service))))))
531 (x
532 (raise
533 (condition (&ambiguous-target-service-error
534 (service service)
535 (target-type target-type))
536 (&message
537 (message
538 (format #f
539 (_ "more than one target service of type '~a'")
540 (service-type-name target-type))))))))))
541
542 (fold add-edge edges (service-type-extensions (service-kind service))))
543
544 (let ((edges (fold add-edges vlist-null services)))
545 (lambda (node)
546 (reverse (vhash-foldq* cons '() node edges)))))
547
d62e201c
LC
548(define* (fold-services services
549 #:key (target-type system-service-type))
0adfe95a
LC
550 "Fold SERVICES by propagating their extensions down to the root of type
551TARGET-TYPE; return the root service adjusted accordingly."
552 (define dependents
553 (service-back-edges services))
554
555 (define (matching-extension target)
556 (let ((target (service-kind target)))
557 (match-lambda
558 (($ <service-extension> type)
559 (eq? type target)))))
560
561 (define (apply-extension target)
562 (lambda (service)
563 (match (find (matching-extension target)
564 (service-type-extensions (service-kind service)))
565 (($ <service-extension> _ compute)
566 (compute (service-parameters service))))))
567
568 (match (filter (lambda (service)
569 (eq? (service-kind service) target-type))
570 services)
571 ((sink)
572 (let loop ((sink sink))
573 (let* ((dependents (map loop (dependents sink)))
574 (extensions (map (apply-extension sink) dependents))
575 (extend (service-type-extend (service-kind sink)))
576 (compose (service-type-compose (service-kind sink)))
577 (params (service-parameters sink)))
578 ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a
579 ;; different type than the elements of EXTENSIONS.
580 (if extend
581 (service (service-kind sink)
582 (extend params (compose extensions)))
583 sink))))
584 (()
585 (raise
586 (condition (&missing-target-service-error
587 (service #f)
588 (target-type target-type))
589 (&message
590 (message (format #f (_ "service of type '~a' not found")
591 (service-type-name target-type)))))))
592 (x
593 (raise
594 (condition (&ambiguous-target-service-error
595 (service #f)
596 (target-type target-type))
597 (&message
598 (message
599 (format #f
600 (_ "more than one target service of type '~a'")
601 (service-type-name target-type)))))))))
db4fdc04
LC
602
603;;; services.scm ends here.