services: Add nftables-service-type.
[jackhill/guix/guix.git] / gnu / services / databases.scm
CommitLineData
105369a4
DT
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2015 David Thompson <davet@gnu.org>
9b1cee97 3;;; Copyright © 2015, 2016 Ludovic Courtès <ludo@gnu.org>
8823ed4e 4;;; Copyright © 2016 Leo Famulari <leo@famulari.name>
67cadaca 5;;; Copyright © 2017 Christopher Baines <mail@cbaines.net>
5ee4cd69 6;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
0d57a50a 7;;; Copyright © 2018 Julien Lepiller <julien@lepiller.eu>
334a2f4d 8;;; Copyright © 2019 Robert Vollmert <rob@vllmrt.net>
105369a4
DT
9;;;
10;;; This file is part of GNU Guix.
11;;;
12;;; GNU Guix is free software; you can redistribute it and/or modify it
13;;; under the terms of the GNU General Public License as published by
14;;; the Free Software Foundation; either version 3 of the License, or (at
15;;; your option) any later version.
16;;;
17;;; GNU Guix is distributed in the hope that it will be useful, but
18;;; WITHOUT ANY WARRANTY; without even the implied warranty of
19;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;;; GNU General Public License for more details.
21;;;
22;;; You should have received a copy of the GNU General Public License
23;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
24
25(define-module (gnu services databases)
26 #:use-module (gnu services)
0190c1c0 27 #:use-module (gnu services shepherd)
105369a4
DT
28 #:use-module (gnu system shadow)
29 #:use-module (gnu packages admin)
30 #:use-module (gnu packages databases)
0d57a50a
JL
31 #:use-module (guix build-system trivial)
32 #:use-module (guix build union)
119fdd0d 33 #:use-module (guix modules)
0d57a50a 34 #:use-module (guix packages)
105369a4 35 #:use-module (guix records)
105369a4 36 #:use-module (guix gexp)
936e7a52 37 #:use-module (srfi srfi-1)
0adfe95a 38 #:use-module (ice-9 match)
936e7a52
CB
39 #:export (<postgresql-config-file>
40 postgresql-config-file
41 postgresql-config-file?
42 postgresql-config-file-log-destination
43 postgresql-config-file-hba-file
44 postgresql-config-file-ident-file
45 postgresql-config-file-extra-config
46
488ea71e
CB
47 <postgresql-configuration>
48 postgresql-configuration
24e96431 49 postgresql-configuration?
488ea71e
CB
50 postgresql-configuration-postgresql
51 postgresql-configuration-port
52 postgresql-configuration-locale
53 postgresql-configuration-file
54 postgresql-configuration-data-directory
55
24e96431
56 postgresql-service
57 postgresql-service-type
58
119fdd0d
CB
59 memcached-service-type
60 <memcached-configuration>
61 memcached-configuration
62 memcached-configuration?
63 memcached-configuration-memecached
64 memcached-configuration-interfaces
65 memcached-configuration-tcp-port
66 memcached-configuration-udp-port
67 memcached-configuration-additional-options
68
5266ff71
CB
69 <mongodb-configuration>
70 mongodb-configuration
71 mongodb-configuration?
72 mongodb-configuration-mongodb
73 mongodb-configuration-config-file
74 mongodb-configuration-data-directory
75 mongodb-service-type
76
6575183b 77 mysql-service
24e96431
78 mysql-service-type
79 mysql-configuration
67cadaca
CB
80 mysql-configuration?
81
82 redis-configuration
83 redis-configuration?
84 redis-service-type))
105369a4
DT
85
86;;; Commentary:
87;;;
88;;; Database services.
89;;;
90;;; Code:
91
936e7a52
CB
92(define %default-postgres-hba
93 (plain-file "pg_hba.conf"
94 "
334a2f4d
RV
95local all all peer
96host all all 127.0.0.1/32 md5
97host all all ::1/128 md5"))
936e7a52
CB
98
99(define %default-postgres-ident
100 (plain-file "pg_ident.conf"
101 "# MAPNAME SYSTEM-USERNAME PG-USERNAME"))
102
103(define-record-type* <postgresql-config-file>
104 postgresql-config-file make-postgresql-config-file
105 postgresql-config-file?
106 (log-destination postgresql-config-file-log-destination
107 (default "syslog"))
108 (hba-file postgresql-config-file-hba-file
109 (default %default-postgres-hba))
110 (ident-file postgresql-config-file-ident-file
111 (default %default-postgres-ident))
112 (extra-config postgresql-config-file-extra-config
113 (default '())))
114
115(define-gexp-compiler (postgresql-config-file-compiler
116 (file <postgresql-config-file>) system target)
117 (match file
118 (($ <postgresql-config-file> log-destination hba-file
119 ident-file extra-config)
53380838 120 (define (single-quote string)
936e7a52
CB
121 (if string
122 (list "'" string "'")
123 '()))
124
125 (define contents
126 (append-map
127 (match-lambda
128 ((key) '())
129 ((key . #f) '())
130 ((key values ...) `(,key " = " ,@values "\n")))
131
53380838
CB
132 `(("log_destination" ,@(single-quote log-destination))
133 ("hba_file" ,@(single-quote hba-file))
134 ("ident_file" ,@(single-quote ident-file))
936e7a52
CB
135 ,@extra-config)))
136
137 (gexp->derivation
138 "postgresql.conf"
139 #~(call-with-output-file (ungexp output "out")
140 (lambda (port)
141 (display
142 (string-append #$@contents)
143 port)))
144 #:local-build? #t))))
145
0adfe95a
LC
146(define-record-type* <postgresql-configuration>
147 postgresql-configuration make-postgresql-configuration
148 postgresql-configuration?
0d57a50a
JL
149 (postgresql postgresql-configuration-postgresql ;<package>
150 (default postgresql))
151 (port postgresql-configuration-port
152 (default 5432))
153 (locale postgresql-configuration-locale
154 (default "en_US.utf8"))
155 (config-file postgresql-configuration-file
156 (default (postgresql-config-file)))
157 (data-directory postgresql-configuration-data-directory
158 (default "/var/lib/postgresql/data"))
159 (extension-packages postgresql-configuration-extension-packages
160 (default '())))
0adfe95a 161
0adfe95a
LC
162(define %postgresql-accounts
163 (list (user-group (name "postgres") (system? #t))
164 (user-account
165 (name "postgres")
166 (group "postgres")
167 (system? #t)
168 (comment "PostgreSQL server user")
169 (home-directory "/var/empty")
9e41130b 170 (shell (file-append shadow "/sbin/nologin")))))
0adfe95a 171
0d57a50a
JL
172(define (final-postgresql postgresql extension-packages)
173 (if (null? extension-packages)
174 postgresql
175 (package
176 (inherit postgresql)
177 (source #f)
178 (build-system trivial-build-system)
179 (arguments
180 `(#:modules ((guix build utils) (guix build union))
181 #:builder
182 (begin
183 (use-modules (guix build utils) (guix build union) (srfi srfi-26))
184 (union-build (assoc-ref %outputs "out") (map (lambda (input) (cdr input)) %build-inputs))
185 #t)))
186 (inputs
187 `(("postgresql" ,postgresql)
188 ,@(map (lambda (extension) (list "extension" extension))
189 extension-packages))))))
190
0adfe95a
LC
191(define postgresql-activation
192 (match-lambda
0d57a50a
JL
193 (($ <postgresql-configuration> postgresql port locale config-file data-directory
194 extension-packages)
0adfe95a
LC
195 #~(begin
196 (use-modules (guix build utils)
197 (ice-9 match))
198
199 (let ((user (getpwnam "postgres"))
0d57a50a
JL
200 (initdb (string-append #$(final-postgresql postgresql extension-packages)
201 "/bin/initdb"))
e05b780a
CB
202 (initdb-args
203 (append
204 (if #$locale
205 (list (string-append "--locale=" #$locale))
206 '()))))
0adfe95a
LC
207 ;; Create db state directory.
208 (mkdir-p #$data-directory)
209 (chown #$data-directory (passwd:uid user) (passwd:gid user))
210
211 ;; Drop privileges and init state directory in a new
212 ;; process. Wait for it to finish before proceeding.
213 (match (primitive-fork)
214 (0
215 ;; Exit with a non-zero status code if an exception is thrown.
216 (dynamic-wind
217 (const #t)
218 (lambda ()
219 (setgid (passwd:gid user))
220 (setuid (passwd:uid user))
e05b780a
CB
221 (primitive-exit
222 (apply system*
223 initdb
224 "-D"
225 #$data-directory
226 initdb-args)))
0adfe95a
LC
227 (lambda ()
228 (primitive-exit 1))))
229 (pid (waitpid pid))))))))
230
d4053c71 231(define postgresql-shepherd-service
0adfe95a 232 (match-lambda
0d57a50a
JL
233 (($ <postgresql-configuration> postgresql port locale config-file data-directory
234 extension-packages)
5ee4cd69
CL
235 (let* ((pg_ctl-wrapper
236 ;; Wrapper script that switches to the 'postgres' user before
237 ;; launching daemon.
238 (program-file
239 "pg_ctl-wrapper"
240 #~(begin
241 (use-modules (ice-9 match)
242 (ice-9 format))
243 (match (command-line)
244 ((_ mode)
245 (let ((user (getpwnam "postgres"))
0d57a50a
JL
246 (pg_ctl #$(file-append (final-postgresql postgresql extension-packages)
247 "/bin/pg_ctl"))
5ee4cd69
CL
248 (options (format #f "--config-file=~a -p ~d"
249 #$config-file #$port)))
250 (setgid (passwd:gid user))
251 (setuid (passwd:uid user))
252 (execl pg_ctl pg_ctl "-D" #$data-directory "-o" options
253 mode)))))))
ef2dda8e 254 (pid-file (in-vicinity data-directory "postmaster.pid"))
5ee4cd69
CL
255 (action (lambda args
256 #~(lambda _
ef2dda8e
CL
257 (invoke #$pg_ctl-wrapper #$@args)
258 (match '#$args
259 (("start")
260 (call-with-input-file #$pid-file read))
261 (_ #t))))))
d4053c71 262 (list (shepherd-service
0adfe95a
LC
263 (provision '(postgres))
264 (documentation "Run the PostgreSQL daemon.")
9b1cee97 265 (requirement '(user-processes loopback syslogd))
ef2dda8e
CL
266 (modules `((ice-9 match)
267 ,@%default-modules))
5ee4cd69
CL
268 (start (action "start"))
269 (stop (action "stop"))))))))
0adfe95a
LC
270
271(define postgresql-service-type
272 (service-type (name 'postgresql)
273 (extensions
d4053c71
AK
274 (list (service-extension shepherd-root-service-type
275 postgresql-shepherd-service)
0adfe95a
LC
276 (service-extension activation-service-type
277 postgresql-activation)
278 (service-extension account-service-type
1e6b9c6e
CB
279 (const %postgresql-accounts))))
280 (default-value (postgresql-configuration))))
0adfe95a 281
105369a4 282(define* (postgresql-service #:key (postgresql postgresql)
2d3d5cc5 283 (port 5432)
e05b780a 284 (locale "en_US.utf8")
936e7a52 285 (config-file (postgresql-config-file))
0d57a50a
JL
286 (data-directory "/var/lib/postgresql/data")
287 (extension-packages '()))
105369a4
DT
288 "Return a service that runs @var{postgresql}, the PostgreSQL database server.
289
290The PostgreSQL daemon loads its runtime configuration from @var{config-file}
291and stores the database cluster in @var{data-directory}."
0adfe95a
LC
292 (service postgresql-service-type
293 (postgresql-configuration
294 (postgresql postgresql)
2d3d5cc5 295 (port port)
e05b780a 296 (locale locale)
0adfe95a 297 (config-file config-file)
0d57a50a
JL
298 (data-directory data-directory)
299 (extension-packages extension-packages))))
6575183b
SB
300
301\f
119fdd0d
CB
302;;;
303;;; Memcached
304;;;
305
306(define-record-type* <memcached-configuration>
307 memcached-configuration make-memcached-configuration
308 memcached-configuration?
309 (memcached memcached-configuration-memcached ;<package>
310 (default memcached))
311 (interfaces memcached-configuration-interfaces
312 (default '("0.0.0.0")))
313 (tcp-port memcached-configuration-tcp-port
314 (default 11211))
315 (udp-port memcached-configuration-udp-port
316 (default 11211))
317 (additional-options memcached-configuration-additional-options
318 (default '())))
319
320(define %memcached-accounts
321 (list (user-group (name "memcached") (system? #t))
322 (user-account
323 (name "memcached")
324 (group "memcached")
325 (system? #t)
326 (comment "Memcached server user")
327 (home-directory "/var/empty")
328 (shell (file-append shadow "/sbin/nologin")))))
329
6230e155
CB
330(define memcached-activation
331 #~(begin
332 (use-modules (guix build utils))
333 (let ((user (getpwnam "memcached")))
334 (mkdir-p "/var/run/memcached")
335 (chown "/var/run/memcached"
336 (passwd:uid user) (passwd:gid user)))))
337
119fdd0d
CB
338(define memcached-shepherd-service
339 (match-lambda
340 (($ <memcached-configuration> memcached interfaces tcp-port udp-port
341 additional-options)
342 (with-imported-modules (source-module-closure
343 '((gnu build shepherd)))
344 (list (shepherd-service
345 (provision '(memcached))
346 (documentation "Run the Memcached daemon.")
347 (requirement '(user-processes loopback))
348 (modules '((gnu build shepherd)))
349 (start #~(make-forkexec-constructor
350 `(#$(file-append memcached "/bin/memcached")
351 "-l" #$(string-join interfaces ",")
352 "-p" #$(number->string tcp-port)
353 "-U" #$(number->string udp-port)
354 "--daemon"
6230e155
CB
355 ;; Memcached changes to the memcached user prior to
356 ;; writing the pid file, so write it to a directory
357 ;; that memcached owns.
358 "-P" "/var/run/memcached/pid"
119fdd0d
CB
359 "-u" "memcached"
360 ,#$@additional-options)
361 #:log-file "/var/log/memcached"
6230e155 362 #:pid-file "/var/run/memcached/pid"))
119fdd0d
CB
363 (stop #~(make-kill-destructor))))))))
364
365(define memcached-service-type
366 (service-type (name 'memcached)
367 (extensions
368 (list (service-extension shepherd-root-service-type
369 memcached-shepherd-service)
6230e155
CB
370 (service-extension activation-service-type
371 (const memcached-activation))
119fdd0d
CB
372 (service-extension account-service-type
373 (const %memcached-accounts))))
374 (default-value (memcached-configuration))))
375
376\f
5266ff71
CB
377;;;
378;;; MongoDB
379;;;
380
381(define %default-mongodb-configuration-file
382 (plain-file
383 "mongodb.yaml"
384 "# GNU Guix: MongoDB default configuration file
385processManagement:
386 pidFilePath: /var/run/mongodb/pid
387storage:
388 dbPath: /var/lib/mongodb
389"))
390
391
392(define-record-type* <mongodb-configuration>
393 mongodb-configuration make-mongodb-configuration
394 mongodb-configuration?
395 (mongodb mongodb-configuration-mongodb
396 (default mongodb))
397 (config-file mongodb-configuration-config-file
398 (default %default-mongodb-configuration-file))
399 (data-directory mongodb-configuration-data-directory
400 (default "/var/lib/mongodb")))
401
402(define %mongodb-accounts
403 (list (user-group (name "mongodb") (system? #t))
404 (user-account
405 (name "mongodb")
406 (group "mongodb")
407 (system? #t)
408 (comment "Mongodb server user")
409 (home-directory "/var/lib/mongodb")
410 (shell (file-append shadow "/sbin/nologin")))))
411
412(define mongodb-activation
413 (match-lambda
414 (($ <mongodb-configuration> mongodb config-file data-directory)
415 #~(begin
416 (use-modules (guix build utils))
417 (let ((user (getpwnam "mongodb")))
418 (for-each
419 (lambda (directory)
420 (mkdir-p directory)
421 (chown directory
422 (passwd:uid user) (passwd:gid user)))
423 '("/var/run/mongodb" #$data-directory)))))))
424
425(define mongodb-shepherd-service
426 (match-lambda
427 (($ <mongodb-configuration> mongodb config-file data-directory)
428 (shepherd-service
429 (provision '(mongodb))
430 (documentation "Run the Mongodb daemon.")
431 (requirement '(user-processes loopback))
432 (start #~(make-forkexec-constructor
433 `(,(string-append #$mongodb "/bin/mongod")
434 "--config"
435 ,#$config-file)
436 #:user "mongodb"
437 #:group "mongodb"
438 #:pid-file "/var/run/mongodb/pid"
439 #:log-file "/var/log/mongodb.log"))
440 (stop #~(make-kill-destructor))))))
441
442(define mongodb-service-type
443 (service-type
444 (name 'mongodb)
445 (description "Run the MongoDB document database server.")
446 (extensions
447 (list (service-extension shepherd-root-service-type
448 (compose list
449 mongodb-shepherd-service))
450 (service-extension activation-service-type
451 mongodb-activation)
452 (service-extension account-service-type
453 (const %mongodb-accounts))))
454 (default-value
455 (mongodb-configuration))))
456
457\f
6575183b
SB
458;;;
459;;; MySQL.
460;;;
461
462(define-record-type* <mysql-configuration>
463 mysql-configuration make-mysql-configuration
464 mysql-configuration?
4b41febf
CB
465 (mysql mysql-configuration-mysql (default mariadb))
466 (port mysql-configuration-port (default 3306)))
6575183b
SB
467
468(define %mysql-accounts
469 (list (user-group
470 (name "mysql")
471 (system? #t))
472 (user-account
473 (name "mysql")
474 (group "mysql")
475 (system? #t)
476 (home-directory "/var/empty")
9e41130b 477 (shell (file-append shadow "/sbin/nologin")))))
6575183b
SB
478
479(define mysql-configuration-file
480 (match-lambda
4b41febf
CB
481 (($ <mysql-configuration> mysql port)
482 (mixed-text-file "my.cnf" "[mysqld]
6575183b
SB
483datadir=/var/lib/mysql
484socket=/run/mysqld/mysqld.sock
4b41febf 485port=" (number->string port) "
6575183b
SB
486"))))
487
488(define (%mysql-activation config)
489 "Return an activation gexp for the MySQL or MariaDB database server."
490 (let ((mysql (mysql-configuration-mysql config))
491 (my.cnf (mysql-configuration-file config)))
492 #~(begin
493 (use-modules (ice-9 popen)
494 (guix build utils))
495 (let* ((mysqld (string-append #$mysql "/bin/mysqld"))
496 (user (getpwnam "mysql"))
497 (uid (passwd:uid user))
498 (gid (passwd:gid user))
499 (datadir "/var/lib/mysql")
500 (rundir "/run/mysqld"))
501 (mkdir-p datadir)
502 (chown datadir uid gid)
503 (mkdir-p rundir)
504 (chown rundir uid gid)
505 ;; Initialize the database when it doesn't exist.
506 (when (not (file-exists? (string-append datadir "/mysql")))
507 (if (string-prefix? "mysql-" (strip-store-file-name #$mysql))
508 ;; For MySQL.
509 (system* mysqld
510 (string-append "--defaults-file=" #$my.cnf)
511 "--initialize"
512 "--user=mysql")
513 ;; For MariaDB.
514 ;; XXX: The 'mysql_install_db' script doesn't work directly
515 ;; due to missing 'mkdir' in PATH.
516 (let ((p (open-pipe* OPEN_WRITE mysqld
517 (string-append
518 "--defaults-file=" #$my.cnf)
519 "--bootstrap"
520 "--user=mysql")))
521 ;; Create the system database, as does by 'mysql_install_db'.
522 (display "create database mysql;\n" p)
523 (display "use mysql;\n" p)
524 (for-each
525 (lambda (sql)
526 (call-with-input-file
527 (string-append #$mysql "/share/mysql/" sql)
528 (lambda (in) (dump-port in p))))
529 '("mysql_system_tables.sql"
530 "mysql_performance_tables.sql"
531 "mysql_system_tables_data.sql"
532 "fill_help_tables.sql"))
533 ;; Remove the anonymous user and disable root access from
534 ;; remote machines, as does by 'mysql_secure_installation'.
535 (display "
536DELETE FROM user WHERE User='';
537DELETE FROM user WHERE User='root' AND
538 Host NOT IN ('localhost', '127.0.0.1', '::1');
539FLUSH PRIVILEGES;
540" p)
541 (close-pipe p))))))))
542
543(define (mysql-shepherd-service config)
544 (list (shepherd-service
545 (provision '(mysql))
546 (documentation "Run the MySQL server.")
547 (start (let ((mysql (mysql-configuration-mysql config))
548 (my.cnf (mysql-configuration-file config)))
549 #~(make-forkexec-constructor
550 (list (string-append #$mysql "/bin/mysqld")
551 (string-append "--defaults-file=" #$my.cnf))
552 #:user "mysql" #:group "mysql")))
553 (stop #~(make-kill-destructor)))))
554
555(define mysql-service-type
556 (service-type
557 (name 'mysql)
558 (extensions
559 (list (service-extension account-service-type
560 (const %mysql-accounts))
561 (service-extension activation-service-type
562 %mysql-activation)
563 (service-extension shepherd-root-service-type
e903738f
CB
564 mysql-shepherd-service)))
565 (default-value (mysql-configuration))))
6575183b
SB
566
567(define* (mysql-service #:key (config (mysql-configuration)))
568 "Return a service that runs @command{mysqld}, the MySQL or MariaDB
569database server.
570
571The optional @var{config} argument specifies the configuration for
572@command{mysqld}, which should be a @code{<mysql-configuration>} object."
573 (service mysql-service-type config))
67cadaca
CB
574
575\f
576;;;
577;;; Redis
578;;;
579
580(define-record-type* <redis-configuration>
581 redis-configuration make-redis-configuration
582 redis-configuration?
583 (redis redis-configuration-redis ;<package>
584 (default redis))
585 (bind redis-configuration-bind
586 (default "127.0.0.1"))
587 (port redis-configuration-port
588 (default 6379))
589 (working-directory redis-configuration-working-directory
590 (default "/var/lib/redis"))
591 (config-file redis-configuration-config-file
592 (default #f)))
593
594(define (default-redis.conf bind port working-directory)
595 (mixed-text-file "redis.conf"
596 "bind " bind "\n"
597 "port " (number->string port) "\n"
598 "dir " working-directory "\n"
599 "daemonize no\n"))
600
601(define %redis-accounts
602 (list (user-group (name "redis") (system? #t))
603 (user-account
604 (name "redis")
605 (group "redis")
606 (system? #t)
607 (comment "Redis server user")
608 (home-directory "/var/empty")
609 (shell (file-append shadow "/sbin/nologin")))))
610
611(define redis-activation
612 (match-lambda
613 (($ <redis-configuration> redis bind port working-directory config-file)
614 #~(begin
615 (use-modules (guix build utils)
616 (ice-9 match))
617
618 (let ((user (getpwnam "redis")))
619 (mkdir-p #$working-directory)
620 (chown #$working-directory (passwd:uid user) (passwd:gid user)))))))
621
622(define redis-shepherd-service
623 (match-lambda
624 (($ <redis-configuration> redis bind port working-directory config-file)
625 (let ((config-file
626 (or config-file
627 (default-redis.conf bind port working-directory))))
628 (list (shepherd-service
629 (provision '(redis))
630 (documentation "Run the Redis daemon.")
631 (requirement '(user-processes syslogd))
632 (start #~(make-forkexec-constructor
633 '(#$(file-append redis "/bin/redis-server")
634 #$config-file)
635 #:user "redis"
636 #:group "redis"))
637 (stop #~(make-kill-destructor))))))))
638
639(define redis-service-type
640 (service-type (name 'redis)
641 (extensions
642 (list (service-extension shepherd-root-service-type
643 redis-shepherd-service)
644 (service-extension activation-service-type
645 redis-activation)
646 (service-extension account-service-type
bc037c1b
CB
647 (const %redis-accounts))))
648 (default-value (redis-configuration))))