]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - modules/private/databases/postgresql.nix
Add new machine to nixops
[perso/Immae/Config/Nix.git] / modules / private / databases / postgresql.nix
CommitLineData
4aac110f 1{ lib, pkgs, config, ... }:
4ff90563 2let
182ae57f 3 cfg = config.myServices.databases.postgresql;
4ff90563 4in {
182ae57f 5 options.myServices.databases = {
4ff90563
IB
6 postgresql = {
7 enable = lib.mkOption {
8415083e 8 default = false;
4ff90563
IB
9 example = true;
10 description = "Whether to enable postgresql database";
11 type = lib.types.bool;
12 };
4aac110f
IB
13 package = lib.mkOption {
14 type = lib.types.package;
15 default = pkgs.postgresql;
16 description = ''
17 Postgresql package to use.
18 '';
19 };
20 ldapConfig = lib.mkOption {
21 description = "LDAP configuration to allow PAM identification via LDAP";
22 type = lib.types.submodule {
23 options = {
24 host = lib.mkOption { type = lib.types.str; };
25 base = lib.mkOption { type = lib.types.str; };
26 dn = lib.mkOption { type = lib.types.str; };
27 password = lib.mkOption { type = lib.types.str; };
28 filter = lib.mkOption { type = lib.types.str; };
29 };
30 };
31 };
32 replicationLdapConfig = lib.mkOption {
33 description = "LDAP configuration to allow replication";
34 type = lib.types.submodule {
35 options = {
36 host = lib.mkOption { type = lib.types.str; };
37 base = lib.mkOption { type = lib.types.str; };
38 dn = lib.mkOption { type = lib.types.str; };
39 password = lib.mkOption { type = lib.types.str; };
40 };
41 };
42 };
43 authorizedHosts = lib.mkOption {
44 default = {};
45 description = "Hosts to allow connections from";
46 type = lib.types.attrsOf (lib.types.listOf (lib.types.submodule {
47 options = {
48 method = lib.mkOption {
49 default = "md5";
50 type = lib.types.str;
51 };
52 username = lib.mkOption {
53 default = "all";
54 type = lib.types.str;
55 };
56 database = lib.mkOption {
57 default = "all";
58 type = lib.types.str;
59 };
60 ip4 = lib.mkOption {
61 default = [];
62 type = lib.types.listOf lib.types.str;
63 };
64 ip6 = lib.mkOption {
65 default = [];
66 type = lib.types.listOf lib.types.str;
67 };
68 };
69 }));
70 };
71 replicationHosts = lib.mkOption {
72 default = {};
73 description = "Hosts to allow replication from";
74 type = lib.types.attrsOf (lib.types.submodule {
75 options = {
76 ip4 = lib.mkOption {
77 type = lib.types.listOf lib.types.str;
78 };
79 ip6 = lib.mkOption {
80 type = lib.types.listOf lib.types.str;
81 };
82 };
83 });
84 };
182ae57f
IB
85 # Output variables
86 socketsDir = lib.mkOption {
87 type = lib.types.path;
88 default = "/run/postgresql";
89 description = ''
90 The directory where Postgresql puts sockets.
91 '';
92 readOnly = true;
93 };
94 systemdRuntimeDirectory = lib.mkOption {
95 type = lib.types.str;
96 # Use ReadWritePaths= instead if socketsDir is outside of /run
97 default = assert lib.strings.hasPrefix "/run/" cfg.socketsDir;
98 lib.strings.removePrefix "/run/" cfg.socketsDir;
99 description = ''
100 Adjusted Postgresql sockets directory for systemd
101 '';
102 readOnly = true;
103 };
4ff90563
IB
104 };
105 };
106
107 config = lib.mkIf cfg.enable {
4ff90563
IB
108 networking.firewall.allowedTCPPorts = [ 5432 ];
109
182ae57f 110 security.acme.certs."postgresql" = config.myServices.databasesCerts // {
4ff90563
IB
111 user = "postgres";
112 group = "postgres";
113 plugins = [ "fullchain.pem" "key.pem" "account_key.json" ];
114 domain = "db-1.immae.eu";
115 postRun = ''
116 systemctl reload postgresql.service
117 '';
118 };
119
182ae57f
IB
120 systemd.services.postgresql.serviceConfig = {
121 SupplementaryGroups = "keys";
122 RuntimeDirectory = cfg.systemdRuntimeDirectory;
123 };
4aac110f 124 services.postgresql = {
182ae57f 125 enable = true;
4aac110f 126 package = cfg.package;
4ff90563
IB
127 enableTCPIP = true;
128 extraConfig = ''
129 max_connections = 100
130 wal_level = logical
131 shared_buffers = 512MB
132 work_mem = 10MB
133 max_wal_size = 1GB
134 min_wal_size = 80MB
135 log_timezone = 'Europe/Paris'
136 datestyle = 'iso, mdy'
137 timezone = 'Europe/Paris'
138 lc_messages = 'en_US.UTF-8'
139 lc_monetary = 'en_US.UTF-8'
140 lc_numeric = 'en_US.UTF-8'
141 lc_time = 'en_US.UTF-8'
142 default_text_search_config = 'pg_catalog.english'
143 ssl = on
9ade8f6e
IB
144 ssl_cert_file = '${config.security.acme.directory}/postgresql/fullchain.pem'
145 ssl_key_file = '${config.security.acme.directory}/postgresql/key.pem'
4ff90563 146 '';
4aac110f
IB
147 authentication = let
148 hosts = builtins.concatStringsSep "\n" (
149 lib.lists.flatten (lib.mapAttrsToList (k: vs: map (v:
150 map (ip6: "hostssl ${v.database} ${v.username} ${ip6}/128 ${v.method}") v.ip6
151 ++ map (ip4: "hostssl ${v.database} ${v.username} ${ip4}/32 ${v.method}") v.ip4
152 ) vs) cfg.authorizedHosts
153 ));
154 replication = builtins.concatStringsSep "\n" (
155 lib.lists.flatten (lib.mapAttrsToList (k: v:
156 map (ip6: "hostssl replication ${k} ${ip6}/128 pam pamservice=postgresql_replication") v.ip6
157 ++ map (ip4: "hostssl replication ${k} ${ip4}/32 pam pamservice=postgresql_replication") v.ip4
158 ) cfg.replicationHosts
159 ));
160 in ''
4ff90563
IB
161 local all postgres ident
162 local all all md5
4aac110f 163 ${hosts}
4ff90563 164 hostssl all all all pam
4aac110f 165 ${replication}
4ff90563
IB
166 '';
167 };
168
1a718805 169 secrets.keys = [
7178c2b1
IB
170 {
171 dest = "postgresql/pam";
e1da84b0
IB
172 permissions = "0400";
173 group = "postgres";
174 user = "postgres";
4aac110f
IB
175 text = with cfg.ldapConfig; ''
176 host ${host}
177 base ${base}
e1da84b0
IB
178 binddn ${dn}
179 bindpw ${password}
180 pam_filter ${filter}
181 ssl start_tls
4ff90563 182 '';
7178c2b1
IB
183 }
184 {
185 dest = "postgresql/pam_replication";
e1da84b0
IB
186 permissions = "0400";
187 group = "postgres";
188 user = "postgres";
4aac110f
IB
189 text = with cfg.replicationLdapConfig; ''
190 host ${host}
191 base ${base}
192 binddn ${dn}
193 bindpw ${password}
e1da84b0
IB
194 pam_login_attribute cn
195 ssl start_tls
4ff90563 196 '';
7178c2b1
IB
197 }
198 ];
e1da84b0
IB
199
200 security.pam.services = let
201 pam_ldap = "${pkgs.pam_ldap}/lib/security/pam_ldap.so";
4ff90563
IB
202 in [
203 {
204 name = "postgresql";
205 text = ''
182ae57f
IB
206 auth required ${pam_ldap} config=${config.secrets.location}/postgresql/pam
207 account required ${pam_ldap} config=${config.secrets.location}/postgresql/pam
4ff90563
IB
208 '';
209 }
210 {
211 name = "postgresql_replication";
212 text = ''
182ae57f
IB
213 auth required ${pam_ldap} config=${config.secrets.location}/postgresql/pam_replication
214 account required ${pam_ldap} config=${config.secrets.location}/postgresql/pam_replication
4ff90563
IB
215 '';
216 }
217 ];
218 };
219}
220