services.scm 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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 (activation-profile-entry gexps)
  544. "Return, as a monadic value, an entry for the activation script in the
  545. system directory."
  546. (mlet %store-monad ((activate (lower-object (activation-script gexps))))
  547. (return `(("activate" ,activate)))))
  548. (define (second-argument a b) b)
  549. (define activation-service-type
  550. (service-type (name 'activate)
  551. (extensions
  552. (list (service-extension boot-service-type
  553. gexps->activation-gexp)
  554. (service-extension system-service-type
  555. activation-profile-entry)))
  556. (compose identity)
  557. (extend second-argument)
  558. (description
  559. "Run @dfn{activation} code at boot time and upon
  560. @command{guix system reconfigure} completion.")))
  561. (define %activation-service
  562. ;; The activation service produces the activation script from the gexps it
  563. ;; receives.
  564. (service activation-service-type #t))
  565. (define %modprobe-wrapper
  566. ;; Wrapper for the 'modprobe' command that knows where modules live.
  567. ;;
  568. ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
  569. ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
  570. ;; environment variable is not set---hence the need for this wrapper.
  571. (let ((modprobe "/run/current-system/profile/bin/modprobe"))
  572. (program-file "modprobe"
  573. #~(begin
  574. (setenv "LINUX_MODULE_DIRECTORY"
  575. "/run/booted-system/kernel/lib/modules")
  576. ;; FIXME: Remove this crutch when the patch #40422,
  577. ;; updating to kmod 27 is merged.
  578. (setenv "MODPROBE_OPTIONS"
  579. "-C /etc/modprobe.d")
  580. (apply execl #$modprobe
  581. (cons #$modprobe (cdr (command-line))))))))
  582. (define %linux-kernel-activation
  583. ;; Activation of the Linux kernel running on the bare metal (as opposed to
  584. ;; running in a container.)
  585. #~(begin
  586. ;; Tell the kernel to use our 'modprobe' command.
  587. (activate-modprobe #$%modprobe-wrapper)
  588. ;; Let users debug their own processes!
  589. (activate-ptrace-attach)))
  590. (define %linux-bare-metal-service
  591. ;; The service that does things that are needed on the "bare metal", but not
  592. ;; necessary or impossible in a container.
  593. (simple-service 'linux-bare-metal
  594. activation-service-type
  595. %linux-kernel-activation))
  596. (define %hurd-rc-script
  597. ;; The RC script to be started upon boot.
  598. (program-file "rc"
  599. (with-imported-modules (source-module-closure
  600. '((guix build utils)
  601. (gnu build hurd-boot)
  602. (guix build syscalls)))
  603. #~(begin
  604. (use-modules (guix build utils)
  605. (gnu build hurd-boot)
  606. (guix build syscalls)
  607. (ice-9 match)
  608. (system repl repl)
  609. (srfi srfi-1)
  610. (srfi srfi-26))
  611. (boot-hurd-system)))))
  612. (define (hurd-rc-entry rc)
  613. "Return, as a monadic value, an entry for the RC script in the system
  614. directory."
  615. (mlet %store-monad ((rc (lower-object rc)))
  616. (return `(("rc" ,rc)))))
  617. (define hurd-startup-service-type
  618. ;; The service that creates the initial SYSTEM/rc startup file.
  619. (service-type (name 'startup)
  620. (extensions
  621. (list (service-extension system-service-type hurd-rc-entry)))
  622. (default-value %hurd-rc-script)))
  623. (define %hurd-startup-service
  624. ;; The service that produces the RC script.
  625. (service hurd-startup-service-type %hurd-rc-script))
  626. (define special-files-service-type
  627. ;; Service to install "special files" such as /bin/sh and /usr/bin/env.
  628. (service-type
  629. (name 'special-files)
  630. (extensions
  631. (list (service-extension activation-service-type
  632. (lambda (files)
  633. #~(activate-special-files '#$files)))))
  634. (compose concatenate)
  635. (extend append)
  636. (description
  637. "Add special files to the root file system---e.g.,
  638. @file{/usr/bin/env}.")))
  639. (define (extra-special-file file target)
  640. "Use TARGET as the \"special file\" FILE. For example, TARGET might be
  641. (file-append coreutils \"/bin/env\")
  642. and FILE could be \"/usr/bin/env\"."
  643. (simple-service (string->symbol (string-append "special-file-" file))
  644. special-files-service-type
  645. `((,file ,target))))
  646. (define (etc-directory service)
  647. "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
  648. (files->etc-directory (service-value service)))
  649. (define (files->etc-directory files)
  650. (define (assert-no-duplicates files)
  651. (let loop ((files files)
  652. (seen (set)))
  653. (match files
  654. (() #t)
  655. (((file _) rest ...)
  656. (when (set-contains? seen file)
  657. (raise (formatted-message (G_ "duplicate '~a' entry for /etc")
  658. file)))
  659. (loop rest (set-insert file seen))))))
  660. ;; Detect duplicates early instead of letting them through, eventually
  661. ;; leading to a build failure of "etc.drv".
  662. (assert-no-duplicates files)
  663. (file-union "etc" files))
  664. (define (etc-entry files)
  665. "Return an entry for the /etc directory consisting of FILES in the system
  666. directory."
  667. (with-monad %store-monad
  668. (return `(("etc" ,(files->etc-directory files))))))
  669. (define etc-service-type
  670. (service-type (name 'etc)
  671. (extensions
  672. (list
  673. (service-extension activation-service-type
  674. (lambda (files)
  675. (let ((etc
  676. (files->etc-directory files)))
  677. #~(activate-etc #$etc))))
  678. (service-extension system-service-type etc-entry)))
  679. (compose concatenate)
  680. (extend append)
  681. (description "Populate the @file{/etc} directory.")))
  682. (define (etc-service files)
  683. "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
  684. FILES must be a list of name/file-like object pairs."
  685. (service etc-service-type files))
  686. (define setuid-program-service-type
  687. (service-type (name 'setuid-program)
  688. (extensions
  689. (list (service-extension activation-service-type
  690. (lambda (programs)
  691. #~(activate-setuid-programs
  692. (list #$@programs))))))
  693. (compose concatenate)
  694. (extend append)
  695. (description
  696. "Populate @file{/run/setuid-programs} with the specified
  697. executables, making them setuid-root.")))
  698. (define (packages->profile-entry packages)
  699. "Return a system entry for the profile containing PACKAGES."
  700. ;; XXX: 'mlet' is needed here for one reason: to get the proper
  701. ;; '%current-target' and '%current-target-system' bindings when
  702. ;; 'packages->manifest' is called, and thus when the 'package-inputs'
  703. ;; etc. procedures are called on PACKAGES. That way, conditionals in those
  704. ;; inputs see the "correct" value of these two parameters. See
  705. ;; <https://issues.guix.gnu.org/44952>.
  706. (mlet %store-monad ((_ (current-target-system)))
  707. (return `(("profile" ,(profile
  708. (content (packages->manifest
  709. (delete-duplicates packages eq?)))))))))
  710. (define profile-service-type
  711. ;; The service that populates the system's profile---i.e.,
  712. ;; /run/current-system/profile. It is extended by package lists.
  713. (service-type (name 'profile)
  714. (extensions
  715. (list (service-extension system-service-type
  716. packages->profile-entry)))
  717. (compose concatenate)
  718. (extend append)
  719. (description
  720. "This is the @dfn{system profile}, available as
  721. @file{/run/current-system/profile}. It contains packages that the sysadmin
  722. wants to be globally available to all the system users.")))
  723. (define (firmware->activation-gexp firmware)
  724. "Return a gexp to make the packages listed in FIRMWARE loadable by the
  725. kernel."
  726. (let ((directory (directory-union "firmware" firmware)))
  727. ;; Tell the kernel where firmware is.
  728. #~(activate-firmware (string-append #$directory "/lib/firmware"))))
  729. (define firmware-service-type
  730. ;; The service that collects firmware.
  731. (service-type (name 'firmware)
  732. (extensions
  733. (list (service-extension activation-service-type
  734. firmware->activation-gexp)))
  735. (compose concatenate)
  736. (extend append)
  737. (description
  738. "Make ``firmware'' files loadable by the operating system
  739. kernel. Firmware may then be uploaded to some of the machine's devices, such
  740. as Wifi cards.")))
  741. (define (gc-roots->system-entry roots)
  742. "Return an entry in the system's output containing symlinks to ROOTS."
  743. (mlet %store-monad ((entry (gexp->derivation
  744. "gc-roots"
  745. #~(let ((roots '#$roots))
  746. (mkdir #$output)
  747. (chdir #$output)
  748. (for-each symlink
  749. roots
  750. (map number->string
  751. (iota (length roots))))))))
  752. (return (if (null? roots)
  753. '()
  754. `(("gc-roots" ,entry))))))
  755. (define gc-root-service-type
  756. ;; A service to associate extra garbage-collector roots to the system. This
  757. ;; is a simple hack that guarantees that the system retains references to
  758. ;; the given list of roots. Roots must be "lowerable" objects like
  759. ;; packages, or derivations.
  760. (service-type (name 'gc-roots)
  761. (extensions
  762. (list (service-extension system-service-type
  763. gc-roots->system-entry)))
  764. (compose concatenate)
  765. (extend append)
  766. (description
  767. "Register garbage-collector roots---i.e., store items that
  768. will not be reclaimed by the garbage collector.")
  769. (default-value '())))
  770. ;;;
  771. ;;; Service folding.
  772. ;;;
  773. (define-condition-type &missing-target-service-error &service-error
  774. missing-target-service-error?
  775. (service missing-target-service-error-service)
  776. (target-type missing-target-service-error-target-type))
  777. (define-condition-type &ambiguous-target-service-error &service-error
  778. ambiguous-target-service-error?
  779. (service ambiguous-target-service-error-service)
  780. (target-type ambiguous-target-service-error-target-type))
  781. (define (missing-target-error service target-type)
  782. (raise
  783. (condition (&missing-target-service-error
  784. (service service)
  785. (target-type target-type))
  786. (&message
  787. (message
  788. (format #f (G_ "no target of type '~a' for service '~a'")
  789. (service-type-name target-type)
  790. (service-type-name
  791. (service-kind service))))))))
  792. (define (service-back-edges services)
  793. "Return a procedure that, when passed a <service>, returns the list of
  794. <service> objects that depend on it."
  795. (define (add-edges service edges)
  796. (define (add-edge extension edges)
  797. (let ((target-type (service-extension-target extension)))
  798. (match (filter (lambda (service)
  799. (eq? (service-kind service) target-type))
  800. services)
  801. ((target)
  802. (vhash-consq target service edges))
  803. (()
  804. (missing-target-error service target-type))
  805. (x
  806. (raise
  807. (condition (&ambiguous-target-service-error
  808. (service service)
  809. (target-type target-type))
  810. (&message
  811. (message
  812. (format #f
  813. (G_ "more than one target service of type '~a'")
  814. (service-type-name target-type))))))))))
  815. (fold add-edge edges (service-type-extensions (service-kind service))))
  816. (let ((edges (fold add-edges vlist-null services)))
  817. (lambda (node)
  818. (reverse (vhash-foldq* cons '() node edges)))))
  819. (define (instantiate-missing-services services)
  820. "Return SERVICES, a list, augmented with any services targeted by extensions
  821. and missing from SERVICES. Only service types with a default value can be
  822. instantiated; other missing services lead to a
  823. '&missing-target-service-error'."
  824. (define (adjust-service-list svc result instances)
  825. (fold2 (lambda (extension result instances)
  826. (define target-type
  827. (service-extension-target extension))
  828. (match (vhash-assq target-type instances)
  829. (#f
  830. (let ((default (service-type-default-value target-type)))
  831. (if (eq? &no-default-value default)
  832. (missing-target-error svc target-type)
  833. (let ((new (service target-type)))
  834. (values (cons new result)
  835. (vhash-consq target-type new instances))))))
  836. (_
  837. (values result instances))))
  838. result
  839. instances
  840. (service-type-extensions (service-kind svc))))
  841. (let loop ((services services))
  842. (define instances
  843. (fold (lambda (service result)
  844. (vhash-consq (service-kind service) service
  845. result))
  846. vlist-null services))
  847. (define adjusted
  848. (fold2 adjust-service-list
  849. services instances
  850. services))
  851. ;; If we instantiated services, they might in turn depend on missing
  852. ;; services. Loop until we've reached fixed point.
  853. (if (= (length adjusted) (vlist-length instances))
  854. adjusted
  855. (loop adjusted))))
  856. (define* (fold-services services
  857. #:key (target-type system-service-type))
  858. "Fold SERVICES by propagating their extensions down to the root of type
  859. TARGET-TYPE; return the root service adjusted accordingly."
  860. (define dependents
  861. (service-back-edges services))
  862. (define (matching-extension target)
  863. (let ((target (service-kind target)))
  864. (match-lambda
  865. (($ <service-extension> type)
  866. (eq? type target)))))
  867. (define (apply-extension target)
  868. (lambda (service)
  869. (match (find (matching-extension target)
  870. (service-type-extensions (service-kind service)))
  871. (($ <service-extension> _ compute)
  872. (compute (service-value service))))))
  873. (match (filter (lambda (service)
  874. (eq? (service-kind service) target-type))
  875. services)
  876. ((sink)
  877. ;; Use the state monad to keep track of already-visited services in the
  878. ;; graph and to memoize their value once folded.
  879. (run-with-state
  880. (let loop ((sink sink))
  881. (mlet %state-monad ((visited (current-state)))
  882. (match (vhash-assq sink visited)
  883. (#f
  884. (mlet* %state-monad
  885. ((dependents (mapm %state-monad loop (dependents sink)))
  886. (visited (current-state))
  887. (extensions -> (map (apply-extension sink) dependents))
  888. (extend -> (service-type-extend (service-kind sink)))
  889. (compose -> (service-type-compose (service-kind sink)))
  890. (params -> (service-value sink))
  891. (service
  892. ->
  893. ;; Distinguish COMPOSE and EXTEND because PARAMS typically
  894. ;; has a different type than the elements of EXTENSIONS.
  895. (if extend
  896. (service (service-kind sink)
  897. (extend params (compose extensions)))
  898. sink)))
  899. (mbegin %state-monad
  900. (set-current-state (vhash-consq sink service visited))
  901. (return service))))
  902. ((_ . service) ;SINK was already visited
  903. (return service)))))
  904. vlist-null))
  905. (()
  906. (raise
  907. (make-compound-condition
  908. (condition (&missing-target-service-error
  909. (service #f)
  910. (target-type target-type)))
  911. (formatted-message (G_ "service of type '~a' not found")
  912. (service-type-name target-type)))))
  913. (x
  914. (raise
  915. (condition (&ambiguous-target-service-error
  916. (service #f)
  917. (target-type target-type))
  918. (&message
  919. (message
  920. (format #f
  921. (G_ "more than one target service of type '~a'")
  922. (service-type-name target-type)))))))))
  923. ;;; services.scm ends here.