Merge branch 'staging'
[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 ;; Set up /run/current-system. Among other things this
342 ;; sets up locales, which the activation snippets
343 ;; executed below may expect.
344 (activate-current-system)
345
346 ;; Run the services' activation snippets.
347 ;; TODO: Use 'load-compiled'.
348 (for-each primitive-load '#$actions))))))
349
350 (define (gexps->activation-gexp gexps)
351 "Return a gexp that runs the activation script containing GEXPS."
352 (mlet %store-monad ((script (activation-script gexps)))
353 (return #~(primitive-load #$script))))
354
355 (define (second-argument a b) b)
356
357 (define activation-service-type
358 (service-type (name 'activate)
359 (extensions
360 (list (service-extension boot-service-type
361 gexps->activation-gexp)))
362 (compose append)
363 (extend second-argument)))
364
365 (define %activation-service
366 ;; The activation service produces the activation script from the gexps it
367 ;; receives.
368 (service activation-service-type #t))
369
370 (define %modprobe-wrapper
371 ;; Wrapper for the 'modprobe' command that knows where modules live.
372 ;;
373 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
374 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
375 ;; environment variable is not set---hence the need for this wrapper.
376 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
377 (program-file "modprobe"
378 #~(begin
379 (setenv "LINUX_MODULE_DIRECTORY"
380 "/run/booted-system/kernel/lib/modules")
381 (apply execl #$modprobe
382 (cons #$modprobe (cdr (command-line))))))))
383
384 (define %linux-kernel-activation
385 ;; Activation of the Linux kernel running on the bare metal (as opposed to
386 ;; running in a container.)
387 #~(begin
388 ;; Tell the kernel to use our 'modprobe' command.
389 (activate-modprobe #$%modprobe-wrapper)
390
391 ;; Let users debug their own processes!
392 (activate-ptrace-attach)))
393
394 (define linux-bare-metal-service-type
395 (service-type (name 'linux-bare-metal)
396 (extensions
397 (list (service-extension activation-service-type
398 (const %linux-kernel-activation))))))
399
400 (define %linux-bare-metal-service
401 ;; The service that does things that are needed on the "bare metal", but not
402 ;; necessary or impossible in a container.
403 (service linux-bare-metal-service-type #f))
404
405 (define (etc-directory service)
406 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
407 (files->etc-directory (service-parameters service)))
408
409 (define (files->etc-directory files)
410 (file-union "etc" files))
411
412 (define (etc-entry files)
413 "Return an entry for the /etc directory consisting of FILES in the system
414 directory."
415 (with-monad %store-monad
416 (return `(("etc" ,(files->etc-directory files))))))
417
418 (define etc-service-type
419 (service-type (name 'etc)
420 (extensions
421 (list
422 (service-extension activation-service-type
423 (lambda (files)
424 (let ((etc
425 (files->etc-directory files)))
426 #~(activate-etc #$etc))))
427 (service-extension system-service-type etc-entry)))
428 (compose concatenate)
429 (extend append)))
430
431 (define (etc-service files)
432 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
433 FILES must be a list of name/file-like object pairs."
434 (service etc-service-type files))
435
436 (define setuid-program-service-type
437 (service-type (name 'setuid-program)
438 (extensions
439 (list (service-extension activation-service-type
440 (lambda (programs)
441 #~(activate-setuid-programs
442 (list #$@programs))))))
443 (compose concatenate)
444 (extend append)))
445
446 (define (packages->profile-entry packages)
447 "Return a system entry for the profile containing PACKAGES."
448 (mlet %store-monad ((profile (profile-derivation
449 (packages->manifest
450 (delete-duplicates packages eq?)))))
451 (return `(("profile" ,profile)))))
452
453 (define profile-service-type
454 ;; The service that populates the system's profile---i.e.,
455 ;; /run/current-system/profile. It is extended by package lists.
456 (service-type (name 'profile)
457 (extensions
458 (list (service-extension system-service-type
459 packages->profile-entry)))
460 (compose concatenate)
461 (extend append)))
462
463 (define (firmware->activation-gexp firmware)
464 "Return a gexp to make the packages listed in FIRMWARE loadable by the
465 kernel."
466 (let ((directory (directory-union "firmware" firmware)))
467 ;; Tell the kernel where firmware is.
468 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
469
470 (define firmware-service-type
471 ;; The service that collects firmware.
472 (service-type (name 'firmware)
473 (extensions
474 (list (service-extension activation-service-type
475 firmware->activation-gexp)))
476 (compose concatenate)
477 (extend append)))
478
479 (define (gc-roots->system-entry roots)
480 "Return an entry in the system's output containing symlinks to ROOTS."
481 (mlet %store-monad ((entry (gexp->derivation
482 "gc-roots"
483 #~(let ((roots '#$roots))
484 (mkdir #$output)
485 (chdir #$output)
486 (for-each symlink
487 roots
488 (map number->string
489 (iota (length roots))))))))
490 (return (if (null? roots)
491 '()
492 `(("gc-roots" ,entry))))))
493
494 (define gc-root-service-type
495 ;; A service to associate extra garbage-collector roots to the system. This
496 ;; is a simple hack that guarantees that the system retains references to
497 ;; the given list of roots. Roots must be "lowerable" objects like
498 ;; packages, or derivations.
499 (service-type (name 'gc-roots)
500 (extensions
501 (list (service-extension system-service-type
502 gc-roots->system-entry)))
503 (compose concatenate)
504 (extend append)))
505
506 \f
507 ;;;
508 ;;; Service folding.
509 ;;;
510
511 (define-condition-type &service-error &error
512 service-error?)
513
514 (define-condition-type &missing-target-service-error &service-error
515 missing-target-service-error?
516 (service missing-target-service-error-service)
517 (target-type missing-target-service-error-target-type))
518
519 (define-condition-type &ambiguous-target-service-error &service-error
520 ambiguous-target-service-error?
521 (service ambiguous-target-service-error-service)
522 (target-type ambiguous-target-service-error-target-type))
523
524 (define (service-back-edges services)
525 "Return a procedure that, when passed a <service>, returns the list of
526 <service> objects that depend on it."
527 (define (add-edges service edges)
528 (define (add-edge extension edges)
529 (let ((target-type (service-extension-target extension)))
530 (match (filter (lambda (service)
531 (eq? (service-kind service) target-type))
532 services)
533 ((target)
534 (vhash-consq target service edges))
535 (()
536 (raise
537 (condition (&missing-target-service-error
538 (service service)
539 (target-type target-type))
540 (&message
541 (message
542 (format #f (_ "no target of type '~a' for service ~s")
543 (service-type-name target-type)
544 service))))))
545 (x
546 (raise
547 (condition (&ambiguous-target-service-error
548 (service service)
549 (target-type target-type))
550 (&message
551 (message
552 (format #f
553 (_ "more than one target service of type '~a'")
554 (service-type-name target-type))))))))))
555
556 (fold add-edge edges (service-type-extensions (service-kind service))))
557
558 (let ((edges (fold add-edges vlist-null services)))
559 (lambda (node)
560 (reverse (vhash-foldq* cons '() node edges)))))
561
562 (define* (fold-services services
563 #:key (target-type system-service-type))
564 "Fold SERVICES by propagating their extensions down to the root of type
565 TARGET-TYPE; return the root service adjusted accordingly."
566 (define dependents
567 (service-back-edges services))
568
569 (define (matching-extension target)
570 (let ((target (service-kind target)))
571 (match-lambda
572 (($ <service-extension> type)
573 (eq? type target)))))
574
575 (define (apply-extension target)
576 (lambda (service)
577 (match (find (matching-extension target)
578 (service-type-extensions (service-kind service)))
579 (($ <service-extension> _ compute)
580 (compute (service-parameters service))))))
581
582 (match (filter (lambda (service)
583 (eq? (service-kind service) target-type))
584 services)
585 ((sink)
586 (let loop ((sink sink))
587 (let* ((dependents (map loop (dependents sink)))
588 (extensions (map (apply-extension sink) dependents))
589 (extend (service-type-extend (service-kind sink)))
590 (compose (service-type-compose (service-kind sink)))
591 (params (service-parameters sink)))
592 ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a
593 ;; different type than the elements of EXTENSIONS.
594 (if extend
595 (service (service-kind sink)
596 (extend params (compose extensions)))
597 sink))))
598 (()
599 (raise
600 (condition (&missing-target-service-error
601 (service #f)
602 (target-type target-type))
603 (&message
604 (message (format #f (_ "service of type '~a' not found")
605 (service-type-name target-type)))))))
606 (x
607 (raise
608 (condition (&ambiguous-target-service-error
609 (service #f)
610 (target-type target-type))
611 (&message
612 (message
613 (format #f
614 (_ "more than one target service of type '~a'")
615 (service-type-name target-type)))))))))
616
617 ;;; services.scm ends here.