blob: 397a377cb7281b49ad519b95bbae7cf257bb325a [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
Skyler Greyfe1740c2023-10-21 01:24:18 +000013 ensureDatabases =
14 [ "vaultwarden" "gerrit" "privatebin" "keycloak" "nextcloud" ];
TheCodedProfb6184602023-06-13 17:04:59 -040015
Skyler Greya78aa672023-05-20 13:48:18 +020016 ensureUsers = [
17 {
18 name = "clicks_grafana";
19 ensurePermissions = {
20 "ALL TABLES IN SCHEMA public" = "SELECT";
21 "SCHEMA public" = "USAGE";
22 };
23 }
24 {
Skyler Grey8e32c832023-05-20 22:54:30 +020025 name = "synapse";
Skyler Greyfe1740c2023-10-21 01:24:18 +000026 ensurePermissions = { "DATABASE synapse" = "ALL PRIVILEGES"; };
Skyler Greya78aa672023-05-20 13:48:18 +020027 }
TheCodedProfb6184602023-06-13 17:04:59 -040028 {
Skyler Grey0e05d262023-10-09 07:04:36 +000029 name = "keycloak";
Skyler Greyfe1740c2023-10-21 01:24:18 +000030 ensurePermissions = { "DATABASE keycloak" = "ALL PRIVILEGES"; };
Skyler Grey0e05d262023-10-09 07:04:36 +000031 }
32 {
TheCodedProfb6184602023-06-13 17:04:59 -040033 name = "vaultwarden";
Skyler Greyfe1740c2023-10-21 01:24:18 +000034 ensurePermissions = { "DATABASE vaultwarden" = "ALL PRIVILEGES"; };
TheCodedProfb6184602023-06-13 17:04:59 -040035 }
Skyler Grey9fe61282023-08-20 21:52:48 +000036 {
37 name = "privatebin";
Skyler Greyfe1740c2023-10-21 01:24:18 +000038 ensurePermissions = { "DATABASE privatebin" = "ALL PRIVILEGES"; };
Skyler Grey9fe61282023-08-20 21:52:48 +000039 }
Skyler Grey09c5cda2023-10-09 07:10:10 +000040 {
41 name = "nextcloud";
Skyler Greyfe1740c2023-10-21 01:24:18 +000042 ensurePermissions = { "DATABASE nextcloud" = "ALL PRIVILEGES"; };
Skyler Grey09c5cda2023-10-09 07:10:10 +000043 }
Skyler Greyfe1740c2023-10-21 01:24:18 +000044 ] ++ (map (name: ({
45 inherit name;
46 ensurePermissions = { "ALL TABLES IN SCHEMA public" = "ALL PRIVILEGES"; };
Skyler Greya7b38dd2023-10-25 21:42:45 +000047 })) [ "minion" "coded" "pineafan" ]);
Skyler Greya78aa672023-05-20 13:48:18 +020048
Skyler Greya78aa672023-05-20 13:48:18 +020049 };
50
Skyler Grey8e32c832023-05-20 22:54:30 +020051 systemd.services.postgresql.postStart = lib.mkMerge [
Skyler Greyfe1740c2023-10-21 01:24:18 +000052 (let
53 database = "synapse";
54 cfg = config.services.postgresql;
55 in lib.mkBefore (''
56 PSQL="psql --port=${toString cfg.port}"
Skyler Grey8e32c832023-05-20 22:54:30 +020057
Skyler Greyfe1740c2023-10-21 01:24:18 +000058 while ! $PSQL -d postgres -c "" 2> /dev/null; do
59 if ! kill -0 "$MAINPID"; then exit 1; fi
60 sleep 0.1
61 done
Skyler Grey8e32c832023-05-20 22:54:30 +020062
Skyler Greyfe1740c2023-10-21 01:24:18 +000063 $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"'
64 '') # synapse needs C collation, so we can't use ensureDatabases for it
Skyler Grey8e32c832023-05-20 22:54:30 +020065 )
66 (lib.mkAfter (lib.pipe [
Skyler Greyfe1740c2023-10-21 01:24:18 +000067 {
68 user = "clicks_grafana";
69 passwordFile = config.sops.secrets.clicks_grafana_db_password.path;
70 }
71 {
72 user = "keycloak";
73 passwordFile = config.sops.secrets.clicks_keycloak_db_password.path;
74 }
75 {
Skyler Greyfe1740c2023-10-21 01:24:18 +000076 user = "vaultwarden";
77 passwordFile = config.sops.secrets.clicks_bitwarden_db_password.path;
78 }
79 {
80 user = "privatebin";
81 passwordFile = config.sops.secrets.clicks_privatebin_db_password.path;
82 }
83 {
84 user = "nextcloud";
85 passwordFile = config.sops.secrets.clicks_nextcloud_db_password.path;
86 }
Skyler Grey8e32c832023-05-20 22:54:30 +020087 ] [
88 (map (userData: ''
89 $PSQL -tAc "ALTER USER ${userData.user} PASSWORD '$(cat ${userData.passwordFile})';"
90 ''))
91 (lib.concatStringsSep "\n")
92 ]))
93 ];
Skyler Greya78aa672023-05-20 13:48:18 +020094
95 sops.secrets = lib.pipe [
96 "clicks_grafana_db_password"
Skyler Grey0e05d262023-10-09 07:04:36 +000097 "clicks_keycloak_db_password"
TheCodedProfb6184602023-06-13 17:04:59 -040098 "clicks_bitwarden_db_password"
Skyler Grey9fe61282023-08-20 21:52:48 +000099 "clicks_privatebin_db_password"
Skyler Grey09c5cda2023-10-09 07:10:10 +0000100 "clicks_nextcloud_db_password"
Skyler Greya78aa672023-05-20 13:48:18 +0200101 ] [
102 (map (name: {
103 inherit name;
104 value = {
105 mode = "0400";
106 owner = config.services.postgresql.superUser;
Skyler Greyfe1740c2023-10-21 01:24:18 +0000107 group =
108 config.users.users.${config.services.postgresql.superUser}.group;
Samuel Shuertf68685d2023-10-28 20:07:56 -0400109 sopsFile = ../../secrets/postgres.json;
Skyler Greya78aa672023-05-20 13:48:18 +0200110 format = "json";
111 };
112 }))
113 builtins.listToAttrs
114 ];
115}