1 { lib, pkgs, config, myconfig, mylibs, ... }:
3 cfg = config.services.myDatabases;
5 options.services.myDatabases = {
7 enable = lib.mkOption {
10 description = "Whether to enable postgresql database";
11 type = lib.types.bool;
16 config = lib.mkIf cfg.enable {
17 nixpkgs.overlays = [ (self: super: rec {
18 postgresql = self.postgresql_11_custom;
21 networking.firewall.allowedTCPPorts = [ 5432 ];
23 security.acme.certs."postgresql" = config.services.myCertificates.certConfig // {
26 plugins = [ "fullchain.pem" "key.pem" "account_key.json" ];
27 domain = "db-1.immae.eu";
29 systemctl reload postgresql.service
33 system.activationScripts.postgresql = ''
34 install -m 0755 -o postgres -g postgres -d ${myconfig.env.databases.postgresql.socket}
37 systemd.services.postgresql.serviceConfig.SupplementaryGroups = "keys";
38 services.postgresql = rec {
39 enable = cfg.postgresql.enable;
40 package = pkgs.postgresql;
45 shared_buffers = 512MB
49 log_timezone = 'Europe/Paris'
50 datestyle = 'iso, mdy'
51 timezone = 'Europe/Paris'
52 lc_messages = 'en_US.UTF-8'
53 lc_monetary = 'en_US.UTF-8'
54 lc_numeric = 'en_US.UTF-8'
55 lc_time = 'en_US.UTF-8'
56 default_text_search_config = 'pg_catalog.english'
58 ssl_cert_file = '/var/lib/acme/postgresql/fullchain.pem'
59 ssl_key_file = '/var/lib/acme/postgresql/key.pem'
62 local all postgres ident
64 hostssl all all 188.165.209.148/32 md5
65 hostssl all all 178.33.252.96/32 md5
66 hostssl all all all pam
67 hostssl replication backup-1 2001:41d0:302:1100::9:e5a9/128 pam pamservice=postgresql_replication
68 hostssl replication backup-1 54.37.151.137/32 pam pamservice=postgresql_replication
74 dest = "postgresql/pam";
78 text = with myconfig.env.databases.postgresql.pam; ''
79 host ${myconfig.env.ldap.host}
80 base ${myconfig.env.ldap.base}
88 dest = "postgresql/pam_replication";
93 host ${myconfig.env.ldap.host}
94 base ${myconfig.env.ldap.base}
95 binddn ${myconfig.env.ldap.host_dn}
96 bindpw ${myconfig.env.ldap.password}
97 pam_login_attribute cn
103 security.pam.services = let
104 pam_ldap = "${pkgs.pam_ldap}/lib/security/pam_ldap.so";
109 auth required ${pam_ldap} config=/var/secrets/postgresql/pam
110 account required ${pam_ldap} config=/var/secrets/postgresql/pam
114 name = "postgresql_replication";
116 auth required ${pam_ldap} config=/var/secrets/postgresql/pam_replication
117 account required ${pam_ldap} config=/var/secrets/postgresql/pam_replication