gnu: openjpeg-2.1.2: Use a meaningful file-name for the source code.
[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)
232ccbef 28 #:use-module (guix modules)
0adfe95a
LC
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?
5152d13b
LC
44 service-type-name
45 service-type-extensions
46 service-type-compose
47 service-type-extend
0adfe95a 48
db4fdc04 49 service
0adfe95a
LC
50 service?
51 service-kind
52 service-parameters
53
71654dfd 54 simple-service
cd6f6c22 55 modify-services
5152d13b 56 service-back-edges
0adfe95a
LC
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
d62e201c 67 system-service-type
0adfe95a 68 boot-service-type
be7be9e8 69 cleanup-service-type
0adfe95a
LC
70 activation-service-type
71 activation-service->script
a241a7ac 72 %linux-bare-metal-service
0adfe95a
LC
73 etc-service-type
74 etc-directory
75 setuid-program-service-type
af4c3fd5 76 profile-service-type
0adfe95a 77 firmware-service-type
e0b47290 78 gc-root-service-type
0adfe95a
LC
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."
db4fdc04 89;;;
0adfe95a
LC
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;
d4053c71
AK
93;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of
94;;; <shepherd-service>.
0adfe95a
LC
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;;;
d62e201c
LC
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'.
0adfe95a
LC
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.
db4fdc04
LC
108;;;
109;;; Code:
110
0adfe95a
LC
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)
db4fdc04 142 service?
0adfe95a
LC
143 (type service-kind)
144 (parameters service-parameters))
145
71654dfd
LC
146(define (simple-service name target value)
147 "Return a service that extends TARGET with VALUE. This works by creating a
148singleton 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)))
0adfe95a 153
cd6f6c22
LC
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 ()
4d343a14
CM
167 "Modify the services listed in SERVICES according to CLAUSES and return
168the resulting list of services. Each clause must have the form:
cd6f6c22
LC
169
170 (TYPE VARIABLE => BODY)
171
172where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
173identifier that is bound within BODY to the value of the service of that
174TYPE. 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
187It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
188all the MINGETTY-SERVICE-TYPE instances.
189
190This is a shorthand for (map (lambda (svc) ...) %base-services)."
191 ((_ services clauses ...)
192 (map (lambda (service)
193 (%modify-service service clauses ...))
194 services))))
0adfe95a
LC
195
196\f
197;;;
198;;; Core services.
199;;;
200
d62e201c
LC
201(define (system-derivation mentries mextensions)
202 "Return as a monadic value the derivation of the 'system' directory
203containing 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
3a391e68 220(define (compute-boot-script _ mexps)
be7be9e8 221 (mlet %store-monad ((gexps (sequence %store-monad mexps)))
0adfe95a 222 (gexp->file "boot"
d4053c71 223 ;; Clean up and activate the system, then spawn shepherd.
be7be9e8 224 #~(begin #$@gexps))))
0adfe95a 225
d62e201c
LC
226(define (boot-script-entry mboot)
227 "Return, as a monadic value, an entry for the boot script in the system
228directory."
229 (mlet %store-monad ((boot mboot))
230 (return `(("boot" ,boot)))))
231
0adfe95a
LC
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)
d62e201c
LC
237 (extensions
238 (list (service-extension system-service-type
239 boot-script-entry)))
3a391e68
LC
240 (compose append)
241 (extend compute-boot-script)))
0adfe95a
LC
242
243(define %boot-service
d62e201c 244 ;; The service that produces the boot script.
0adfe95a 245 (service boot-service-type #t))
be7be9e8
LC
246
247(define (cleanup-gexp _)
248 "Return as a monadic value a gexp to clean up /tmp and similar places upon
249boot."
fd129893
LC
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))))))))
be7be9e8
LC
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)))))
0adfe95a
LC
284
285(define* (file-union name files) ;FIXME: Factorize.
286 "Return a <computed-file> that builds a directory containing all of FILES.
287Each item in FILES must be a list where the first element is the file name to
288use in the new directory, and the second element is a gexp denoting the target
289file."
290 (computed-file name
291 #~(begin
292 (mkdir #$output)
293 (chdir #$output)
294 #$@(map (match-lambda
295 ((target source)
37dd1e6a
LC
296 #~(begin
297 ;; Stat the source to abort early if it
298 ;; does not exist.
299 (stat #$source)
300
301 (symlink #$source #$target))))
0adfe95a
LC
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
4ee96a79
LC
312 (with-imported-modules '((guix build union))
313 #~(begin
314 (use-modules (guix build union))
315 (union-build #$output '#$things)))))))
0adfe95a 316
0adfe95a
LC
317(define* (activation-service->script service)
318 "Return as a monadic value the activation script for SERVICE, a service of
319ACTIVATION-SCRIPT-TYPE."
320 (activation-script (service-parameters service)))
321
322(define (activation-script gexps)
323 "Return the system's activation script, which evaluates GEXPS."
0adfe95a
LC
324 (define (service-activations)
325 ;; Return the activation scripts for SERVICES.
326 (mapm %store-monad
327 (cut gexp->file "activate-service" <>)
328 gexps))
329
fd129893 330 (mlet* %store-monad ((actions (service-activations)))
0adfe95a 331 (gexp->file "activate"
232ccbef
LC
332 (with-imported-modules (source-module-closure
333 '((gnu build activation)))
fd129893
LC
334 #~(begin
335 (use-modules (gnu build activation))
0adfe95a 336
fd129893
LC
337 ;; Make sure /bin/sh is valid and current.
338 (activate-/bin/sh
339 (string-append #$(canonical-package bash) "/bin/sh"))
0adfe95a 340
fd129893
LC
341 ;; Run the services' activation snippets.
342 ;; TODO: Use 'load-compiled'.
343 (for-each primitive-load '#$actions)
0adfe95a 344
fd129893
LC
345 ;; Set up /run/current-system.
346 (activate-current-system))))))
0adfe95a
LC
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
3a391e68
LC
353(define (second-argument a b) b)
354
0adfe95a
LC
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
a241a7ac
LC
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
0adfe95a
LC
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
d62e201c
LC
410(define (etc-entry files)
411 "Return an entry for the /etc directory consisting of FILES in the system
412directory."
413 (with-monad %store-monad
414 (return `(("etc" ,(files->etc-directory files))))))
415
0adfe95a
LC
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)))
d62e201c
LC
424 #~(activate-etc #$etc))))
425 (service-extension system-service-type etc-entry)))
0adfe95a
LC
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.
431FILES 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
af4c3fd5
LC
444(define (packages->profile-entry packages)
445 "Return a system entry for the profile containing PACKAGES."
446 (mlet %store-monad ((profile (profile-derivation
60a0886d
SB
447 (packages->manifest
448 (delete-duplicates packages eq?)))))
af4c3fd5
LC
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
0adfe95a
LC
461(define (firmware->activation-gexp firmware)
462 "Return a gexp to make the packages listed in FIRMWARE loadable by the
463kernel."
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
e0b47290
LC
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
0adfe95a
LC
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
d62e201c
LC
560(define* (fold-services services
561 #:key (target-type system-service-type))
0adfe95a
LC
562 "Fold SERVICES by propagating their extensions down to the root of type
563TARGET-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)))))))))
db4fdc04
LC
614
615;;; services.scm ends here.