guix.scm 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019, 2020, 2021, 2022 Christopher Baines <mail@cbaines.net>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify
  7. ;;; it under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation, either version 3 of the License, or
  9. ;;; (at your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful,
  12. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (gnu services guix)
  19. #:use-module (srfi srfi-1)
  20. #:use-module (ice-9 match)
  21. #:use-module (guix gexp)
  22. #:use-module (guix records)
  23. #:use-module (guix packages)
  24. #:use-module ((gnu packages base)
  25. #:select (glibc-utf8-locales))
  26. #:use-module (gnu packages admin)
  27. #:use-module (gnu packages databases)
  28. #:use-module (gnu packages web)
  29. #:use-module (gnu packages guile)
  30. #:use-module (gnu packages guile-xyz)
  31. #:use-module (gnu packages package-management)
  32. #:use-module (gnu services)
  33. #:use-module (gnu services base)
  34. #:use-module (gnu services admin)
  35. #:use-module (gnu services shepherd)
  36. #:use-module (gnu services getmail)
  37. #:use-module (gnu system shadow)
  38. #:export (guix-build-coordinator-configuration
  39. guix-build-coordinator-configuration?
  40. guix-build-coordinator-configuration-package
  41. guix-build-coordinator-configuration-user
  42. guix-build-coordinator-configuration-group
  43. guix-build-coordinator-configuration-datastore-uri-string
  44. guix-build-coordinator-configuration-agent-communication-uri-string
  45. guix-build-coordinator-configuration-client-communication-uri-string
  46. guix-build-coordinator-configuration-allocation-strategy
  47. guix-build-coordinator-configuration-hooks
  48. guix-build-coordinator-configuration-guile
  49. guix-build-coordinator-service-type
  50. guix-build-coordinator-agent-configuration
  51. guix-build-coordinator-agent-configuration?
  52. guix-build-coordinator-agent-configuration-package
  53. guix-build-coordinator-agent-configuration-user
  54. guix-build-coordinator-agent-configuration-coordinator
  55. guix-build-coordinator-agent-configuration-authentication
  56. guix-build-coordinator-agent-configuration-systems
  57. guix-build-coordinator-agent-configuration-max-parallel-builds
  58. guix-build-coordinator-agent-configuration-max-1min-load-average
  59. guix-build-coordinator-agent-configuration-derivation-substitute-urls
  60. guix-build-coordinator-agent-configuration-non-derivation-substitute-urls
  61. guix-build-coordinator-agent-password-auth
  62. guix-build-coordinator-agent-password-auth?
  63. guix-build-coordinator-agent-password-auth-uuid
  64. guix-build-coordinator-agent-password-auth-password
  65. guix-build-coordinator-agent-password-file-auth
  66. guix-build-coordinator-agent-password-file-auth?
  67. guix-build-coordinator-agent-password-file-auth-uuid
  68. guix-build-coordinator-agent-password-file-auth-password-file
  69. guix-build-coordinator-agent-dynamic-auth
  70. guix-build-coordinator-agent-dynamic-auth?
  71. guix-build-coordinator-agent-dynamic-auth-agent-name
  72. guix-build-coordinator-agent-dynamic-auth-token
  73. guix-build-coordinator-agent-dynamic-auth-with-file
  74. guix-build-coordinator-agent-dynamic-auth-with-file?
  75. guix-build-coordinator-agent-dynamic-auth-with-file-agent-name
  76. guix-build-coordinator-agent-dynamic-auth-with-file-token-file
  77. guix-build-coordinator-agent-service-type
  78. guix-build-coordinator-queue-builds-configuration
  79. guix-build-coordinator-queue-builds-configuration?
  80. guix-build-coordinator-queue-builds-configuration-package
  81. guix-build-coordinator-queue-builds-configuration-user
  82. guix-build-coordinator-queue-builds-coordinator
  83. guix-build-coordinator-queue-builds-configuration-systems
  84. guix-build-coordinator-queue-builds-configuration-system-and-targets
  85. guix-build-coordinator-queue-builds-configuration-guix-data-service
  86. guix-build-coordinator-queue-builds-configuration-processed-commits-file
  87. guix-build-coordinator-queue-builds-service-type
  88. <guix-data-service-configuration>
  89. guix-data-service-configuration
  90. guix-data-service-configuration?
  91. guix-data-service-package
  92. guix-data-service-user
  93. guix-data-service-group
  94. guix-data-service-port
  95. guix-data-service-host
  96. guix-data-service-getmail-idle-mailboxes
  97. guix-data-service-commits-getmail-retriever-configuration
  98. guix-data-service-type
  99. nar-herder-service-type
  100. nar-herder-configuration
  101. nar-herder-configuration?
  102. nar-herder-configuration-package
  103. nar-herder-configuration-user
  104. nar-herder-configuration-group
  105. nar-herder-configuration-mirror
  106. nar-herder-configuration-database
  107. nar-herder-configuration-database-dump
  108. nar-herder-configuration-host
  109. nar-herder-configuration-port
  110. nar-herder-configuration-storage
  111. nar-herder-configuration-storage-limit
  112. nar-herder-configuration-storage-nar-removal-criteria))
  113. ;;;; Commentary:
  114. ;;;
  115. ;;; Services specifically related to GNU Guix.
  116. ;;;
  117. ;;;; Code:
  118. (define-record-type* <guix-build-coordinator-configuration>
  119. guix-build-coordinator-configuration make-guix-build-coordinator-configuration
  120. guix-build-coordinator-configuration?
  121. (package guix-build-coordinator-configuration-package
  122. (default guix-build-coordinator))
  123. (user guix-build-coordinator-configuration-user
  124. (default "guix-build-coordinator"))
  125. (group guix-build-coordinator-configuration-group
  126. (default "guix-build-coordinator"))
  127. (database-uri-string
  128. guix-build-coordinator-configuration-datastore-uri-string
  129. (default "sqlite:///var/lib/guix-build-coordinator/guix_build_coordinator.db"))
  130. (agent-communication-uri-string
  131. guix-build-coordinator-configuration-agent-communication-uri-string
  132. (default "http://0.0.0.0:8745"))
  133. (client-communication-uri-string
  134. guix-build-coordinator-configuration-client-communication-uri-string
  135. (default "http://127.0.0.1:8746"))
  136. (allocation-strategy
  137. guix-build-coordinator-configuration-allocation-strategy
  138. (default #~basic-build-allocation-strategy))
  139. (hooks guix-build-coordinator-configuration-hooks
  140. (default '()))
  141. (guile guix-build-coordinator-configuration-guile
  142. (default guile-3.0-latest)))
  143. (define-record-type* <guix-build-coordinator-agent-configuration>
  144. guix-build-coordinator-agent-configuration
  145. make-guix-build-coordinator-agent-configuration
  146. guix-build-coordinator-agent-configuration?
  147. (package guix-build-coordinator-agent-configuration-package
  148. (default guix-build-coordinator/agent-only))
  149. (user guix-build-coordinator-agent-configuration-user
  150. (default "guix-build-coordinator-agent"))
  151. (coordinator guix-build-coordinator-agent-configuration-coordinator
  152. (default "http://localhost:8745"))
  153. (authentication guix-build-coordinator-agent-configuration-authentication)
  154. (systems guix-build-coordinator-agent-configuration-systems
  155. (default #f))
  156. (max-parallel-builds
  157. guix-build-coordinator-agent-configuration-max-parallel-builds
  158. (default 1))
  159. (max-1min-load-average
  160. guix-build-coordinator-agent-configuration-max-1min-load-average
  161. (default #f))
  162. (derivation-substitute-urls
  163. guix-build-coordinator-agent-configuration-derivation-substitute-urls
  164. (default #f))
  165. (non-derivation-substitute-urls
  166. guix-build-coordinator-agent-configuration-non-derivation-substitute-urls
  167. (default #f)))
  168. (define-record-type* <guix-build-coordinator-agent-password-auth>
  169. guix-build-coordinator-agent-password-auth
  170. make-guix-build-coordinator-agent-password-auth
  171. guix-build-coordinator-agent-password-auth?
  172. (uuid guix-build-coordinator-agent-password-auth-uuid)
  173. (password guix-build-coordinator-agent-password-auth-password))
  174. (define-record-type* <guix-build-coordinator-agent-password-file-auth>
  175. guix-build-coordinator-agent-password-file-auth
  176. make-guix-build-coordinator-agent-password-file-auth
  177. guix-build-coordinator-agent-password-file-auth?
  178. (uuid guix-build-coordinator-agent-password-file-auth-uuid)
  179. (password-file
  180. guix-build-coordinator-agent-password-file-auth-password-file))
  181. (define-record-type* <guix-build-coordinator-agent-dynamic-auth>
  182. guix-build-coordinator-agent-dynamic-auth
  183. make-guix-build-coordinator-agent-dynamic-auth
  184. guix-build-coordinator-agent-dynamic-auth?
  185. (agent-name guix-build-coordinator-agent-dynamic-auth-agent-name)
  186. (token guix-build-coordinator-agent-dynamic-auth-token))
  187. (define-record-type* <guix-build-coordinator-agent-dynamic-auth-with-file>
  188. guix-build-coordinator-agent-dynamic-auth-with-file
  189. make-guix-build-coordinator-agent-dynamic-auth-with-file
  190. guix-build-coordinator-agent-dynamic-auth-with-file?
  191. (agent-name guix-build-coordinator-agent-dynamic-auth-with-file-agent-name)
  192. (token-file guix-build-coordinator-agent-dynamic-auth-with-file-token-file))
  193. (define-record-type* <guix-build-coordinator-queue-builds-configuration>
  194. guix-build-coordinator-queue-builds-configuration
  195. make-guix-build-coordinator-queue-builds-configuration
  196. guix-build-coordinator-queue-builds-configuration?
  197. (package guix-build-coordinator-queue-builds-configuration-package
  198. (default guix-build-coordinator))
  199. (user guix-build-coordinator-queue-builds-configuration-user
  200. (default "guix-build-coordinator-queue-builds"))
  201. (coordinator guix-build-coordinator-queue-builds-coordinator
  202. (default "http://localhost:8746"))
  203. (systems guix-build-coordinator-queue-builds-configuration-systems
  204. (default #f))
  205. (systems-and-targets
  206. guix-build-coordinator-queue-builds-configuration-system-and-targets
  207. (default #f))
  208. (guix-data-service
  209. guix-build-coordinator-queue-builds-configuration-guix-data-service
  210. (default "https://data.guix.gnu.org"))
  211. (processed-commits-file
  212. guix-build-coordinator-queue-builds-configuration-processed-commits-file
  213. (default "/var/cache/guix-build-coordinator-queue-builds/processed-commits")))
  214. (define* (make-guix-build-coordinator-start-script database-uri-string
  215. allocation-strategy
  216. pid-file
  217. guix-build-coordinator-package
  218. #:key
  219. agent-communication-uri-string
  220. client-communication-uri-string
  221. (hooks '())
  222. (guile guile-3.0))
  223. (program-file
  224. "start-guix-build-coordinator"
  225. (with-extensions (cons guix-build-coordinator-package
  226. ;; This is a poorly constructed Guile load path,
  227. ;; since it contains things that aren't Guile
  228. ;; libraries, but it means that the Guile libraries
  229. ;; needed for the Guix Build Coordinator don't need
  230. ;; to be individually specified here.
  231. (append
  232. (map second (package-inputs
  233. guix-build-coordinator-package))
  234. (map second (package-propagated-inputs
  235. guix-build-coordinator-package))))
  236. #~(begin
  237. (use-modules (srfi srfi-1)
  238. (ice-9 match)
  239. (web uri)
  240. (prometheus)
  241. (guix-build-coordinator hooks)
  242. (guix-build-coordinator datastore)
  243. (guix-build-coordinator build-allocator)
  244. (guix-build-coordinator coordinator))
  245. (setvbuf (current-output-port) 'line)
  246. (setvbuf (current-error-port) 'line)
  247. (simple-format #t "starting the guix-build-coordinator:\n ~A\n"
  248. (current-filename))
  249. (let* ((metrics-registry (make-metrics-registry
  250. #:namespace
  251. "guixbuildcoordinator"))
  252. (datastore (database-uri->datastore
  253. #$database-uri-string
  254. #:metrics-registry metrics-registry))
  255. (hooks
  256. (list #$@(map (match-lambda
  257. ((name . hook-gexp)
  258. #~(cons '#$name #$hook-gexp)))
  259. hooks)))
  260. (hooks-with-defaults
  261. `(,@hooks
  262. ,@(remove (match-lambda
  263. ((name . _) (assq-ref hooks name)))
  264. %default-hooks)))
  265. (build-coordinator (make-build-coordinator
  266. #:datastore datastore
  267. #:hooks hooks-with-defaults
  268. #:metrics-registry metrics-registry
  269. #:allocation-strategy #$allocation-strategy)))
  270. (run-coordinator-service
  271. build-coordinator
  272. #:update-datastore? #t
  273. #:pid-file #$pid-file
  274. #:agent-communication-uri (string->uri
  275. #$agent-communication-uri-string)
  276. #:client-communication-uri (string->uri
  277. #$client-communication-uri-string)))))
  278. #:guile guile))
  279. (define (guix-build-coordinator-shepherd-services config)
  280. (match-record config <guix-build-coordinator-configuration>
  281. (package user group database-uri-string
  282. agent-communication-uri-string
  283. client-communication-uri-string
  284. allocation-strategy
  285. hooks
  286. guile)
  287. (list
  288. (shepherd-service
  289. (documentation "Guix Build Coordinator")
  290. (provision '(guix-build-coordinator))
  291. (requirement '(networking))
  292. (start #~(make-forkexec-constructor
  293. (list #$(make-guix-build-coordinator-start-script
  294. database-uri-string
  295. allocation-strategy
  296. "/var/run/guix-build-coordinator/pid"
  297. package
  298. #:agent-communication-uri-string
  299. agent-communication-uri-string
  300. #:client-communication-uri-string
  301. client-communication-uri-string
  302. #:hooks hooks
  303. #:guile guile))
  304. #:user #$user
  305. #:group #$group
  306. #:pid-file "/var/run/guix-build-coordinator/pid"
  307. ;; Allow time for migrations to run
  308. #:pid-file-timeout 60
  309. #:environment-variables
  310. `(,(string-append
  311. "GUIX_LOCPATH=" #$glibc-utf8-locales "/lib/locale")
  312. "LC_ALL=en_US.utf8"
  313. "PATH=/run/current-system/profile/bin") ; for hooks
  314. #:log-file "/var/log/guix-build-coordinator/coordinator.log"))
  315. (stop #~(make-kill-destructor))))))
  316. (define (guix-build-coordinator-activation config)
  317. #~(begin
  318. (use-modules (guix build utils))
  319. (define %user
  320. (getpw #$(guix-build-coordinator-configuration-user
  321. config)))
  322. (chmod "/var/lib/guix-build-coordinator" #o755)
  323. (mkdir-p "/var/log/guix-build-coordinator")
  324. ;; Allow writing the PID file
  325. (mkdir-p "/var/run/guix-build-coordinator")
  326. (chown "/var/run/guix-build-coordinator"
  327. (passwd:uid %user)
  328. (passwd:gid %user))))
  329. (define (guix-build-coordinator-account config)
  330. (match-record config <guix-build-coordinator-configuration>
  331. (user group)
  332. (list (user-group
  333. (name group)
  334. (system? #t))
  335. (user-account
  336. (name user)
  337. (group group)
  338. (system? #t)
  339. (comment "Guix Build Coordinator user")
  340. (home-directory "/var/lib/guix-build-coordinator")
  341. (shell (file-append shadow "/sbin/nologin"))))))
  342. (define guix-build-coordinator-service-type
  343. (service-type
  344. (name 'guix-build-coordinator)
  345. (extensions
  346. (list
  347. (service-extension shepherd-root-service-type
  348. guix-build-coordinator-shepherd-services)
  349. (service-extension activation-service-type
  350. guix-build-coordinator-activation)
  351. (service-extension account-service-type
  352. guix-build-coordinator-account)))
  353. (default-value
  354. (guix-build-coordinator-configuration))
  355. (description
  356. "Run an instance of the Guix Build Coordinator.")))
  357. (define (guix-build-coordinator-agent-shepherd-services config)
  358. (match-record config <guix-build-coordinator-agent-configuration>
  359. (package user coordinator authentication max-parallel-builds
  360. max-1min-load-average
  361. derivation-substitute-urls non-derivation-substitute-urls
  362. systems)
  363. (list
  364. (shepherd-service
  365. (documentation "Guix Build Coordinator Agent")
  366. (provision '(guix-build-coordinator-agent))
  367. (requirement '(networking))
  368. (start #~(make-forkexec-constructor
  369. (list #$(file-append package "/bin/guix-build-coordinator-agent")
  370. #$(string-append "--coordinator=" coordinator)
  371. #$@(match authentication
  372. (($ <guix-build-coordinator-agent-password-auth>
  373. uuid password)
  374. #~(#$(string-append "--uuid=" uuid)
  375. #$(string-append "--password=" password)))
  376. (($ <guix-build-coordinator-agent-password-file-auth>
  377. uuid password-file)
  378. #~(#$(string-append "--uuid=" uuid)
  379. #$(string-append "--password-file="
  380. password-file)))
  381. (($ <guix-build-coordinator-agent-dynamic-auth>
  382. agent-name token)
  383. #~(#$(string-append "--name=" agent-name)
  384. #$(string-append "--dynamic-auth-token=" token)))
  385. (($
  386. <guix-build-coordinator-agent-dynamic-auth-with-file>
  387. agent-name token-file)
  388. #~(#$(string-append "--name=" agent-name)
  389. #$(string-append "--dynamic-auth-token-file="
  390. token-file))))
  391. #$(simple-format #f "--max-parallel-builds=~A"
  392. max-parallel-builds)
  393. #$@(if max-1min-load-average
  394. #~(#$(simple-format #f "--max-1min-load-average=~A"
  395. max-1min-load-average))
  396. #~())
  397. #$@(if derivation-substitute-urls
  398. #~(#$(string-append
  399. "--derivation-substitute-urls="
  400. (string-join derivation-substitute-urls " ")))
  401. #~())
  402. #$@(if non-derivation-substitute-urls
  403. #~(#$(string-append
  404. "--non-derivation-substitute-urls="
  405. (string-join non-derivation-substitute-urls " ")))
  406. #~())
  407. #$@(map (lambda (system)
  408. (string-append "--system=" system))
  409. (or systems '())))
  410. #:user #$user
  411. #:environment-variables
  412. `(,(string-append
  413. "GUIX_LOCPATH=" #$glibc-utf8-locales "/lib/locale")
  414. ;; XDG_CACHE_HOME is used by Guix when caching narinfo files
  415. "XDG_CACHE_HOME=/var/cache/guix-build-coordinator-agent"
  416. "LC_ALL=en_US.utf8")
  417. #:log-file "/var/log/guix-build-coordinator/agent.log"))
  418. (stop #~(make-kill-destructor))))))
  419. (define (guix-build-coordinator-agent-activation config)
  420. #~(begin
  421. (use-modules (guix build utils))
  422. (define %user
  423. (getpw #$(guix-build-coordinator-agent-configuration-user
  424. config)))
  425. (mkdir-p "/var/log/guix-build-coordinator")
  426. ;; Create a cache directory for storing narinfo files if downloaded
  427. (mkdir-p "/var/cache/guix-build-coordinator-agent")
  428. (chown "/var/cache/guix-build-coordinator-agent"
  429. (passwd:uid %user)
  430. (passwd:gid %user))))
  431. (define (guix-build-coordinator-agent-account config)
  432. (list (user-account
  433. (name (guix-build-coordinator-agent-configuration-user config))
  434. (group "nogroup")
  435. (system? #t)
  436. (comment "Guix Build Coordinator agent user")
  437. (home-directory "/var/empty")
  438. (shell (file-append shadow "/sbin/nologin")))))
  439. (define guix-build-coordinator-agent-service-type
  440. (service-type
  441. (name 'guix-build-coordinator-agent)
  442. (extensions
  443. (list
  444. (service-extension shepherd-root-service-type
  445. guix-build-coordinator-agent-shepherd-services)
  446. (service-extension activation-service-type
  447. guix-build-coordinator-agent-activation)
  448. (service-extension account-service-type
  449. guix-build-coordinator-agent-account)))
  450. (description
  451. "Run a Guix Build Coordinator agent.")))
  452. (define (guix-build-coordinator-queue-builds-shepherd-services config)
  453. (match-record config <guix-build-coordinator-queue-builds-configuration>
  454. (package user coordinator systems systems-and-targets
  455. guix-data-service processed-commits-file)
  456. (list
  457. (shepherd-service
  458. (documentation "Guix Build Coordinator queue builds from Guix Data Service")
  459. (provision '(guix-build-coordinator-queue-builds))
  460. (requirement '(networking))
  461. (start
  462. #~(make-forkexec-constructor
  463. (list
  464. #$(file-append
  465. package
  466. "/bin/guix-build-coordinator-queue-builds-from-guix-data-service")
  467. #$(string-append "--coordinator=" coordinator)
  468. #$@(map (lambda (system)
  469. (string-append "--system=" system))
  470. (or systems '()))
  471. #$@(map (match-lambda
  472. ((system . target)
  473. (string-append "--system-and-target=" system "=" target)))
  474. (or systems-and-targets '()))
  475. #$@(if guix-data-service
  476. #~(#$(string-append "--guix-data-service=" guix-data-service))
  477. #~())
  478. #$@(if processed-commits-file
  479. #~(#$(string-append "--processed-commits-file="
  480. processed-commits-file))
  481. #~()))
  482. #:user #$user
  483. #:environment-variables
  484. `(,(string-append
  485. "GUIX_LOCPATH=" #$glibc-utf8-locales "/lib/locale")
  486. "LC_ALL=en_US.utf8")
  487. #:log-file "/var/log/guix-build-coordinator/queue-builds.log"))
  488. (stop #~(make-kill-destructor))))))
  489. (define (guix-build-coordinator-queue-builds-activation config)
  490. #~(begin
  491. (use-modules (guix build utils))
  492. (define %user
  493. (getpw #$(guix-build-coordinator-queue-builds-configuration-user
  494. config)))
  495. (mkdir-p "/var/log/guix-build-coordinator")
  496. ;; Allow writing the processed commits file
  497. (mkdir-p "/var/cache/guix-build-coordinator-queue-builds")
  498. (chown "/var/cache/guix-build-coordinator-queue-builds"
  499. (passwd:uid %user)
  500. (passwd:gid %user))))
  501. (define (guix-build-coordinator-queue-builds-account config)
  502. (list (user-account
  503. (name (guix-build-coordinator-queue-builds-configuration-user config))
  504. (group "nogroup")
  505. (system? #t)
  506. (comment "Guix Build Coordinator queue-builds user")
  507. (home-directory "/var/empty")
  508. (shell (file-append shadow "/sbin/nologin")))))
  509. (define guix-build-coordinator-queue-builds-service-type
  510. (service-type
  511. (name 'guix-build-coordinator-queue-builds)
  512. (extensions
  513. (list
  514. (service-extension shepherd-root-service-type
  515. guix-build-coordinator-queue-builds-shepherd-services)
  516. (service-extension activation-service-type
  517. guix-build-coordinator-queue-builds-activation)
  518. (service-extension account-service-type
  519. guix-build-coordinator-queue-builds-account)))
  520. (description
  521. "Run the guix-build-coordinator-queue-builds-from-guix-data-service
  522. script.
  523. This is a script to assist in having the Guix Build Coordinator build
  524. derivations stored in an instance of the Guix Data Service.")))
  525. ;;;
  526. ;;; Guix Data Service
  527. ;;;
  528. (define-record-type* <guix-data-service-configuration>
  529. guix-data-service-configuration make-guix-data-service-configuration
  530. guix-data-service-configuration?
  531. (package guix-data-service-package
  532. (default guix-data-service))
  533. (user guix-data-service-configuration-user
  534. (default "guix-data-service"))
  535. (group guix-data-service-configuration-group
  536. (default "guix-data-service"))
  537. (port guix-data-service-port
  538. (default 8765))
  539. (host guix-data-service-host
  540. (default "127.0.0.1"))
  541. (getmail-idle-mailboxes
  542. guix-data-service-getmail-idle-mailboxes
  543. (default #f))
  544. (commits-getmail-retriever-configuration
  545. guix-data-service-commits-getmail-retriever-configuration
  546. (default #f))
  547. (extra-options guix-data-service-extra-options
  548. (default '()))
  549. (extra-process-jobs-options
  550. guix-data-service-extra-process-jobs-options
  551. (default '())))
  552. (define (guix-data-service-profile-packages config)
  553. "Return the guix-data-service package, this will populate the
  554. ca-certificates.crt file in the system profile."
  555. (list
  556. (guix-data-service-package config)))
  557. (define (guix-data-service-shepherd-services config)
  558. (match-record config <guix-data-service-configuration>
  559. (package user group port host extra-options extra-process-jobs-options)
  560. (list
  561. (shepherd-service
  562. (documentation "Guix Data Service web server")
  563. (provision '(guix-data-service))
  564. (requirement '(postgres networking))
  565. (start #~(make-forkexec-constructor
  566. (list #$(file-append package
  567. "/bin/guix-data-service")
  568. "--pid-file=/var/run/guix-data-service/pid"
  569. #$(string-append "--port=" (number->string port))
  570. #$(string-append "--host=" host)
  571. ;; Perform any database migrations when the
  572. ;; service is started
  573. "--update-database"
  574. #$@extra-options)
  575. #:user #$user
  576. #:group #$group
  577. #:pid-file "/var/run/guix-data-service/pid"
  578. ;; Allow time for migrations to run
  579. #:pid-file-timeout 60
  580. #:environment-variables
  581. `(,(string-append
  582. "GUIX_LOCPATH=" #$glibc-utf8-locales "/lib/locale")
  583. "LC_ALL=en_US.UTF-8")
  584. #:log-file "/var/log/guix-data-service/web.log"))
  585. (stop #~(make-kill-destructor)))
  586. (shepherd-service
  587. (documentation "Guix Data Service process jobs")
  588. (provision '(guix-data-service-process-jobs))
  589. (requirement '(postgres
  590. networking
  591. ;; Require guix-data-service, as that the database
  592. ;; migrations are handled through this service
  593. guix-data-service))
  594. (start #~(make-forkexec-constructor
  595. (list
  596. #$(file-append package
  597. "/bin/guix-data-service-process-jobs")
  598. #$@extra-process-jobs-options)
  599. #:user #$user
  600. #:group #$group
  601. #:environment-variables
  602. `("HOME=/var/lib/guix-data-service"
  603. "GIT_SSL_CAINFO=/etc/ssl/certs/ca-certificates.crt"
  604. ,(string-append
  605. "GUIX_LOCPATH=" #$glibc-utf8-locales "/lib/locale")
  606. "LC_ALL=en_US.UTF-8")
  607. #:log-file "/var/log/guix-data-service/process-jobs.log"))
  608. (stop #~(make-kill-destructor))))))
  609. (define (guix-data-service-activation config)
  610. #~(begin
  611. (use-modules (guix build utils))
  612. (define %user (getpw "guix-data-service"))
  613. (chmod "/var/lib/guix-data-service" #o755)
  614. (mkdir-p "/var/log/guix-data-service")
  615. ;; Allow writing the PID file
  616. (mkdir-p "/var/run/guix-data-service")
  617. (chown "/var/run/guix-data-service"
  618. (passwd:uid %user)
  619. (passwd:gid %user))))
  620. (define (guix-data-service-account config)
  621. (match-record config <guix-data-service-configuration>
  622. (user group)
  623. (list (user-group
  624. (name group)
  625. (system? #t))
  626. (user-account
  627. (name user)
  628. (group group)
  629. (system? #t)
  630. (comment "Guix Data Service user")
  631. (home-directory "/var/lib/guix-data-service")
  632. (shell (file-append shadow "/sbin/nologin"))))))
  633. (define (guix-data-service-getmail-configuration config)
  634. (match config
  635. (($ <guix-data-service-configuration> package user group
  636. port host
  637. #f #f)
  638. '())
  639. (($ <guix-data-service-configuration> package user group
  640. port host
  641. getmail-idle-mailboxes
  642. commits-getmail-retriever-configuration)
  643. (list
  644. (getmail-configuration
  645. (name 'guix-data-service)
  646. (user user)
  647. (group group)
  648. (directory "/var/lib/getmail/guix-data-service")
  649. (rcfile
  650. (getmail-configuration-file
  651. (retriever commits-getmail-retriever-configuration)
  652. (destination
  653. (getmail-destination-configuration
  654. (type "MDA_external")
  655. (path (file-append
  656. package
  657. "/bin/guix-data-service-process-branch-updated-email"))))
  658. (options
  659. (getmail-options-configuration
  660. (read-all #f)
  661. (delivered-to #f)
  662. (received #f)))))
  663. (idle getmail-idle-mailboxes))))))
  664. (define guix-data-service-type
  665. (service-type
  666. (name 'guix-data-service)
  667. (extensions
  668. (list
  669. (service-extension profile-service-type
  670. guix-data-service-profile-packages)
  671. (service-extension shepherd-root-service-type
  672. guix-data-service-shepherd-services)
  673. (service-extension activation-service-type
  674. guix-data-service-activation)
  675. (service-extension account-service-type
  676. guix-data-service-account)
  677. (service-extension getmail-service-type
  678. guix-data-service-getmail-configuration)))
  679. (default-value
  680. (guix-data-service-configuration))
  681. (description
  682. "Run an instance of the Guix Data Service.")))
  683. ;;;
  684. ;;; Nar Herder
  685. ;;;
  686. (define-record-type* <nar-herder-configuration>
  687. nar-herder-configuration make-nar-herder-configuration
  688. nar-herder-configuration?
  689. (package nar-herder-configuration-package
  690. (default nar-herder))
  691. (user nar-herder-configuration-user
  692. (default "nar-herder"))
  693. (group nar-herder-configuration-group
  694. (default "nar-herder"))
  695. (mirror nar-herder-configuration-mirror
  696. (default #f))
  697. (database nar-herder-configuration-database
  698. (default "/var/lib/nar-herder/nar_herder.db"))
  699. (database-dump nar-herder-configuration-database-dump
  700. (default "/var/lib/nar-herder/nar_herder_dump.db"))
  701. (host nar-herder-configuration-host
  702. (default "127.0.0.1"))
  703. (port nar-herder-configuration-port
  704. (default 8734))
  705. (storage nar-herder-configuration-storage
  706. (default #f))
  707. (storage-limit nar-herder-configuration-storage-limit
  708. (default "none"))
  709. (storage-nar-removal-criteria
  710. nar-herder-configuration-storage-nar-removal-criteria
  711. (default '()))
  712. (ttl nar-herder-configuration-ttl
  713. (default #f))
  714. (negative-ttl nar-herder-configuration-negative-ttl
  715. (default #f)))
  716. (define (nar-herder-shepherd-services config)
  717. (match-record config <nar-herder-configuration>
  718. (package user group
  719. mirror
  720. database database-dump
  721. host port
  722. storage storage-limit storage-nar-removal-criteria
  723. ttl negative-ttl)
  724. (unless (or mirror storage)
  725. (error "nar-herder: mirror or storage must be set"))
  726. (list
  727. (shepherd-service
  728. (documentation "Nar Herder")
  729. (provision '(nar-herder))
  730. (requirement '(networking))
  731. (start #~(make-forkexec-constructor
  732. (list #$(file-append package
  733. "/bin/nar-herder")
  734. "run-server"
  735. "--pid-file=/var/run/nar-herder/pid"
  736. #$(string-append "--port=" (number->string port))
  737. #$(string-append "--host=" host)
  738. #$@(if mirror
  739. (list (string-append "--mirror=" mirror))
  740. '())
  741. #$(string-append "--database=" database)
  742. #$(string-append "--database-dump=" database-dump)
  743. #$@(if storage
  744. (list (string-append "--storage=" storage))
  745. '())
  746. #$(string-append "--storage-limit="
  747. (if (number? storage-limit)
  748. (number->string storage-limit)
  749. storage-limit))
  750. #$@(map (lambda (criteria)
  751. (string-append
  752. "--storage-nar-removal-criteria="
  753. (match criteria
  754. ((k . v) (simple-format #f "~A=~A" k v))
  755. (str str))))
  756. storage-nar-removal-criteria)
  757. #$@(if ttl
  758. (list (string-append "--ttl=" ttl))
  759. '())
  760. #$@(if negative-ttl
  761. (list (string-append "--negative-ttl=" negative-ttl))
  762. '()))
  763. #:user #$user
  764. #:group #$group
  765. #:pid-file "/var/run/nar-herder/pid"
  766. #:environment-variables
  767. `(,(string-append
  768. "GUIX_LOCPATH=" #$glibc-utf8-locales "/lib/locale")
  769. "LC_ALL=en_US.utf8")
  770. #:log-file "/var/log/nar-herder/server.log"))
  771. (stop #~(make-kill-destructor))))))
  772. (define (nar-herder-activation config)
  773. #~(begin
  774. (use-modules (guix build utils))
  775. (define %user
  776. (getpw #$(nar-herder-configuration-user
  777. config)))
  778. (chmod "/var/lib/nar-herder" #o755)
  779. (mkdir-p "/var/log/nar-herder")
  780. ;; Allow writing the PID file
  781. (mkdir-p "/var/run/nar-herder")
  782. (chown "/var/run/nar-herder"
  783. (passwd:uid %user)
  784. (passwd:gid %user))))
  785. (define (nar-herder-account config)
  786. (match-record config <nar-herder-configuration>
  787. (user group)
  788. (list (user-group
  789. (name group)
  790. (system? #t))
  791. (user-account
  792. (name user)
  793. (group group)
  794. (system? #t)
  795. (comment "Nar Herder user")
  796. (home-directory "/var/lib/nar-herder")
  797. (shell (file-append shadow "/sbin/nologin"))))))
  798. (define nar-herder-service-type
  799. (service-type
  800. (name 'nar-herder)
  801. (extensions
  802. (list
  803. (service-extension shepherd-root-service-type
  804. nar-herder-shepherd-services)
  805. (service-extension activation-service-type
  806. nar-herder-activation)
  807. (service-extension account-service-type
  808. nar-herder-account)))
  809. (description
  810. "Run a Nar Herder server.")))