Improvements to server

This commit is contained in:
soraefir
2026-05-06 22:48:09 +02:00
parent d73bbd8b18
commit c457867440
6 changed files with 85 additions and 29 deletions

View File

@@ -17,14 +17,14 @@ in {
settings = {
listen_addresses = lib.mkForce "*";
};
# authentication = pkgs.lib.mkOverride 10 ''
# # TYPE DATABASE USER ADDRESS METHOD
# local all all trust
# host all all 127.0.0.1/32 trust
# host all all 10.0.0.0/8 scram-sha-256
# host all all 169.254.0.0/16 scram-sha-256
# host all all ::1/128 trust
# '';
authentication = pkgs.lib.mkOverride 10 ''
# TYPE DATABASE USER ADDRESS METHOD
local all all trust
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
host all all 10.0.0.0/8 scram-sha-256
host all all 169.254.0.0/16 scram-sha-256
'';
ensureDatabases = map (name: "${name}_db") allApps;
ensureUsers = map (name: { name = "${name}_user"; }) allApps;
};
@@ -35,21 +35,42 @@ in {
backupAll = true; # Backs up all databases and roles
};
systemd.services.postgresql.postStart = lib.mkAfter ''
services.redis.servers."main" = {
enable = true;
port = 6379;
bind = "*";
settings.protected-mode = "no";
};
systemd.services.postgresql-init = {
description = "Custom Postgres Setup (Ownership & Passwords)";
after = [ "postgresql.service" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
User = "postgres";
RemainAfterExit = true;
};
script = ''
${pkgs.coreutils}/bin/sleep 2
PSQL="${pkgs.postgresql}/bin/psql"
${lib.concatMapStringsSep "\n" (name: ''
until $PSQL -tAc "SELECT 1 FROM pg_roles WHERE rolname = '${name}_user'" | grep -q 1; do
echo "Waiting for user ${name}_user..."
sleep 1
done
$PSQL -tAc "ALTER DATABASE ${name}_db OWNER TO ${name}_user;"
if [ -f "${config.sops.secrets."${lib.toUpper name}".path}" ]; then
PASS=$(grep "^DB_PASSWORD=" "${config.sops.secrets."${lib.toUpper name}".path}" | cut -d'=' -f2-)
$PSQL -tAc "ALTER USER ${name}_user WITH PASSWORD '$PASS';"
echo $PASS
if $PSQL -tAc "ALTER USER ${name}_user WITH PASSWORD '$PASS';" ; then
echo " Successfully set password for ${name}_user"
else
echo " FAILED to set password for ${name}_user"
fi
fi
'') allApps}
'';
'';
};
};
}