diff --git a/flake.lock b/flake.lock index 31fe385..0ab1040 100644 Binary files a/flake.lock and b/flake.lock differ diff --git a/flake.nix b/flake.nix index 3401548..cab8b70 100755 --- a/flake.nix +++ b/flake.nix @@ -34,7 +34,7 @@ ./hardware-configuration.nix #./modules/packages/ooye/packages/module.nix - ./host/matrix/matrix/ooye.nix +# ./host/matrix/matrix/ooye.nix ./host/matrix/unstable-overlay.nix home-manager.nixosModules.home-manager diff --git a/modules/packages/ooye/packages/default.nix b/modules/packages/ooye/packages/default.nix index d8ff76b..ec4c4a3 100644 --- a/modules/packages/ooye/packages/default.nix +++ b/modules/packages/ooye/packages/default.nix @@ -11,8 +11,8 @@ buildNpmPackage rec { version = "0"; src = fetchgit { url = "https://gitdab.com/cadence/out-of-your-element.git"; - rev = "07d6eb3c1272c2526a4749724c07c4fd530893d4"; - sha256 = "3Y6s9pNKKeqF6s4I2Rd4TpxXPCwqizXeil/sTDVnpr0="; + rev = "bf01db13d66f4251dda4f20f3ef16a44f593d4ab"; + sha256 = "/Q21Q4lhFXQOdGBhLBDfHnk/9WFSKjLiSzs96ugEcLU="; }; npmDepsHash = "sha256-1STam+Sjy2MQcK5TmRacoxmgErd2sNqw0yIFX2M+iZk="; dontNpmBuild = true; diff --git a/modules/packages/ooye/packages/module.nix b/modules/packages/ooye/packages/module.nix index 82f6b0f..d76a33c 100644 --- a/modules/packages/ooye/packages/module.nix +++ b/modules/packages/ooye/packages/module.nix @@ -26,7 +26,9 @@ in homeserverName = mkStringOption "The name of the homeserver to connect to." "localhost"; namespace = mkStringOption "The prefix to use for the MXIDs/aliases of bridged users/rooms. Should end with a _!" "_ooye_"; discordTokenPath = mkStringOption "The path to the discord token file." "/etc/ooye-discord-token"; + discordClientSecretPath = mkStringOption "The path to the discord token file." "/etc/ooye-discord-client-secret"; socket = mkStringOption "The socket to listen on, can either be a port number or a unix socket path." "6693"; + bridgeOrigin = mkStringOption "The web frontend URL for the bridge, defaults to http://localhost:{socket}" ""; enableSynapseIntegration = lib.mkEnableOption "Enable Synapse integration"; }; @@ -62,51 +64,63 @@ in content_length_workaround = false; include_user_id_in_mxid = true; server_origin = cfg.homeserver; + bridge_origin = if (cfg.bridgeOrigin == "") then "http://localhost:${cfg.socket}" else cfg.bridgeOrigin; }; } ); -script = pkgs.writeScript "matrix-ooye-pre-start.sh" '' - #!${lib.getExe pkgs.bash} - REGISTRATION_FILE=registration.yaml + script = pkgs.writeScript "matrix-ooye-pre-start.sh" '' + #!${lib.getExe pkgs.bash} + REGISTRATION_FILE=registration.yaml - id - echo "Before if statement" - stat ''${REGISTRATION_FILE} + id + echo "Before if statement" + stat ''${REGISTRATION_FILE} - if [[ ! -f ''${REGISTRATION_FILE} ]]; then - echo "No registration file found at '$REGISTRATION_FILE'" - cp --no-preserve=mode,ownership ${baseConfig} ''${REGISTRATION_FILE} - fi - - echo "After if statement" - stat ''${REGISTRATION_FILE} + if [[ ! -f ''${REGISTRATION_FILE} ]]; then + echo "No registration file found at '$REGISTRATION_FILE'" + cp --no-preserve=mode,ownership ${baseConfig} ''${REGISTRATION_FILE} + fi - AS_TOKEN=$(${lib.getExe pkgs.jq} -r .as_token ''${REGISTRATION_FILE}) - HS_TOKEN=$(${lib.getExe pkgs.jq} -r .hs_token ''${REGISTRATION_FILE}) - DISCORD_TOKEN=$(cat /run/credentials/matrix-ooye-pre-start.service/discord_token) + echo "After if statement" + stat ''${REGISTRATION_FILE} - if [[ -z "$AS_TOKEN" || "$AS_TOKEN" == "null" ]]; then - AS_TOKEN=$(${lib.getExe pkgs.openssl} rand -hex 64) - echo "Generated new AS token: ''${AS_TOKEN}" - fi - if [[ -z "$HS_TOKEN" || "$HS_TOKEN" == "null" ]]; then - HS_TOKEN=$(${lib.getExe pkgs.openssl} rand -hex 64) - echo "Generated new HS token: ''${HS_TOKEN}" - fi - if [[ -z "$DISCORD_TOKEN" ]]; then - echo "No Discord token found at '${cfg.discordTokenPath}'" - exit 1 - fi + AS_TOKEN=$(${lib.getExe pkgs.jq} -r .as_token ''${REGISTRATION_FILE}) + HS_TOKEN=$(${lib.getExe pkgs.jq} -r .hs_token ''${REGISTRATION_FILE}) + DISCORD_TOKEN=$(cat /run/credentials/matrix-ooye-pre-start.service/discord_token) + DISCORD_CLIENT_SECRET=$(cat /run/credentials/matrix-ooye-pre-start.service/discord_client_secret) - shred -u ''${REGISTRATION_FILE} - cp --no-preserve=mode,ownership ${baseConfig} ''${REGISTRATION_FILE} + # Check if we have all required tokens + if [[ -z "$AS_TOKEN" || "$AS_TOKEN" == "null" ]]; then + AS_TOKEN=$(${lib.getExe pkgs.openssl} rand -hex 64) + echo "Generated new AS token: ''${AS_TOKEN}" + fi + + if [[ -z "$HS_TOKEN" || "$HS_TOKEN" == "null" ]]; then + HS_TOKEN=$(${lib.getExe pkgs.openssl} rand -hex 64) + echo "Generated new HS token: ''${HS_TOKEN}" + fi + + if [[ -z "$DISCORD_TOKEN" ]]; then + echo "No Discord token found at '${cfg.discordTokenPath}'" + echo "You can find this on the 'Bot' tab of your Discord application." + exit 1 + fi + + if [[ -z "$DISCORD_CLIENT_SECRET" ]]; then + echo "No Discord client secret found at '${cfg.discordTokenPath}'" + echo "You can find this on the 'OAuth2' tab of your Discord application." + exit 1 + fi - ${lib.getExe pkgs.jq} '.as_token = "'$AS_TOKEN'" | .hs_token = "'$HS_TOKEN'" | .ooye.discord_token = "'$DISCORD_TOKEN'"' ''${REGISTRATION_FILE} > ''${REGISTRATION_FILE}.tmp + shred -u ''${REGISTRATION_FILE} + cp --no-preserve=mode,ownership ${baseConfig} ''${REGISTRATION_FILE} - shred -u ''${REGISTRATION_FILE} - mv ''${REGISTRATION_FILE}.tmp ''${REGISTRATION_FILE} -''; + ${lib.getExe pkgs.jq} '.as_token = "'$AS_TOKEN'" | .hs_token = "'$HS_TOKEN'" | .ooye.discord_token = "'$DISCORD_TOKEN'" | .ooye.discord_client_secret = "'$DISCORD_CLIENT_SECRET'"' ''${REGISTRATION_FILE} > ''${REGISTRATION_FILE}.tmp + + shred -u ''${REGISTRATION_FILE} + mv ''${REGISTRATION_FILE}.tmp ''${REGISTRATION_FILE} + ''; in { @@ -118,6 +132,8 @@ script = pkgs.writeScript "matrix-ooye-pre-start.sh" '' "OOYE namespace does not start with an underscore! This is recommended to avoid conflicts with registered users. Provided: '${cfg.namespace}'" ]; + environment.systemPackages = [ cfg.package ]; + systemd.services."matrix-ooye-pre-start" = { enable = true; wantedBy = [ "multi-user.target" ]; @@ -126,9 +142,11 @@ script = pkgs.writeScript "matrix-ooye-pre-start.sh" '' WorkingDirectory = "/var/lib/matrix-ooye"; StateDirectory = "matrix-ooye"; DynamicUser = true; + Type = "oneshot"; LoadCredential = [ "discord_token:${cfg.discordTokenPath}" + "discord_client_secret:${cfg.discordClientSecretPath}" ]; }; }; @@ -147,8 +165,8 @@ script = pkgs.writeScript "matrix-ooye-pre-start.sh" '' "matrix-ooye-pre-start.service" "network-online.target" ]; - wantedBy = [ "multi-user.target" ]; requires = [ "matrix-ooye-pre-start.service" ]; + wantedBy = [ "multi-user.target" ]; serviceConfig = { ExecStart = lib.getExe config.services.matrix-ooye.package; @@ -163,12 +181,23 @@ script = pkgs.writeScript "matrix-ooye-pre-start.sh" '' DynamicUser = true; }; }; - - systemd.services."matrix-synapse".serviceConfig = lib.mkIf cfg.enableSynapseIntegration { - LoadCredential = [ - "matrix-ooye-registration:/var/lib/matrix-ooye/registration.yaml" + + systemd.services."matrix-synapse" = lib.mkIf cfg.enableSynapseIntegration { + + after = [ + "matrix-ooye-pre-start.service" + "network-online.target" ]; - ExecStartPre = "cp /run/credentials/matrix-synapse.service/registration.yaml ${config.services.matrix-synapse.dataDir}/ooye-registration.yaml"; + requires = [ "matrix-ooye-pre-start.service" ]; + serviceConfig = { + LoadCredential = [ + "matrix-ooye-registration:/var/lib/matrix-ooye/registration.yaml" + ]; + ExecStartPre = [ + "+${pkgs.coreutils}/bin/cp /run/credentials/matrix-synapse.service/matrix-ooye-registration ${config.services.matrix-synapse.dataDir}/ooye-registration.yaml" + "+${pkgs.coreutils}/bin/chown matrix-synapse:matrix-synapse ${config.services.matrix-synapse.dataDir}/ooye-registration.yaml" + ]; + }; }; services.matrix-synapse.settings.app_service_config_files = lib.mkIf cfg.enableSynapseIntegration [ diff --git a/test b/test new file mode 100755 index 0000000..83a676e --- /dev/null +++ b/test @@ -0,0 +1,22 @@ +#!/nix/store/p6k7xp1lsfmbdd731mlglrdj2d66mr82-bash-5.2p37/bin/bash +set -e + +echo /nix/store/6a0f3b152k8h8rsnmp2zv8vi21z4812h-matrix-ooye-config.json + +REGISTRATION_FILE=ooye-registration.json +AS_TOKEN=$(jq -r .as_token ${REGISTRATION_FILE}) +HS_TOKEN=$(jq -r .hs_token ${REGISTRATION_FILE}) + +if [ -z "$AS_TOKEN" -o "$AS_TOKEN" == "null" ]; then + AS_TOKEN=$(openssl rand -hex 64) + echo "Generated new AS token: ${AS_TOKEN}" +fi +if [ -z "$HS_TOKEN" -o "$HS_TOKEN" == "null" ]; then + HS_TOKEN=$(openssl rand -hex 64) + echo "Generated new HS token: ${HS_TOKEN}" +fi + +/nix/store/iha35mp2hjhc77bkg6mf2zr2gyi6d05s-jq-1.7.1-bin/bin/jq '.as_token = "'$AS_TOKEN'" | .hs_token = "'$HS_TOKEN'"' ${REGISTRATION_FILE} > ${REGISTRATION_FILE}.tmp + + + diff --git a/test-build.sh b/test-build.sh new file mode 100644 index 0000000..e69de29