services.scm 31 KB

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