1 { lib, pkgs, config, myconfig, ... }:
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 systemd.services.postgresql.serviceConfig.SupplementaryGroups = "keys";
34 systemd.services.postgresql.serviceConfig.RuntimeDirectory = "postgresql";
35 services.postgresql = rec {
36 enable = cfg.postgresql.enable;
37 package = pkgs.postgresql;
42 shared_buffers = 512MB
46 log_timezone = 'Europe/Paris'
47 datestyle = 'iso, mdy'
48 timezone = 'Europe/Paris'
49 lc_messages = 'en_US.UTF-8'
50 lc_monetary = 'en_US.UTF-8'
51 lc_numeric = 'en_US.UTF-8'
52 lc_time = 'en_US.UTF-8'
53 default_text_search_config = 'pg_catalog.english'
55 ssl_cert_file = '/var/lib/acme/postgresql/fullchain.pem'
56 ssl_key_file = '/var/lib/acme/postgresql/key.pem'
59 local all postgres ident
61 hostssl all all 188.165.209.148/32 md5
62 hostssl all all 178.33.252.96/32 md5
63 hostssl all all all pam
64 hostssl replication backup-1 2001:41d0:302:1100::9:e5a9/128 pam pamservice=postgresql_replication
65 hostssl replication backup-1 54.37.151.137/32 pam pamservice=postgresql_replication
71 dest = "postgresql/pam";
75 text = with myconfig.env.databases.postgresql.pam; ''
76 host ${myconfig.env.ldap.host}
77 base ${myconfig.env.ldap.base}
85 dest = "postgresql/pam_replication";
90 host ${myconfig.env.ldap.host}
91 base ${myconfig.env.ldap.base}
92 binddn ${myconfig.env.ldap.host_dn}
93 bindpw ${myconfig.env.ldap.password}
94 pam_login_attribute cn
100 security.pam.services = let
101 pam_ldap = "${pkgs.pam_ldap}/lib/security/pam_ldap.so";
106 auth required ${pam_ldap} config=/var/secrets/postgresql/pam
107 account required ${pam_ldap} config=/var/secrets/postgresql/pam
111 name = "postgresql_replication";
113 auth required ${pam_ldap} config=/var/secrets/postgresql/pam_replication
114 account required ${pam_ldap} config=/var/secrets/postgresql/pam_replication