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