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