1 { lib, pkgs, config, myconfig, mylibs, ... }:
3 cfg = config.services.myDatabases;
5 options.services.myDatabases = {
6 enable = lib.mkEnableOption "my databases service";
8 enable = lib.mkOption {
11 description = "Whether to enable postgresql database";
12 type = lib.types.bool;
17 enable = lib.mkOption {
20 description = "Whether to enable mariadb database";
21 type = lib.types.bool;
26 enable = lib.mkOption {
29 description = "Whether to enable redis database";
30 type = lib.types.bool;
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";
42 url = "mirror://postgresql/source/v11.1/${name}.tar.bz2";
43 sha256 = "026v0sicsh7avzi45waf8shcbhivyxmi7qgn9fd1x0vl520mx0ch";
45 configureFlags = old.configureFlags ++ [ "--with-pam" ];
46 buildInputs = (old.buildInputs or []) ++ [ pkgs.pam ];
47 patches = old.patches ++ [
48 ./postgresql_run_socket_path.patch
52 mariadbPAM = oldpkgs.mariadb.overrideAttrs(old: rec {
53 cmakeFlags = old.cmakeFlags ++ [ "-DWITH_AUTHENTICATION_PAM=ON" ];
54 buildInputs = old.buildInputs ++ [ pkgs.pam ];
58 networking.firewall.allowedTCPPorts = [ 3306 5432 ];
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;
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
77 security.acme.certs."postgresql" = config.services.myCertificates.certConfig // {
80 plugins = [ "fullchain.pem" "key.pem" "account_key.json" ];
81 domain = "db-1.immae.eu";
83 systemctl reload postgresql.service
87 security.acme.certs."mysql" = config.services.myCertificates.certConfig // {
90 plugins = [ "fullchain.pem" "key.pem" "account_key.json" ];
91 domain = "db-1.immae.eu";
93 systemctl restart mysql.service
97 system.activationScripts.postgresql = ''
98 install -m 0755 -o postgres -g postgres -d ${myconfig.env.databases.postgresql.socket}
101 services.postgresql = rec {
102 enable = cfg.postgresql.enable;
103 package = pkgs.postgresql;
106 max_connections = 100
108 shared_buffers = 512MB
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'
121 ssl_cert_file = '/var/lib/acme/postgresql/fullchain.pem'
122 ssl_key_file = '/var/lib/acme/postgresql/key.pem'
125 local all postgres ident
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
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}
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}
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
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}
174 auth required ${pam_ldap} config=${pam_ldap_postgresql}
175 account required ${pam_ldap} config=${pam_ldap_postgresql}
179 name = "postgresql_replication";
181 auth required ${pam_ldap} config=${pam_ldap_postgresql_replication}
182 account required ${pam_ldap} config=${pam_ldap_postgresql_replication}
187 ids.uids.redis = myconfig.env.users.redis.uid;
188 ids.gids.redis = myconfig.env.users.redis.gid;
189 users.users.redis.uid = config.ids.uids.redis;
190 users.groups.redis.gid = config.ids.gids.redis;
191 services.redis = rec {
192 enable = config.services.myDatabases.redis.enable;
194 unixSocket = myconfig.env.databases.redis.socket;
200 system.activationScripts.redis = ''
201 mkdir -p $(dirname ${myconfig.env.databases.redis.socket})
202 chown redis $(dirname ${myconfig.env.databases.redis.socket})