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 = 128MB
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'
120 ssl_cert_file = '/var/lib/acme/postgresql/fullchain.pem'
121 ssl_key_file = '/var/lib/acme/postgresql/key.pem'
124 local all postgres ident
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
132 security.pam.services = let
133 pam_ldap = "${pkgs.pam_ldap}/lib/security/pam_ldap.so";
134 pam_ldap_mysql = with myconfig.env.databases.mysql.pam;
135 pkgs.writeText "mysql.conf" ''
136 host ${myconfig.env.ldap.host}
137 base ${myconfig.env.ldap.base}
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}
152 pam_ldap_postgresql_replication = pkgs.writeText "postgresql.conf" ''
153 host ${myconfig.env.ldap.host}
154 base ${myconfig.env.ldap.base}
155 binddn ${myconfig.env.ldap.host_dn}
156 bindpw ${myconfig.env.ldap.password}
157 pam_login_attribute cn
164 # https://mariadb.com/kb/en/mariadb/pam-authentication-plugin/
165 auth required ${pam_ldap} config=${pam_ldap_mysql}
166 account required ${pam_ldap} config=${pam_ldap_mysql}
172 auth required ${pam_ldap} config=${pam_ldap_postgresql}
173 account required ${pam_ldap} config=${pam_ldap_postgresql}
177 name = "postgresql_replication";
179 auth required ${pam_ldap} config=${pam_ldap_postgresql_replication}
180 account required ${pam_ldap} config=${pam_ldap_postgresql_replication}
185 services.redis = rec {
186 enable = config.services.myDatabases.redis.enable;
188 unixSocket = myconfig.env.databases.redis.socket;
194 system.activationScripts.redis = ''
195 mkdir -p $(dirname ${myconfig.env.databases.redis.socket})
196 chown redis $(dirname ${myconfig.env.databases.redis.socket})