blob: 7955bc26a9ea9f31b9197b7eecbd8394ebadde0c [file] [log] [blame]
Skyler Greya78aa672023-05-20 13:48:18 +02001{ lib, config, pkgs, ... }: {
Skyler Grey915067d2023-12-03 13:46:53 +00002 systemd.services.postgresql.after = [
3 "docker-network-taiga.service" # Needed to listen in 172.20.0.1
4 ];
5
Skyler Greya78aa672023-05-20 13:48:18 +02006 services.postgresql = {
7 enable = true;
8
9 package = pkgs.postgresql;
10 settings = {
Samuel Shuert45489982023-11-29 15:29:36 -050011 listen_addresses = lib.mkForce "standard, 172.20.0.1";
Skyler Greya78aa672023-05-20 13:48:18 +020012 log_connections = true;
Skyler Greya78aa672023-05-20 13:48:18 +020013 logging_collector = true;
14 log_disconnections = true;
15 log_destination = lib.mkForce "syslog";
16 };
17
Skyler Grey8b4f7b62024-02-17 12:23:02 +000018 ensureDatabases = [ "vaultwarden" "gerrit" "privatebin" "keycloak" "nextcloud" "synapse" "taiga" "jinx" ];
TheCodedProfb6184602023-06-13 17:04:59 -040019
Skyler Greya78aa672023-05-20 13:48:18 +020020 ensureUsers = [
21 {
22 name = "clicks_grafana";
Skyler Greya78aa672023-05-20 13:48:18 +020023 }
24 {
Skyler Grey22428b02023-11-19 13:20:56 +000025 name = "matrix-synapse";
Skyler Greya78aa672023-05-20 13:48:18 +020026 }
TheCodedProfb6184602023-06-13 17:04:59 -040027 {
Skyler Grey0e05d262023-10-09 07:04:36 +000028 name = "keycloak";
Skyler Grey915067d2023-12-03 13:46:53 +000029 ensureDBOwnership = true;
Skyler Grey0e05d262023-10-09 07:04:36 +000030 }
31 {
TheCodedProfb6184602023-06-13 17:04:59 -040032 name = "vaultwarden";
Skyler Grey915067d2023-12-03 13:46:53 +000033 ensureDBOwnership = true;
TheCodedProfb6184602023-06-13 17:04:59 -040034 }
Skyler Grey9fe61282023-08-20 21:52:48 +000035 {
36 name = "privatebin";
Skyler Grey915067d2023-12-03 13:46:53 +000037 ensureDBOwnership = true;
Skyler Grey9fe61282023-08-20 21:52:48 +000038 }
Skyler Grey09c5cda2023-10-09 07:10:10 +000039 {
40 name = "nextcloud";
Skyler Grey915067d2023-12-03 13:46:53 +000041 ensureDBOwnership = true;
Skyler Grey09c5cda2023-10-09 07:10:10 +000042 }
Samuel Shuert45489982023-11-29 15:29:36 -050043 {
44 name = "taiga";
Skyler Grey915067d2023-12-03 13:46:53 +000045 ensureDBOwnership = true;
Samuel Shuert45489982023-11-29 15:29:36 -050046 }
Skyler Grey8b4f7b62024-02-17 12:23:02 +000047 {
48 name = "taiga";
49 ensureDBOwnership = true;
50 }
51 {
52 name = "jinx";
53 ensureDBOwnership = true;
54 }
Skyler Greyfe1740c2023-10-21 01:24:18 +000055 ] ++ (map (name: ({
56 inherit name;
Skyler Greya7b38dd2023-10-25 21:42:45 +000057 })) [ "minion" "coded" "pineafan" ]);
Skyler Greya78aa672023-05-20 13:48:18 +020058
Samuel Shuert45489982023-11-29 15:29:36 -050059 # method database user address auth-method
60 authentication = "host all all samenet scram-sha-256";
Skyler Greya78aa672023-05-20 13:48:18 +020061 };
62
Skyler Grey8b4f7b62024-02-17 12:23:02 +000063 systemd.services.postgresql.restartTriggers = [
64 config.systemd.services.postgresql.postStart
65 ];
Skyler Grey8e32c832023-05-20 22:54:30 +020066 systemd.services.postgresql.postStart = lib.mkMerge [
Skyler Greyfe1740c2023-10-21 01:24:18 +000067 (let
68 database = "synapse";
69 cfg = config.services.postgresql;
70 in lib.mkBefore (''
71 PSQL="psql --port=${toString cfg.port}"
Skyler Grey8e32c832023-05-20 22:54:30 +020072
Skyler Greyfe1740c2023-10-21 01:24:18 +000073 while ! $PSQL -d postgres -c "" 2> /dev/null; do
74 if ! kill -0 "$MAINPID"; then exit 1; fi
75 sleep 0.1
76 done
Skyler Grey8e32c832023-05-20 22:54:30 +020077
Skyler Greyfe1740c2023-10-21 01:24:18 +000078 $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"'
79 '') # synapse needs C collation, so we can't use ensureDatabases for it
Skyler Grey8e32c832023-05-20 22:54:30 +020080 )
81 (lib.mkAfter (lib.pipe [
Skyler Greyfe1740c2023-10-21 01:24:18 +000082 {
83 user = "clicks_grafana";
84 passwordFile = config.sops.secrets.clicks_grafana_db_password.path;
85 }
86 {
87 user = "keycloak";
88 passwordFile = config.sops.secrets.clicks_keycloak_db_password.path;
89 }
90 {
Skyler Greyfe1740c2023-10-21 01:24:18 +000091 user = "vaultwarden";
Skyler Grey22428b02023-11-19 13:20:56 +000092 passwordFile = config.sops.secrets.clicks_vaultwarden_db_password.path;
Skyler Greyfe1740c2023-10-21 01:24:18 +000093 }
94 {
95 user = "privatebin";
96 passwordFile = config.sops.secrets.clicks_privatebin_db_password.path;
97 }
98 {
99 user = "nextcloud";
100 passwordFile = config.sops.secrets.clicks_nextcloud_db_password.path;
101 }
Samuel Shuert45489982023-11-29 15:29:36 -0500102 {
103 user = "taiga";
104 passwordFile = config.sops.secrets.clicks_taiga_db_password.path;
105 }
Skyler Grey8b4f7b62024-02-17 12:23:02 +0000106 {
107 user = "jinx";
108 passwordFile = config.sops.secrets.clicks_jinx_db_password.path;
109 }
Skyler Grey8e32c832023-05-20 22:54:30 +0200110 ] [
111 (map (userData: ''
112 $PSQL -tAc "ALTER USER ${userData.user} PASSWORD '$(cat ${userData.passwordFile})';"
113 ''))
114 (lib.concatStringsSep "\n")
115 ]))
Skyler Grey915067d2023-12-03 13:46:53 +0000116 ''
117 $PSQL -tAc 'ALTER DATABASE synapse OWNER TO "matrix-synapse";'
118 # matrix-synapse is done manually, because the database does not have the same name as the user
119
120 $PSQL -tAc 'GRANT SELECT ON ALL TABLES IN SCHEMA public TO "clicks_grafana"'
121 $PSQL -tAc 'GRANT USAGE ON SCHEMA public TO "clicks_grafana"'
122 # grafana is done manually, because it needs read permission in lots of places
123
124 $PSQL -tAc 'GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO "coded"'
125 $PSQL -tAc 'GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO "minion"'
126 $PSQL -tAc 'GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO "pineafan"'
127 # leadership is done manually, because we need owner-level permissions in lots of places but cannot specify ourselves as the database owners (as there may only be 1)
128 ''
Skyler Grey8e32c832023-05-20 22:54:30 +0200129 ];
Skyler Greya78aa672023-05-20 13:48:18 +0200130
131 sops.secrets = lib.pipe [
132 "clicks_grafana_db_password"
Skyler Grey0e05d262023-10-09 07:04:36 +0000133 "clicks_keycloak_db_password"
Skyler Grey22428b02023-11-19 13:20:56 +0000134 "clicks_vaultwarden_db_password"
Skyler Grey9fe61282023-08-20 21:52:48 +0000135 "clicks_privatebin_db_password"
Skyler Grey09c5cda2023-10-09 07:10:10 +0000136 "clicks_nextcloud_db_password"
Samuel Shuert45489982023-11-29 15:29:36 -0500137 "clicks_taiga_db_password"
Skyler Grey8b4f7b62024-02-17 12:23:02 +0000138 "clicks_jinx_db_password"
Skyler Greya78aa672023-05-20 13:48:18 +0200139 ] [
140 (map (name: {
141 inherit name;
142 value = {
143 mode = "0400";
144 owner = config.services.postgresql.superUser;
Skyler Greyfe1740c2023-10-21 01:24:18 +0000145 group =
146 config.users.users.${config.services.postgresql.superUser}.group;
Samuel Shuertf68685d2023-10-28 20:07:56 -0400147 sopsFile = ../../secrets/postgres.json;
Skyler Greya78aa672023-05-20 13:48:18 +0200148 format = "json";
149 };
150 }))
151 builtins.listToAttrs
152 ];
153}