gnu: guix: Update to 846403e.
[jackhill/guix/guix.git] / gnu / services.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
4 ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
5 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
12 ;;;
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21 (define-module (gnu services)
22 #:use-module (guix gexp)
23 #:use-module (guix monads)
24 #:use-module (guix store)
25 #:use-module (guix records)
26 #:use-module (guix profiles)
27 #:use-module (guix discovery)
28 #:use-module (guix combinators)
29 #:use-module (guix channels)
30 #:use-module (guix describe)
31 #:use-module (guix sets)
32 #:use-module (guix ui)
33 #:use-module (guix diagnostics)
34 #:autoload (guix openpgp) (openpgp-format-fingerprint)
35 #:use-module (guix modules)
36 #:use-module (gnu packages base)
37 #:use-module (gnu packages bash)
38 #:use-module (gnu packages hurd)
39 #:use-module (srfi srfi-1)
40 #:use-module (srfi srfi-9)
41 #:use-module (srfi srfi-9 gnu)
42 #:use-module (srfi srfi-26)
43 #:use-module (srfi srfi-34)
44 #:use-module (srfi srfi-35)
45 #:use-module (ice-9 vlist)
46 #:use-module (ice-9 match)
47 #:autoload (ice-9 pretty-print) (pretty-print)
48 #:export (service-extension
49 service-extension?
50 service-extension-target
51 service-extension-compute
52
53 service-type
54 service-type?
55 service-type-name
56 service-type-extensions
57 service-type-compose
58 service-type-extend
59 service-type-default-value
60 service-type-description
61 service-type-location
62
63 %service-type-path
64 fold-service-types
65 lookup-service-types
66
67 service
68 service?
69 service-kind
70 service-value
71 service-parameters ;deprecated
72
73 simple-service
74 modify-services
75 service-back-edges
76 instantiate-missing-services
77 fold-services
78
79 service-error?
80 missing-value-service-error?
81 missing-value-service-error-type
82 missing-value-service-error-location
83 missing-target-service-error?
84 missing-target-service-error-service
85 missing-target-service-error-target-type
86 ambiguous-target-service-error?
87 ambiguous-target-service-error-service
88 ambiguous-target-service-error-target-type
89
90 system-service-type
91 provenance-service-type
92 sexp->system-provenance
93 system-provenance
94 boot-service-type
95 cleanup-service-type
96 activation-service-type
97 activation-service->script
98 %linux-bare-metal-service
99 %hurd-rc-script
100 %hurd-startup-service
101 special-files-service-type
102 extra-special-file
103 etc-service-type
104 etc-directory
105 setuid-program-service-type
106 profile-service-type
107 firmware-service-type
108 gc-root-service-type
109
110 %boot-service
111 %activation-service
112 etc-service))
113
114 ;;; Comment:
115 ;;;
116 ;;; This module defines a broad notion of "service types" and "services."
117 ;;;
118 ;;; A service type describe how its instances extend instances of other
119 ;;; service types. For instance, some services extend the instance of
120 ;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create;
121 ;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of
122 ;;; <shepherd-service>.
123 ;;;
124 ;;; When applicable, the service type defines how it can itself be extended,
125 ;;; by providing one procedure to compose extensions, and one procedure to
126 ;;; extend itself.
127 ;;;
128 ;;; A notable service type is SYSTEM-SERVICE-TYPE, which has a single
129 ;;; instance, which is the root of the service DAG. Its value is the
130 ;;; derivation that produces the 'system' directory as returned by
131 ;;; 'operating-system-derivation'.
132 ;;;
133 ;;; The 'fold-services' procedure can be passed a list of procedures, which it
134 ;;; "folds" by propagating extensions down the graph; it returns the root
135 ;;; service after the applying all its extensions.
136 ;;;
137 ;;; Code:
138
139 (define-record-type <service-extension>
140 (service-extension target compute)
141 service-extension?
142 (target service-extension-target) ;<service-type>
143 (compute service-extension-compute)) ;params -> params
144
145 (define &no-default-value
146 ;; Value used to denote service types that have no associated default value.
147 '(no default value))
148
149 (define-record-type* <service-type> service-type make-service-type
150 service-type?
151 (name service-type-name) ;symbol (for debugging)
152
153 ;; Things extended by services of this type.
154 (extensions service-type-extensions) ;list of <service-extensions>
155
156 ;; Given a list of extensions, "compose" them.
157 (compose service-type-compose ;list of Any -> Any
158 (default #f))
159
160 ;; Extend the services' own parameters with the extension composition.
161 (extend service-type-extend ;list of Any -> parameters
162 (default #f))
163
164 ;; Optional default value for instances of this type.
165 (default-value service-type-default-value ;Any
166 (default &no-default-value))
167
168 ;; Meta-data.
169 (description service-type-description ;string
170 (default #f))
171 (location service-type-location ;<location>
172 (default (and=> (current-source-location)
173 source-properties->location))
174 (innate)))
175
176 (define (write-service-type type port)
177 (format port "#<service-type ~a ~a>"
178 (service-type-name type)
179 (number->string (object-address type) 16)))
180
181 (set-record-type-printer! <service-type> write-service-type)
182
183 (define %distro-root-directory
184 ;; Absolute file name of the module hierarchy.
185 (dirname (search-path %load-path "guix.scm")))
186
187 (define %service-type-path
188 ;; Search path for service types.
189 (make-parameter `((,%distro-root-directory . "gnu/services")
190 (,%distro-root-directory . "gnu/system"))))
191
192 (define (all-service-modules)
193 "Return the default set of service modules."
194 (cons (resolve-interface '(gnu services))
195 (all-modules (%service-type-path)
196 #:warn warn-about-load-error)))
197
198 (define* (fold-service-types proc seed
199 #:optional
200 (modules (all-service-modules)))
201 "For each service type exported by one of MODULES, call (PROC RESULT). SEED
202 is used as the initial value of RESULT."
203 (fold-module-public-variables (lambda (object result)
204 (if (service-type? object)
205 (proc object result)
206 result))
207 seed
208 modules))
209
210 (define lookup-service-types
211 (let ((table
212 (delay (fold-service-types (lambda (type result)
213 (vhash-consq (service-type-name type)
214 type result))
215 vlist-null))))
216 (lambda (name)
217 "Return the list of services with the given NAME (a symbol)."
218 (vhash-foldq* cons '() name (force table)))))
219
220 ;; Services of a given type.
221 (define-record-type <service>
222 (make-service type value)
223 service?
224 (type service-kind)
225 (value service-value))
226
227 (define-syntax service
228 (syntax-rules ()
229 "Return a service instance of TYPE. The service value is VALUE or, if
230 omitted, TYPE's default value."
231 ((_ type value)
232 (make-service type value))
233 ((_ type)
234 (%service-with-default-value (current-source-location)
235 type))))
236
237 (define (%service-with-default-value location type)
238 "Return a instance of service type TYPE with its default value, if any. If
239 TYPE does not have a default value, an error is raised."
240 ;; TODO: Currently this is a run-time error but with a little bit macrology
241 ;; we could turn it into an expansion-time error.
242 (let ((default (service-type-default-value type)))
243 (if (eq? default &no-default-value)
244 (let ((location (source-properties->location location)))
245 (raise
246 (make-compound-condition
247 (condition
248 (&missing-value-service-error (type type) (location location)))
249 (formatted-message (G_ "~a: no value specified \
250 for service of type '~a'")
251 (location->string location)
252 (service-type-name type)))))
253 (service type default))))
254
255 (define-condition-type &service-error &error
256 service-error?)
257
258 (define-condition-type &missing-value-service-error &service-error
259 missing-value-service-error?
260 (type missing-value-service-error-type)
261 (location missing-value-service-error-location))
262
263
264 \f
265 ;;;
266 ;;; Helpers.
267 ;;;
268
269 (define service-parameters
270 ;; Deprecated alias.
271 service-value)
272
273 (define (simple-service name target value)
274 "Return a service that extends TARGET with VALUE. This works by creating a
275 singleton service type NAME, of which the returned service is an instance."
276 (let* ((extension (service-extension target identity))
277 (type (service-type (name name)
278 (extensions (list extension)))))
279 (service type value)))
280
281 (define-syntax %modify-service
282 (syntax-rules (=>)
283 ((_ service)
284 service)
285 ((_ svc (kind param => exp ...) clauses ...)
286 (if (eq? (service-kind svc) kind)
287 (let ((param (service-value svc)))
288 (service (service-kind svc)
289 (begin exp ...)))
290 (%modify-service svc clauses ...)))))
291
292 (define-syntax modify-services
293 (syntax-rules ()
294 "Modify the services listed in SERVICES according to CLAUSES and return
295 the resulting list of services. Each clause must have the form:
296
297 (TYPE VARIABLE => BODY)
298
299 where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
300 identifier that is bound within BODY to the value of the service of that
301 TYPE. Consider this example:
302
303 (modify-services %base-services
304 (guix-service-type config =>
305 (guix-configuration
306 (inherit config)
307 (use-substitutes? #f)
308 (extra-options '(\"--gc-keep-derivations\"))))
309 (mingetty-service-type config =>
310 (mingetty-configuration
311 (inherit config)
312 (motd (plain-file \"motd\" \"Hi there!\")))))
313
314 It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
315 all the MINGETTY-SERVICE-TYPE instances.
316
317 This is a shorthand for (map (lambda (svc) ...) %base-services)."
318 ((_ services clauses ...)
319 (map (lambda (service)
320 (%modify-service service clauses ...))
321 services))))
322
323 \f
324 ;;;
325 ;;; Core services.
326 ;;;
327
328 (define (system-derivation entries mextensions)
329 "Return as a monadic value the derivation of the 'system' directory
330 containing the given entries."
331 (mlet %store-monad ((extensions (mapm/accumulate-builds identity
332 mextensions)))
333 (lower-object
334 (file-union "system"
335 (append entries (concatenate extensions))))))
336
337 (define system-service-type
338 ;; This is the ultimate service type, the root of the service DAG. The
339 ;; service of this type is extended by monadic name/item pairs. These items
340 ;; end up in the "system directory" as returned by
341 ;; 'operating-system-derivation'.
342 (service-type (name 'system)
343 (extensions '())
344 (compose identity)
345 (extend system-derivation)
346 (description
347 "Build the operating system top-level directory, which in
348 turn refers to everything the operating system needs: its kernel, initrd,
349 system profile, boot script, and so on.")))
350
351 (define (compute-boot-script _ gexps)
352 ;; Reverse GEXPS so that extensions appear in the boot script in the right
353 ;; order. That is, user extensions would come first, and extensions added
354 ;; by 'essential-services' (e.g., running shepherd) are guaranteed to come
355 ;; last.
356 (gexp->file "boot"
357 ;; Clean up and activate the system, then spawn shepherd.
358 #~(begin #$@(reverse gexps))))
359
360 (define (boot-script-entry mboot)
361 "Return, as a monadic value, an entry for the boot script in the system
362 directory."
363 (mlet %store-monad ((boot mboot))
364 (return `(("boot" ,boot)))))
365
366 (define boot-service-type
367 ;; The service of this type is extended by being passed gexps. It
368 ;; aggregates them in a single script, as a monadic value, which becomes its
369 ;; value.
370 (service-type (name 'boot)
371 (extensions
372 (list (service-extension system-service-type
373 boot-script-entry)))
374 (compose identity)
375 (extend compute-boot-script)
376 (description
377 "Produce the operating system's boot script, which is spawned
378 by the initrd once the root file system is mounted.")))
379
380 (define %boot-service
381 ;; The service that produces the boot script.
382 (service boot-service-type #t))
383
384 \f
385 ;;;
386 ;;; Provenance tracking.
387 ;;;
388
389 (define (object->pretty-string obj)
390 "Like 'object->string', but using 'pretty-print'."
391 (call-with-output-string
392 (lambda (port)
393 (pretty-print obj port))))
394
395 (define (channel->code channel)
396 "Return code to build CHANNEL, ready to be dropped in a 'channels.scm'
397 file."
398 ;; Since the 'introduction' field is backward-incompatible, and since it's
399 ;; optional when using the "official" 'guix channel, include it if and only
400 ;; if we're referring to a different channel.
401 (let ((intro (and (not (equal? (list channel) %default-channels))
402 (channel-introduction channel))))
403 `(channel (name ',(channel-name channel))
404 (url ,(channel-url channel))
405 (branch ,(channel-branch channel))
406 (commit ,(channel-commit channel))
407 ,@(if intro
408 `((introduction
409 (make-channel-introduction
410 ,(channel-introduction-first-signed-commit intro)
411 (openpgp-fingerprint
412 ,(openpgp-format-fingerprint
413 (channel-introduction-first-commit-signer
414 intro))))))
415 '()))))
416
417 (define (channel->sexp channel)
418 "Return an sexp describing CHANNEL. The sexp is _not_ code and is meant to
419 be parsed by tools; it's potentially more future-proof than code."
420 ;; TODO: Add CHANNEL's introduction. Currently we can't do that because
421 ;; older 'guix system describe' expect exactly name/url/branch/commit
422 ;; without any additional fields.
423 `(channel (name ,(channel-name channel))
424 (url ,(channel-url channel))
425 (branch ,(channel-branch channel))
426 (commit ,(channel-commit channel))))
427
428 (define (sexp->channel sexp)
429 "Return the channel corresponding to SEXP, an sexp as found in the
430 \"provenance\" file produced by 'provenance-service-type'."
431 (match sexp
432 (('channel ('name name)
433 ('url url)
434 ('branch branch)
435 ('commit commit)
436 rest ...)
437 ;; XXX: In the future REST may include a channel introduction.
438 (channel (name name) (url url)
439 (branch branch) (commit commit)))))
440
441 (define (provenance-file channels config-file)
442 "Return a 'provenance' file describing CHANNELS, a list of channels, and
443 CONFIG-FILE, which can be either #f or a <local-file> containing the OS
444 configuration being used."
445 (scheme-file "provenance"
446 #~(provenance
447 (version 0)
448 (channels #+@(if channels
449 (map channel->sexp channels)
450 '()))
451 (configuration-file #+config-file))))
452
453 (define (provenance-entry config-file)
454 "Return system entries describing the operating system provenance: the
455 channels in use and CONFIG-FILE, if it is true."
456 (define profile
457 (current-profile))
458
459 (define channels
460 (and=> profile profile-channels))
461
462 (mbegin %store-monad
463 (let ((config-file (cond ((string? config-file)
464 (local-file config-file "configuration.scm"))
465 ((not config-file)
466 #f)
467 (else
468 config-file))))
469 (return `(("provenance" ,(provenance-file channels config-file))
470 ,@(if channels
471 `(("channels.scm"
472 ,(plain-file "channels.scm"
473 (object->pretty-string
474 `(list
475 ,@(map channel->code channels))))))
476 '())
477 ,@(if config-file
478 `(("configuration.scm" ,config-file))
479 '()))))))
480
481 (define provenance-service-type
482 (service-type (name 'provenance)
483 (extensions
484 (list (service-extension system-service-type
485 provenance-entry)))
486 (default-value #f) ;the OS config file
487 (description
488 "Store provenance information about the system in the system
489 itself: the channels used when building the system, and its configuration
490 file, when available.")))
491
492 (define (sexp->system-provenance sexp)
493 "Parse SEXP, an s-expression read from /run/current-system/provenance or
494 similar, and return two values: the list of channels listed therein, and the
495 OS configuration file or #f."
496 (match sexp
497 (('provenance ('version 0)
498 ('channels channels ...)
499 ('configuration-file config-file))
500 (values (map sexp->channel channels)
501 config-file))
502 (_
503 (values '() #f))))
504
505 (define (system-provenance system)
506 "Given SYSTEM, the file name of a system generation, return two values: the
507 list of channels SYSTEM is built from, and its configuration file. If that
508 information is missing, return the empty list (for channels) and possibly
509 #false (for the configuration file)."
510 (catch 'system-error
511 (lambda ()
512 (sexp->system-provenance
513 (call-with-input-file (string-append system "/provenance")
514 read)))
515 (lambda _
516 (values '() #f))))
517 \f
518 ;;;
519 ;;; Cleanup.
520 ;;;
521
522 (define (cleanup-gexp _)
523 "Return a gexp to clean up /tmp and similar places upon boot."
524 (with-imported-modules '((guix build utils))
525 #~(begin
526 (use-modules (guix build utils))
527
528 ;; Clean out /tmp and /var/run.
529 ;;
530 ;; XXX This needs to happen before service activations, so it
531 ;; has to be here, but this also implicitly assumes that /tmp
532 ;; and /var/run are on the root partition.
533 (letrec-syntax ((fail-safe (syntax-rules ()
534 ((_ exp rest ...)
535 (begin
536 (catch 'system-error
537 (lambda () exp)
538 (const #f))
539 (fail-safe rest ...)))
540 ((_)
541 #t))))
542 ;; Ignore I/O errors so the system can boot.
543 (fail-safe
544 ;; Remove stale Shadow lock files as they would lead to
545 ;; failures of 'useradd' & co.
546 (delete-file "/etc/group.lock")
547 (delete-file "/etc/passwd.lock")
548 (delete-file "/etc/.pwd.lock") ;from 'lckpwdf'
549
550 ;; Force file names to be decoded as UTF-8. See
551 ;; <https://bugs.gnu.org/26353>.
552 (setenv "GUIX_LOCPATH"
553 #+(file-append glibc-utf8-locales "/lib/locale"))
554 (setlocale LC_CTYPE "en_US.utf8")
555 (delete-file-recursively "/tmp")
556 (delete-file-recursively "/var/run")
557
558 (mkdir "/tmp")
559 (chmod "/tmp" #o1777)
560 (mkdir "/var/run")
561 (chmod "/var/run" #o755)
562 (delete-file-recursively "/run/udev/watch.old"))))))
563
564 (define cleanup-service-type
565 ;; Service that cleans things up in /tmp and similar.
566 (service-type (name 'cleanup)
567 (extensions
568 (list (service-extension boot-service-type
569 cleanup-gexp)))
570 (description
571 "Delete files from @file{/tmp}, @file{/var/run}, and other
572 temporary locations at boot time.")))
573
574 (define* (activation-service->script service)
575 "Return as a monadic value the activation script for SERVICE, a service of
576 ACTIVATION-SCRIPT-TYPE."
577 (activation-script (service-value service)))
578
579 (define (activation-script gexps)
580 "Return the system's activation script, which evaluates GEXPS."
581 (define actions
582 (map (cut program-file "activate-service.scm" <>) gexps))
583
584 (program-file "activate.scm"
585 (with-imported-modules (source-module-closure
586 '((gnu build activation)
587 (guix build utils)))
588 #~(begin
589 (use-modules (gnu build activation)
590 (guix build utils))
591
592 ;; Make sure the user accounting database exists. If it
593 ;; does not exist, 'setutxent' does not create it and
594 ;; thus there is no accounting at all.
595 (close-port (open-file "/var/run/utmpx" "a0"))
596
597 ;; Same for 'wtmp', which is populated by mingetty et
598 ;; al.
599 (mkdir-p "/var/log")
600 (close-port (open-file "/var/log/wtmp" "a0"))
601
602 ;; Set up /run/current-system. Among other things this
603 ;; sets up locales, which the activation snippets
604 ;; executed below may expect.
605 (activate-current-system)
606
607 ;; Run the services' activation snippets.
608 ;; TODO: Use 'load-compiled'.
609 (for-each primitive-load '#$actions)))))
610
611 (define (gexps->activation-gexp gexps)
612 "Return a gexp that runs the activation script containing GEXPS."
613 #~(primitive-load #$(activation-script gexps)))
614
615 (define (second-argument a b) b)
616
617 (define activation-service-type
618 (service-type (name 'activate)
619 (extensions
620 (list (service-extension boot-service-type
621 gexps->activation-gexp)))
622 (compose identity)
623 (extend second-argument)
624 (description
625 "Run @dfn{activation} code at boot time and upon
626 @command{guix system reconfigure} completion.")))
627
628 (define %activation-service
629 ;; The activation service produces the activation script from the gexps it
630 ;; receives.
631 (service activation-service-type #t))
632
633 (define %modprobe-wrapper
634 ;; Wrapper for the 'modprobe' command that knows where modules live.
635 ;;
636 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
637 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
638 ;; environment variable is not set---hence the need for this wrapper.
639 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
640 (program-file "modprobe"
641 #~(begin
642 (setenv "LINUX_MODULE_DIRECTORY"
643 "/run/booted-system/kernel/lib/modules")
644 ;; FIXME: Remove this crutch when the patch #40422,
645 ;; updating to kmod 27 is merged.
646 (setenv "MODPROBE_OPTIONS"
647 "-C /etc/modprobe.d")
648 (apply execl #$modprobe
649 (cons #$modprobe (cdr (command-line))))))))
650
651 (define %linux-kernel-activation
652 ;; Activation of the Linux kernel running on the bare metal (as opposed to
653 ;; running in a container.)
654 #~(begin
655 ;; Tell the kernel to use our 'modprobe' command.
656 (activate-modprobe #$%modprobe-wrapper)
657
658 ;; Let users debug their own processes!
659 (activate-ptrace-attach)))
660
661 (define %linux-bare-metal-service
662 ;; The service that does things that are needed on the "bare metal", but not
663 ;; necessary or impossible in a container.
664 (simple-service 'linux-bare-metal
665 activation-service-type
666 %linux-kernel-activation))
667
668 (define %hurd-rc-script
669 ;; The RC script to be started upon boot.
670 (program-file "rc"
671 (with-imported-modules (source-module-closure
672 '((guix build utils)
673 (gnu build hurd-boot)
674 (guix build syscalls)))
675 #~(begin
676 (use-modules (guix build utils)
677 (gnu build hurd-boot)
678 (guix build syscalls)
679 (ice-9 match)
680 (system repl repl)
681 (srfi srfi-1)
682 (srfi srfi-26))
683 (boot-hurd-system)))))
684
685 (define (hurd-rc-entry rc)
686 "Return, as a monadic value, an entry for the RC script in the system
687 directory."
688 (mlet %store-monad ((rc (lower-object rc)))
689 (return `(("rc" ,rc)))))
690
691 (define hurd-startup-service-type
692 ;; The service that creates the initial SYSTEM/rc startup file.
693 (service-type (name 'startup)
694 (extensions
695 (list (service-extension system-service-type hurd-rc-entry)))
696 (default-value %hurd-rc-script)))
697
698 (define %hurd-startup-service
699 ;; The service that produces the RC script.
700 (service hurd-startup-service-type %hurd-rc-script))
701
702 (define special-files-service-type
703 ;; Service to install "special files" such as /bin/sh and /usr/bin/env.
704 (service-type
705 (name 'special-files)
706 (extensions
707 (list (service-extension activation-service-type
708 (lambda (files)
709 #~(activate-special-files '#$files)))))
710 (compose concatenate)
711 (extend append)
712 (description
713 "Add special files to the root file system---e.g.,
714 @file{/usr/bin/env}.")))
715
716 (define (extra-special-file file target)
717 "Use TARGET as the \"special file\" FILE. For example, TARGET might be
718 (file-append coreutils \"/bin/env\")
719 and FILE could be \"/usr/bin/env\"."
720 (simple-service (string->symbol (string-append "special-file-" file))
721 special-files-service-type
722 `((,file ,target))))
723
724 (define (etc-directory service)
725 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
726 (files->etc-directory (service-value service)))
727
728 (define (files->etc-directory files)
729 (define (assert-no-duplicates files)
730 (let loop ((files files)
731 (seen (set)))
732 (match files
733 (() #t)
734 (((file _) rest ...)
735 (when (set-contains? seen file)
736 (raise (formatted-message (G_ "duplicate '~a' entry for /etc")
737 file)))
738 (loop rest (set-insert file seen))))))
739
740 ;; Detect duplicates early instead of letting them through, eventually
741 ;; leading to a build failure of "etc.drv".
742 (assert-no-duplicates files)
743
744 (file-union "etc" files))
745
746 (define (etc-entry files)
747 "Return an entry for the /etc directory consisting of FILES in the system
748 directory."
749 (with-monad %store-monad
750 (return `(("etc" ,(files->etc-directory files))))))
751
752 (define etc-service-type
753 (service-type (name 'etc)
754 (extensions
755 (list
756 (service-extension activation-service-type
757 (lambda (files)
758 (let ((etc
759 (files->etc-directory files)))
760 #~(activate-etc #$etc))))
761 (service-extension system-service-type etc-entry)))
762 (compose concatenate)
763 (extend append)
764 (description "Populate the @file{/etc} directory.")))
765
766 (define (etc-service files)
767 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
768 FILES must be a list of name/file-like object pairs."
769 (service etc-service-type files))
770
771 (define setuid-program-service-type
772 (service-type (name 'setuid-program)
773 (extensions
774 (list (service-extension activation-service-type
775 (lambda (programs)
776 #~(activate-setuid-programs
777 (list #$@programs))))))
778 (compose concatenate)
779 (extend append)
780 (description
781 "Populate @file{/run/setuid-programs} with the specified
782 executables, making them setuid-root.")))
783
784 (define (packages->profile-entry packages)
785 "Return a system entry for the profile containing PACKAGES."
786 (with-monad %store-monad
787 (return `(("profile" ,(profile
788 (content (packages->manifest
789 (delete-duplicates packages eq?)))))))))
790
791 (define profile-service-type
792 ;; The service that populates the system's profile---i.e.,
793 ;; /run/current-system/profile. It is extended by package lists.
794 (service-type (name 'profile)
795 (extensions
796 (list (service-extension system-service-type
797 packages->profile-entry)))
798 (compose concatenate)
799 (extend append)
800 (description
801 "This is the @dfn{system profile}, available as
802 @file{/run/current-system/profile}. It contains packages that the sysadmin
803 wants to be globally available to all the system users.")))
804
805 (define (firmware->activation-gexp firmware)
806 "Return a gexp to make the packages listed in FIRMWARE loadable by the
807 kernel."
808 (let ((directory (directory-union "firmware" firmware)))
809 ;; Tell the kernel where firmware is.
810 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
811
812 (define firmware-service-type
813 ;; The service that collects firmware.
814 (service-type (name 'firmware)
815 (extensions
816 (list (service-extension activation-service-type
817 firmware->activation-gexp)))
818 (compose concatenate)
819 (extend append)
820 (description
821 "Make ``firmware'' files loadable by the operating system
822 kernel. Firmware may then be uploaded to some of the machine's devices, such
823 as Wifi cards.")))
824
825 (define (gc-roots->system-entry roots)
826 "Return an entry in the system's output containing symlinks to ROOTS."
827 (mlet %store-monad ((entry (gexp->derivation
828 "gc-roots"
829 #~(let ((roots '#$roots))
830 (mkdir #$output)
831 (chdir #$output)
832 (for-each symlink
833 roots
834 (map number->string
835 (iota (length roots))))))))
836 (return (if (null? roots)
837 '()
838 `(("gc-roots" ,entry))))))
839
840 (define gc-root-service-type
841 ;; A service to associate extra garbage-collector roots to the system. This
842 ;; is a simple hack that guarantees that the system retains references to
843 ;; the given list of roots. Roots must be "lowerable" objects like
844 ;; packages, or derivations.
845 (service-type (name 'gc-roots)
846 (extensions
847 (list (service-extension system-service-type
848 gc-roots->system-entry)))
849 (compose concatenate)
850 (extend append)
851 (description
852 "Register garbage-collector roots---i.e., store items that
853 will not be reclaimed by the garbage collector.")
854 (default-value '())))
855
856 \f
857 ;;;
858 ;;; Service folding.
859 ;;;
860
861 (define-condition-type &missing-target-service-error &service-error
862 missing-target-service-error?
863 (service missing-target-service-error-service)
864 (target-type missing-target-service-error-target-type))
865
866 (define-condition-type &ambiguous-target-service-error &service-error
867 ambiguous-target-service-error?
868 (service ambiguous-target-service-error-service)
869 (target-type ambiguous-target-service-error-target-type))
870
871 (define (missing-target-error service target-type)
872 (raise
873 (condition (&missing-target-service-error
874 (service service)
875 (target-type target-type))
876 (&message
877 (message
878 (format #f (G_ "no target of type '~a' for service '~a'")
879 (service-type-name target-type)
880 (service-type-name
881 (service-kind service))))))))
882
883 (define (service-back-edges services)
884 "Return a procedure that, when passed a <service>, returns the list of
885 <service> objects that depend on it."
886 (define (add-edges service edges)
887 (define (add-edge extension edges)
888 (let ((target-type (service-extension-target extension)))
889 (match (filter (lambda (service)
890 (eq? (service-kind service) target-type))
891 services)
892 ((target)
893 (vhash-consq target service edges))
894 (()
895 (missing-target-error service target-type))
896 (x
897 (raise
898 (condition (&ambiguous-target-service-error
899 (service service)
900 (target-type target-type))
901 (&message
902 (message
903 (format #f
904 (G_ "more than one target service of type '~a'")
905 (service-type-name target-type))))))))))
906
907 (fold add-edge edges (service-type-extensions (service-kind service))))
908
909 (let ((edges (fold add-edges vlist-null services)))
910 (lambda (node)
911 (reverse (vhash-foldq* cons '() node edges)))))
912
913 (define (instantiate-missing-services services)
914 "Return SERVICES, a list, augmented with any services targeted by extensions
915 and missing from SERVICES. Only service types with a default value can be
916 instantiated; other missing services lead to a
917 '&missing-target-service-error'."
918 (define (adjust-service-list svc result instances)
919 (fold2 (lambda (extension result instances)
920 (define target-type
921 (service-extension-target extension))
922
923 (match (vhash-assq target-type instances)
924 (#f
925 (let ((default (service-type-default-value target-type)))
926 (if (eq? &no-default-value default)
927 (missing-target-error svc target-type)
928 (let ((new (service target-type)))
929 (values (cons new result)
930 (vhash-consq target-type new instances))))))
931 (_
932 (values result instances))))
933 result
934 instances
935 (service-type-extensions (service-kind svc))))
936
937 (let loop ((services services))
938 (define instances
939 (fold (lambda (service result)
940 (vhash-consq (service-kind service) service
941 result))
942 vlist-null services))
943
944 (define adjusted
945 (fold2 adjust-service-list
946 services instances
947 services))
948
949 ;; If we instantiated services, they might in turn depend on missing
950 ;; services. Loop until we've reached fixed point.
951 (if (= (length adjusted) (vlist-length instances))
952 adjusted
953 (loop adjusted))))
954
955 (define* (fold-services services
956 #:key (target-type system-service-type))
957 "Fold SERVICES by propagating their extensions down to the root of type
958 TARGET-TYPE; return the root service adjusted accordingly."
959 (define dependents
960 (service-back-edges services))
961
962 (define (matching-extension target)
963 (let ((target (service-kind target)))
964 (match-lambda
965 (($ <service-extension> type)
966 (eq? type target)))))
967
968 (define (apply-extension target)
969 (lambda (service)
970 (match (find (matching-extension target)
971 (service-type-extensions (service-kind service)))
972 (($ <service-extension> _ compute)
973 (compute (service-value service))))))
974
975 (match (filter (lambda (service)
976 (eq? (service-kind service) target-type))
977 services)
978 ((sink)
979 ;; Use the state monad to keep track of already-visited services in the
980 ;; graph and to memoize their value once folded.
981 (run-with-state
982 (let loop ((sink sink))
983 (mlet %state-monad ((visited (current-state)))
984 (match (vhash-assq sink visited)
985 (#f
986 (mlet* %state-monad
987 ((dependents (mapm %state-monad loop (dependents sink)))
988 (visited (current-state))
989 (extensions -> (map (apply-extension sink) dependents))
990 (extend -> (service-type-extend (service-kind sink)))
991 (compose -> (service-type-compose (service-kind sink)))
992 (params -> (service-value sink))
993 (service
994 ->
995 ;; Distinguish COMPOSE and EXTEND because PARAMS typically
996 ;; has a different type than the elements of EXTENSIONS.
997 (if extend
998 (service (service-kind sink)
999 (extend params (compose extensions)))
1000 sink)))
1001 (mbegin %state-monad
1002 (set-current-state (vhash-consq sink service visited))
1003 (return service))))
1004 ((_ . service) ;SINK was already visited
1005 (return service)))))
1006 vlist-null))
1007 (()
1008 (raise
1009 (make-compound-condition
1010 (condition (&missing-target-service-error
1011 (service #f)
1012 (target-type target-type)))
1013 (formatted-message (G_ "service of type '~a' not found")
1014 (service-type-name target-type)))))
1015 (x
1016 (raise
1017 (condition (&ambiguous-target-service-error
1018 (service #f)
1019 (target-type target-type))
1020 (&message
1021 (message
1022 (format #f
1023 (G_ "more than one target service of type '~a'")
1024 (service-type-name target-type)))))))))
1025
1026 ;;; services.scm ends here.