services.scm 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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. (local-file config-file "configuration.scm"))
  406. ((not config-file)
  407. #f)
  408. (else
  409. config-file))))
  410. (return `(("provenance" ,(provenance-file channels config-file))
  411. ,@(if channels
  412. `(("channels.scm"
  413. ,(plain-file "channels.scm"
  414. (object->pretty-string
  415. `(list
  416. ,@(map channel->code channels))))))
  417. '())
  418. ,@(if config-file
  419. `(("configuration.scm" ,config-file))
  420. '()))))))
  421. (define provenance-service-type
  422. (service-type (name 'provenance)
  423. (extensions
  424. (list (service-extension system-service-type
  425. provenance-entry)))
  426. (default-value #f) ;the OS config file
  427. (description
  428. "Store provenance information about the system in the system
  429. itself: the channels used when building the system, and its configuration
  430. file, when available.")))
  431. (define (sexp->system-provenance sexp)
  432. "Parse SEXP, an s-expression read from /run/current-system/provenance or
  433. similar, and return two values: the list of channels listed therein, and the
  434. OS configuration file or #f."
  435. (match sexp
  436. (('provenance ('version 0)
  437. ('channels channels ...)
  438. ('configuration-file config-file))
  439. (values (map sexp->channel channels)
  440. config-file))
  441. (_
  442. (values '() #f))))
  443. (define (system-provenance system)
  444. "Given SYSTEM, the file name of a system generation, return two values: the
  445. list of channels SYSTEM is built from, and its configuration file. If that
  446. information is missing, return the empty list (for channels) and possibly
  447. #false (for the configuration file)."
  448. (catch 'system-error
  449. (lambda ()
  450. (sexp->system-provenance
  451. (call-with-input-file (string-append system "/provenance")
  452. read)))
  453. (lambda _
  454. (values '() #f))))
  455. ;;;
  456. ;;; Cleanup.
  457. ;;;
  458. (define (cleanup-gexp _)
  459. "Return a gexp to clean up /tmp and similar places upon boot."
  460. (with-imported-modules '((guix build utils))
  461. #~(begin
  462. (use-modules (guix build utils))
  463. ;; Clean out /tmp and /var/run.
  464. ;;
  465. ;; XXX This needs to happen before service activations, so it
  466. ;; has to be here, but this also implicitly assumes that /tmp
  467. ;; and /var/run are on the root partition.
  468. (letrec-syntax ((fail-safe (syntax-rules ()
  469. ((_ exp rest ...)
  470. (begin
  471. (catch 'system-error
  472. (lambda () exp)
  473. (const #f))
  474. (fail-safe rest ...)))
  475. ((_)
  476. #t))))
  477. ;; Ignore I/O errors so the system can boot.
  478. (fail-safe
  479. ;; Remove stale Shadow lock files as they would lead to
  480. ;; failures of 'useradd' & co.
  481. (delete-file "/etc/group.lock")
  482. (delete-file "/etc/passwd.lock")
  483. (delete-file "/etc/.pwd.lock") ;from 'lckpwdf'
  484. ;; Force file names to be decoded as UTF-8. See
  485. ;; <https://bugs.gnu.org/26353>.
  486. (setenv "GUIX_LOCPATH"
  487. #+(file-append glibc-utf8-locales "/lib/locale"))
  488. (setlocale LC_CTYPE "en_US.utf8")
  489. (delete-file-recursively "/tmp")
  490. (delete-file-recursively "/var/run")
  491. (mkdir "/tmp")
  492. (chmod "/tmp" #o1777)
  493. (mkdir "/var/run")
  494. (chmod "/var/run" #o755)
  495. (delete-file-recursively "/run/udev/watch.old"))))))
  496. (define cleanup-service-type
  497. ;; Service that cleans things up in /tmp and similar.
  498. (service-type (name 'cleanup)
  499. (extensions
  500. (list (service-extension boot-service-type
  501. cleanup-gexp)))
  502. (description
  503. "Delete files from @file{/tmp}, @file{/var/run}, and other
  504. temporary locations at boot time.")))
  505. (define* (activation-service->script service)
  506. "Return as a monadic value the activation script for SERVICE, a service of
  507. ACTIVATION-SCRIPT-TYPE."
  508. (activation-script (service-value service)))
  509. (define (activation-script gexps)
  510. "Return the system's activation script, which evaluates GEXPS."
  511. (define actions
  512. (map (cut program-file "activate-service.scm" <>) gexps))
  513. (program-file "activate.scm"
  514. (with-imported-modules (source-module-closure
  515. '((gnu build activation)
  516. (guix build utils)))
  517. #~(begin
  518. (use-modules (gnu build activation)
  519. (guix build utils))
  520. ;; Make sure the user accounting database exists. If it
  521. ;; does not exist, 'setutxent' does not create it and
  522. ;; thus there is no accounting at all.
  523. (close-port (open-file "/var/run/utmpx" "a0"))
  524. ;; Same for 'wtmp', which is populated by mingetty et
  525. ;; al.
  526. (mkdir-p "/var/log")
  527. (close-port (open-file "/var/log/wtmp" "a0"))
  528. ;; Set up /run/current-system. Among other things this
  529. ;; sets up locales, which the activation snippets
  530. ;; executed below may expect.
  531. (activate-current-system)
  532. ;; Run the services' activation snippets.
  533. ;; TODO: Use 'load-compiled'.
  534. (for-each primitive-load '#$actions)))))
  535. (define (gexps->activation-gexp gexps)
  536. "Return a gexp that runs the activation script containing GEXPS."
  537. #~(primitive-load #$(activation-script gexps)))
  538. (define (second-argument a b) b)
  539. (define activation-service-type
  540. (service-type (name 'activate)
  541. (extensions
  542. (list (service-extension boot-service-type
  543. gexps->activation-gexp)))
  544. (compose identity)
  545. (extend second-argument)
  546. (description
  547. "Run @dfn{activation} code at boot time and upon
  548. @command{guix system reconfigure} completion.")))
  549. (define %activation-service
  550. ;; The activation service produces the activation script from the gexps it
  551. ;; receives.
  552. (service activation-service-type #t))
  553. (define %modprobe-wrapper
  554. ;; Wrapper for the 'modprobe' command that knows where modules live.
  555. ;;
  556. ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
  557. ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
  558. ;; environment variable is not set---hence the need for this wrapper.
  559. (let ((modprobe "/run/current-system/profile/bin/modprobe"))
  560. (program-file "modprobe"
  561. #~(begin
  562. (setenv "LINUX_MODULE_DIRECTORY"
  563. "/run/booted-system/kernel/lib/modules")
  564. ;; FIXME: Remove this crutch when the patch #40422,
  565. ;; updating to kmod 27 is merged.
  566. (setenv "MODPROBE_OPTIONS"
  567. "-C /etc/modprobe.d")
  568. (apply execl #$modprobe
  569. (cons #$modprobe (cdr (command-line))))))))
  570. (define %linux-kernel-activation
  571. ;; Activation of the Linux kernel running on the bare metal (as opposed to
  572. ;; running in a container.)
  573. #~(begin
  574. ;; Tell the kernel to use our 'modprobe' command.
  575. (activate-modprobe #$%modprobe-wrapper)
  576. ;; Let users debug their own processes!
  577. (activate-ptrace-attach)))
  578. (define %linux-bare-metal-service
  579. ;; The service that does things that are needed on the "bare metal", but not
  580. ;; necessary or impossible in a container.
  581. (simple-service 'linux-bare-metal
  582. activation-service-type
  583. %linux-kernel-activation))
  584. (define %hurd-rc-script
  585. ;; The RC script to be started upon boot.
  586. (program-file "rc"
  587. (with-imported-modules (source-module-closure
  588. '((guix build utils)
  589. (gnu build hurd-boot)
  590. (guix build syscalls)))
  591. #~(begin
  592. (use-modules (guix build utils)
  593. (gnu build hurd-boot)
  594. (guix build syscalls)
  595. (ice-9 match)
  596. (system repl repl)
  597. (srfi srfi-1)
  598. (srfi srfi-26))
  599. (boot-hurd-system)))))
  600. (define (hurd-rc-entry rc)
  601. "Return, as a monadic value, an entry for the RC script in the system
  602. directory."
  603. (mlet %store-monad ((rc (lower-object rc)))
  604. (return `(("rc" ,rc)))))
  605. (define hurd-startup-service-type
  606. ;; The service that creates the initial SYSTEM/rc startup file.
  607. (service-type (name 'startup)
  608. (extensions
  609. (list (service-extension system-service-type hurd-rc-entry)))
  610. (default-value %hurd-rc-script)))
  611. (define %hurd-startup-service
  612. ;; The service that produces the RC script.
  613. (service hurd-startup-service-type %hurd-rc-script))
  614. (define special-files-service-type
  615. ;; Service to install "special files" such as /bin/sh and /usr/bin/env.
  616. (service-type
  617. (name 'special-files)
  618. (extensions
  619. (list (service-extension activation-service-type
  620. (lambda (files)
  621. #~(activate-special-files '#$files)))))
  622. (compose concatenate)
  623. (extend append)
  624. (description
  625. "Add special files to the root file system---e.g.,
  626. @file{/usr/bin/env}.")))
  627. (define (extra-special-file file target)
  628. "Use TARGET as the \"special file\" FILE. For example, TARGET might be
  629. (file-append coreutils \"/bin/env\")
  630. and FILE could be \"/usr/bin/env\"."
  631. (simple-service (string->symbol (string-append "special-file-" file))
  632. special-files-service-type
  633. `((,file ,target))))
  634. (define (etc-directory service)
  635. "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
  636. (files->etc-directory (service-value service)))
  637. (define (files->etc-directory files)
  638. (define (assert-no-duplicates files)
  639. (let loop ((files files)
  640. (seen (set)))
  641. (match files
  642. (() #t)
  643. (((file _) rest ...)
  644. (when (set-contains? seen file)
  645. (raise (formatted-message (G_ "duplicate '~a' entry for /etc")
  646. file)))
  647. (loop rest (set-insert file seen))))))
  648. ;; Detect duplicates early instead of letting them through, eventually
  649. ;; leading to a build failure of "etc.drv".
  650. (assert-no-duplicates files)
  651. (file-union "etc" files))
  652. (define (etc-entry files)
  653. "Return an entry for the /etc directory consisting of FILES in the system
  654. directory."
  655. (with-monad %store-monad
  656. (return `(("etc" ,(files->etc-directory files))))))
  657. (define etc-service-type
  658. (service-type (name 'etc)
  659. (extensions
  660. (list
  661. (service-extension activation-service-type
  662. (lambda (files)
  663. (let ((etc
  664. (files->etc-directory files)))
  665. #~(activate-etc #$etc))))
  666. (service-extension system-service-type etc-entry)))
  667. (compose concatenate)
  668. (extend append)
  669. (description "Populate the @file{/etc} directory.")))
  670. (define (etc-service files)
  671. "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
  672. FILES must be a list of name/file-like object pairs."
  673. (service etc-service-type files))
  674. (define setuid-program-service-type
  675. (service-type (name 'setuid-program)
  676. (extensions
  677. (list (service-extension activation-service-type
  678. (lambda (programs)
  679. #~(activate-setuid-programs
  680. (list #$@programs))))))
  681. (compose concatenate)
  682. (extend append)
  683. (description
  684. "Populate @file{/run/setuid-programs} with the specified
  685. executables, making them setuid-root.")))
  686. (define (packages->profile-entry packages)
  687. "Return a system entry for the profile containing PACKAGES."
  688. (with-monad %store-monad
  689. (return `(("profile" ,(profile
  690. (content (packages->manifest
  691. (delete-duplicates packages eq?)))))))))
  692. (define profile-service-type
  693. ;; The service that populates the system's profile---i.e.,
  694. ;; /run/current-system/profile. It is extended by package lists.
  695. (service-type (name 'profile)
  696. (extensions
  697. (list (service-extension system-service-type
  698. packages->profile-entry)))
  699. (compose concatenate)
  700. (extend append)
  701. (description
  702. "This is the @dfn{system profile}, available as
  703. @file{/run/current-system/profile}. It contains packages that the sysadmin
  704. wants to be globally available to all the system users.")))
  705. (define (firmware->activation-gexp firmware)
  706. "Return a gexp to make the packages listed in FIRMWARE loadable by the
  707. kernel."
  708. (let ((directory (directory-union "firmware" firmware)))
  709. ;; Tell the kernel where firmware is.
  710. #~(activate-firmware (string-append #$directory "/lib/firmware"))))
  711. (define firmware-service-type
  712. ;; The service that collects firmware.
  713. (service-type (name 'firmware)
  714. (extensions
  715. (list (service-extension activation-service-type
  716. firmware->activation-gexp)))
  717. (compose concatenate)
  718. (extend append)
  719. (description
  720. "Make ``firmware'' files loadable by the operating system
  721. kernel. Firmware may then be uploaded to some of the machine's devices, such
  722. as Wifi cards.")))
  723. (define (gc-roots->system-entry roots)
  724. "Return an entry in the system's output containing symlinks to ROOTS."
  725. (mlet %store-monad ((entry (gexp->derivation
  726. "gc-roots"
  727. #~(let ((roots '#$roots))
  728. (mkdir #$output)
  729. (chdir #$output)
  730. (for-each symlink
  731. roots
  732. (map number->string
  733. (iota (length roots))))))))
  734. (return (if (null? roots)
  735. '()
  736. `(("gc-roots" ,entry))))))
  737. (define gc-root-service-type
  738. ;; A service to associate extra garbage-collector roots to the system. This
  739. ;; is a simple hack that guarantees that the system retains references to
  740. ;; the given list of roots. Roots must be "lowerable" objects like
  741. ;; packages, or derivations.
  742. (service-type (name 'gc-roots)
  743. (extensions
  744. (list (service-extension system-service-type
  745. gc-roots->system-entry)))
  746. (compose concatenate)
  747. (extend append)
  748. (description
  749. "Register garbage-collector roots---i.e., store items that
  750. will not be reclaimed by the garbage collector.")
  751. (default-value '())))
  752. ;;;
  753. ;;; Service folding.
  754. ;;;
  755. (define-condition-type &missing-target-service-error &service-error
  756. missing-target-service-error?
  757. (service missing-target-service-error-service)
  758. (target-type missing-target-service-error-target-type))
  759. (define-condition-type &ambiguous-target-service-error &service-error
  760. ambiguous-target-service-error?
  761. (service ambiguous-target-service-error-service)
  762. (target-type ambiguous-target-service-error-target-type))
  763. (define (missing-target-error service target-type)
  764. (raise
  765. (condition (&missing-target-service-error
  766. (service service)
  767. (target-type target-type))
  768. (&message
  769. (message
  770. (format #f (G_ "no target of type '~a' for service '~a'")
  771. (service-type-name target-type)
  772. (service-type-name
  773. (service-kind service))))))))
  774. (define (service-back-edges services)
  775. "Return a procedure that, when passed a <service>, returns the list of
  776. <service> objects that depend on it."
  777. (define (add-edges service edges)
  778. (define (add-edge extension edges)
  779. (let ((target-type (service-extension-target extension)))
  780. (match (filter (lambda (service)
  781. (eq? (service-kind service) target-type))
  782. services)
  783. ((target)
  784. (vhash-consq target service edges))
  785. (()
  786. (missing-target-error service target-type))
  787. (x
  788. (raise
  789. (condition (&ambiguous-target-service-error
  790. (service service)
  791. (target-type target-type))
  792. (&message
  793. (message
  794. (format #f
  795. (G_ "more than one target service of type '~a'")
  796. (service-type-name target-type))))))))))
  797. (fold add-edge edges (service-type-extensions (service-kind service))))
  798. (let ((edges (fold add-edges vlist-null services)))
  799. (lambda (node)
  800. (reverse (vhash-foldq* cons '() node edges)))))
  801. (define (instantiate-missing-services services)
  802. "Return SERVICES, a list, augmented with any services targeted by extensions
  803. and missing from SERVICES. Only service types with a default value can be
  804. instantiated; other missing services lead to a
  805. '&missing-target-service-error'."
  806. (define (adjust-service-list svc result instances)
  807. (fold2 (lambda (extension result instances)
  808. (define target-type
  809. (service-extension-target extension))
  810. (match (vhash-assq target-type instances)
  811. (#f
  812. (let ((default (service-type-default-value target-type)))
  813. (if (eq? &no-default-value default)
  814. (missing-target-error svc target-type)
  815. (let ((new (service target-type)))
  816. (values (cons new result)
  817. (vhash-consq target-type new instances))))))
  818. (_
  819. (values result instances))))
  820. result
  821. instances
  822. (service-type-extensions (service-kind svc))))
  823. (let loop ((services services))
  824. (define instances
  825. (fold (lambda (service result)
  826. (vhash-consq (service-kind service) service
  827. result))
  828. vlist-null services))
  829. (define adjusted
  830. (fold2 adjust-service-list
  831. services instances
  832. services))
  833. ;; If we instantiated services, they might in turn depend on missing
  834. ;; services. Loop until we've reached fixed point.
  835. (if (= (length adjusted) (vlist-length instances))
  836. adjusted
  837. (loop adjusted))))
  838. (define* (fold-services services
  839. #:key (target-type system-service-type))
  840. "Fold SERVICES by propagating their extensions down to the root of type
  841. TARGET-TYPE; return the root service adjusted accordingly."
  842. (define dependents
  843. (service-back-edges services))
  844. (define (matching-extension target)
  845. (let ((target (service-kind target)))
  846. (match-lambda
  847. (($ <service-extension> type)
  848. (eq? type target)))))
  849. (define (apply-extension target)
  850. (lambda (service)
  851. (match (find (matching-extension target)
  852. (service-type-extensions (service-kind service)))
  853. (($ <service-extension> _ compute)
  854. (compute (service-value service))))))
  855. (match (filter (lambda (service)
  856. (eq? (service-kind service) target-type))
  857. services)
  858. ((sink)
  859. ;; Use the state monad to keep track of already-visited services in the
  860. ;; graph and to memoize their value once folded.
  861. (run-with-state
  862. (let loop ((sink sink))
  863. (mlet %state-monad ((visited (current-state)))
  864. (match (vhash-assq sink visited)
  865. (#f
  866. (mlet* %state-monad
  867. ((dependents (mapm %state-monad loop (dependents sink)))
  868. (visited (current-state))
  869. (extensions -> (map (apply-extension sink) dependents))
  870. (extend -> (service-type-extend (service-kind sink)))
  871. (compose -> (service-type-compose (service-kind sink)))
  872. (params -> (service-value sink))
  873. (service
  874. ->
  875. ;; Distinguish COMPOSE and EXTEND because PARAMS typically
  876. ;; has a different type than the elements of EXTENSIONS.
  877. (if extend
  878. (service (service-kind sink)
  879. (extend params (compose extensions)))
  880. sink)))
  881. (mbegin %state-monad
  882. (set-current-state (vhash-consq sink service visited))
  883. (return service))))
  884. ((_ . service) ;SINK was already visited
  885. (return service)))))
  886. vlist-null))
  887. (()
  888. (raise
  889. (make-compound-condition
  890. (condition (&missing-target-service-error
  891. (service #f)
  892. (target-type target-type)))
  893. (formatted-message (G_ "service of type '~a' not found")
  894. (service-type-name target-type)))))
  895. (x
  896. (raise
  897. (condition (&ambiguous-target-service-error
  898. (service #f)
  899. (target-type target-type))
  900. (&message
  901. (message
  902. (format #f
  903. (G_ "more than one target service of type '~a'")
  904. (service-type-name target-type)))))))))
  905. ;;; services.scm ends here.