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