blob: f77346a10efda42ad3b4ffceee7c312ed72aa816 [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 = {
Samuel Shuertfe00e182023-11-22 18:35:01 -05007 listen_addresses = lib.mkForce "standard";
Skyler Greya78aa672023-05-20 13:48:18 +02008 log_connections = true;
Skyler Greya78aa672023-05-20 13:48:18 +02009 logging_collector = true;
10 log_disconnections = true;
11 log_destination = lib.mkForce "syslog";
12 };
13
Skyler Greyfe1740c2023-10-21 01:24:18 +000014 ensureDatabases =
Skyler Grey22428b02023-11-19 13:20:56 +000015 [ "vaultwarden" "gerrit" "privatebin" "keycloak" "nextcloud" "synapse" ];
TheCodedProfb6184602023-06-13 17:04:59 -040016
Skyler Greya78aa672023-05-20 13:48:18 +020017 ensureUsers = [
18 {
19 name = "clicks_grafana";
20 ensurePermissions = {
21 "ALL TABLES IN SCHEMA public" = "SELECT";
22 "SCHEMA public" = "USAGE";
23 };
24 }
25 {
Skyler Grey22428b02023-11-19 13:20:56 +000026 name = "matrix-synapse";
Skyler Greyfe1740c2023-10-21 01:24:18 +000027 ensurePermissions = { "DATABASE synapse" = "ALL PRIVILEGES"; };
Skyler Greya78aa672023-05-20 13:48:18 +020028 }
TheCodedProfb6184602023-06-13 17:04:59 -040029 {
Skyler Grey0e05d262023-10-09 07:04:36 +000030 name = "keycloak";
Skyler Greyfe1740c2023-10-21 01:24:18 +000031 ensurePermissions = { "DATABASE keycloak" = "ALL PRIVILEGES"; };
Skyler Grey0e05d262023-10-09 07:04:36 +000032 }
33 {
TheCodedProfb6184602023-06-13 17:04:59 -040034 name = "vaultwarden";
Skyler Greyfe1740c2023-10-21 01:24:18 +000035 ensurePermissions = { "DATABASE vaultwarden" = "ALL PRIVILEGES"; };
TheCodedProfb6184602023-06-13 17:04:59 -040036 }
Skyler Grey9fe61282023-08-20 21:52:48 +000037 {
38 name = "privatebin";
Skyler Greyfe1740c2023-10-21 01:24:18 +000039 ensurePermissions = { "DATABASE privatebin" = "ALL PRIVILEGES"; };
Skyler Grey9fe61282023-08-20 21:52:48 +000040 }
Skyler Grey09c5cda2023-10-09 07:10:10 +000041 {
42 name = "nextcloud";
Skyler Greyfe1740c2023-10-21 01:24:18 +000043 ensurePermissions = { "DATABASE nextcloud" = "ALL PRIVILEGES"; };
Skyler Grey09c5cda2023-10-09 07:10:10 +000044 }
Skyler Greyfe1740c2023-10-21 01:24:18 +000045 ] ++ (map (name: ({
46 inherit name;
47 ensurePermissions = { "ALL TABLES IN SCHEMA public" = "ALL PRIVILEGES"; };
Skyler Greya7b38dd2023-10-25 21:42:45 +000048 })) [ "minion" "coded" "pineafan" ]);
Skyler Greya78aa672023-05-20 13:48:18 +020049
Skyler Greya78aa672023-05-20 13:48:18 +020050 };
51
Skyler Grey8e32c832023-05-20 22:54:30 +020052 systemd.services.postgresql.postStart = lib.mkMerge [
Skyler Greyfe1740c2023-10-21 01:24:18 +000053 (let
54 database = "synapse";
55 cfg = config.services.postgresql;
56 in lib.mkBefore (''
57 PSQL="psql --port=${toString cfg.port}"
Skyler Grey8e32c832023-05-20 22:54:30 +020058
Skyler Greyfe1740c2023-10-21 01:24:18 +000059 while ! $PSQL -d postgres -c "" 2> /dev/null; do
60 if ! kill -0 "$MAINPID"; then exit 1; fi
61 sleep 0.1
62 done
Skyler Grey8e32c832023-05-20 22:54:30 +020063
Skyler Greyfe1740c2023-10-21 01:24:18 +000064 $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"'
65 '') # synapse needs C collation, so we can't use ensureDatabases for it
Skyler Grey8e32c832023-05-20 22:54:30 +020066 )
67 (lib.mkAfter (lib.pipe [
Skyler Greyfe1740c2023-10-21 01:24:18 +000068 {
69 user = "clicks_grafana";
70 passwordFile = config.sops.secrets.clicks_grafana_db_password.path;
71 }
72 {
73 user = "keycloak";
74 passwordFile = config.sops.secrets.clicks_keycloak_db_password.path;
75 }
76 {
Skyler Greyfe1740c2023-10-21 01:24:18 +000077 user = "vaultwarden";
Skyler Grey22428b02023-11-19 13:20:56 +000078 passwordFile = config.sops.secrets.clicks_vaultwarden_db_password.path;
Skyler Greyfe1740c2023-10-21 01:24:18 +000079 }
80 {
81 user = "privatebin";
82 passwordFile = config.sops.secrets.clicks_privatebin_db_password.path;
83 }
84 {
85 user = "nextcloud";
86 passwordFile = config.sops.secrets.clicks_nextcloud_db_password.path;
87 }
Skyler Grey8e32c832023-05-20 22:54:30 +020088 ] [
89 (map (userData: ''
90 $PSQL -tAc "ALTER USER ${userData.user} PASSWORD '$(cat ${userData.passwordFile})';"
91 ''))
92 (lib.concatStringsSep "\n")
93 ]))
94 ];
Skyler Greya78aa672023-05-20 13:48:18 +020095
96 sops.secrets = lib.pipe [
97 "clicks_grafana_db_password"
Skyler Grey0e05d262023-10-09 07:04:36 +000098 "clicks_keycloak_db_password"
Skyler Grey22428b02023-11-19 13:20:56 +000099 "clicks_vaultwarden_db_password"
Skyler Grey9fe61282023-08-20 21:52:48 +0000100 "clicks_privatebin_db_password"
Skyler Grey09c5cda2023-10-09 07:10:10 +0000101 "clicks_nextcloud_db_password"
Skyler Greya78aa672023-05-20 13:48:18 +0200102 ] [
103 (map (name: {
104 inherit name;
105 value = {
106 mode = "0400";
107 owner = config.services.postgresql.superUser;
Skyler Greyfe1740c2023-10-21 01:24:18 +0000108 group =
109 config.users.users.${config.services.postgresql.superUser}.group;
Samuel Shuertf68685d2023-10-28 20:07:56 -0400110 sopsFile = ../../secrets/postgres.json;
Skyler Greya78aa672023-05-20 13:48:18 +0200111 format = "json";
112 };
113 }))
114 builtins.listToAttrs
115 ];
116}