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 services.mysql = rec {
61 enable = cfg.mariadb.enable;
62 package = pkgs.mariadb;
65 security.acme.certs."postgresql" = config.services.myCertificates.certConfig // {
68 plugins = [ "fullchain.pem" "key.pem" "account_key.json" ];
69 domain = "db-1.immae.eu";
71 systemctl reload postgresql.service
75 system.activationScripts.postgresql = ''
76 install -m 0755 -o postgres -g postgres -d ${myconfig.env.databases.postgresql.socket}
79 services.postgresql = rec {
80 enable = cfg.postgresql.enable;
81 package = pkgs.postgresql;
86 shared_buffers = 128MB
89 log_timezone = 'Europe/Paris'
90 datestyle = 'iso, mdy'
91 timezone = 'Europe/Paris'
92 lc_messages = 'en_US.UTF-8'
93 lc_monetary = 'en_US.UTF-8'
94 lc_numeric = 'en_US.UTF-8'
95 lc_time = 'en_US.UTF-8'
96 default_text_search_config = 'pg_catalog.english'
98 ssl_cert_file = '/var/lib/acme/postgresql/fullchain.pem'
99 ssl_key_file = '/var/lib/acme/postgresql/key.pem'
102 local all postgres ident
104 hostssl all all samehost md5
105 hostssl all all 178.33.252.96/32 md5
106 hostssl all all 188.165.209.148/32 md5
107 hostssl all all all pam
108 hostssl replication backup-1 2001:41d0:302:1100::9:e5a9/128 pam pamservice=postgresql_replication
109 hostssl replication backup-1 54.37.151.137/32 pam pamservice=postgresql_replication
113 security.pam.services = let
114 pam_ldap = "${pkgs.pam_ldap}/lib/security/pam_ldap.so";
115 pam_ldap_mysql = pkgs.writeText "mysql.conf" ''
116 host ${myconfig.env.ldap.host}
117 base ${myconfig.env.ldap.base}
118 binddn cn=mysql,cn=pam,ou=services,dc=immae,dc=eu
119 bindpw ${myconfig.env.databases.mysql.pam_password}
121 pam_filter memberOf=cn=users,cn=mysql,cn=pam,ou=services,dc=immae,dc=eu
123 pam_ldap_postgresql_replication = pkgs.writeText "postgresql.conf" ''
124 host ${myconfig.env.ldap.host}
125 base ${myconfig.env.ldap.base}
126 binddn ${myconfig.env.ldap.host_dn}
127 bindpw ${myconfig.env.ldap.password}
129 pam_login_attribute cn
135 # https://mariadb.com/kb/en/mariadb/pam-authentication-plugin/
136 auth required ${pam_ldap} config=${pam_ldap_mysql}
137 account required ${pam_ldap} config=${pam_ldap_mysql}
143 auth required ${pam_ldap} config=${pam_ldap_postgresql_replication}
144 account required ${pam_ldap} config=${pam_ldap_postgresql_replication}
148 name = "postgresql_replication";
150 auth required ${pam_ldap} config=${pam_ldap_postgresql_replication}
151 account required ${pam_ldap} config=${pam_ldap_postgresql_replication}
156 services.redis = rec {
157 enable = config.services.myDatabases.redis.enable;
159 unixSocket = myconfig.env.databases.redis.socket;
165 system.activationScripts.redis = ''
166 mkdir -p $(dirname ${myconfig.env.databases.redis.socket})
167 chown redis $(dirname ${myconfig.env.databases.redis.socket})