services.scm 47 KB

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