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>
9 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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
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
56 postgresql-service-type
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
68 <mongodb-configuration>
70 mongodb-configuration?
71 mongodb-configuration-mongodb
72 mongodb-configuration-config-file
73 mongodb-configuration-data-directory
87 ;;; Database services.
91 (define %default-postgres-hba
92 (plain-file "pg_hba.conf"
95 host all all 127.0.0.1/32 trust
96 host all all ::1/128 trust"))
98 (define %default-postgres-ident
99 (plain-file "pg_ident.conf"
100 "# MAPNAME SYSTEM-USERNAME PG-USERNAME"))
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
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
114 (define-gexp-compiler (postgresql-config-file-compiler
115 (file <postgresql-config-file>) system target)
117 (($ <postgresql-config-file> log-destination hba-file
118 ident-file extra-config)
119 (define (single-quote string)
121 (list "'" string "'")
129 ((key values ...) `(,key " = " ,@values "\n")))
131 `(("log_destination" ,@(single-quote log-destination))
132 ("hba_file" ,@(single-quote hba-file))
133 ("ident_file" ,@(single-quote ident-file))
138 #~(call-with-output-file (ungexp output "out")
141 (string-append #$@contents)
143 #:local-build? #t))))
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
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
161 (define %postgresql-accounts
162 (list (user-group (name "postgres") (system? #t))
167 (comment "PostgreSQL server user")
168 (home-directory "/var/empty")
169 (shell (file-append shadow "/sbin/nologin")))))
171 (define (final-postgresql postgresql extension-packages)
172 (if (null? extension-packages)
177 (build-system trivial-build-system)
179 `(#:modules ((guix build utils) (guix build union))
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))
186 `(("postgresql" ,postgresql)
187 ,@(map (lambda (extension) (list "extension" extension))
188 extension-packages))))))
190 (define postgresql-activation
192 (($ <postgresql-configuration> postgresql port locale config-file data-directory
195 (use-modules (guix build utils)
198 (let ((user (getpwnam "postgres"))
199 (initdb (string-append #$(final-postgresql postgresql extension-packages)
204 (list (string-append "--locale=" #$locale))
206 ;; Create db state directory.
207 (mkdir-p #$data-directory)
208 (chown #$data-directory (passwd:uid user) (passwd:gid user))
210 ;; Drop privileges and init state directory in a new
211 ;; process. Wait for it to finish before proceeding.
212 (match (primitive-fork)
214 ;; Exit with a non-zero status code if an exception is thrown.
218 (setgid (passwd:gid user))
219 (setuid (passwd:uid user))
227 (primitive-exit 1))))
228 (pid (waitpid pid))))))))
230 (define postgresql-shepherd-service
232 (($ <postgresql-configuration> postgresql port locale config-file data-directory
234 (let* ((pg_ctl-wrapper
235 ;; Wrapper script that switches to the 'postgres' user before
240 (use-modules (ice-9 match)
242 (match (command-line)
244 (let ((user (getpwnam "postgres"))
245 (pg_ctl #$(file-append (final-postgresql postgresql extension-packages)
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
253 (pid-file (in-vicinity data-directory "postmaster.pid"))
256 (invoke #$pg_ctl-wrapper #$@args)
259 (call-with-input-file #$pid-file read))
261 (list (shepherd-service
262 (provision '(postgres))
263 (documentation "Run the PostgreSQL daemon.")
264 (requirement '(user-processes loopback syslogd))
265 (modules `((ice-9 match)
267 (start (action "start"))
268 (stop (action "stop"))))))))
270 (define postgresql-service-type
271 (service-type (name 'postgresql)
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))))
281 (define* (postgresql-service #:key (postgresql postgresql)
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.
289 The PostgreSQL daemon loads its runtime configuration from @var{config-file}
290 and stores the database cluster in @var{data-directory}."
291 (service postgresql-service-type
292 (postgresql-configuration
293 (postgresql postgresql)
296 (config-file config-file)
297 (data-directory data-directory)
298 (extension-packages extension-packages))))
305 (define-record-type* <memcached-configuration>
306 memcached-configuration make-memcached-configuration
307 memcached-configuration?
308 (memcached memcached-configuration-memcached ;<package>
310 (interfaces memcached-configuration-interfaces
311 (default '("0.0.0.0")))
312 (tcp-port memcached-configuration-tcp-port
314 (udp-port memcached-configuration-udp-port
316 (additional-options memcached-configuration-additional-options
319 (define %memcached-accounts
320 (list (user-group (name "memcached") (system? #t))
325 (comment "Memcached server user")
326 (home-directory "/var/empty")
327 (shell (file-append shadow "/sbin/nologin")))))
329 (define memcached-activation
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)))))
337 (define memcached-shepherd-service
339 (($ <memcached-configuration> memcached interfaces tcp-port udp-port
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)
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"
359 ,#$@additional-options)
360 #:log-file "/var/log/memcached"
361 #:pid-file "/var/run/memcached/pid"))
362 (stop #~(make-kill-destructor))))))))
364 (define memcached-service-type
365 (service-type (name 'memcached)
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))))
380 (define %default-mongodb-configuration-file
383 "# GNU Guix: MongoDB default configuration file
385 pidFilePath: /var/run/mongodb/pid
387 dbPath: /var/lib/mongodb
391 (define-record-type* <mongodb-configuration>
392 mongodb-configuration make-mongodb-configuration
393 mongodb-configuration?
394 (mongodb mongodb-configuration-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")))
401 (define %mongodb-accounts
402 (list (user-group (name "mongodb") (system? #t))
407 (comment "Mongodb server user")
408 (home-directory "/var/lib/mongodb")
409 (shell (file-append shadow "/sbin/nologin")))))
411 (define mongodb-activation
413 (($ <mongodb-configuration> mongodb config-file data-directory)
415 (use-modules (guix build utils))
416 (let ((user (getpwnam "mongodb")))
421 (passwd:uid user) (passwd:gid user)))
422 '("/var/run/mongodb" #$data-directory)))))))
424 (define mongodb-shepherd-service
426 (($ <mongodb-configuration> mongodb config-file data-directory)
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")
437 #:pid-file "/var/run/mongodb/pid"
438 #:log-file "/var/log/mongodb.log"))
439 (stop #~(make-kill-destructor))))))
441 (define mongodb-service-type
444 (description "Run the MongoDB document database server.")
446 (list (service-extension shepherd-root-service-type
448 mongodb-shepherd-service))
449 (service-extension activation-service-type
451 (service-extension account-service-type
452 (const %mongodb-accounts))))
454 (mongodb-configuration))))
461 (define-record-type* <mysql-configuration>
462 mysql-configuration make-mysql-configuration
464 (mysql mysql-configuration-mysql (default mariadb))
465 (port mysql-configuration-port (default 3306)))
467 (define %mysql-accounts
475 (home-directory "/var/empty")
476 (shell (file-append shadow "/sbin/nologin")))))
478 (define mysql-configuration-file
480 (($ <mysql-configuration> mysql port)
481 (mixed-text-file "my.cnf" "[mysqld]
482 datadir=/var/lib/mysql
483 socket=/run/mysqld/mysqld.sock
484 port=" (number->string port) "
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)))
492 (use-modules (ice-9 popen)
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"))
501 (chown datadir uid gid)
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))
509 (string-append "--defaults-file=" #$my.cnf)
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
517 "--defaults-file=" #$my.cnf)
520 ;; Create the system database, as does by 'mysql_install_db'.
521 (display "create database mysql;\n" p)
522 (display "use mysql;\n" p)
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'.
535 DELETE FROM user WHERE User='';
536 DELETE FROM user WHERE User='root' AND
537 Host NOT IN ('localhost', '127.0.0.1', '::1');
540 (close-pipe p))))))))
542 (define (mysql-shepherd-service config)
543 (list (shepherd-service
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)))))
554 (define mysql-service-type
558 (list (service-extension account-service-type
559 (const %mysql-accounts))
560 (service-extension activation-service-type
562 (service-extension shepherd-root-service-type
563 mysql-shepherd-service)))
564 (default-value (mysql-configuration))))
566 (define* (mysql-service #:key (config (mysql-configuration)))
567 "Return a service that runs @command{mysqld}, the MySQL or MariaDB
570 The 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))
579 (define-record-type* <redis-configuration>
580 redis-configuration make-redis-configuration
582 (redis redis-configuration-redis ;<package>
584 (bind redis-configuration-bind
585 (default "127.0.0.1"))
586 (port redis-configuration-port
588 (working-directory redis-configuration-working-directory
589 (default "/var/lib/redis"))
590 (config-file redis-configuration-config-file
593 (define (default-redis.conf bind port working-directory)
594 (mixed-text-file "redis.conf"
596 "port " (number->string port) "\n"
597 "dir " working-directory "\n"
600 (define %redis-accounts
601 (list (user-group (name "redis") (system? #t))
606 (comment "Redis server user")
607 (home-directory "/var/empty")
608 (shell (file-append shadow "/sbin/nologin")))))
610 (define redis-activation
612 (($ <redis-configuration> redis bind port working-directory config-file)
614 (use-modules (guix build utils)
617 (let ((user (getpwnam "redis")))
618 (mkdir-p #$working-directory)
619 (chown #$working-directory (passwd:uid user) (passwd:gid user)))))))
621 (define redis-shepherd-service
623 (($ <redis-configuration> redis bind port working-directory config-file)
626 (default-redis.conf bind port working-directory))))
627 (list (shepherd-service
629 (documentation "Run the Redis daemon.")
630 (requirement '(user-processes syslogd))
631 (start #~(make-forkexec-constructor
632 '(#$(file-append redis "/bin/redis-server")
636 (stop #~(make-kill-destructor))))))))
638 (define redis-service-type
639 (service-type (name 'redis)
641 (list (service-extension shepherd-root-service-type
642 redis-shepherd-service)
643 (service-extension activation-service-type
645 (service-extension account-service-type
646 (const %redis-accounts))))
647 (default-value (redis-configuration))))