forked from nixcloud/nixcloud-webservices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgresql.nix
147 lines (136 loc) · 4.66 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
{ lib, mkUnique, 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 ];
});
dbservices = lib.listToAttrs (lib.concatMap (cfg: let
dbuser = mkUnique cfg.user;
mkStateFile = action: let
filename = ".database-${action}-${cfg.name}";
in "${config.stateDir}/${filename}";
in [
{ 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";
};
};
}
{ 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;
};
};
}
]) (lib.attrValues dbs));
inherit (config.postgresql) dataDir;
inherit (package) psqlSchema;
configuration = {
hba_file = pkgs.writeText "pg_hba.conf" ''
local all all peer
'';
unix_socket_directories = config.runtimeDir;
log_destination = "stderr";
port = "5432";
listen_addresses = "";
};
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 != {}) {
users.postgres = {
group = "postgres";
description = "PostgreSQL Server User";
};
groups.postgres = {};
systemd.services = {
postgresql-initdb = {
description = "Initialize PostgreSQL Cluster";
instance.requiredBy = [ "postgresql.service" ];
instance.before = [ "postgresql.service" ];
preStart = let
escDataDir = lib.escapeShellArg dataDir;
escUserGroup = lib.escapeShellArg (mkUnique "postgres");
in ''
mkdir -m 0711 -p ${escDataDir}
chmod 0700 ${escDataDir}
chown ${escUserGroup}:${escUserGroup} ${escDataDir}
'';
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;
};
}