]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - nixops/modules/databases/default.nix
Migrate discourse to postgresql
[perso/Immae/Config/Nix.git] / nixops / modules / databases / default.nix
1 { lib, pkgs, config, myconfig, mylibs, ... }:
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 };
45 configureFlags = old.configureFlags ++ [ "--with-pam" ];
46 buildInputs = (old.buildInputs or []) ++ [ pkgs.pam ];
47 patches = old.patches ++ [
48 ./postgresql_run_socket_path.patch
49 ];
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
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';
67 services.mysql = rec {
68 enable = cfg.mariadb.enable;
69 package = pkgs.mariadb;
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 '';
75 };
76
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
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
97 system.activationScripts.postgresql = ''
98 install -m 0755 -o postgres -g postgres -d ${myconfig.env.databases.postgresql.socket}
99 '';
100
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 = 512MB
109 work_mem = 10MB
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'
120 ssl = on
121 ssl_cert_file = '/var/lib/acme/postgresql/fullchain.pem'
122 ssl_key_file = '/var/lib/acme/postgresql/key.pem'
123 '';
124 authentication = ''
125 local all postgres ident
126 local all all md5
127 hostssl discourse discourse all md5
128 hostssl all all all pam
129 hostssl replication backup-1 2001:41d0:302:1100::9:e5a9/128 pam pamservice=postgresql_replication
130 hostssl replication backup-1 54.37.151.137/32 pam pamservice=postgresql_replication
131 '';
132 };
133
134 security.pam.services = let
135 pam_ldap = "${pkgs.pam_ldap}/lib/security/pam_ldap.so";
136 pam_ldap_mysql = with myconfig.env.databases.mysql.pam;
137 pkgs.writeText "mysql.conf" ''
138 host ${myconfig.env.ldap.host}
139 base ${myconfig.env.ldap.base}
140 binddn ${dn}
141 bindpw ${password}
142 pam_filter ${filter}
143 ssl start_tls
144 '';
145 pam_ldap_postgresql = with myconfig.env.databases.postgresql.pam;
146 pkgs.writeText "postgresql.conf" ''
147 host ${myconfig.env.ldap.host}
148 base ${myconfig.env.ldap.base}
149 binddn ${dn}
150 bindpw ${password}
151 pam_filter ${filter}
152 ssl start_tls
153 '';
154 pam_ldap_postgresql_replication = pkgs.writeText "postgresql.conf" ''
155 host ${myconfig.env.ldap.host}
156 base ${myconfig.env.ldap.base}
157 binddn ${myconfig.env.ldap.host_dn}
158 bindpw ${myconfig.env.ldap.password}
159 pam_login_attribute cn
160 ssl start_tls
161 '';
162 in [
163 {
164 name = "mysql";
165 text = ''
166 # https://mariadb.com/kb/en/mariadb/pam-authentication-plugin/
167 auth required ${pam_ldap} config=${pam_ldap_mysql}
168 account required ${pam_ldap} config=${pam_ldap_mysql}
169 '';
170 }
171 {
172 name = "postgresql";
173 text = ''
174 auth required ${pam_ldap} config=${pam_ldap_postgresql}
175 account required ${pam_ldap} config=${pam_ldap_postgresql}
176 '';
177 }
178 {
179 name = "postgresql_replication";
180 text = ''
181 auth required ${pam_ldap} config=${pam_ldap_postgresql_replication}
182 account required ${pam_ldap} config=${pam_ldap_postgresql_replication}
183 '';
184 }
185 ];
186
187 services.redis = rec {
188 enable = config.services.myDatabases.redis.enable;
189 bind = "127.0.0.1";
190 unixSocket = myconfig.env.databases.redis.socket;
191 extraConfig = ''
192 unixsocketperm 777
193 maxclients 1024
194 '';
195 };
196 system.activationScripts.redis = ''
197 mkdir -p $(dirname ${myconfig.env.databases.redis.socket})
198 chown redis $(dirname ${myconfig.env.databases.redis.socket})
199 '';
200 };
201 }