{ config, pkgs, lib, ... }: let livekitKeyFile = "/var/lib/livekit/livekit_keyFile"; in lib.mkIf config.sovran_systemsOS.features.element-calling { ####### LIVEKIT KEY GENERATION ####### systemd.tmpfiles.rules = [ "d /var/lib/livekit 0750 root root -" ]; systemd.services.livekit-key-setup = { description = "Generate LiveKit key file if missing"; wantedBy = [ "multi-user.target" ]; before = [ "livekit.service" "lk-jwt-service.service" ]; serviceConfig = { Type = "oneshot"; RemainAfterExit = true; }; path = [ pkgs.openssl ]; script = '' if [ ! -f ${livekitKeyFile} ]; then API_KEY="devkey_$(openssl rand -hex 16)" API_SECRET="$(openssl rand -base64 36 | tr -d '\n')" echo "$API_KEY: $API_SECRET" > ${livekitKeyFile} chmod 600 ${livekitKeyFile} echo "LiveKit key file generated at ${livekitKeyFile}" else echo "LiveKit key file already exists, skipping generation" fi ''; }; ####### ENSURE SERVICES START AFTER KEY EXISTS ####### systemd.services.livekit.after = [ "livekit-key-setup.service" ]; systemd.services.livekit.wants = [ "livekit-key-setup.service" ]; systemd.services.lk-jwt-service.after = [ "livekit-key-setup.service" ]; systemd.services.lk-jwt-service.wants = [ "livekit-key-setup.service" ]; ####### CADDY SNIPPET — written to /run/caddy for caddy.nix to pick up ####### systemd.services.element-calling-caddy-config = { description = "Generate Element Calling Caddy config snippet"; before = [ "caddy-generate-config.service" ]; requiredBy = [ "caddy-generate-config.service" ]; wantedBy = [ "multi-user.target" ]; serviceConfig = { Type = "oneshot"; RemainAfterExit = true; }; unitConfig = { ConditionPathExists = "/var/lib/domains/element-calling"; }; path = [ pkgs.coreutils ]; script = '' MATRIX=$(cat /var/lib/domains/matrix) ELEMENT_CALLING=$(cat /var/lib/domains/element-calling) mkdir -p /run/caddy cat > /run/caddy/element-calling.snippet < /run/livekit/runtime-config.yaml < /run/lk-jwt-service/env < /run/matrix-synapse/element-calling-config.yaml <