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>
8 ;;; This file is part of GNU Guix.
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.
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.
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/>.
23 (define-module (gnu services databases)
24 #:use-module (gnu services)
25 #:use-module (gnu services shepherd)
26 #:use-module (gnu system shadow)
27 #:use-module (gnu packages admin)
28 #:use-module (gnu packages databases)
29 #:use-module (guix modules)
30 #:use-module (guix records)
31 #:use-module (guix gexp)
32 #:use-module (srfi srfi-1)
33 #:use-module (ice-9 match)
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
42 <postgresql-configuration>
43 postgresql-configuration
44 postgresql-configuration?
45 postgresql-configuration-postgresql
46 postgresql-configuration-port
47 postgresql-configuration-locale
48 postgresql-configuration-file
49 postgresql-configuration-data-directory
52 postgresql-service-type
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
64 <mongodb-configuration>
66 mongodb-configuration?
67 mongodb-configuration-mongodb
68 mongodb-configuration-config-file
69 mongodb-configuration-data-directory
83 ;;; Database services.
87 (define %default-postgres-hba
88 (plain-file "pg_hba.conf"
91 host all all 127.0.0.1/32 trust
92 host all all ::1/128 trust"))
94 (define %default-postgres-ident
95 (plain-file "pg_ident.conf"
96 "# MAPNAME SYSTEM-USERNAME PG-USERNAME"))
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
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
110 (define-gexp-compiler (postgresql-config-file-compiler
111 (file <postgresql-config-file>) system target)
113 (($ <postgresql-config-file> log-destination hba-file
114 ident-file extra-config)
115 (define (single-quote string)
117 (list "'" string "'")
125 ((key values ...) `(,key " = " ,@values "\n")))
127 `(("log_destination" ,@(single-quote log-destination))
128 ("hba_file" ,@(single-quote hba-file))
129 ("ident_file" ,@(single-quote ident-file))
134 #~(call-with-output-file (ungexp output "out")
137 (string-append #$@contents)
139 #:local-build? #t))))
141 (define-record-type* <postgresql-configuration>
142 postgresql-configuration make-postgresql-configuration
143 postgresql-configuration?
144 (postgresql postgresql-configuration-postgresql ;<package>
145 (default postgresql))
146 (port postgresql-configuration-port
148 (locale postgresql-configuration-locale
149 (default "en_US.utf8"))
150 (config-file postgresql-configuration-file
151 (default (postgresql-config-file)))
152 (data-directory postgresql-configuration-data-directory
153 (default "/var/lib/postgresql/data")))
155 (define %postgresql-accounts
156 (list (user-group (name "postgres") (system? #t))
161 (comment "PostgreSQL server user")
162 (home-directory "/var/empty")
163 (shell (file-append shadow "/sbin/nologin")))))
165 (define postgresql-activation
167 (($ <postgresql-configuration> postgresql port locale config-file data-directory)
169 (use-modules (guix build utils)
172 (let ((user (getpwnam "postgres"))
173 (initdb (string-append #$postgresql "/bin/initdb"))
177 (list (string-append "--locale=" #$locale))
179 ;; Create db state directory.
180 (mkdir-p #$data-directory)
181 (chown #$data-directory (passwd:uid user) (passwd:gid user))
183 ;; Drop privileges and init state directory in a new
184 ;; process. Wait for it to finish before proceeding.
185 (match (primitive-fork)
187 ;; Exit with a non-zero status code if an exception is thrown.
191 (setgid (passwd:gid user))
192 (setuid (passwd:uid user))
200 (primitive-exit 1))))
201 (pid (waitpid pid))))))))
203 (define postgresql-shepherd-service
205 (($ <postgresql-configuration> postgresql port locale config-file data-directory)
206 (let* ((pg_ctl-wrapper
207 ;; Wrapper script that switches to the 'postgres' user before
212 (use-modules (ice-9 match)
214 (match (command-line)
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
226 (invoke #$pg_ctl-wrapper #$@args)))))
227 (list (shepherd-service
228 (provision '(postgres))
229 (documentation "Run the PostgreSQL daemon.")
230 (requirement '(user-processes loopback syslogd))
231 (start (action "start"))
232 (stop (action "stop"))))))))
234 (define postgresql-service-type
235 (service-type (name 'postgresql)
237 (list (service-extension shepherd-root-service-type
238 postgresql-shepherd-service)
239 (service-extension activation-service-type
240 postgresql-activation)
241 (service-extension account-service-type
242 (const %postgresql-accounts))))
243 (default-value (postgresql-configuration))))
245 (define* (postgresql-service #:key (postgresql postgresql)
247 (locale "en_US.utf8")
248 (config-file (postgresql-config-file))
249 (data-directory "/var/lib/postgresql/data"))
250 "Return a service that runs @var{postgresql}, the PostgreSQL database server.
252 The PostgreSQL daemon loads its runtime configuration from @var{config-file}
253 and stores the database cluster in @var{data-directory}."
254 (service postgresql-service-type
255 (postgresql-configuration
256 (postgresql postgresql)
259 (config-file config-file)
260 (data-directory data-directory))))
267 (define-record-type* <memcached-configuration>
268 memcached-configuration make-memcached-configuration
269 memcached-configuration?
270 (memcached memcached-configuration-memcached ;<package>
272 (interfaces memcached-configuration-interfaces
273 (default '("0.0.0.0")))
274 (tcp-port memcached-configuration-tcp-port
276 (udp-port memcached-configuration-udp-port
278 (additional-options memcached-configuration-additional-options
281 (define %memcached-accounts
282 (list (user-group (name "memcached") (system? #t))
287 (comment "Memcached server user")
288 (home-directory "/var/empty")
289 (shell (file-append shadow "/sbin/nologin")))))
291 (define memcached-activation
293 (use-modules (guix build utils))
294 (let ((user (getpwnam "memcached")))
295 (mkdir-p "/var/run/memcached")
296 (chown "/var/run/memcached"
297 (passwd:uid user) (passwd:gid user)))))
299 (define memcached-shepherd-service
301 (($ <memcached-configuration> memcached interfaces tcp-port udp-port
303 (with-imported-modules (source-module-closure
304 '((gnu build shepherd)))
305 (list (shepherd-service
306 (provision '(memcached))
307 (documentation "Run the Memcached daemon.")
308 (requirement '(user-processes loopback))
309 (modules '((gnu build shepherd)))
310 (start #~(make-forkexec-constructor
311 `(#$(file-append memcached "/bin/memcached")
312 "-l" #$(string-join interfaces ",")
313 "-p" #$(number->string tcp-port)
314 "-U" #$(number->string udp-port)
316 ;; Memcached changes to the memcached user prior to
317 ;; writing the pid file, so write it to a directory
318 ;; that memcached owns.
319 "-P" "/var/run/memcached/pid"
321 ,#$@additional-options)
322 #:log-file "/var/log/memcached"
323 #:pid-file "/var/run/memcached/pid"))
324 (stop #~(make-kill-destructor))))))))
326 (define memcached-service-type
327 (service-type (name 'memcached)
329 (list (service-extension shepherd-root-service-type
330 memcached-shepherd-service)
331 (service-extension activation-service-type
332 (const memcached-activation))
333 (service-extension account-service-type
334 (const %memcached-accounts))))
335 (default-value (memcached-configuration))))
342 (define %default-mongodb-configuration-file
345 "# GNU Guix: MongoDB default configuration file
347 pidFilePath: /var/run/mongodb/pid
349 dbPath: /var/lib/mongodb
353 (define-record-type* <mongodb-configuration>
354 mongodb-configuration make-mongodb-configuration
355 mongodb-configuration?
356 (mongodb mongodb-configuration-mongodb
358 (config-file mongodb-configuration-config-file
359 (default %default-mongodb-configuration-file))
360 (data-directory mongodb-configuration-data-directory
361 (default "/var/lib/mongodb")))
363 (define %mongodb-accounts
364 (list (user-group (name "mongodb") (system? #t))
369 (comment "Mongodb server user")
370 (home-directory "/var/lib/mongodb")
371 (shell (file-append shadow "/sbin/nologin")))))
373 (define mongodb-activation
375 (($ <mongodb-configuration> mongodb config-file data-directory)
377 (use-modules (guix build utils))
378 (let ((user (getpwnam "mongodb")))
383 (passwd:uid user) (passwd:gid user)))
384 '("/var/run/mongodb" #$data-directory)))))))
386 (define mongodb-shepherd-service
388 (($ <mongodb-configuration> mongodb config-file data-directory)
390 (provision '(mongodb))
391 (documentation "Run the Mongodb daemon.")
392 (requirement '(user-processes loopback))
393 (start #~(make-forkexec-constructor
394 `(,(string-append #$mongodb "/bin/mongod")
399 #:pid-file "/var/run/mongodb/pid"
400 #:log-file "/var/log/mongodb.log"))
401 (stop #~(make-kill-destructor))))))
403 (define mongodb-service-type
406 (description "Run the MongoDB document database server.")
408 (list (service-extension shepherd-root-service-type
410 mongodb-shepherd-service))
411 (service-extension activation-service-type
413 (service-extension account-service-type
414 (const %mongodb-accounts))))
416 (mongodb-configuration))))
423 (define-record-type* <mysql-configuration>
424 mysql-configuration make-mysql-configuration
426 (mysql mysql-configuration-mysql (default mariadb))
427 (port mysql-configuration-port (default 3306)))
429 (define %mysql-accounts
437 (home-directory "/var/empty")
438 (shell (file-append shadow "/sbin/nologin")))))
440 (define mysql-configuration-file
442 (($ <mysql-configuration> mysql port)
443 (mixed-text-file "my.cnf" "[mysqld]
444 datadir=/var/lib/mysql
445 socket=/run/mysqld/mysqld.sock
446 port=" (number->string port) "
449 (define (%mysql-activation config)
450 "Return an activation gexp for the MySQL or MariaDB database server."
451 (let ((mysql (mysql-configuration-mysql config))
452 (my.cnf (mysql-configuration-file config)))
454 (use-modules (ice-9 popen)
456 (let* ((mysqld (string-append #$mysql "/bin/mysqld"))
457 (user (getpwnam "mysql"))
458 (uid (passwd:uid user))
459 (gid (passwd:gid user))
460 (datadir "/var/lib/mysql")
461 (rundir "/run/mysqld"))
463 (chown datadir uid gid)
465 (chown rundir uid gid)
466 ;; Initialize the database when it doesn't exist.
467 (when (not (file-exists? (string-append datadir "/mysql")))
468 (if (string-prefix? "mysql-" (strip-store-file-name #$mysql))
471 (string-append "--defaults-file=" #$my.cnf)
475 ;; XXX: The 'mysql_install_db' script doesn't work directly
476 ;; due to missing 'mkdir' in PATH.
477 (let ((p (open-pipe* OPEN_WRITE mysqld
479 "--defaults-file=" #$my.cnf)
482 ;; Create the system database, as does by 'mysql_install_db'.
483 (display "create database mysql;\n" p)
484 (display "use mysql;\n" p)
487 (call-with-input-file
488 (string-append #$mysql "/share/mysql/" sql)
489 (lambda (in) (dump-port in p))))
490 '("mysql_system_tables.sql"
491 "mysql_performance_tables.sql"
492 "mysql_system_tables_data.sql"
493 "fill_help_tables.sql"))
494 ;; Remove the anonymous user and disable root access from
495 ;; remote machines, as does by 'mysql_secure_installation'.
497 DELETE FROM user WHERE User='';
498 DELETE FROM user WHERE User='root' AND
499 Host NOT IN ('localhost', '127.0.0.1', '::1');
502 (close-pipe p))))))))
504 (define (mysql-shepherd-service config)
505 (list (shepherd-service
507 (documentation "Run the MySQL server.")
508 (start (let ((mysql (mysql-configuration-mysql config))
509 (my.cnf (mysql-configuration-file config)))
510 #~(make-forkexec-constructor
511 (list (string-append #$mysql "/bin/mysqld")
512 (string-append "--defaults-file=" #$my.cnf))
513 #:user "mysql" #:group "mysql")))
514 (stop #~(make-kill-destructor)))))
516 (define mysql-service-type
520 (list (service-extension account-service-type
521 (const %mysql-accounts))
522 (service-extension activation-service-type
524 (service-extension shepherd-root-service-type
525 mysql-shepherd-service)))
526 (default-value (mysql-configuration))))
528 (define* (mysql-service #:key (config (mysql-configuration)))
529 "Return a service that runs @command{mysqld}, the MySQL or MariaDB
532 The optional @var{config} argument specifies the configuration for
533 @command{mysqld}, which should be a @code{<mysql-configuration>} object."
534 (service mysql-service-type config))
541 (define-record-type* <redis-configuration>
542 redis-configuration make-redis-configuration
544 (redis redis-configuration-redis ;<package>
546 (bind redis-configuration-bind
547 (default "127.0.0.1"))
548 (port redis-configuration-port
550 (working-directory redis-configuration-working-directory
551 (default "/var/lib/redis"))
552 (config-file redis-configuration-config-file
555 (define (default-redis.conf bind port working-directory)
556 (mixed-text-file "redis.conf"
558 "port " (number->string port) "\n"
559 "dir " working-directory "\n"
562 (define %redis-accounts
563 (list (user-group (name "redis") (system? #t))
568 (comment "Redis server user")
569 (home-directory "/var/empty")
570 (shell (file-append shadow "/sbin/nologin")))))
572 (define redis-activation
574 (($ <redis-configuration> redis bind port working-directory config-file)
576 (use-modules (guix build utils)
579 (let ((user (getpwnam "redis")))
580 (mkdir-p #$working-directory)
581 (chown #$working-directory (passwd:uid user) (passwd:gid user)))))))
583 (define redis-shepherd-service
585 (($ <redis-configuration> redis bind port working-directory config-file)
588 (default-redis.conf bind port working-directory))))
589 (list (shepherd-service
591 (documentation "Run the Redis daemon.")
592 (requirement '(user-processes syslogd))
593 (start #~(make-forkexec-constructor
594 '(#$(file-append redis "/bin/redis-server")
598 (stop #~(make-kill-destructor))))))))
600 (define redis-service-type
601 (service-type (name 'redis)
603 (list (service-extension shepherd-root-service-type
604 redis-shepherd-service)
605 (service-extension activation-service-type
607 (service-extension account-service-type
608 (const %redis-accounts))))
609 (default-value (redis-configuration))))