-
Notifications
You must be signed in to change notification settings - Fork 26
/
postgresql.nix
182 lines (162 loc) · 5.99 KB
/
postgresql.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
{ lib, mkUniqueUser, mkUniqueGroup, pkgs, config, ... }:
let
filterDb = lib.const (db: db.type == "postgresql");
dbs = lib.filterAttrs filterDb config.database;
package = config.postgresql.package.overrideAttrs (drv: {
configureFlags = (drv.configureFlags or []) ++ [ "--with-systemd" ];
buildInputs = (drv.buildInputs or []) ++ [ pkgs.systemd ];
});
mkMapName = database: "db-owners-${builtins.hashString "sha256" database}";
# This maps additional owners to the user which is the main owner of the
# database.
pgIdent = pkgs.writeText "pg_ident.conf" (lib.concatMapStrings (cfg: ''
${mkMapName cfg.name} "${mkUniqueUser cfg.user}" "${mkUniqueUser cfg.user}"
${lib.concatMapStrings (owner: ''
${mkMapName cfg.name} "${mkUniqueUser owner}" "${mkUniqueUser cfg.user}"
'') cfg.owners}
'') (lib.attrValues dbs));
dbservices = lib.listToAttrs (lib.concatMap (cfg: let
dbuser = mkUniqueUser cfg.user;
mkStateFile = action: let
filename = ".database-${action}-${cfg.name}";
in "${config.stateDir}/${filename}";
# XXX: Make this dry, because it's also used in a similar vein in
# mysql.nix!
createDbService = {
name = "database-${cfg.name}";
value = {
instance.requiredBy = [ "database-${cfg.name}.target" ];
instance.before = [ "database-${cfg.name}.target" ];
instance.after = [ "postgresql.service" ];
environment.PGHOST = cfg.socketPath;
script = ''
${package}/bin/createuser ${lib.escapeShellArg dbuser}
${package}/bin/createdb ${lib.escapeShellArg cfg.name} \
-O ${lib.escapeShellArg dbuser}
'';
postStart = "touch ${lib.escapeShellArg (mkStateFile "create")}";
unitConfig.ConditionPathExists = "!${mkStateFile "create"}";
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
PermissionsStartOnly = true;
User = "postgres";
Group = "postgres";
};
};
};
# XXX: Make this dry, because it's also used in a similar vein in
# mysql.nix!
postCreateDbService = {
name = "database-${cfg.name}-post-create";
value = {
instance.requiredBy = [ "database-${cfg.name}.target" ];
instance.before = [ "database-${cfg.name}.target" ];
instance.after = [
"postgresql.service"
"database-${cfg.name}.service"
];
environment.PGHOST = cfg.socketPath;
script = cfg.postCreate;
path = lib.singleton (pkgs.writeScriptBin "sqlsh" ''
#!${pkgs.stdenv.shell}
exec ${package}/bin/psql ${lib.escapeShellArg cfg.name} "$@"
'');
postStart = "touch ${lib.escapeShellArg (mkStateFile "post-create")}";
unitConfig.ConditionPathExists = "!${mkStateFile "post-create"}";
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
PermissionsStartOnly = true;
User = cfg.user;
};
};
};
services = lib.singleton createDbService
++ lib.optional (cfg.postCreate != "") postCreateDbService;
in services) (lib.attrValues dbs));
inherit (config.postgresql) dataDir;
inherit (package) psqlSchema;
configuration = {
hba_file = pkgs.writeText "pg_hba.conf" (lib.concatMapStrings (cfg: ''
local "${cfg.name}" all peer map=${mkMapName cfg.name}
'') (lib.attrValues dbs) + ''
local all all peer
'');
unix_socket_directories = config.runtimeDir;
log_destination = "stderr";
port = "5432";
listen_addresses = "";
ident_file = pgIdent;
};
in {
options.postgresql = {
package = lib.mkOption {
type = lib.types.package;
default = pkgs.postgresql96;
example = lib.literalExample "pkgs.postgresql96";
description = "PostgreSQL package to use.";
};
dataDir = lib.mkOption {
type = lib.types.path;
default = "${config.stateDir}/postgresql/${psqlSchema}";
readOnly = true;
description = "Data directory for PostgreSQL";
};
};
config = lib.mkIf (dbs != {} && config.enable) {
users.postgres = {
group = "postgres";
description = "PostgreSQL Server User";
};
groups.postgres = {};
tempDbSetupHook.dependencies = [ package ];
tempDbSetupHook.script = ./postgresql-hook.sh;
dbShellCommand.postgresql = ''
export PGHOST=${lib.escapeShellArg config.runtimeDir}
exec ${lib.escapeShellArg "${package}/bin/psql"} "$2"
'';
directories."postgresql/${psqlSchema}" = {
instance.before = [ "postgresql-initdb.service" ];
permissions.defaultDirectoryMode = "0711";
permissions.group.noAccess = true;
permissions.others.noAccess = true;
permissions.enableACLs = false;
owner = mkUniqueUser "postgres";
group = mkUniqueGroup "postgres";
};
systemd.services = {
postgresql-initdb = {
description = "Initialize PostgreSQL Cluster";
instance.requiredBy = [ "postgresql.service" ];
instance.before = [ "postgresql.service" ];
environment.PGDATA = dataDir;
unitConfig.ConditionPathExists = "!${dataDir}/PG_VERSION";
serviceConfig = {
ExecStart = "${package}/bin/initdb";
Type = "oneshot";
RemainAfterExit = true;
PermissionsStartOnly = true;
User = "postgres";
Group = "postgres";
};
};
postgresql = {
description = "PostgreSQL Server";
instance.requiredBy = [ "db-server.target" ];
instance.before = [ "db-server.target" ];
after = [ "network.target" ];
environment.PGDATA = dataDir;
serviceConfig = {
ExecStart = let
mkCfgVal = name: val: "-c ${lib.escapeShellArg "${name}=${val}"}";
cfgVals = lib.mapAttrsToList mkCfgVal configuration;
in "${package}/bin/postgres ${lib.concatStringsSep " " cfgVals}";
User = "postgres";
Group = "postgres";
Type = "notify";
};
};
} // dbservices;
};
}