services.scm 47 KB

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