]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - nixops/modules/databases/postgresql.nix
Upgrade to latest nixpkgs
[perso/Immae/Config/Nix.git] / nixops / modules / databases / postgresql.nix
1 { lib, pkgs, config, myconfig, mylibs, ... }:
2 let
3 cfg = config.services.myDatabases;
4 in {
5 options.services.myDatabases = {
6 postgresql = {
7 enable = lib.mkOption {
8 default = cfg.enable;
9 example = true;
10 description = "Whether to enable postgresql database";
11 type = lib.types.bool;
12 };
13 };
14 };
15
16 config = lib.mkIf cfg.enable {
17 nixpkgs.overlays = [ (self: super: rec {
18 postgresql = postgresql_11;
19 postgresql_11 = super.postgresql_11.overrideAttrs(old: rec {
20 passthru = old.passthru // { psqlSchema = "11.0"; };
21 configureFlags = old.configureFlags ++ [ "--with-pam" ];
22 buildInputs = (old.buildInputs or []) ++ [ self.pam ];
23 patches = old.patches ++ [
24 ./postgresql_run_socket_path.patch
25 ];
26 });
27 }) ];
28
29 networking.firewall.allowedTCPPorts = [ 5432 ];
30
31 security.acme.certs."postgresql" = config.services.myCertificates.certConfig // {
32 user = "postgres";
33 group = "postgres";
34 plugins = [ "fullchain.pem" "key.pem" "account_key.json" ];
35 domain = "db-1.immae.eu";
36 postRun = ''
37 systemctl reload postgresql.service
38 '';
39 };
40
41 system.activationScripts.postgresql = ''
42 install -m 0755 -o postgres -g postgres -d ${myconfig.env.databases.postgresql.socket}
43 '';
44
45 services.postgresql = rec {
46 enable = cfg.postgresql.enable;
47 package = pkgs.postgresql;
48 enableTCPIP = true;
49 extraConfig = ''
50 max_connections = 100
51 wal_level = logical
52 shared_buffers = 512MB
53 work_mem = 10MB
54 max_wal_size = 1GB
55 min_wal_size = 80MB
56 log_timezone = 'Europe/Paris'
57 datestyle = 'iso, mdy'
58 timezone = 'Europe/Paris'
59 lc_messages = 'en_US.UTF-8'
60 lc_monetary = 'en_US.UTF-8'
61 lc_numeric = 'en_US.UTF-8'
62 lc_time = 'en_US.UTF-8'
63 default_text_search_config = 'pg_catalog.english'
64 ssl = on
65 ssl_cert_file = '/var/lib/acme/postgresql/fullchain.pem'
66 ssl_key_file = '/var/lib/acme/postgresql/key.pem'
67 '';
68 authentication = ''
69 local all postgres ident
70 local all all md5
71 hostssl all all 188.165.209.148/32 md5
72 hostssl all all 178.33.252.96/32 md5
73 hostssl all all all pam
74 hostssl replication backup-1 2001:41d0:302:1100::9:e5a9/128 pam pamservice=postgresql_replication
75 hostssl replication backup-1 54.37.151.137/32 pam pamservice=postgresql_replication
76 '';
77 };
78
79 security.pam.services = let
80 pam_ldap = "${pkgs.pam_ldap}/lib/security/pam_ldap.so";
81 pam_ldap_postgresql = with myconfig.env.databases.postgresql.pam;
82 pkgs.writeText "postgresql.conf" ''
83 host ${myconfig.env.ldap.host}
84 base ${myconfig.env.ldap.base}
85 binddn ${dn}
86 bindpw ${password}
87 pam_filter ${filter}
88 ssl start_tls
89 '';
90 pam_ldap_postgresql_replication = pkgs.writeText "postgresql.conf" ''
91 host ${myconfig.env.ldap.host}
92 base ${myconfig.env.ldap.base}
93 binddn ${myconfig.env.ldap.host_dn}
94 bindpw ${myconfig.env.ldap.password}
95 pam_login_attribute cn
96 ssl start_tls
97 '';
98 in [
99 {
100 name = "postgresql";
101 text = ''
102 auth required ${pam_ldap} config=${pam_ldap_postgresql}
103 account required ${pam_ldap} config=${pam_ldap_postgresql}
104 '';
105 }
106 {
107 name = "postgresql_replication";
108 text = ''
109 auth required ${pam_ldap} config=${pam_ldap_postgresql_replication}
110 account required ${pam_ldap} config=${pam_ldap_postgresql_replication}
111 '';
112 }
113 ];
114 };
115 }
116