blob: cedb2223c26049507a1884ce7d38a094074630bf [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"
Skyler Grey09c5cda2023-10-09 07:10:10 +000017 "nextcloud"
TheCodedProfb6184602023-06-13 17:04:59 -040018 ];
19
Skyler Greya78aa672023-05-20 13:48:18 +020020 ensureUsers = [
21 {
22 name = "clicks_grafana";
23 ensurePermissions = {
24 "ALL TABLES IN SCHEMA public" = "SELECT";
25 "SCHEMA public" = "USAGE";
26 };
27 }
28 {
Skyler Grey8e32c832023-05-20 22:54:30 +020029 name = "synapse";
Skyler Greya78aa672023-05-20 13:48:18 +020030 ensurePermissions = {
Skyler Grey8e32c832023-05-20 22:54:30 +020031 "DATABASE synapse" = "ALL PRIVILEGES";
Skyler Greya78aa672023-05-20 13:48:18 +020032 };
33 }
TheCodedProfb6184602023-06-13 17:04:59 -040034 {
Skyler Grey0e05d262023-10-09 07:04:36 +000035 name = "keycloak";
36 ensurePermissions = {
37 "DATABASE keycloak" = "ALL PRIVILEGES";
38 };
39 }
40 {
TheCodedProfb6184602023-06-13 17:04:59 -040041 name = "vaultwarden";
42 ensurePermissions = {
43 "DATABASE vaultwarden" = "ALL PRIVILEGES";
44 };
45 }
Skyler Grey9fe61282023-08-20 21:52:48 +000046 {
47 name = "privatebin";
48 ensurePermissions = {
49 "DATABASE privatebin" = "ALL PRIVILEGES";
50 };
51 }
Skyler Grey09c5cda2023-10-09 07:10:10 +000052 {
53 name = "nextcloud";
54 ensurePermissions = {
55 "DATABASE nextcloud" = "ALL PRIVILEGES";
56 };
57 }
Skyler Greya78aa672023-05-20 13:48:18 +020058 ] ++ (map
59 (name: (
60 {
61 inherit name;
62 ensurePermissions = { "ALL TABLES IN SCHEMA public" = "ALL PRIVILEGES"; };
63 }
64 )) [ "minion" "coded" "pinea" ]);
65
Skyler Greya78aa672023-05-20 13:48:18 +020066 };
67
Skyler Grey8e32c832023-05-20 22:54:30 +020068 systemd.services.postgresql.postStart = lib.mkMerge [
69 (
70 let
71 database = "synapse";
72 cfg = config.services.postgresql;
73 in
74 lib.mkBefore (
75 ''
76 PSQL="psql --port=${toString cfg.port}"
77
78 while ! $PSQL -d postgres -c "" 2> /dev/null; do
79 if ! kill -0 "$MAINPID"; then exit 1; fi
80 sleep 0.1
81 done
82
83 $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"'
84 ''
85 ) # synapse needs C collation, so we can't use ensureDatabases for it
86 )
87 (lib.mkAfter (lib.pipe [
88 { user = "clicks_grafana"; passwordFile = config.sops.secrets.clicks_grafana_db_password.path; }
Skyler Grey0e05d262023-10-09 07:04:36 +000089 { user = "keycloak"; passwordFile = config.sops.secrets.clicks_keycloak_db_password.path; }
TheCodedProfb6184602023-06-13 17:04:59 -040090 { user = "vaultwarden"; passwordFile = config.sops.secrets.clicks_bitwarden_db_password.path; }
Skyler Grey9fe61282023-08-20 21:52:48 +000091 { user = "privatebin"; passwordFile = config.sops.secrets.clicks_privatebin_db_password.path; }
Skyler Grey09c5cda2023-10-09 07:10:10 +000092 { user = "nextcloud"; passwordFile = config.sops.secrets.clicks_nextcloud_db_password.path; }
Skyler Grey8e32c832023-05-20 22:54:30 +020093 ] [
94 (map (userData: ''
95 $PSQL -tAc "ALTER USER ${userData.user} PASSWORD '$(cat ${userData.passwordFile})';"
96 ''))
97 (lib.concatStringsSep "\n")
98 ]))
99 ];
Skyler Greya78aa672023-05-20 13:48:18 +0200100
101 sops.secrets = lib.pipe [
102 "clicks_grafana_db_password"
Skyler Grey0e05d262023-10-09 07:04:36 +0000103 "clicks_keycloak_db_password"
TheCodedProfb6184602023-06-13 17:04:59 -0400104 "clicks_bitwarden_db_password"
Skyler Grey9fe61282023-08-20 21:52:48 +0000105 "clicks_privatebin_db_password"
Skyler Grey09c5cda2023-10-09 07:10:10 +0000106 "clicks_nextcloud_db_password"
Skyler Greya78aa672023-05-20 13:48:18 +0200107 ] [
108 (map (name: {
109 inherit name;
110 value = {
111 mode = "0400";
112 owner = config.services.postgresql.superUser;
113 group = config.users.users.${config.services.postgresql.superUser}.group;
114 sopsFile = ../secrets/postgres.json;
115 format = "json";
116 };
117 }))
118 builtins.listToAttrs
119 ];
120}