services.scm 28 KB

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