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 ;;; Copyright © 2019 Robert Vollmert <rob@vllmrt.net>
9 ;;; Copyright © 2020 Marius Bakke <marius@gnu.org>
11 ;;; This file is part of GNU Guix.
13 ;;; GNU Guix is free software; you can redistribute it and/or modify it
14 ;;; under the terms of the GNU General Public License as published by
15 ;;; the Free Software Foundation; either version 3 of the License, or (at
16 ;;; your option) any later version.
18 ;;; GNU Guix is distributed in the hope that it will be useful, but
19 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;;; GNU General Public License for more details.
23 ;;; You should have received a copy of the GNU General Public License
24 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
26 (define-module (gnu services databases)
27 #:use-module (gnu services)
28 #:use-module (gnu services shepherd)
29 #:use-module (gnu system shadow)
30 #:use-module (gnu packages admin)
31 #:use-module (gnu packages databases)
32 #:use-module (guix build-system trivial)
33 #:use-module (guix build union)
34 #:use-module (guix deprecation)
35 #:use-module (guix modules)
36 #:use-module (guix packages)
37 #:use-module (guix records)
38 #:use-module (guix gexp)
39 #:use-module (srfi srfi-1)
40 #:use-module (ice-9 match)
41 #:export (postgresql-config-file
42 postgresql-config-file?
43 postgresql-config-file-log-destination
44 postgresql-config-file-hba-file
45 postgresql-config-file-ident-file
46 postgresql-config-file-extra-config
48 postgresql-configuration
49 postgresql-configuration?
50 postgresql-configuration-postgresql
51 postgresql-configuration-port
52 postgresql-configuration-locale
53 postgresql-configuration-file
54 postgresql-configuration-data-directory
57 postgresql-service-type
59 memcached-service-type
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
69 mongodb-configuration?
70 mongodb-configuration-mongodb
71 mongodb-configuration-config-file
72 mongodb-configuration-data-directory
86 ;;; Database services.
90 (define %default-postgres-hba
91 (plain-file "pg_hba.conf"
94 host all all 127.0.0.1/32 md5
95 host all all ::1/128 md5"))
97 (define %default-postgres-ident
98 (plain-file "pg_ident.conf"
99 "# MAPNAME SYSTEM-USERNAME PG-USERNAME"))
101 (define-record-type* <postgresql-config-file>
102 postgresql-config-file make-postgresql-config-file
103 postgresql-config-file?
104 (log-destination postgresql-config-file-log-destination
106 (hba-file postgresql-config-file-hba-file
107 (default %default-postgres-hba))
108 (ident-file postgresql-config-file-ident-file
109 (default %default-postgres-ident))
110 (extra-config postgresql-config-file-extra-config
113 (define-gexp-compiler (postgresql-config-file-compiler
114 (file <postgresql-config-file>) system target)
116 (($ <postgresql-config-file> log-destination hba-file
117 ident-file extra-config)
118 (define (single-quote string)
120 (list "'" string "'")
128 ((key values ...) `(,key " = " ,@values "\n")))
130 `(("log_destination" ,@(single-quote log-destination))
131 ("hba_file" ,@(single-quote hba-file))
132 ("ident_file" ,@(single-quote ident-file))
137 #~(call-with-output-file (ungexp output "out")
140 (string-append #$@contents)
142 #:local-build? #t))))
144 (define-record-type* <postgresql-configuration>
145 postgresql-configuration make-postgresql-configuration
146 postgresql-configuration?
147 (postgresql postgresql-configuration-postgresql) ;<package>
148 (port postgresql-configuration-port
150 (locale postgresql-configuration-locale
151 (default "en_US.utf8"))
152 (config-file postgresql-configuration-file
153 (default (postgresql-config-file)))
154 (data-directory postgresql-configuration-data-directory
155 (default "/var/lib/postgresql/data"))
156 (extension-packages postgresql-configuration-extension-packages
159 (define %postgresql-accounts
160 (list (user-group (name "postgres") (system? #t))
165 (comment "PostgreSQL server user")
166 (home-directory "/var/empty")
167 (shell (file-append shadow "/sbin/nologin")))))
169 (define (final-postgresql postgresql extension-packages)
170 (if (null? extension-packages)
175 (build-system trivial-build-system)
177 `(#:modules ((guix build utils) (guix build union))
180 (use-modules (guix build utils) (guix build union) (srfi srfi-26))
181 (union-build (assoc-ref %outputs "out") (map (lambda (input) (cdr input)) %build-inputs))
184 `(("postgresql" ,postgresql)
185 ,@(map (lambda (extension) (list "extension" extension))
186 extension-packages))))))
188 (define postgresql-activation
190 (($ <postgresql-configuration> postgresql port locale config-file data-directory
193 (use-modules (guix build utils)
196 (let ((user (getpwnam "postgres"))
197 (initdb (string-append #$(final-postgresql postgresql extension-packages)
202 (list (string-append "--locale=" #$locale))
204 ;; Create db state directory.
205 (mkdir-p #$data-directory)
206 (chown #$data-directory (passwd:uid user) (passwd:gid user))
208 ;; Drop privileges and init state directory in a new
209 ;; process. Wait for it to finish before proceeding.
210 (match (primitive-fork)
212 ;; Exit with a non-zero status code if an exception is thrown.
216 (setgid (passwd:gid user))
217 (setuid (passwd:uid user))
225 (primitive-exit 1))))
226 (pid (waitpid pid))))))))
228 (define postgresql-shepherd-service
230 (($ <postgresql-configuration> postgresql port locale config-file data-directory
232 (let* ((pg_ctl-wrapper
233 ;; Wrapper script that switches to the 'postgres' user before
238 (use-modules (ice-9 match)
240 (match (command-line)
242 (let ((user (getpwnam "postgres"))
243 (pg_ctl #$(file-append (final-postgresql postgresql extension-packages)
245 (options (format #f "--config-file=~a -p ~d"
246 #$config-file #$port)))
247 (setgid (passwd:gid user))
248 (setuid (passwd:uid user))
249 (execl pg_ctl pg_ctl "-D" #$data-directory "-o" options
251 (pid-file (in-vicinity data-directory "postmaster.pid"))
254 (invoke #$pg_ctl-wrapper #$@args)
257 (call-with-input-file #$pid-file read))
259 (list (shepherd-service
260 (provision '(postgres))
261 (documentation "Run the PostgreSQL daemon.")
262 (requirement '(user-processes loopback syslogd))
263 (modules `((ice-9 match)
265 (start (action "start"))
266 (stop (action "stop"))))))))
268 (define postgresql-service-type
269 (service-type (name 'postgresql)
271 (list (service-extension shepherd-root-service-type
272 postgresql-shepherd-service)
273 (service-extension activation-service-type
274 postgresql-activation)
275 (service-extension account-service-type
276 (const %postgresql-accounts))
277 (service-extension profile-service-type
278 (compose list postgresql-configuration-postgresql))))))
280 (define-deprecated (postgresql-service #:key (postgresql postgresql)
282 (locale "en_US.utf8")
283 (config-file (postgresql-config-file))
284 (data-directory "/var/lib/postgresql/data")
285 (extension-packages '()))
286 postgresql-service-type
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 (bind-address mysql-configuration-bind-address (default "127.0.0.1"))
466 (port mysql-configuration-port (default 3306))
467 (socket mysql-configuration-socket (default "/run/mysqld/mysqld.sock"))
468 (extra-content mysql-configuration-extra-content (default ""))
469 (auto-upgrade? mysql-configuration-auto-upgrade? (default #t)))
471 (define %mysql-accounts
479 (home-directory "/var/empty")
480 (shell (file-append shadow "/sbin/nologin")))))
482 (define mysql-configuration-file
484 (($ <mysql-configuration> mysql bind-address port socket extra-content)
485 (mixed-text-file "my.cnf" "[mysqld]
486 datadir=/var/lib/mysql
488 bind-address=" bind-address "
489 port=" (number->string port) "
493 (define (%mysql-activation config)
494 "Return an activation gexp for the MySQL or MariaDB database server."
495 (let ((mysql (mysql-configuration-mysql config))
496 (my.cnf (mysql-configuration-file config)))
498 (use-modules (ice-9 popen)
500 (let* ((mysqld (string-append #$mysql "/bin/mysqld"))
501 (user (getpwnam "mysql"))
502 (uid (passwd:uid user))
503 (gid (passwd:gid user))
504 (datadir "/var/lib/mysql")
505 (rundir "/run/mysqld"))
507 (chown datadir uid gid)
509 (chown rundir uid gid)
510 ;; Initialize the database when it doesn't exist.
511 (when (not (file-exists? (string-append datadir "/mysql")))
512 (if (string-prefix? "mysql-" (strip-store-file-name #$mysql))
515 (string-append "--defaults-file=" #$my.cnf)
519 ;; XXX: The 'mysql_install_db' script doesn't work directly
520 ;; due to missing 'mkdir' in PATH.
521 (let ((p (open-pipe* OPEN_WRITE mysqld
523 "--defaults-file=" #$my.cnf)
526 ;; Create the system database, as does by 'mysql_install_db'.
527 (display "create database mysql;\n" p)
528 (display "use mysql;\n" p)
531 (call-with-input-file
532 (string-append #$mysql:lib "/share/mysql/" sql)
533 (lambda (in) (dump-port in p))))
534 '("mysql_system_tables.sql"
535 "mysql_performance_tables.sql"
536 "mysql_system_tables_data.sql"
537 "fill_help_tables.sql"))
538 ;; Remove the anonymous user and disable root access from
539 ;; remote machines, as does by 'mysql_secure_installation'.
541 DELETE FROM user WHERE User='';
542 DELETE FROM user WHERE User='root' AND
543 Host NOT IN ('localhost', '127.0.0.1', '::1');
546 (close-pipe p))))))))
548 (define (mysql-shepherd-service config)
549 (list (shepherd-service
551 (documentation "Run the MySQL server.")
552 (start (let ((mysql (mysql-configuration-mysql config))
553 (my.cnf (mysql-configuration-file config)))
554 #~(make-forkexec-constructor
555 (list (string-append #$mysql "/bin/mysqld")
556 (string-append "--defaults-file=" #$my.cnf))
557 #:user "mysql" #:group "mysql")))
558 (stop #~(make-kill-destructor)))))
560 (define (mysql-upgrade-wrapper mysql socket-file)
561 ;; The MySQL socket and PID file may appear before the server is ready to
562 ;; accept connections. Ensure the socket is responsive before attempting
563 ;; to run the upgrade script.
565 "mysql-upgrade-wrapper"
567 (let ((mysql-upgrade #$(file-append mysql "/bin/mysql_upgrade"))
573 (let ((sock (socket PF_UNIX SOCK_STREAM 0)))
574 (connect sock AF_UNIX #$socket-file)
576 ;; The socket is ready!
577 (execl mysql-upgrade mysql-upgrade
578 (string-append "--socket=" #$socket-file))))
585 (throw 'timeout-error
586 "MySQL server did not appear in time!"))))))))))
588 (define (mysql-upgrade-shepherd-service config)
589 (list (shepherd-service
590 (provision '(mysql-upgrade))
591 (requirement '(mysql))
593 (documentation "Upgrade MySQL database schemas.")
594 (start (let ((mysql (mysql-configuration-mysql config))
595 (socket (mysql-configuration-socket config)))
596 #~(make-forkexec-constructor
597 (list #$(mysql-upgrade-wrapper mysql socket))
598 #:user "mysql" #:group "mysql"))))))
600 (define (mysql-shepherd-services config)
601 (if (mysql-configuration-auto-upgrade? config)
602 (append (mysql-shepherd-service config)
603 (mysql-upgrade-shepherd-service config))
604 (mysql-shepherd-service config)))
606 (define mysql-service-type
610 (list (service-extension account-service-type
611 (const %mysql-accounts))
612 (service-extension activation-service-type
614 (service-extension shepherd-root-service-type
615 mysql-shepherd-services)))
616 (default-value (mysql-configuration))))
618 (define-deprecated (mysql-service #:key (config (mysql-configuration)))
620 (service mysql-service-type config))
627 (define-record-type* <redis-configuration>
628 redis-configuration make-redis-configuration
630 (redis redis-configuration-redis ;<package>
632 (bind redis-configuration-bind
633 (default "127.0.0.1"))
634 (port redis-configuration-port
636 (working-directory redis-configuration-working-directory
637 (default "/var/lib/redis"))
638 (config-file redis-configuration-config-file
641 (define (default-redis.conf bind port working-directory)
642 (mixed-text-file "redis.conf"
644 "port " (number->string port) "\n"
645 "dir " working-directory "\n"
648 (define %redis-accounts
649 (list (user-group (name "redis") (system? #t))
654 (comment "Redis server user")
655 (home-directory "/var/empty")
656 (shell (file-append shadow "/sbin/nologin")))))
658 (define redis-activation
660 (($ <redis-configuration> redis bind port working-directory config-file)
662 (use-modules (guix build utils)
665 (let ((user (getpwnam "redis")))
666 (mkdir-p #$working-directory)
667 (chown #$working-directory (passwd:uid user) (passwd:gid user)))))))
669 (define redis-shepherd-service
671 (($ <redis-configuration> redis bind port working-directory config-file)
674 (default-redis.conf bind port working-directory))))
675 (list (shepherd-service
677 (documentation "Run the Redis daemon.")
678 (requirement '(user-processes syslogd))
679 (start #~(make-forkexec-constructor
680 '(#$(file-append redis "/bin/redis-server")
684 (stop #~(make-kill-destructor))))))))
686 (define redis-service-type
687 (service-type (name 'redis)
689 (list (service-extension shepherd-root-service-type
690 redis-shepherd-service)
691 (service-extension activation-service-type
693 (service-extension account-service-type
694 (const %redis-accounts))))
695 (default-value (redis-configuration))))