services.scm 28 KB

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