guix.scm 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019 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-derivation-substitute-urls
  59. guix-build-coordinator-agent-configuration-non-derivation-substitute-urls
  60. guix-build-coordinator-agent-password-auth
  61. guix-build-coordinator-agent-password-auth?
  62. guix-build-coordinator-agent-password-auth-uuid
  63. guix-build-coordinator-agent-password-auth-password
  64. guix-build-coordinator-agent-password-file-auth
  65. guix-build-coordinator-agent-password-file-auth?
  66. guix-build-coordinator-agent-password-file-auth-uuid
  67. guix-build-coordinator-agent-password-file-auth-password-file
  68. guix-build-coordinator-agent-dynamic-auth
  69. guix-build-coordinator-agent-dynamic-auth?
  70. guix-build-coordinator-agent-dynamic-auth-agent-name
  71. guix-build-coordinator-agent-dynamic-auth-token
  72. guix-build-coordinator-agent-dynamic-auth-with-file
  73. guix-build-coordinator-agent-dynamic-auth-with-file?
  74. guix-build-coordinator-agent-dynamic-auth-with-file-agent-name
  75. guix-build-coordinator-agent-dynamic-auth-with-file-token-file
  76. guix-build-coordinator-agent-service-type
  77. guix-build-coordinator-queue-builds-configuration
  78. guix-build-coordinator-queue-builds-configuration?
  79. guix-build-coordinator-queue-builds-configuration-package
  80. guix-build-coordinator-queue-builds-configuration-user
  81. guix-build-coordinator-queue-builds-coordinator
  82. guix-build-coordinator-queue-builds-configuration-systems
  83. guix-build-coordinator-queue-builds-configuration-system-and-targets
  84. guix-build-coordinator-queue-builds-configuration-guix-data-service
  85. guix-build-coordinator-queue-builds-configuration-processed-commits-file
  86. guix-build-coordinator-queue-builds-service-type
  87. <guix-data-service-configuration>
  88. guix-data-service-configuration
  89. guix-data-service-configuration?
  90. guix-data-service-package
  91. guix-data-service-user
  92. guix-data-service-group
  93. guix-data-service-port
  94. guix-data-service-host
  95. guix-data-service-getmail-idle-mailboxes
  96. guix-data-service-commits-getmail-retriever-configuration
  97. guix-data-service-type))
  98. ;;;; Commentary:
  99. ;;;
  100. ;;; Services specifically related to GNU Guix.
  101. ;;;
  102. ;;;; Code:
  103. (define-record-type* <guix-build-coordinator-configuration>
  104. guix-build-coordinator-configuration make-guix-build-coordinator-configuration
  105. guix-build-coordinator-configuration?
  106. (package guix-build-coordinator-configuration-package
  107. (default guix-build-coordinator))
  108. (user guix-build-coordinator-configuration-user
  109. (default "guix-build-coordinator"))
  110. (group guix-build-coordinator-configuration-group
  111. (default "guix-build-coordinator"))
  112. (database-uri-string
  113. guix-build-coordinator-configuration-datastore-uri-string
  114. (default "sqlite:///var/lib/guix-build-coordinator/guix_build_coordinator.db"))
  115. (agent-communication-uri-string
  116. guix-build-coordinator-configuration-agent-communication-uri-string
  117. (default "http://0.0.0.0:8745"))
  118. (client-communication-uri-string
  119. guix-build-coordinator-configuration-client-communication-uri-string
  120. (default "http://127.0.0.1:8746"))
  121. (allocation-strategy
  122. guix-build-coordinator-configuration-allocation-strategy
  123. (default #~basic-build-allocation-strategy))
  124. (hooks guix-build-coordinator-configuration-hooks
  125. (default '()))
  126. (guile guix-build-coordinator-configuration-guile
  127. (default guile-3.0-latest)))
  128. (define-record-type* <guix-build-coordinator-agent-configuration>
  129. guix-build-coordinator-agent-configuration
  130. make-guix-build-coordinator-agent-configuration
  131. guix-build-coordinator-agent-configuration?
  132. (package guix-build-coordinator-agent-configuration-package
  133. (default guix-build-coordinator))
  134. (user guix-build-coordinator-agent-configuration-user
  135. (default "guix-build-coordinator-agent"))
  136. (coordinator guix-build-coordinator-agent-configuration-coordinator
  137. (default "http://localhost:8745"))
  138. (authentication guix-build-coordinator-agent-configuration-authentication)
  139. (systems guix-build-coordinator-agent-configuration-systems
  140. (default #f))
  141. (max-parallel-builds
  142. guix-build-coordinator-agent-configuration-max-parallel-builds
  143. (default 1))
  144. (derivation-substitute-urls
  145. guix-build-coordinator-agent-configuration-derivation-substitute-urls
  146. (default #f))
  147. (non-derivation-substitute-urls
  148. guix-build-coordinator-agent-configuration-non-derivation-substitute-urls
  149. (default #f)))
  150. (define-record-type* <guix-build-coordinator-agent-password-auth>
  151. guix-build-coordinator-agent-password-auth
  152. make-guix-build-coordinator-agent-password-auth
  153. guix-build-coordinator-agent-password-auth?
  154. (uuid guix-build-coordinator-agent-password-auth-uuid)
  155. (password guix-build-coordinator-agent-password-auth-password))
  156. (define-record-type* <guix-build-coordinator-agent-password-file-auth>
  157. guix-build-coordinator-agent-password-file-auth
  158. make-guix-build-coordinator-agent-password-file-auth
  159. guix-build-coordinator-agent-password-file-auth?
  160. (uuid guix-build-coordinator-agent-password-file-auth-uuid)
  161. (password-file
  162. guix-build-coordinator-agent-password-file-auth-password-file))
  163. (define-record-type* <guix-build-coordinator-agent-dynamic-auth>
  164. guix-build-coordinator-agent-dynamic-auth
  165. make-guix-build-coordinator-agent-dynamic-auth
  166. guix-build-coordinator-agent-dynamic-auth?
  167. (agent-name guix-build-coordinator-agent-dynamic-auth-agent-name)
  168. (token guix-build-coordinator-agent-dynamic-auth-token))
  169. (define-record-type* <guix-build-coordinator-agent-dynamic-auth-with-file>
  170. guix-build-coordinator-agent-dynamic-auth-with-file
  171. make-guix-build-coordinator-agent-dynamic-auth-with-file
  172. guix-build-coordinator-agent-dynamic-auth-with-file?
  173. (agent-name guix-build-coordinator-agent-dynamic-auth-with-file-agent-name)
  174. (token-file guix-build-coordinator-agent-dynamic-auth-with-file-token-file))
  175. (define-record-type* <guix-build-coordinator-queue-builds-configuration>
  176. guix-build-coordinator-queue-builds-configuration
  177. make-guix-build-coordinator-queue-builds-configuration
  178. guix-build-coordinator-queue-builds-configuration?
  179. (package guix-build-coordinator-queue-builds-configuration-package
  180. (default guix-build-coordinator))
  181. (user guix-build-coordinator-queue-builds-configuration-user
  182. (default "guix-build-coordinator-queue-builds"))
  183. (coordinator guix-build-coordinator-queue-builds-coordinator
  184. (default "http://localhost:8745"))
  185. (systems guix-build-coordinator-queue-builds-configuration-systems
  186. (default #f))
  187. (systems-and-targets
  188. guix-build-coordinator-queue-builds-configuration-system-and-targets
  189. (default #f))
  190. (guix-data-service
  191. guix-build-coordinator-queue-builds-configuration-guix-data-service
  192. (default "https://data.guix.gnu.org"))
  193. (processed-commits-file
  194. guix-build-coordinator-queue-builds-configuration-processed-commits-file
  195. (default "/var/cache/guix-build-coordinator-queue-builds/processed-commits")))
  196. (define* (make-guix-build-coordinator-start-script database-uri-string
  197. allocation-strategy
  198. pid-file
  199. guix-build-coordinator-package
  200. #:key
  201. agent-communication-uri-string
  202. client-communication-uri-string
  203. (hooks '())
  204. (guile guile-3.0))
  205. (program-file
  206. "start-guix-build-coordinator"
  207. (with-extensions (cons guix-build-coordinator-package
  208. ;; This is a poorly constructed Guile load path,
  209. ;; since it contains things that aren't Guile
  210. ;; libraries, but it means that the Guile libraries
  211. ;; needed for the Guix Build Coordinator don't need
  212. ;; to be individually specified here.
  213. (append
  214. (map second (package-inputs
  215. guix-build-coordinator-package))
  216. (map second (package-propagated-inputs
  217. guix-build-coordinator-package))))
  218. #~(begin
  219. (use-modules (srfi srfi-1)
  220. (ice-9 match)
  221. (web uri)
  222. (prometheus)
  223. (guix-build-coordinator hooks)
  224. (guix-build-coordinator datastore)
  225. (guix-build-coordinator build-allocator)
  226. (guix-build-coordinator coordinator))
  227. (setvbuf (current-output-port) 'line)
  228. (setvbuf (current-error-port) 'line)
  229. (simple-format #t "starting the guix-build-coordinator:\n ~A\n"
  230. (current-filename))
  231. (let* ((metrics-registry (make-metrics-registry
  232. #:namespace
  233. "guixbuildcoordinator"))
  234. (datastore (database-uri->datastore
  235. #$database-uri-string
  236. #:metrics-registry metrics-registry))
  237. (hooks
  238. (list #$@(map (match-lambda
  239. ((name . hook-gexp)
  240. #~(cons '#$name #$hook-gexp)))
  241. hooks)))
  242. (hooks-with-defaults
  243. `(,@hooks
  244. ,@(remove (match-lambda
  245. ((name . _) (assq-ref hooks name)))
  246. %default-hooks)))
  247. (build-coordinator (make-build-coordinator
  248. #:datastore datastore
  249. #:hooks hooks-with-defaults
  250. #:metrics-registry metrics-registry
  251. #:allocation-strategy #$allocation-strategy)))
  252. (run-coordinator-service
  253. build-coordinator
  254. #:update-datastore? #t
  255. #:pid-file #$pid-file
  256. #:agent-communication-uri (string->uri
  257. #$agent-communication-uri-string)
  258. #:client-communication-uri (string->uri
  259. #$client-communication-uri-string)))))
  260. #:guile guile))
  261. (define (guix-build-coordinator-shepherd-services config)
  262. (match-record config <guix-build-coordinator-configuration>
  263. (package user group database-uri-string
  264. agent-communication-uri-string
  265. client-communication-uri-string
  266. allocation-strategy
  267. hooks
  268. guile)
  269. (list
  270. (shepherd-service
  271. (documentation "Guix Build Coordinator")
  272. (provision '(guix-build-coordinator))
  273. (requirement '(networking))
  274. (start #~(make-forkexec-constructor
  275. (list #$(make-guix-build-coordinator-start-script
  276. database-uri-string
  277. allocation-strategy
  278. "/var/run/guix-build-coordinator/pid"
  279. package
  280. #:agent-communication-uri-string
  281. agent-communication-uri-string
  282. #:client-communication-uri-string
  283. client-communication-uri-string
  284. #:hooks hooks
  285. #:guile guile))
  286. #:user #$user
  287. #:group #$group
  288. #:pid-file "/var/run/guix-build-coordinator/pid"
  289. ;; Allow time for migrations to run
  290. #:pid-file-timeout 60
  291. #:environment-variables
  292. `(,(string-append
  293. "GUIX_LOCPATH=" #$glibc-utf8-locales "/lib/locale")
  294. "LC_ALL=en_US.utf8"
  295. "PATH=/run/current-system/profile/bin") ; for hooks
  296. #:log-file "/var/log/guix-build-coordinator/coordinator.log"))
  297. (stop #~(make-kill-destructor))))))
  298. (define (guix-build-coordinator-activation config)
  299. #~(begin
  300. (use-modules (guix build utils))
  301. (define %user (getpw "guix-build-coordinator"))
  302. (chmod "/var/lib/guix-build-coordinator" #o755)
  303. (mkdir-p "/var/log/guix-build-coordinator")
  304. ;; Allow writing the PID file
  305. (mkdir-p "/var/run/guix-build-coordinator")
  306. (chown "/var/run/guix-build-coordinator"
  307. (passwd:uid %user)
  308. (passwd:gid %user))))
  309. (define (guix-build-coordinator-account config)
  310. (match-record config <guix-build-coordinator-configuration>
  311. (user group)
  312. (list (user-group
  313. (name group)
  314. (system? #t))
  315. (user-account
  316. (name user)
  317. (group group)
  318. (system? #t)
  319. (comment "Guix Build Coordinator user")
  320. (home-directory "/var/lib/guix-build-coordinator")
  321. (shell (file-append shadow "/sbin/nologin"))))))
  322. (define guix-build-coordinator-service-type
  323. (service-type
  324. (name 'guix-build-coordinator)
  325. (extensions
  326. (list
  327. (service-extension shepherd-root-service-type
  328. guix-build-coordinator-shepherd-services)
  329. (service-extension activation-service-type
  330. guix-build-coordinator-activation)
  331. (service-extension account-service-type
  332. guix-build-coordinator-account)))
  333. (default-value
  334. (guix-build-coordinator-configuration))
  335. (description
  336. "Run an instance of the Guix Build Coordinator.")))
  337. (define (guix-build-coordinator-agent-shepherd-services config)
  338. (match-record config <guix-build-coordinator-agent-configuration>
  339. (package user coordinator authentication max-parallel-builds
  340. derivation-substitute-urls non-derivation-substitute-urls
  341. systems)
  342. (list
  343. (shepherd-service
  344. (documentation "Guix Build Coordinator Agent")
  345. (provision '(guix-build-coordinator-agent))
  346. (requirement '(networking))
  347. (start #~(make-forkexec-constructor
  348. (list #$(file-append package "/bin/guix-build-coordinator-agent")
  349. #$(string-append "--coordinator=" coordinator)
  350. #$@(match authentication
  351. (($ <guix-build-coordinator-agent-password-auth>
  352. uuid password)
  353. #~(#$(string-append "--uuid=" uuid)
  354. #$(string-append "--password=" password)))
  355. (($ <guix-build-coordinator-agent-password-file-auth>
  356. uuid password-file)
  357. #~(#$(string-append "--uuid=" uuid)
  358. #$(string-append "--password-file="
  359. password-file)))
  360. (($ <guix-build-coordinator-agent-dynamic-auth>
  361. agent-name token)
  362. #~(#$(string-append "--name=" agent-name)
  363. #$(string-append "--dynamic-auth-token=" token)))
  364. (($
  365. <guix-build-coordinator-agent-dynamic-auth-with-file>
  366. agent-name token-file)
  367. #~(#$(string-append "--name=" agent-name)
  368. #$(string-append "--dynamic-auth-token-file="
  369. token-file))))
  370. #$(simple-format #f "--max-parallel-builds=~A"
  371. max-parallel-builds)
  372. #$@(if derivation-substitute-urls
  373. #~(#$(string-append
  374. "--derivation-substitute-urls="
  375. (string-join derivation-substitute-urls " ")))
  376. #~())
  377. #$@(if non-derivation-substitute-urls
  378. #~(#$(string-append
  379. "--non-derivation-substitute-urls="
  380. (string-join non-derivation-substitute-urls " ")))
  381. #~())
  382. #$@(map (lambda (system)
  383. (string-append "--system=" system))
  384. (or systems '())))
  385. #:user #$user
  386. #:environment-variables
  387. `(,(string-append
  388. "GUIX_LOCPATH=" #$glibc-utf8-locales "/lib/locale")
  389. ;; XDG_CACHE_HOME is used by Guix when caching narinfo files
  390. "XDG_CACHE_HOME=/var/cache/guix-build-coordinator-agent"
  391. "LC_ALL=en_US.utf8")
  392. #:log-file "/var/log/guix-build-coordinator/agent.log"))
  393. (stop #~(make-kill-destructor))))))
  394. (define (guix-build-coordinator-agent-activation config)
  395. #~(begin
  396. (use-modules (guix build utils))
  397. (define %user (getpw "guix-build-coordinator-agent"))
  398. (mkdir-p "/var/log/guix-build-coordinator")
  399. ;; Create a cache directory for storing narinfo files if downloaded
  400. (mkdir-p "/var/cache/guix-build-coordinator-agent")
  401. (chown "/var/cache/guix-build-coordinator-agent"
  402. (passwd:uid %user)
  403. (passwd:gid %user))))
  404. (define (guix-build-coordinator-agent-account config)
  405. (list (user-account
  406. (name (guix-build-coordinator-agent-configuration-user config))
  407. (group "nogroup")
  408. (system? #t)
  409. (comment "Guix Build Coordinator agent user")
  410. (home-directory "/var/empty")
  411. (shell (file-append shadow "/sbin/nologin")))))
  412. (define guix-build-coordinator-agent-service-type
  413. (service-type
  414. (name 'guix-build-coordinator-agent)
  415. (extensions
  416. (list
  417. (service-extension shepherd-root-service-type
  418. guix-build-coordinator-agent-shepherd-services)
  419. (service-extension activation-service-type
  420. guix-build-coordinator-agent-activation)
  421. (service-extension account-service-type
  422. guix-build-coordinator-agent-account)))
  423. (description
  424. "Run a Guix Build Coordinator agent.")))
  425. (define (guix-build-coordinator-queue-builds-shepherd-services config)
  426. (match-record config <guix-build-coordinator-queue-builds-configuration>
  427. (package user coordinator systems systems-and-targets
  428. guix-data-service processed-commits-file)
  429. (list
  430. (shepherd-service
  431. (documentation "Guix Build Coordinator queue builds from Guix Data Service")
  432. (provision '(guix-build-coordinator-queue-builds))
  433. (requirement '(networking))
  434. (start
  435. #~(make-forkexec-constructor
  436. (list
  437. #$(file-append
  438. package
  439. "/bin/guix-build-coordinator-queue-builds-from-guix-data-service")
  440. #$(string-append "--coordinator=" coordinator)
  441. #$@(map (lambda (system)
  442. (string-append "--system=" system))
  443. (or systems '()))
  444. #$@(map (match-lambda
  445. ((system . target)
  446. (string-append "--system-and-target=" system "=" target)))
  447. (or systems-and-targets '()))
  448. #$@(if guix-data-service
  449. #~(#$(string-append "--guix-data-service=" guix-data-service))
  450. #~())
  451. #$@(if processed-commits-file
  452. #~(#$(string-append "--processed-commits-file="
  453. processed-commits-file))
  454. #~()))
  455. #:user #$user
  456. #:pid-file "/var/run/guix-build-coordinator-queue-builds/pid"
  457. #:environment-variables
  458. `(,(string-append
  459. "GUIX_LOCPATH=" #$glibc-utf8-locales "/lib/locale")
  460. "LC_ALL=en_US.utf8")
  461. #:log-file "/var/log/guix-build-coordinator/queue-builds.log"))
  462. (stop #~(make-kill-destructor))))))
  463. (define (guix-build-coordinator-queue-builds-activation config)
  464. #~(begin
  465. (use-modules (guix build utils))
  466. (mkdir-p "/var/log/guix-build-coordinator")
  467. ;; Allow writing the PID file
  468. (mkdir-p "/var/run/guix-build-coordinator-queue-builds")
  469. (chown "/var/run/guix-build-coordinator-queue-builds"
  470. (passwd:uid %user)
  471. (passwd:gid %user))))
  472. (define (guix-build-coordinator-queue-builds-account config)
  473. (list (user-account
  474. (name (guix-build-coordinator-queue-builds-configuration-user config))
  475. (group "nogroup")
  476. (system? #t)
  477. (comment "Guix Build Coordinator queue-builds user")
  478. (home-directory "/var/empty")
  479. (shell (file-append shadow "/sbin/nologin")))))
  480. (define guix-build-coordinator-queue-builds-service-type
  481. (service-type
  482. (name 'guix-build-coordinator-queue-builds)
  483. (extensions
  484. (list
  485. (service-extension shepherd-root-service-type
  486. guix-build-coordinator-queue-builds-shepherd-services)
  487. (service-extension activation-service-type
  488. guix-build-coordinator-queue-builds-activation)
  489. (service-extension account-service-type
  490. guix-build-coordinator-queue-builds-account)))
  491. (description
  492. "Run the guix-build-coordinator-queue-builds-from-guix-data-service
  493. script.
  494. This is a script to assist in having the Guix Build Coordinator build
  495. derivations stored in an instance of the Guix Data Service.")))
  496. ;;;
  497. ;;; Guix Data Service
  498. ;;;
  499. (define-record-type* <guix-data-service-configuration>
  500. guix-data-service-configuration make-guix-data-service-configuration
  501. guix-data-service-configuration?
  502. (package guix-data-service-package
  503. (default guix-data-service))
  504. (user guix-data-service-configuration-user
  505. (default "guix-data-service"))
  506. (group guix-data-service-configuration-group
  507. (default "guix-data-service"))
  508. (port guix-data-service-port
  509. (default 8765))
  510. (host guix-data-service-host
  511. (default "127.0.0.1"))
  512. (getmail-idle-mailboxes
  513. guix-data-service-getmail-idle-mailboxes
  514. (default #f))
  515. (commits-getmail-retriever-configuration
  516. guix-data-service-commits-getmail-retriever-configuration
  517. (default #f))
  518. (extra-options guix-data-service-extra-options
  519. (default '()))
  520. (extra-process-jobs-options
  521. guix-data-service-extra-process-jobs-options
  522. (default '())))
  523. (define (guix-data-service-profile-packages config)
  524. "Return the guix-data-service package, this will populate the
  525. ca-certificates.crt file in the system profile."
  526. (list
  527. (guix-data-service-package config)))
  528. (define (guix-data-service-shepherd-services config)
  529. (match-record config <guix-data-service-configuration>
  530. (package user group port host extra-options extra-process-jobs-options)
  531. (list
  532. (shepherd-service
  533. (documentation "Guix Data Service web server")
  534. (provision '(guix-data-service))
  535. (requirement '(postgres networking))
  536. (start #~(make-forkexec-constructor
  537. (list #$(file-append package
  538. "/bin/guix-data-service")
  539. "--pid-file=/var/run/guix-data-service/pid"
  540. #$(string-append "--port=" (number->string port))
  541. #$(string-append "--host=" host)
  542. ;; Perform any database migrations when the
  543. ;; service is started
  544. "--update-database"
  545. #$@extra-options)
  546. #:user #$user
  547. #:group #$group
  548. #:pid-file "/var/run/guix-data-service/pid"
  549. ;; Allow time for migrations to run
  550. #:pid-file-timeout 60
  551. #:environment-variables
  552. `(,(string-append
  553. "GUIX_LOCPATH=" #$glibc-utf8-locales "/lib/locale")
  554. "LC_ALL=en_US.UTF-8")
  555. #:log-file "/var/log/guix-data-service/web.log"))
  556. (stop #~(make-kill-destructor)))
  557. (shepherd-service
  558. (documentation "Guix Data Service process jobs")
  559. (provision '(guix-data-service-process-jobs))
  560. (requirement '(postgres
  561. networking
  562. ;; Require guix-data-service, as that the database
  563. ;; migrations are handled through this service
  564. guix-data-service))
  565. (start #~(make-forkexec-constructor
  566. (list
  567. #$(file-append package
  568. "/bin/guix-data-service-process-jobs")
  569. #$@extra-process-jobs-options)
  570. #:user #$user
  571. #:group #$group
  572. #:environment-variables
  573. `("HOME=/var/lib/guix-data-service"
  574. "GIT_SSL_CAINFO=/etc/ssl/certs/ca-certificates.crt"
  575. ,(string-append
  576. "GUIX_LOCPATH=" #$glibc-utf8-locales "/lib/locale")
  577. "LC_ALL=en_US.UTF-8")
  578. #:log-file "/var/log/guix-data-service/process-jobs.log"))
  579. (stop #~(make-kill-destructor))))))
  580. (define (guix-data-service-activation config)
  581. #~(begin
  582. (use-modules (guix build utils))
  583. (define %user (getpw "guix-data-service"))
  584. (chmod "/var/lib/guix-data-service" #o755)
  585. (mkdir-p "/var/log/guix-data-service")
  586. ;; Allow writing the PID file
  587. (mkdir-p "/var/run/guix-data-service")
  588. (chown "/var/run/guix-data-service"
  589. (passwd:uid %user)
  590. (passwd:gid %user))))
  591. (define (guix-data-service-account config)
  592. (match-record config <guix-data-service-configuration>
  593. (user group)
  594. (list (user-group
  595. (name group)
  596. (system? #t))
  597. (user-account
  598. (name user)
  599. (group group)
  600. (system? #t)
  601. (comment "Guix Data Service user")
  602. (home-directory "/var/lib/guix-data-service")
  603. (shell (file-append shadow "/sbin/nologin"))))))
  604. (define (guix-data-service-getmail-configuration config)
  605. (match config
  606. (($ <guix-data-service-configuration> package user group
  607. port host
  608. #f #f)
  609. '())
  610. (($ <guix-data-service-configuration> package user group
  611. port host
  612. getmail-idle-mailboxes
  613. commits-getmail-retriever-configuration)
  614. (list
  615. (getmail-configuration
  616. (name 'guix-data-service)
  617. (user user)
  618. (group group)
  619. (directory "/var/lib/getmail/guix-data-service")
  620. (rcfile
  621. (getmail-configuration-file
  622. (retriever commits-getmail-retriever-configuration)
  623. (destination
  624. (getmail-destination-configuration
  625. (type "MDA_external")
  626. (path (file-append
  627. package
  628. "/bin/guix-data-service-process-branch-updated-email"))))
  629. (options
  630. (getmail-options-configuration
  631. (read-all #f)
  632. (delivered-to #f)
  633. (received #f)))))
  634. (idle getmail-idle-mailboxes))))))
  635. (define guix-data-service-type
  636. (service-type
  637. (name 'guix-data-service)
  638. (extensions
  639. (list
  640. (service-extension profile-service-type
  641. guix-data-service-profile-packages)
  642. (service-extension shepherd-root-service-type
  643. guix-data-service-shepherd-services)
  644. (service-extension activation-service-type
  645. guix-data-service-activation)
  646. (service-extension account-service-type
  647. guix-data-service-account)
  648. (service-extension getmail-service-type
  649. guix-data-service-getmail-configuration)))
  650. (default-value
  651. (guix-data-service-configuration))
  652. (description
  653. "Run an instance of the Guix Data Service.")))