2024-09-09 15:19:57 +02:00
|
|
|
{
|
2025-01-23 14:13:19 +01:00
|
|
|
lib,
|
|
|
|
config,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}: let
|
|
|
|
cfg = config.services.vm_postgresql;
|
|
|
|
in {
|
2024-09-09 15:19:57 +02:00
|
|
|
options.services.vm_postgresql = {
|
|
|
|
enable = lib.mkEnableOption "Enable minimal config";
|
|
|
|
};
|
|
|
|
config = lib.mkIf cfg.enable {
|
2025-01-28 11:22:37 +01:00
|
|
|
age.secrets = {
|
2025-01-28 10:33:51 +01:00
|
|
|
nextcloudDBPass = {
|
|
|
|
file = ./secrets/nextcloudDBPass.age;
|
|
|
|
owner = "postgres";
|
|
|
|
};
|
|
|
|
giteaDBPass = {
|
|
|
|
file = ./secrets/giteaDBPass.age;
|
|
|
|
owner = "postgres";
|
|
|
|
};
|
|
|
|
authentikDBPass = {
|
|
|
|
file = ./secrets/authentikDBPass.age;
|
|
|
|
owner = "postgres";
|
|
|
|
};
|
|
|
|
grafanaDBPass = {
|
|
|
|
file = ./secrets/grafanaDBPass.age;
|
|
|
|
owner = "postgres";
|
|
|
|
};
|
2025-01-28 11:22:37 +01:00
|
|
|
};
|
2024-09-09 15:19:57 +02:00
|
|
|
services.postgresql = {
|
|
|
|
enable = true;
|
2024-11-28 10:52:27 +01:00
|
|
|
package = pkgs.postgresql_16;
|
2024-09-09 15:19:57 +02:00
|
|
|
enableTCPIP = true;
|
|
|
|
settings.port = 5432;
|
2024-10-24 14:26:28 +02:00
|
|
|
authentication = "
|
2025-01-28 12:02:05 +01:00
|
|
|
host nextcloud nextcloud 192.168.1.45/32 md5
|
2024-10-24 14:26:28 +02:00
|
|
|
host gitea gitea 192.168.1.14/32 md5
|
|
|
|
host authentik authentik 192.168.1.125/32 md5
|
|
|
|
host grafana grafana 192.168.1.27/32 md5
|
|
|
|
";
|
2024-09-09 15:19:57 +02:00
|
|
|
initialScript = pkgs.writeText "init-sql-script" ''
|
2025-01-28 11:18:06 +01:00
|
|
|
CREATE ROLE nextcloud WITH LOGIN CREATEDB;
|
2025-01-28 11:22:37 +01:00
|
|
|
CREATE DATABASE nextcloud;
|
|
|
|
GRANT ALL PRIVILEGES ON DATABASE nextcloud TO nextcloud;
|
|
|
|
|
2025-01-28 11:18:06 +01:00
|
|
|
CREATE ROLE gitea WITH LOGIN CREATEDB;
|
2025-01-28 11:22:37 +01:00
|
|
|
CREATE DATABASE gitea;
|
|
|
|
GRANT ALL PRIVILEGES ON DATABASE gitea TO gitea;
|
|
|
|
|
2025-01-28 11:18:06 +01:00
|
|
|
CREATE ROLE authentik WITH LOGIN CREATEDB;
|
2025-01-28 11:22:37 +01:00
|
|
|
CREATE DATABASE authentik;
|
|
|
|
GRANT ALL PRIVILEGES ON DATABASE authentik TO authentik;
|
|
|
|
|
2025-01-28 11:18:06 +01:00
|
|
|
CREATE ROLE grafana WITH LOGIN CREATEDB;
|
2025-01-28 11:22:37 +01:00
|
|
|
CREATE DATABASE grafana;
|
|
|
|
GRANT ALL PRIVILEGES ON DATABASE grafana TO grafana;
|
2024-09-09 15:19:57 +02:00
|
|
|
'';
|
|
|
|
};
|
2025-01-28 11:18:06 +01:00
|
|
|
# Stolen from https://discourse.nixos.org/t/assign-password-to-postgres-user-declaratively/9726/3
|
|
|
|
# This is an awful situation
|
|
|
|
systemd.services.postgresql.postStart = let
|
|
|
|
nextcloudDBPass = config.age.secrets.nextcloudDBPass.path;
|
|
|
|
giteaDBPass = config.age.secrets.giteaDBPass.path;
|
|
|
|
authentikDBPass = config.age.secrets.authentikDBPass.path;
|
|
|
|
grafanaDBPass = config.age.secrets.grafanaDBPass.path;
|
|
|
|
in ''
|
|
|
|
$PSQL -tA <<'EOF'
|
|
|
|
DO $$
|
|
|
|
DECLARE password TEXT;
|
|
|
|
BEGIN
|
|
|
|
password := trim(both from replace(pg_read_file('${nextcloudDBPass}'), E'\n', '''));
|
|
|
|
EXECUTE format('ALTER ROLE nextcloud WITH PASSWORD '''%s''';', password);
|
|
|
|
|
|
|
|
password := trim(both from replace(pg_read_file('${giteaDBPass}'), E'\n', '''));
|
|
|
|
EXECUTE format('ALTER ROLE gitea WITH PASSWORD '''%s''';', password);
|
|
|
|
|
|
|
|
password := trim(both from replace(pg_read_file('${authentikDBPass}'), E'\n', '''));
|
|
|
|
EXECUTE format('ALTER ROLE authentik WITH PASSWORD '''%s''';', password);
|
|
|
|
|
|
|
|
password := trim(both from replace(pg_read_file('${grafanaDBPass}'), E'\n', '''));
|
|
|
|
EXECUTE format('ALTER ROLE grafana WITH PASSWORD '''%s''';', password);
|
|
|
|
END $$;
|
|
|
|
EOF
|
|
|
|
'';
|
2025-01-23 14:13:19 +01:00
|
|
|
networking.firewall.allowedTCPPorts = [5432];
|
2024-09-09 15:19:57 +02:00
|
|
|
};
|
|
|
|
}
|