services.scm 46 KB

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