blob: d2844c186a96dc31d06355a0d27111fd4d6efa97 [file] [log] [blame]
Skyler Greya78aa672023-05-20 13:48:18 +02001{ lib, config, pkgs, ... }: {
2 services.postgresql = {
3 enable = true;
4
5 package = pkgs.postgresql;
6 settings = {
7 log_connections = true;
Skyler Greya78aa672023-05-20 13:48:18 +02008 logging_collector = true;
9 log_disconnections = true;
10 log_destination = lib.mkForce "syslog";
11 };
12
TheCodedProfb6184602023-06-13 17:04:59 -040013 ensureDatabases = [
14 "vaultwarden"
Skyler Grey9fe61282023-08-20 21:52:48 +000015 "privatebin"
Skyler Grey0e05d262023-10-09 07:04:36 +000016 "keycloak"
TheCodedProfb6184602023-06-13 17:04:59 -040017 ];
18
Skyler Greya78aa672023-05-20 13:48:18 +020019 ensureUsers = [
20 {
21 name = "clicks_grafana";
22 ensurePermissions = {
23 "ALL TABLES IN SCHEMA public" = "SELECT";
24 "SCHEMA public" = "USAGE";
25 };
26 }
27 {
Skyler Grey8e32c832023-05-20 22:54:30 +020028 name = "synapse";
Skyler Greya78aa672023-05-20 13:48:18 +020029 ensurePermissions = {
Skyler Grey8e32c832023-05-20 22:54:30 +020030 "DATABASE synapse" = "ALL PRIVILEGES";
Skyler Greya78aa672023-05-20 13:48:18 +020031 };
32 }
TheCodedProfb6184602023-06-13 17:04:59 -040033 {
Skyler Grey0e05d262023-10-09 07:04:36 +000034 name = "keycloak";
35 ensurePermissions = {
36 "DATABASE keycloak" = "ALL PRIVILEGES";
37 };
38 }
39 {
TheCodedProfb6184602023-06-13 17:04:59 -040040 name = "vaultwarden";
41 ensurePermissions = {
42 "DATABASE vaultwarden" = "ALL PRIVILEGES";
43 };
44 }
Skyler Grey9fe61282023-08-20 21:52:48 +000045 {
46 name = "privatebin";
47 ensurePermissions = {
48 "DATABASE privatebin" = "ALL PRIVILEGES";
49 };
50 }
Skyler Greya78aa672023-05-20 13:48:18 +020051 ] ++ (map
52 (name: (
53 {
54 inherit name;
55 ensurePermissions = { "ALL TABLES IN SCHEMA public" = "ALL PRIVILEGES"; };
56 }
57 )) [ "minion" "coded" "pinea" ]);
58
Skyler Greya78aa672023-05-20 13:48:18 +020059 };
60
Skyler Grey8e32c832023-05-20 22:54:30 +020061 systemd.services.postgresql.postStart = lib.mkMerge [
62 (
63 let
64 database = "synapse";
65 cfg = config.services.postgresql;
66 in
67 lib.mkBefore (
68 ''
69 PSQL="psql --port=${toString cfg.port}"
70
71 while ! $PSQL -d postgres -c "" 2> /dev/null; do
72 if ! kill -0 "$MAINPID"; then exit 1; fi
73 sleep 0.1
74 done
75
76 $PSQL -tAc "SELECT 1 FROM pg_database WHERE datname = '${database}'" | grep -q 1 || $PSQL -tAc 'CREATE DATABASE "${database}" WITH LC_CTYPE="C" LC_COLLATE="C" TEMPLATE="template0"'
77 ''
78 ) # synapse needs C collation, so we can't use ensureDatabases for it
79 )
80 (lib.mkAfter (lib.pipe [
81 { user = "clicks_grafana"; passwordFile = config.sops.secrets.clicks_grafana_db_password.path; }
Skyler Grey0e05d262023-10-09 07:04:36 +000082 { user = "keycloak"; passwordFile = config.sops.secrets.clicks_keycloak_db_password.path; }
TheCodedProfb6184602023-06-13 17:04:59 -040083 { user = "vaultwarden"; passwordFile = config.sops.secrets.clicks_bitwarden_db_password.path; }
Skyler Grey9fe61282023-08-20 21:52:48 +000084 { user = "privatebin"; passwordFile = config.sops.secrets.clicks_privatebin_db_password.path; }
Skyler Grey8e32c832023-05-20 22:54:30 +020085 ] [
86 (map (userData: ''
87 $PSQL -tAc "ALTER USER ${userData.user} PASSWORD '$(cat ${userData.passwordFile})';"
88 ''))
89 (lib.concatStringsSep "\n")
90 ]))
91 ];
Skyler Greya78aa672023-05-20 13:48:18 +020092
93 sops.secrets = lib.pipe [
94 "clicks_grafana_db_password"
Skyler Grey0e05d262023-10-09 07:04:36 +000095 "clicks_keycloak_db_password"
TheCodedProfb6184602023-06-13 17:04:59 -040096 "clicks_bitwarden_db_password"
Skyler Grey9fe61282023-08-20 21:52:48 +000097 "clicks_privatebin_db_password"
Skyler Greya78aa672023-05-20 13:48:18 +020098 ] [
99 (map (name: {
100 inherit name;
101 value = {
102 mode = "0400";
103 owner = config.services.postgresql.superUser;
104 group = config.users.users.${config.services.postgresql.superUser}.group;
105 sopsFile = ../secrets/postgres.json;
106 format = "json";
107 };
108 }))
109 builtins.listToAttrs
110 ];
111}