{ config, pkgs, lib, ... }: lib.mkIf config.sovran_systemsOS.services.synapse { services.postgresql = { ensureDatabases = [ "matrix-synapse" ]; ensureUsers = [ { name = "matrix-synapse"; ensureDBOwnership = true; } ]; }; systemd.services.matrix-synapse-db-init = { description = "Generate Matrix Synapse DB password if missing"; wantedBy = [ "multi-user.target" ]; before = [ "matrix-synapse.service" ]; serviceConfig = { Type = "oneshot"; RemainAfterExit = true; }; path = [ pkgs.pwgen ]; script = '' SECRET_FILE="/var/lib/matrix-synapse/db-password" if [ ! -f "$SECRET_FILE" ]; then mkdir -p /var/lib/matrix-synapse pwgen -s 32 1 > "$SECRET_FILE" chown matrix-synapse:matrix-synapse "$SECRET_FILE" chmod 600 "$SECRET_FILE" echo "Generated new DB password at $SECRET_FILE" else echo "DB password already exists, skipping" fi ''; }; systemd.services.matrix-synapse-runtime-config = { description = "Generate Synapse runtime config from domain files"; before = [ "matrix-synapse.service" ]; after = [ "matrix-synapse-db-init.service" "matrix-synapse-secret-init.service" ]; requiredBy = [ "matrix-synapse.service" ]; wantedBy = [ "multi-user.target" ]; serviceConfig = { Type = "oneshot"; RemainAfterExit = true; }; unitConfig = { ConditionPathExists = "/var/lib/domains/matrix"; }; path = [ pkgs.coreutils ]; script = '' MATRIX=$(cat /var/lib/domains/matrix) mkdir -p /run/matrix-synapse cat > /run/matrix-synapse/runtime-config.yaml <