services.scm 40 KB

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