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