]>
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 | |
108 | shared_buffers = 128MB | |
109 | max_wal_size = 1GB | |
110 | min_wal_size = 80MB | |
111 | log_timezone = 'Europe/Paris' | |
112 | datestyle = 'iso, mdy' | |
113 | timezone = 'Europe/Paris' | |
114 | lc_messages = 'en_US.UTF-8' | |
115 | lc_monetary = 'en_US.UTF-8' | |
116 | lc_numeric = 'en_US.UTF-8' | |
117 | lc_time = 'en_US.UTF-8' | |
118 | default_text_search_config = 'pg_catalog.english' | |
f3d9c61e IB |
119 | ssl = on |
120 | ssl_cert_file = '/var/lib/acme/postgresql/fullchain.pem' | |
121 | ssl_key_file = '/var/lib/acme/postgresql/key.pem' | |
4d4f13f4 IB |
122 | ''; |
123 | authentication = '' | |
124 | local all postgres ident | |
125 | local all all md5 | |
f3d9c61e IB |
126 | hostssl all all all pam |
127 | hostssl replication backup-1 2001:41d0:302:1100::9:e5a9/128 pam pamservice=postgresql_replication | |
128 | hostssl replication backup-1 54.37.151.137/32 pam pamservice=postgresql_replication | |
4d4f13f4 IB |
129 | ''; |
130 | }; | |
131 | ||
132 | security.pam.services = let | |
bbba84f5 | 133 | pam_ldap = "${pkgs.pam_ldap}/lib/security/pam_ldap.so"; |
7ebcaad5 IB |
134 | pam_ldap_mysql = with myconfig.env.databases.mysql.pam; |
135 | pkgs.writeText "mysql.conf" '' | |
bbba84f5 IB |
136 | host ${myconfig.env.ldap.host} |
137 | base ${myconfig.env.ldap.base} | |
7ebcaad5 IB |
138 | binddn ${dn} |
139 | bindpw ${password} | |
140 | pam_filter ${filter} | |
141 | ssl start_tls | |
142 | ''; | |
143 | pam_ldap_postgresql = with myconfig.env.databases.postgresql.pam; | |
144 | pkgs.writeText "postgresql.conf" '' | |
145 | host ${myconfig.env.ldap.host} | |
146 | base ${myconfig.env.ldap.base} | |
147 | binddn ${dn} | |
148 | bindpw ${password} | |
149 | pam_filter ${filter} | |
bbba84f5 | 150 | ssl start_tls |
4d4f13f4 | 151 | ''; |
9d90e7e2 | 152 | pam_ldap_postgresql_replication = pkgs.writeText "postgresql.conf" '' |
bbba84f5 IB |
153 | host ${myconfig.env.ldap.host} |
154 | base ${myconfig.env.ldap.base} | |
155 | binddn ${myconfig.env.ldap.host_dn} | |
9d90e7e2 | 156 | bindpw ${myconfig.env.ldap.password} |
f3d9c61e | 157 | pam_login_attribute cn |
7ebcaad5 | 158 | ssl start_tls |
f3d9c61e | 159 | ''; |
4d4f13f4 IB |
160 | in [ |
161 | { | |
162 | name = "mysql"; | |
163 | text = '' | |
164 | # https://mariadb.com/kb/en/mariadb/pam-authentication-plugin/ | |
bbba84f5 IB |
165 | auth required ${pam_ldap} config=${pam_ldap_mysql} |
166 | account required ${pam_ldap} config=${pam_ldap_mysql} | |
4d4f13f4 IB |
167 | ''; |
168 | } | |
f3d9c61e IB |
169 | { |
170 | name = "postgresql"; | |
171 | text = '' | |
7ebcaad5 IB |
172 | auth required ${pam_ldap} config=${pam_ldap_postgresql} |
173 | account required ${pam_ldap} config=${pam_ldap_postgresql} | |
f3d9c61e IB |
174 | ''; |
175 | } | |
176 | { | |
177 | name = "postgresql_replication"; | |
178 | text = '' | |
bbba84f5 IB |
179 | auth required ${pam_ldap} config=${pam_ldap_postgresql_replication} |
180 | account required ${pam_ldap} config=${pam_ldap_postgresql_replication} | |
f3d9c61e IB |
181 | ''; |
182 | } | |
4d4f13f4 IB |
183 | ]; |
184 | ||
4d4f13f4 IB |
185 | services.redis = rec { |
186 | enable = config.services.myDatabases.redis.enable; | |
187 | bind = "127.0.0.1"; | |
b0781dbc | 188 | unixSocket = myconfig.env.databases.redis.socket; |
4d4f13f4 IB |
189 | extraConfig = '' |
190 | unixsocketperm 777 | |
191 | maxclients 1024 | |
192 | ''; | |
193 | }; | |
42429ef0 | 194 | system.activationScripts.redis = '' |
b0781dbc IB |
195 | mkdir -p $(dirname ${myconfig.env.databases.redis.socket}) |
196 | chown redis $(dirname ${myconfig.env.databases.redis.socket}) | |
42429ef0 | 197 | ''; |
4d4f13f4 IB |
198 | }; |
199 | } |