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