Commit | Line | Data |
---|---|---|
105369a4 DT |
1 | ;;; GNU Guix --- Functional package management for GNU |
2 | ;;; Copyright © 2015 David Thompson <davet@gnu.org> | |
be1c2c54 | 3 | ;;; Copyright © 2015 Ludovic Courtès <ludo@gnu.org> |
105369a4 DT |
4 | ;;; |
5 | ;;; This file is part of GNU Guix. | |
6 | ;;; | |
7 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
8 | ;;; under the terms of the GNU General Public License as published by | |
9 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
10 | ;;; your option) any later version. | |
11 | ;;; | |
12 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
13 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | ;;; GNU General Public License for more details. | |
16 | ;;; | |
17 | ;;; You should have received a copy of the GNU General Public License | |
18 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
19 | ||
20 | (define-module (gnu services databases) | |
21 | #:use-module (gnu services) | |
0190c1c0 | 22 | #:use-module (gnu services shepherd) |
105369a4 DT |
23 | #:use-module (gnu system shadow) |
24 | #:use-module (gnu packages admin) | |
25 | #:use-module (gnu packages databases) | |
26 | #:use-module (guix records) | |
105369a4 | 27 | #:use-module (guix gexp) |
0adfe95a | 28 | #:use-module (ice-9 match) |
105369a4 DT |
29 | #:export (postgresql-service)) |
30 | ||
31 | ;;; Commentary: | |
32 | ;;; | |
33 | ;;; Database services. | |
34 | ;;; | |
35 | ;;; Code: | |
36 | ||
0adfe95a LC |
37 | (define-record-type* <postgresql-configuration> |
38 | postgresql-configuration make-postgresql-configuration | |
39 | postgresql-configuration? | |
40 | (postgresql postgresql-configuration-postgresql ;<package> | |
41 | (default postgresql)) | |
42 | (config-file postgresql-configuration-file) | |
43 | (data-directory postgresql-configuration-data-directory)) | |
44 | ||
105369a4 | 45 | (define %default-postgres-hba |
be1c2c54 LC |
46 | (plain-file "pg_hba.conf" |
47 | " | |
105369a4 DT |
48 | local all all trust |
49 | host all all 127.0.0.1/32 trust | |
50 | host all all ::1/128 trust")) | |
51 | ||
52 | (define %default-postgres-ident | |
be1c2c54 | 53 | (plain-file "pg_ident.conf" |
105369a4 DT |
54 | "# MAPNAME SYSTEM-USERNAME PG-USERNAME")) |
55 | ||
56 | (define %default-postgres-config | |
be1c2c54 LC |
57 | (mixed-text-file "postgresql.conf" |
58 | "hba_file = '" %default-postgres-hba "'\n" | |
59 | "ident_file = '" %default-postgres-ident "\n")) | |
105369a4 | 60 | |
0adfe95a LC |
61 | (define %postgresql-accounts |
62 | (list (user-group (name "postgres") (system? #t)) | |
63 | (user-account | |
64 | (name "postgres") | |
65 | (group "postgres") | |
66 | (system? #t) | |
67 | (comment "PostgreSQL server user") | |
68 | (home-directory "/var/empty") | |
69 | (shell #~(string-append #$shadow "/sbin/nologin"))))) | |
70 | ||
71 | (define postgresql-activation | |
72 | (match-lambda | |
73 | (($ <postgresql-configuration> postgresql config-file data-directory) | |
74 | #~(begin | |
75 | (use-modules (guix build utils) | |
76 | (ice-9 match)) | |
77 | ||
78 | (let ((user (getpwnam "postgres")) | |
79 | (initdb (string-append #$postgresql "/bin/initdb"))) | |
80 | ;; Create db state directory. | |
81 | (mkdir-p #$data-directory) | |
82 | (chown #$data-directory (passwd:uid user) (passwd:gid user)) | |
83 | ||
84 | ;; Drop privileges and init state directory in a new | |
85 | ;; process. Wait for it to finish before proceeding. | |
86 | (match (primitive-fork) | |
87 | (0 | |
88 | ;; Exit with a non-zero status code if an exception is thrown. | |
89 | (dynamic-wind | |
90 | (const #t) | |
91 | (lambda () | |
92 | (setgid (passwd:gid user)) | |
93 | (setuid (passwd:uid user)) | |
94 | (primitive-exit (system* initdb "-D" #$data-directory))) | |
95 | (lambda () | |
96 | (primitive-exit 1)))) | |
97 | (pid (waitpid pid)))))))) | |
98 | ||
d4053c71 | 99 | (define postgresql-shepherd-service |
0adfe95a LC |
100 | (match-lambda |
101 | (($ <postgresql-configuration> postgresql config-file data-directory) | |
102 | (let ((start-script | |
103 | ;; Wrapper script that switches to the 'postgres' user before | |
104 | ;; launching daemon. | |
105 | (program-file "start-postgres" | |
106 | #~(let ((user (getpwnam "postgres")) | |
107 | (postgres (string-append #$postgresql | |
108 | "/bin/postgres"))) | |
109 | (setgid (passwd:gid user)) | |
110 | (setuid (passwd:uid user)) | |
111 | (system* postgres | |
112 | (string-append "--config-file=" | |
113 | #$config-file) | |
114 | "-D" #$data-directory))))) | |
d4053c71 | 115 | (list (shepherd-service |
0adfe95a LC |
116 | (provision '(postgres)) |
117 | (documentation "Run the PostgreSQL daemon.") | |
118 | (requirement '(user-processes loopback)) | |
119 | (start #~(make-forkexec-constructor #$start-script)) | |
120 | (stop #~(make-kill-destructor)))))))) | |
121 | ||
122 | (define postgresql-service-type | |
123 | (service-type (name 'postgresql) | |
124 | (extensions | |
d4053c71 AK |
125 | (list (service-extension shepherd-root-service-type |
126 | postgresql-shepherd-service) | |
0adfe95a LC |
127 | (service-extension activation-service-type |
128 | postgresql-activation) | |
129 | (service-extension account-service-type | |
130 | (const %postgresql-accounts)))))) | |
131 | ||
105369a4 DT |
132 | (define* (postgresql-service #:key (postgresql postgresql) |
133 | (config-file %default-postgres-config) | |
134 | (data-directory "/var/lib/postgresql/data")) | |
135 | "Return a service that runs @var{postgresql}, the PostgreSQL database server. | |
136 | ||
137 | The PostgreSQL daemon loads its runtime configuration from @var{config-file} | |
138 | and stores the database cluster in @var{data-directory}." | |
0adfe95a LC |
139 | (service postgresql-service-type |
140 | (postgresql-configuration | |
141 | (postgresql postgresql) | |
142 | (config-file config-file) | |
143 | (data-directory data-directory)))) |