databases.scm 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015 David Thompson <davet@gnu.org>
  3. ;;; Copyright © 2015-2016, 2022-2023 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2016 Leo Famulari <leo@famulari.name>
  5. ;;; Copyright © 2017 Christopher Baines <mail@cbaines.net>
  6. ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
  7. ;;; Copyright © 2018 Julien Lepiller <julien@lepiller.eu>
  8. ;;; Copyright © 2019 Robert Vollmert <rob@vllmrt.net>
  9. ;;; Copyright © 2020, 2022 Marius Bakke <marius@gnu.org>
  10. ;;; Copyright © 2021 David Larsson <david.larsson@selfhosted.xyz>
  11. ;;; Copyright © 2021 Aljosha Papsch <ep@stern-data.com>
  12. ;;;
  13. ;;; This file is part of GNU Guix.
  14. ;;;
  15. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  16. ;;; under the terms of the GNU General Public License as published by
  17. ;;; the Free Software Foundation; either version 3 of the License, or (at
  18. ;;; your option) any later version.
  19. ;;;
  20. ;;; GNU Guix is distributed in the hope that it will be useful, but
  21. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  22. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. ;;; GNU General Public License for more details.
  24. ;;;
  25. ;;; You should have received a copy of the GNU General Public License
  26. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  27. (define-module (gnu services databases)
  28. #:use-module (gnu services)
  29. #:use-module (gnu services shepherd)
  30. #:use-module (gnu system shadow)
  31. #:use-module (gnu packages admin)
  32. #:use-module (gnu packages base)
  33. #:use-module (gnu packages databases)
  34. #:use-module (guix build-system trivial)
  35. #:use-module (guix build union)
  36. #:use-module (guix deprecation)
  37. #:use-module (guix modules)
  38. #:use-module (guix packages)
  39. #:use-module (guix records)
  40. #:use-module (guix gexp)
  41. #:use-module (srfi srfi-1)
  42. #:use-module (ice-9 match)
  43. #:export (postgresql-config-file
  44. postgresql-config-file?
  45. postgresql-config-file-log-destination
  46. postgresql-config-file-hba-file
  47. postgresql-config-file-ident-file
  48. postgresql-config-file-socket-directory
  49. postgresql-config-file-extra-config
  50. postgresql-configuration
  51. postgresql-configuration?
  52. postgresql-configuration-postgresql
  53. postgresql-configuration-port
  54. postgresql-configuration-locale
  55. postgresql-configuration-file
  56. postgresql-configuration-log-directory
  57. postgresql-configuration-data-directory
  58. postgresql-configuration-extension-packages
  59. postgresql-service
  60. postgresql-service-type
  61. postgresql-role
  62. postgresql-role?
  63. postgresql-role-name
  64. postgresql-role-permissions
  65. postgresql-role-create-database?
  66. postgresql-role-configuration
  67. postgresql-role-configuration?
  68. postgresql-role-configuration-host
  69. postgresql-role-configuration-roles
  70. postgresql-role-service-type
  71. memcached-service-type
  72. memcached-configuration
  73. memcached-configuration?
  74. memcached-configuration-memecached
  75. memcached-configuration-interfaces
  76. memcached-configuration-tcp-port
  77. memcached-configuration-udp-port
  78. memcached-configuration-additional-options
  79. mysql-service
  80. mysql-service-type
  81. mysql-configuration
  82. mysql-configuration?
  83. redis-configuration
  84. redis-configuration?
  85. redis-service-type))
  86. ;;; Commentary:
  87. ;;;
  88. ;;; Database services.
  89. ;;;
  90. ;;; Code:
  91. (define %default-postgres-hba
  92. (plain-file "pg_hba.conf"
  93. "
  94. local all all peer
  95. host all all 127.0.0.1/32 md5
  96. host all all ::1/128 md5"))
  97. (define %default-postgres-ident
  98. (plain-file "pg_ident.conf"
  99. "# MAPNAME SYSTEM-USERNAME PG-USERNAME"))
  100. (define-record-type* <postgresql-config-file>
  101. postgresql-config-file make-postgresql-config-file
  102. postgresql-config-file?
  103. (log-destination postgresql-config-file-log-destination
  104. (default "syslog"))
  105. (hba-file postgresql-config-file-hba-file
  106. (default %default-postgres-hba))
  107. (ident-file postgresql-config-file-ident-file
  108. (default %default-postgres-ident))
  109. (socket-directory postgresql-config-file-socket-directory
  110. (default "/var/run/postgresql"))
  111. (extra-config postgresql-config-file-extra-config
  112. (default '())))
  113. (define-gexp-compiler (postgresql-config-file-compiler
  114. (file <postgresql-config-file>) system target)
  115. (match file
  116. (($ <postgresql-config-file> log-destination hba-file
  117. ident-file socket-directory
  118. extra-config)
  119. ;; See: https://www.postgresql.org/docs/current/config-setting.html.
  120. (define (format-value value)
  121. (cond
  122. ((boolean? value)
  123. (list (if value "on" "off")))
  124. ((number? value)
  125. (list (number->string value)))
  126. (else
  127. (list "'" value "'"))))
  128. (define contents
  129. (append-map
  130. (match-lambda
  131. ((key) '())
  132. ((key . #f) '())
  133. ((key values ...)
  134. `(,key " = " ,@(append-map format-value values) "\n")))
  135. `(("log_destination" ,log-destination)
  136. ("hba_file" ,hba-file)
  137. ("ident_file" ,ident-file)
  138. ,@(if socket-directory
  139. `(("unix_socket_directories" ,socket-directory))
  140. '())
  141. ,@extra-config)))
  142. (gexp->derivation
  143. "postgresql.conf"
  144. #~(call-with-output-file (ungexp output "out")
  145. (lambda (port)
  146. (display
  147. (string-append #$@contents)
  148. port)))
  149. #:local-build? #t))))
  150. (define-record-type* <postgresql-configuration>
  151. postgresql-configuration make-postgresql-configuration
  152. postgresql-configuration?
  153. (postgresql postgresql-configuration-postgresql ;file-like
  154. (default postgresql-10))
  155. (port postgresql-configuration-port
  156. (default 5432))
  157. (locale postgresql-configuration-locale
  158. (default "en_US.utf8"))
  159. (config-file postgresql-configuration-file
  160. (default (postgresql-config-file)))
  161. (log-directory postgresql-configuration-log-directory
  162. (default "/var/log/postgresql"))
  163. (data-directory postgresql-configuration-data-directory
  164. (default "/var/lib/postgresql/data"))
  165. (extension-packages postgresql-configuration-extension-packages
  166. (default '())))
  167. (define %postgresql-accounts
  168. (list (user-group (name "postgres") (system? #t))
  169. (user-account
  170. (name "postgres")
  171. (group "postgres")
  172. (system? #t)
  173. (comment "PostgreSQL server user")
  174. (home-directory "/var/empty")
  175. (shell (file-append shadow "/sbin/nologin")))))
  176. (define (final-postgresql postgresql extension-packages)
  177. (if (null? extension-packages)
  178. postgresql
  179. (package
  180. (inherit postgresql)
  181. (source #f)
  182. (build-system trivial-build-system)
  183. (arguments
  184. `(#:modules ((guix build utils) (guix build union))
  185. #:builder
  186. (begin
  187. (use-modules (guix build utils) (guix build union) (srfi srfi-26))
  188. (union-build (assoc-ref %outputs "out")
  189. (map (lambda (input) (cdr input))
  190. %build-inputs))
  191. #t)))
  192. (inputs
  193. `(("postgresql" ,postgresql)
  194. ,@(map (lambda (extension) (list "extension" extension))
  195. extension-packages))))))
  196. (define postgresql-activation
  197. (match-lambda
  198. (($ <postgresql-configuration> postgresql port locale config-file
  199. log-directory data-directory
  200. extension-packages)
  201. #~(begin
  202. (use-modules (guix build utils)
  203. (ice-9 match))
  204. (let ((user (getpwnam "postgres"))
  205. (initdb (string-append
  206. #$(final-postgresql postgresql
  207. extension-packages)
  208. "/bin/initdb"))
  209. (initdb-args
  210. (append
  211. (if #$locale
  212. (list (string-append "--locale=" #$locale))
  213. '()))))
  214. ;; Create db state directory.
  215. (mkdir-p #$data-directory)
  216. (chown #$data-directory (passwd:uid user) (passwd:gid user))
  217. ;; Create the socket directory.
  218. (let ((socket-directory
  219. #$(postgresql-config-file-socket-directory config-file)))
  220. (when (string? socket-directory)
  221. (mkdir-p socket-directory)
  222. (chown socket-directory (passwd:uid user) (passwd:gid user))))
  223. ;; Create the log directory.
  224. (when (string? #$log-directory)
  225. (mkdir-p #$log-directory)
  226. (chown #$log-directory (passwd:uid user) (passwd:gid user)))
  227. ;; Drop privileges and init state directory in a new
  228. ;; process. Wait for it to finish before proceeding.
  229. (match (primitive-fork)
  230. (0
  231. ;; Exit with a non-zero status code if an exception is thrown.
  232. (dynamic-wind
  233. (const #t)
  234. (lambda ()
  235. (setgid (passwd:gid user))
  236. (setuid (passwd:uid user))
  237. (primitive-exit
  238. (apply system*
  239. initdb
  240. "-D"
  241. #$data-directory
  242. initdb-args)))
  243. (lambda ()
  244. (primitive-exit 1))))
  245. (pid (waitpid pid))))))))
  246. (define postgresql-shepherd-service
  247. (match-lambda
  248. (($ <postgresql-configuration> postgresql port locale config-file
  249. log-directory data-directory
  250. extension-packages)
  251. (let* ((pg_ctl-wrapper
  252. ;; Wrapper script that switches to the 'postgres' user before
  253. ;; launching daemon.
  254. (program-file
  255. "pg_ctl-wrapper"
  256. #~(begin
  257. (use-modules (ice-9 match)
  258. (ice-9 format))
  259. (match (command-line)
  260. ((_ mode)
  261. (let ((user (getpwnam "postgres"))
  262. (pg_ctl #$(file-append
  263. (final-postgresql postgresql
  264. extension-packages)
  265. "/bin/pg_ctl"))
  266. (options (format #f "--config-file=~a -p ~d"
  267. #$config-file #$port)))
  268. (setgid (passwd:gid user))
  269. (setuid (passwd:uid user))
  270. (execl pg_ctl pg_ctl "-D" #$data-directory
  271. #$@(if (string? log-directory)
  272. (list "-l"
  273. (string-append log-directory
  274. "/pg_ctl.log"))
  275. '())
  276. "-o" options
  277. mode)))))))
  278. (pid-file (in-vicinity data-directory "postmaster.pid"))
  279. (action (lambda args
  280. #~(lambda _
  281. (invoke #$pg_ctl-wrapper #$@args)
  282. (match '#$args
  283. (("start")
  284. (call-with-input-file #$pid-file read))
  285. (_ #t))))))
  286. (list (shepherd-service
  287. (provision '(postgres postgresql))
  288. (documentation "Run the PostgreSQL daemon.")
  289. (requirement '(user-processes loopback syslogd))
  290. (modules `((ice-9 match)
  291. ,@%default-modules))
  292. (actions (list (shepherd-configuration-action config-file)))
  293. (start (action "start"))
  294. (stop (action "stop"))))))))
  295. (define postgresql-service-type
  296. (service-type
  297. (name 'postgresql)
  298. (extensions
  299. (list (service-extension shepherd-root-service-type
  300. postgresql-shepherd-service)
  301. (service-extension activation-service-type
  302. postgresql-activation)
  303. (service-extension account-service-type
  304. (const %postgresql-accounts))
  305. (service-extension
  306. profile-service-type
  307. (compose list postgresql-configuration-postgresql))))
  308. (default-value (postgresql-configuration))
  309. (description "Run the PostgreSQL database server.")))
  310. (define-deprecated (postgresql-service #:key (postgresql postgresql)
  311. (port 5432)
  312. (locale "en_US.utf8")
  313. (config-file (postgresql-config-file))
  314. (data-directory
  315. "/var/lib/postgresql/data")
  316. (extension-packages '()))
  317. postgresql-service-type
  318. "Return a service that runs @var{postgresql}, the PostgreSQL database
  319. server.
  320. The PostgreSQL daemon loads its runtime configuration from @var{config-file}
  321. and stores the database cluster in @var{data-directory}."
  322. (service postgresql-service-type
  323. (postgresql-configuration
  324. (postgresql postgresql)
  325. (port port)
  326. (locale locale)
  327. (config-file config-file)
  328. (data-directory data-directory)
  329. (extension-packages extension-packages))))
  330. (define-record-type* <postgresql-role>
  331. postgresql-role make-postgresql-role
  332. postgresql-role?
  333. (name postgresql-role-name) ;string
  334. (permissions postgresql-role-permissions
  335. (default '(createdb login))) ;list
  336. (create-database? postgresql-role-create-database? ;boolean
  337. (default #f)))
  338. (define-record-type* <postgresql-role-configuration>
  339. postgresql-role-configuration make-postgresql-role-configuration
  340. postgresql-role-configuration?
  341. (host postgresql-role-configuration-host ;string
  342. (default "/var/run/postgresql"))
  343. (log postgresql-role-configuration-log ;string
  344. (default "/var/log/postgresql_roles.log"))
  345. (roles postgresql-role-configuration-roles
  346. (default '()))) ;list
  347. (define (postgresql-create-roles config)
  348. ;; See: https://www.postgresql.org/docs/current/sql-createrole.html for the
  349. ;; complete permissions list.
  350. (define (format-permissions permissions)
  351. (let ((dict '(bypassrls createdb createrole login replication superuser)))
  352. (string-join (filter-map (lambda (permission)
  353. (and (member permission dict)
  354. (string-upcase
  355. (symbol->string permission))))
  356. permissions)
  357. " ")))
  358. (define (roles->queries roles)
  359. (apply mixed-text-file "queries"
  360. (append-map
  361. (lambda (role)
  362. (match-record role <postgresql-role>
  363. (name permissions create-database?)
  364. `("SELECT NOT(EXISTS(SELECT 1 FROM pg_catalog.pg_roles WHERE \
  365. rolname = '" ,name "')) as not_exists;\n"
  366. "\\gset\n"
  367. "\\if :not_exists\n"
  368. "CREATE ROLE \"" ,name "\""
  369. " WITH " ,(format-permissions permissions)
  370. ";\n"
  371. ,@(if create-database?
  372. `("CREATE DATABASE \"" ,name "\""
  373. " OWNER \"" ,name "\";\n")
  374. '())
  375. "\\endif\n")))
  376. roles)))
  377. (let ((host (postgresql-role-configuration-host config))
  378. (roles (postgresql-role-configuration-roles config)))
  379. #~(let ((psql #$(file-append postgresql "/bin/psql")))
  380. (list psql "-a" "-h" #$host "-f" #$(roles->queries roles)))))
  381. (define (postgresql-role-shepherd-service config)
  382. (match-record config <postgresql-role-configuration>
  383. (log)
  384. (list (shepherd-service
  385. (requirement '(postgres))
  386. (provision '(postgres-roles))
  387. (one-shot? #t)
  388. (start
  389. #~(lambda args
  390. (let ((pid (fork+exec-command
  391. #$(postgresql-create-roles config)
  392. #:user "postgres"
  393. #:group "postgres"
  394. #:log-file #$log)))
  395. (zero? (cdr (waitpid pid))))))
  396. (documentation "Create PostgreSQL roles.")))))
  397. (define postgresql-role-service-type
  398. (service-type (name 'postgresql-role)
  399. (extensions
  400. (list (service-extension shepherd-root-service-type
  401. postgresql-role-shepherd-service)))
  402. (compose concatenate)
  403. (extend (lambda (config extended-roles)
  404. (match-record config <postgresql-role-configuration>
  405. (host roles)
  406. (postgresql-role-configuration
  407. (host host)
  408. (roles (append roles extended-roles))))))
  409. (default-value (postgresql-role-configuration))
  410. (description "Ensure the specified PostgreSQL roles are
  411. created after the PostgreSQL database is started.")))
  412. ;;;
  413. ;;; Memcached
  414. ;;;
  415. (define-record-type* <memcached-configuration>
  416. memcached-configuration make-memcached-configuration
  417. memcached-configuration?
  418. (memcached memcached-configuration-memcached ;file-like
  419. (default memcached))
  420. (interfaces memcached-configuration-interfaces
  421. (default '("0.0.0.0")))
  422. (tcp-port memcached-configuration-tcp-port
  423. (default 11211))
  424. (udp-port memcached-configuration-udp-port
  425. (default 11211))
  426. (additional-options memcached-configuration-additional-options
  427. (default '())))
  428. (define %memcached-accounts
  429. (list (user-group (name "memcached") (system? #t))
  430. (user-account
  431. (name "memcached")
  432. (group "memcached")
  433. (system? #t)
  434. (comment "Memcached server user")
  435. (home-directory "/var/empty")
  436. (shell (file-append shadow "/sbin/nologin")))))
  437. (define memcached-activation
  438. #~(begin
  439. (use-modules (guix build utils))
  440. (let ((user (getpwnam "memcached")))
  441. (mkdir-p "/var/run/memcached")
  442. (chown "/var/run/memcached"
  443. (passwd:uid user) (passwd:gid user)))))
  444. (define memcached-shepherd-service
  445. (match-lambda
  446. (($ <memcached-configuration> memcached interfaces tcp-port udp-port
  447. additional-options)
  448. (with-imported-modules (source-module-closure
  449. '((gnu build shepherd)))
  450. (list (shepherd-service
  451. (provision '(memcached))
  452. (documentation "Run the Memcached daemon.")
  453. (requirement '(user-processes loopback))
  454. (modules '((gnu build shepherd)))
  455. (start #~(make-forkexec-constructor
  456. `(#$(file-append memcached "/bin/memcached")
  457. "-l" #$(string-join interfaces ",")
  458. "-p" #$(number->string tcp-port)
  459. "-U" #$(number->string udp-port)
  460. "--daemon"
  461. ;; Memcached changes to the memcached user prior to
  462. ;; writing the pid file, so write it to a directory
  463. ;; that memcached owns.
  464. "-P" "/var/run/memcached/pid"
  465. "-u" "memcached"
  466. ,#$@additional-options)
  467. #:log-file "/var/log/memcached"
  468. #:pid-file "/var/run/memcached/pid"))
  469. (stop #~(make-kill-destructor))))))))
  470. (define memcached-service-type
  471. (service-type (name 'memcached)
  472. (extensions
  473. (list (service-extension shepherd-root-service-type
  474. memcached-shepherd-service)
  475. (service-extension activation-service-type
  476. (const memcached-activation))
  477. (service-extension account-service-type
  478. (const %memcached-accounts))))
  479. (default-value (memcached-configuration))
  480. (description "Run @command{memcached}, a daemon that provides
  481. an in-memory caching service, intended for use by dynamic web
  482. applications.")))
  483. ;;;
  484. ;;; MySQL.
  485. ;;;
  486. (define-record-type* <mysql-configuration>
  487. mysql-configuration make-mysql-configuration
  488. mysql-configuration?
  489. (mysql mysql-configuration-mysql (default mariadb))
  490. (bind-address mysql-configuration-bind-address (default "127.0.0.1"))
  491. (port mysql-configuration-port (default 3306))
  492. (socket mysql-configuration-socket (default "/run/mysqld/mysqld.sock"))
  493. (datadir mysql-configuration-datadir (default "/var/lib/mysql"))
  494. (extra-content mysql-configuration-extra-content (default ""))
  495. (extra-environment mysql-configuration-extra-environment (default #~'()))
  496. (auto-upgrade? mysql-configuration-auto-upgrade? (default #t)))
  497. (define %mysql-accounts
  498. (list (user-group
  499. (name "mysql")
  500. (system? #t))
  501. (user-account
  502. (name "mysql")
  503. (group "mysql")
  504. (system? #t)
  505. (home-directory "/var/empty")
  506. (shell (file-append shadow "/sbin/nologin")))))
  507. (define mysql-configuration-file
  508. (match-lambda
  509. (($ <mysql-configuration> mysql bind-address port socket datadir extra-content)
  510. (mixed-text-file "my.cnf" "[mysqld]
  511. datadir=" datadir "
  512. socket=" socket "
  513. bind-address=" bind-address "
  514. port=" (number->string port) "
  515. " extra-content "
  516. "))))
  517. (define (mysqld-wrapper config)
  518. "Start mysqld, and initialize the system tables if necessary."
  519. (program-file
  520. "mysqld-wrapper"
  521. (with-imported-modules (source-module-closure
  522. '((guix build utils)))
  523. (let ((mysql (mysql-configuration-mysql config))
  524. (datadir (mysql-configuration-datadir config))
  525. (my.cnf (mysql-configuration-file config)))
  526. #~(begin
  527. (use-modules (guix build utils))
  528. (let* ((mysqld (string-append #$mysql "/bin/mysqld"))
  529. (user (getpwnam "mysql"))
  530. (uid (passwd:uid user))
  531. (gid (passwd:gid user))
  532. (rundir "/run/mysqld"))
  533. (mkdir-p #$datadir)
  534. (chown #$datadir uid gid)
  535. (mkdir-p rundir)
  536. (chown rundir uid gid)
  537. (unless (file-exists? (string-append #$datadir "/mysql"))
  538. (let ((init (system* #$(mysql-install config))))
  539. (unless (= 0 (status:exit-val init))
  540. (throw 'system-error "MySQL initialization failed."))))
  541. ;; Drop privileges and start the server.
  542. (setgid gid) (setuid uid)
  543. (execl mysqld mysqld
  544. (string-append "--defaults-file=" #$my.cnf))))))))
  545. (define (mysql-shepherd-service config)
  546. (list (shepherd-service
  547. (provision '(mysql))
  548. (requirement '(user-processes))
  549. (documentation "Run the MySQL server.")
  550. (actions (list (shepherd-configuration-action
  551. (mysql-configuration-file config))))
  552. (start (let ((mysql (mysql-configuration-mysql config))
  553. (extra-env (mysql-configuration-extra-environment config))
  554. (my.cnf (mysql-configuration-file config)))
  555. #~(make-forkexec-constructor
  556. (list #$(mysqld-wrapper config))
  557. #:log-file "/var/log/mysqld.log"
  558. #:environment-variables #$extra-env)))
  559. (stop #~(make-kill-destructor)))))
  560. (define (mysql-install config)
  561. "Install MySQL system database and secure the installation."
  562. (let ((mysql (mysql-configuration-mysql config))
  563. (my.cnf (mysql-configuration-file config)))
  564. (program-file
  565. "mysql-install"
  566. (with-imported-modules (source-module-closure
  567. '((guix build utils)))
  568. #~(begin
  569. (use-modules (guix build utils))
  570. ;; Make sed, mkdir, uname, etc available for mariadb-install-db.
  571. (set-path-environment-variable "PATH" '("bin")
  572. (list #$sed #$coreutils))
  573. (if (string=? "mariadb" #$(package-name mysql))
  574. ;; For MariaDB.
  575. (system* #$(file-append mysql "/bin/mariadb-install-db")
  576. (string-append "--defaults-file=" #$my.cnf)
  577. "--skip-test-db"
  578. "--user=mysql")
  579. ;; For MySQL.
  580. (system* #$(file-append mysql "/bin/mysqld")
  581. (string-append "--defaults-file=" #$my.cnf)
  582. "--initialize"
  583. "--user=mysql")))))))
  584. (define (mysql-upgrade-wrapper config)
  585. ;; The MySQL socket and PID file may appear before the server is ready to
  586. ;; accept connections. Ensure the socket is responsive before attempting
  587. ;; to run the upgrade script.
  588. (let ((mysql (mysql-configuration-mysql config))
  589. (socket-file (mysql-configuration-socket config))
  590. (config-file (mysql-configuration-file config)))
  591. (program-file
  592. "mysql-upgrade-wrapper"
  593. #~(begin
  594. (let ((mysql-upgrade #$(file-append mysql "/bin/mysql_upgrade"))
  595. (timeout 20))
  596. (begin
  597. (let loop ((i 0))
  598. (catch 'system-error
  599. (lambda ()
  600. (let ((sock (socket PF_UNIX SOCK_STREAM 0)))
  601. (connect sock AF_UNIX #$socket-file)
  602. (close-port sock)
  603. ;; The socket is ready!
  604. (execl mysql-upgrade mysql-upgrade
  605. (string-append "--defaults-file=" #$config-file)
  606. "--user=mysql")))
  607. (lambda args
  608. (if (< i timeout)
  609. (begin
  610. (sleep 1)
  611. (loop (+ 1 i)))
  612. ;; No luck, give up.
  613. (throw 'timeout-error
  614. "MySQL server did not appear in time!")))))))))))
  615. (define (mysql-upgrade-shepherd-service config)
  616. (list (shepherd-service
  617. (provision '(mysql-upgrade))
  618. (requirement '(mysql))
  619. (one-shot? #t)
  620. (documentation "Upgrade MySQL database schemas.")
  621. (start #~(make-forkexec-constructor
  622. (list #$(mysql-upgrade-wrapper config))
  623. #:user "mysql" #:group "mysql"
  624. #:log-file "/var/log/mysql_upgrade.log")))))
  625. (define (mysql-shepherd-services config)
  626. (let ((mysql-services (mysql-shepherd-service config)))
  627. (if (mysql-configuration-auto-upgrade? config)
  628. (append mysql-services
  629. (mysql-upgrade-shepherd-service config))
  630. mysql-services)))
  631. (define mysql-service-type
  632. (service-type
  633. (name 'mysql)
  634. (extensions
  635. (list (service-extension account-service-type
  636. (const %mysql-accounts))
  637. (service-extension shepherd-root-service-type
  638. mysql-shepherd-services)))
  639. (default-value (mysql-configuration))
  640. (description "Run the MySQL or MariaDB database server,
  641. @command{mysqld}.")))
  642. (define-deprecated (mysql-service #:key (config (mysql-configuration)))
  643. mysql-service-type
  644. (service mysql-service-type config))
  645. ;;;
  646. ;;; Redis
  647. ;;;
  648. (define-record-type* <redis-configuration>
  649. redis-configuration make-redis-configuration
  650. redis-configuration?
  651. (redis redis-configuration-redis ;file-like
  652. (default redis))
  653. (bind redis-configuration-bind
  654. (default "127.0.0.1"))
  655. (port redis-configuration-port
  656. (default 6379))
  657. (working-directory redis-configuration-working-directory
  658. (default "/var/lib/redis"))
  659. (config-file redis-configuration-config-file
  660. (default #f)))
  661. (define (default-redis.conf bind port working-directory)
  662. (mixed-text-file "redis.conf"
  663. "bind " bind "\n"
  664. "port " (number->string port) "\n"
  665. "dir " working-directory "\n"
  666. "daemonize no\n"))
  667. (define %redis-accounts
  668. (list (user-group (name "redis") (system? #t))
  669. (user-account
  670. (name "redis")
  671. (group "redis")
  672. (system? #t)
  673. (comment "Redis server user")
  674. (home-directory "/var/empty")
  675. (shell (file-append shadow "/sbin/nologin")))))
  676. (define redis-activation
  677. (match-lambda
  678. (($ <redis-configuration> redis bind port working-directory config-file)
  679. #~(begin
  680. (use-modules (guix build utils)
  681. (ice-9 match))
  682. (let ((user (getpwnam "redis")))
  683. (mkdir-p #$working-directory)
  684. (chown #$working-directory (passwd:uid user) (passwd:gid user)))))))
  685. (define redis-shepherd-service
  686. (match-lambda
  687. (($ <redis-configuration> redis bind port working-directory config-file)
  688. (let ((config-file
  689. (or config-file
  690. (default-redis.conf bind port working-directory))))
  691. (list (shepherd-service
  692. (provision '(redis))
  693. (documentation "Run the Redis daemon.")
  694. (requirement '(user-processes syslogd))
  695. (actions (list (shepherd-configuration-action config-file)))
  696. (start #~(make-forkexec-constructor
  697. '(#$(file-append redis "/bin/redis-server")
  698. #$config-file)
  699. #:user "redis"
  700. #:group "redis"))
  701. (stop #~(make-kill-destructor))))))))
  702. (define redis-service-type
  703. (service-type (name 'redis)
  704. (extensions
  705. (list (service-extension shepherd-root-service-type
  706. redis-shepherd-service)
  707. (service-extension activation-service-type
  708. redis-activation)
  709. (service-extension account-service-type
  710. (const %redis-accounts))))
  711. (default-value (redis-configuration))
  712. (description "Run Redis, a caching key/value store.")))