services.scm 47 KB

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