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