diff options
Diffstat (limited to 'nixops/modules/databases/postgresql.nix')
-rw-r--r-- | nixops/modules/databases/postgresql.nix | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/nixops/modules/databases/postgresql.nix b/nixops/modules/databases/postgresql.nix new file mode 100644 index 0000000..2e658f8 --- /dev/null +++ b/nixops/modules/databases/postgresql.nix | |||
@@ -0,0 +1,121 @@ | |||
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.config.packageOverrides = oldpkgs: rec { | ||
18 | postgresql = postgresql111; | ||
19 | postgresql111 = oldpkgs.postgresql100.overrideAttrs(old: rec { | ||
20 | passthru = old.passthru // { psqlSchema = "11.0"; }; | ||
21 | name = "postgresql-11.1"; | ||
22 | src = pkgs.fetchurl { | ||
23 | url = "mirror://postgresql/source/v11.1/${name}.tar.bz2"; | ||
24 | sha256 = "026v0sicsh7avzi45waf8shcbhivyxmi7qgn9fd1x0vl520mx0ch"; | ||
25 | }; | ||
26 | configureFlags = old.configureFlags ++ [ "--with-pam" ]; | ||
27 | buildInputs = (old.buildInputs or []) ++ [ pkgs.pam ]; | ||
28 | patches = old.patches ++ [ | ||
29 | ./postgresql_run_socket_path.patch | ||
30 | ]; | ||
31 | }); | ||
32 | }; | ||
33 | |||
34 | networking.firewall.allowedTCPPorts = [ 5432 ]; | ||
35 | |||
36 | security.acme.certs."postgresql" = config.services.myCertificates.certConfig // { | ||
37 | user = "postgres"; | ||
38 | group = "postgres"; | ||
39 | plugins = [ "fullchain.pem" "key.pem" "account_key.json" ]; | ||
40 | domain = "db-1.immae.eu"; | ||
41 | postRun = '' | ||
42 | systemctl reload postgresql.service | ||
43 | ''; | ||
44 | }; | ||
45 | |||
46 | system.activationScripts.postgresql = '' | ||
47 | install -m 0755 -o postgres -g postgres -d ${myconfig.env.databases.postgresql.socket} | ||
48 | ''; | ||
49 | |||
50 | services.postgresql = rec { | ||
51 | enable = cfg.postgresql.enable; | ||
52 | package = pkgs.postgresql; | ||
53 | enableTCPIP = true; | ||
54 | extraConfig = '' | ||
55 | max_connections = 100 | ||
56 | wal_level = logical | ||
57 | shared_buffers = 512MB | ||
58 | work_mem = 10MB | ||
59 | max_wal_size = 1GB | ||
60 | min_wal_size = 80MB | ||
61 | log_timezone = 'Europe/Paris' | ||
62 | datestyle = 'iso, mdy' | ||
63 | timezone = 'Europe/Paris' | ||
64 | lc_messages = 'en_US.UTF-8' | ||
65 | lc_monetary = 'en_US.UTF-8' | ||
66 | lc_numeric = 'en_US.UTF-8' | ||
67 | lc_time = 'en_US.UTF-8' | ||
68 | default_text_search_config = 'pg_catalog.english' | ||
69 | ssl = on | ||
70 | ssl_cert_file = '/var/lib/acme/postgresql/fullchain.pem' | ||
71 | ssl_key_file = '/var/lib/acme/postgresql/key.pem' | ||
72 | ''; | ||
73 | authentication = '' | ||
74 | local all postgres ident | ||
75 | local all all md5 | ||
76 | hostssl all all 188.165.209.148/32 md5 | ||
77 | hostssl all all 178.33.252.96/32 md5 | ||
78 | hostssl all all all pam | ||
79 | hostssl replication backup-1 2001:41d0:302:1100::9:e5a9/128 pam pamservice=postgresql_replication | ||
80 | hostssl replication backup-1 54.37.151.137/32 pam pamservice=postgresql_replication | ||
81 | ''; | ||
82 | }; | ||
83 | |||
84 | security.pam.services = let | ||
85 | pam_ldap = "${pkgs.pam_ldap}/lib/security/pam_ldap.so"; | ||
86 | pam_ldap_postgresql = with myconfig.env.databases.postgresql.pam; | ||
87 | pkgs.writeText "postgresql.conf" '' | ||
88 | host ${myconfig.env.ldap.host} | ||
89 | base ${myconfig.env.ldap.base} | ||
90 | binddn ${dn} | ||
91 | bindpw ${password} | ||
92 | pam_filter ${filter} | ||
93 | ssl start_tls | ||
94 | ''; | ||
95 | pam_ldap_postgresql_replication = pkgs.writeText "postgresql.conf" '' | ||
96 | host ${myconfig.env.ldap.host} | ||
97 | base ${myconfig.env.ldap.base} | ||
98 | binddn ${myconfig.env.ldap.host_dn} | ||
99 | bindpw ${myconfig.env.ldap.password} | ||
100 | pam_login_attribute cn | ||
101 | ssl start_tls | ||
102 | ''; | ||
103 | in [ | ||
104 | { | ||
105 | name = "postgresql"; | ||
106 | text = '' | ||
107 | auth required ${pam_ldap} config=${pam_ldap_postgresql} | ||
108 | account required ${pam_ldap} config=${pam_ldap_postgresql} | ||
109 | ''; | ||
110 | } | ||
111 | { | ||
112 | name = "postgresql_replication"; | ||
113 | text = '' | ||
114 | auth required ${pam_ldap} config=${pam_ldap_postgresql_replication} | ||
115 | account required ${pam_ldap} config=${pam_ldap_postgresql_replication} | ||
116 | ''; | ||
117 | } | ||
118 | ]; | ||
119 | }; | ||
120 | } | ||
121 | |||