]>
Commit | Line | Data |
---|---|---|
9d90e7e2 | 1 | { lib, pkgs, config, myconfig, mylibs, ... }: |
4d4f13f4 IB |
2 | let |
3 | cfg = config.services.myDatabases; | |
4 | in { | |
5 | options.services.myDatabases = { | |
6 | enable = lib.mkEnableOption "my databases service"; | |
7 | postgresql = { | |
8 | enable = lib.mkOption { | |
9 | default = cfg.enable; | |
10 | example = true; | |
11 | description = "Whether to enable postgresql database"; | |
12 | type = lib.types.bool; | |
13 | }; | |
14 | }; | |
15 | ||
16 | mariadb = { | |
17 | enable = lib.mkOption { | |
18 | default = cfg.enable; | |
19 | example = true; | |
20 | description = "Whether to enable mariadb database"; | |
21 | type = lib.types.bool; | |
22 | }; | |
23 | }; | |
24 | ||
25 | redis = { | |
26 | enable = lib.mkOption { | |
27 | default = cfg.enable; | |
28 | example = true; | |
29 | description = "Whether to enable redis database"; | |
30 | type = lib.types.bool; | |
31 | }; | |
32 | }; | |
33 | }; | |
34 | ||
35 | config = lib.mkIf cfg.enable { | |
36 | nixpkgs.config.packageOverrides = oldpkgs: rec { | |
37 | postgresql = postgresql111; | |
38 | postgresql111 = oldpkgs.postgresql100.overrideAttrs(old: rec { | |
39 | passthru = old.passthru // { psqlSchema = "11.0"; }; | |
40 | name = "postgresql-11.1"; | |
41 | src = pkgs.fetchurl { | |
42 | url = "mirror://postgresql/source/v11.1/${name}.tar.bz2"; | |
43 | sha256 = "026v0sicsh7avzi45waf8shcbhivyxmi7qgn9fd1x0vl520mx0ch"; | |
44 | }; | |
f3d9c61e IB |
45 | configureFlags = old.configureFlags ++ [ "--with-pam" ]; |
46 | buildInputs = (old.buildInputs or []) ++ [ pkgs.pam ]; | |
47 | patches = old.patches ++ [ | |
48 | ./postgresql_run_socket_path.patch | |
49 | ]; | |
4d4f13f4 IB |
50 | }); |
51 | mariadb = mariadbPAM; | |
52 | mariadbPAM = oldpkgs.mariadb.overrideAttrs(old: rec { | |
53 | cmakeFlags = old.cmakeFlags ++ [ "-DWITH_AUTHENTICATION_PAM=ON" ]; | |
54 | buildInputs = old.buildInputs ++ [ pkgs.pam ]; | |
55 | }); | |
56 | }; | |
57 | ||
58 | networking.firewall.allowedTCPPorts = [ 3306 5432 ]; | |
59 | ||
7ebcaad5 IB |
60 | # for adminer, ssl is implemented with mysqli only, which is |
61 | # currently disabled because it’s not compatible with pam. | |
62 | # Thus we need to generate two users for each 'remote': one remote | |
63 | # with SSL, and one localhost without SSL. | |
64 | # User identified by LDAP: | |
65 | # CREATE USER foo@% IDENTIFIED VIA pam USING 'mysql' REQUIRE SSL; | |
66 | # CREATE USER foo@localhost IDENTIFIED VIA pam USING 'mysql'; | |
4d4f13f4 IB |
67 | services.mysql = rec { |
68 | enable = cfg.mariadb.enable; | |
69 | package = pkgs.mariadb; | |
7ebcaad5 IB |
70 | extraOptions = '' |
71 | ssl_ca = ${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt | |
72 | ssl_key = /var/lib/acme/mysql/key.pem | |
73 | ssl_cert = /var/lib/acme/mysql/fullchain.pem | |
74 | ''; | |
4d4f13f4 IB |
75 | }; |
76 | ||
f3d9c61e IB |
77 | security.acme.certs."postgresql" = config.services.myCertificates.certConfig // { |
78 | user = "postgres"; | |
79 | group = "postgres"; | |
80 | plugins = [ "fullchain.pem" "key.pem" "account_key.json" ]; | |
81 | domain = "db-1.immae.eu"; | |
82 | postRun = '' | |
83 | systemctl reload postgresql.service | |
84 | ''; | |
85 | }; | |
86 | ||
7ebcaad5 IB |
87 | security.acme.certs."mysql" = config.services.myCertificates.certConfig // { |
88 | user = "mysql"; | |
89 | group = "mysql"; | |
90 | plugins = [ "fullchain.pem" "key.pem" "account_key.json" ]; | |
91 | domain = "db-1.immae.eu"; | |
92 | postRun = '' | |
93 | systemctl restart mysql.service | |
94 | ''; | |
95 | }; | |
96 | ||
f3d9c61e | 97 | system.activationScripts.postgresql = '' |
6f4574e7 | 98 | install -m 0755 -o postgres -g postgres -d ${myconfig.env.databases.postgresql.socket} |
f3d9c61e IB |
99 | ''; |
100 | ||
4d4f13f4 IB |
101 | services.postgresql = rec { |
102 | enable = cfg.postgresql.enable; | |
103 | package = pkgs.postgresql; | |
104 | enableTCPIP = true; | |
105 | extraConfig = '' | |
106 | max_connections = 100 | |
107 | wal_level = logical | |
609c2961 IB |
108 | shared_buffers = 512MB |
109 | work_mem = 10MB | |
4d4f13f4 IB |
110 | max_wal_size = 1GB |
111 | min_wal_size = 80MB | |
112 | log_timezone = 'Europe/Paris' | |
113 | datestyle = 'iso, mdy' | |
114 | timezone = 'Europe/Paris' | |
115 | lc_messages = 'en_US.UTF-8' | |
116 | lc_monetary = 'en_US.UTF-8' | |
117 | lc_numeric = 'en_US.UTF-8' | |
118 | lc_time = 'en_US.UTF-8' | |
119 | default_text_search_config = 'pg_catalog.english' | |
f3d9c61e IB |
120 | ssl = on |
121 | ssl_cert_file = '/var/lib/acme/postgresql/fullchain.pem' | |
122 | ssl_key_file = '/var/lib/acme/postgresql/key.pem' | |
4d4f13f4 IB |
123 | ''; |
124 | authentication = '' | |
125 | local all postgres ident | |
126 | local all all md5 | |
b9f79e95 IB |
127 | hostssl all all 188.165.209.148/32 md5 |
128 | hostssl all all 178.33.252.96/32 md5 | |
129 | hostssl all all all pam | |
f3d9c61e IB |
130 | hostssl replication backup-1 2001:41d0:302:1100::9:e5a9/128 pam pamservice=postgresql_replication |
131 | hostssl replication backup-1 54.37.151.137/32 pam pamservice=postgresql_replication | |
4d4f13f4 IB |
132 | ''; |
133 | }; | |
134 | ||
135 | security.pam.services = let | |
bbba84f5 | 136 | pam_ldap = "${pkgs.pam_ldap}/lib/security/pam_ldap.so"; |
7ebcaad5 IB |
137 | pam_ldap_mysql = with myconfig.env.databases.mysql.pam; |
138 | pkgs.writeText "mysql.conf" '' | |
bbba84f5 IB |
139 | host ${myconfig.env.ldap.host} |
140 | base ${myconfig.env.ldap.base} | |
7ebcaad5 IB |
141 | binddn ${dn} |
142 | bindpw ${password} | |
143 | pam_filter ${filter} | |
144 | ssl start_tls | |
145 | ''; | |
146 | pam_ldap_postgresql = with myconfig.env.databases.postgresql.pam; | |
147 | pkgs.writeText "postgresql.conf" '' | |
148 | host ${myconfig.env.ldap.host} | |
149 | base ${myconfig.env.ldap.base} | |
150 | binddn ${dn} | |
151 | bindpw ${password} | |
152 | pam_filter ${filter} | |
bbba84f5 | 153 | ssl start_tls |
4d4f13f4 | 154 | ''; |
9d90e7e2 | 155 | pam_ldap_postgresql_replication = pkgs.writeText "postgresql.conf" '' |
bbba84f5 IB |
156 | host ${myconfig.env.ldap.host} |
157 | base ${myconfig.env.ldap.base} | |
158 | binddn ${myconfig.env.ldap.host_dn} | |
9d90e7e2 | 159 | bindpw ${myconfig.env.ldap.password} |
f3d9c61e | 160 | pam_login_attribute cn |
7ebcaad5 | 161 | ssl start_tls |
f3d9c61e | 162 | ''; |
4d4f13f4 IB |
163 | in [ |
164 | { | |
165 | name = "mysql"; | |
166 | text = '' | |
167 | # https://mariadb.com/kb/en/mariadb/pam-authentication-plugin/ | |
bbba84f5 IB |
168 | auth required ${pam_ldap} config=${pam_ldap_mysql} |
169 | account required ${pam_ldap} config=${pam_ldap_mysql} | |
4d4f13f4 IB |
170 | ''; |
171 | } | |
f3d9c61e IB |
172 | { |
173 | name = "postgresql"; | |
174 | text = '' | |
7ebcaad5 IB |
175 | auth required ${pam_ldap} config=${pam_ldap_postgresql} |
176 | account required ${pam_ldap} config=${pam_ldap_postgresql} | |
f3d9c61e IB |
177 | ''; |
178 | } | |
179 | { | |
180 | name = "postgresql_replication"; | |
181 | text = '' | |
bbba84f5 IB |
182 | auth required ${pam_ldap} config=${pam_ldap_postgresql_replication} |
183 | account required ${pam_ldap} config=${pam_ldap_postgresql_replication} | |
f3d9c61e IB |
184 | ''; |
185 | } | |
4d4f13f4 IB |
186 | ]; |
187 | ||
52c3e9e7 IB |
188 | ids.uids.redis = myconfig.env.users.redis.uid; |
189 | ids.gids.redis = myconfig.env.users.redis.gid; | |
190 | users.users.redis.uid = config.ids.uids.redis; | |
191 | users.groups.redis.gid = config.ids.gids.redis; | |
4d4f13f4 IB |
192 | services.redis = rec { |
193 | enable = config.services.myDatabases.redis.enable; | |
194 | bind = "127.0.0.1"; | |
b0781dbc | 195 | unixSocket = myconfig.env.databases.redis.socket; |
4d4f13f4 IB |
196 | extraConfig = '' |
197 | unixsocketperm 777 | |
198 | maxclients 1024 | |
199 | ''; | |
200 | }; | |
42429ef0 | 201 | system.activationScripts.redis = '' |
b0781dbc IB |
202 | mkdir -p $(dirname ${myconfig.env.databases.redis.socket}) |
203 | chown redis $(dirname ${myconfig.env.databases.redis.socket}) | |
42429ef0 | 204 | ''; |
4d4f13f4 IB |
205 | }; |
206 | } |