blob: 2ae67bba12d92a6bb9127fce93d0d19ed108bd53 [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 Grey08758a62023-10-09 07:35:09 +000015 "gerrit"
Skyler Grey9fe61282023-08-20 21:52:48 +000016 "privatebin"
Skyler Grey0e05d262023-10-09 07:04:36 +000017 "keycloak"
Skyler Grey09c5cda2023-10-09 07:10:10 +000018 "nextcloud"
TheCodedProfb6184602023-06-13 17:04:59 -040019 ];
20
Skyler Greya78aa672023-05-20 13:48:18 +020021 ensureUsers = [
22 {
23 name = "clicks_grafana";
24 ensurePermissions = {
25 "ALL TABLES IN SCHEMA public" = "SELECT";
26 "SCHEMA public" = "USAGE";
27 };
28 }
29 {
Skyler Grey8e32c832023-05-20 22:54:30 +020030 name = "synapse";
Skyler Greya78aa672023-05-20 13:48:18 +020031 ensurePermissions = {
Skyler Grey8e32c832023-05-20 22:54:30 +020032 "DATABASE synapse" = "ALL PRIVILEGES";
Skyler Greya78aa672023-05-20 13:48:18 +020033 };
34 }
TheCodedProfb6184602023-06-13 17:04:59 -040035 {
Skyler Grey0e05d262023-10-09 07:04:36 +000036 name = "keycloak";
37 ensurePermissions = {
38 "DATABASE keycloak" = "ALL PRIVILEGES";
39 };
40 }
41 {
Skyler Grey08758a62023-10-09 07:35:09 +000042 name = "gerrit";
43 ensurePermissions = {
44 "DATABASE gerrit" = "ALL PRIVILEGES";
45 };
46 }
47 {
TheCodedProfb6184602023-06-13 17:04:59 -040048 name = "vaultwarden";
49 ensurePermissions = {
50 "DATABASE vaultwarden" = "ALL PRIVILEGES";
51 };
52 }
Skyler Grey9fe61282023-08-20 21:52:48 +000053 {
54 name = "privatebin";
55 ensurePermissions = {
56 "DATABASE privatebin" = "ALL PRIVILEGES";
57 };
58 }
Skyler Grey09c5cda2023-10-09 07:10:10 +000059 {
60 name = "nextcloud";
61 ensurePermissions = {
62 "DATABASE nextcloud" = "ALL PRIVILEGES";
63 };
64 }
Skyler Greya78aa672023-05-20 13:48:18 +020065 ] ++ (map
66 (name: (
67 {
68 inherit name;
69 ensurePermissions = { "ALL TABLES IN SCHEMA public" = "ALL PRIVILEGES"; };
70 }
71 )) [ "minion" "coded" "pinea" ]);
72
Skyler Greya78aa672023-05-20 13:48:18 +020073 };
74
Skyler Grey8e32c832023-05-20 22:54:30 +020075 systemd.services.postgresql.postStart = lib.mkMerge [
76 (
77 let
78 database = "synapse";
79 cfg = config.services.postgresql;
80 in
81 lib.mkBefore (
82 ''
83 PSQL="psql --port=${toString cfg.port}"
84
85 while ! $PSQL -d postgres -c "" 2> /dev/null; do
86 if ! kill -0 "$MAINPID"; then exit 1; fi
87 sleep 0.1
88 done
89
90 $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"'
91 ''
92 ) # synapse needs C collation, so we can't use ensureDatabases for it
93 )
94 (lib.mkAfter (lib.pipe [
95 { user = "clicks_grafana"; passwordFile = config.sops.secrets.clicks_grafana_db_password.path; }
Skyler Grey0e05d262023-10-09 07:04:36 +000096 { user = "keycloak"; passwordFile = config.sops.secrets.clicks_keycloak_db_password.path; }
Skyler Grey08758a62023-10-09 07:35:09 +000097 { user = "gerrit"; passwordFile = config.sops.secrets.clicks_gerrit_db_password.path; }
TheCodedProfb6184602023-06-13 17:04:59 -040098 { user = "vaultwarden"; passwordFile = config.sops.secrets.clicks_bitwarden_db_password.path; }
Skyler Grey9fe61282023-08-20 21:52:48 +000099 { user = "privatebin"; passwordFile = config.sops.secrets.clicks_privatebin_db_password.path; }
Skyler Grey09c5cda2023-10-09 07:10:10 +0000100 { user = "nextcloud"; passwordFile = config.sops.secrets.clicks_nextcloud_db_password.path; }
Skyler Grey8e32c832023-05-20 22:54:30 +0200101 ] [
102 (map (userData: ''
103 $PSQL -tAc "ALTER USER ${userData.user} PASSWORD '$(cat ${userData.passwordFile})';"
104 ''))
105 (lib.concatStringsSep "\n")
106 ]))
107 ];
Skyler Greya78aa672023-05-20 13:48:18 +0200108
109 sops.secrets = lib.pipe [
110 "clicks_grafana_db_password"
Skyler Grey0e05d262023-10-09 07:04:36 +0000111 "clicks_keycloak_db_password"
Skyler Grey08758a62023-10-09 07:35:09 +0000112 "clicks_gerrit_db_password"
TheCodedProfb6184602023-06-13 17:04:59 -0400113 "clicks_bitwarden_db_password"
Skyler Grey9fe61282023-08-20 21:52:48 +0000114 "clicks_privatebin_db_password"
Skyler Grey09c5cda2023-10-09 07:10:10 +0000115 "clicks_nextcloud_db_password"
Skyler Greya78aa672023-05-20 13:48:18 +0200116 ] [
117 (map (name: {
118 inherit name;
119 value = {
120 mode = "0400";
121 owner = config.services.postgresql.superUser;
122 group = config.users.users.${config.services.postgresql.superUser}.group;
123 sopsFile = ../secrets/postgres.json;
124 format = "json";
125 };
126 }))
127 builtins.listToAttrs
128 ];
129}