Unverified Commit 90a87076 authored by Bruno BELANYI's avatar Bruno BELANYI Committed by GitHub
Browse files

nixos/autobrr: use AUTOBRR__SESSION_SECRET_FILE (#466312)

parents d96cb813 50d1876c
Loading
Loading
Loading
Loading
+54 −23
Original line number Diff line number Diff line
@@ -8,8 +8,7 @@
let
  cfg = config.services.autobrr;
  configFormat = pkgs.formats.toml { };
  configTemplate = configFormat.generate "autobrr.toml" cfg.settings;
  templaterCmd = ''${lib.getExe pkgs.dasel} put -f '${configTemplate}' -v "$(${config.systemd.package}/bin/systemd-creds cat sessionSecret)" -o %S/autobrr/config.toml "sessionSecret"'';
  configFile = configFormat.generate "autobrr.toml" cfg.settings;
in
{
  options = {
@@ -28,13 +27,31 @@ in
      };

      settings = lib.mkOption {
        type = lib.types.submodule { freeformType = configFormat.type; };
        default = {
          host = "127.0.0.1";
          port = 7474;
          checkForUpdates = true;
        type = lib.types.submodule {
          freeformType = configFormat.type;
          options = {
            host = lib.mkOption {
              type = lib.types.str;
              default = "127.0.0.1";
              description = "The host address autobrr listens on.";
            };

            port = lib.mkOption {
              type = lib.types.port;
              default = 7474;
              description = "The port autobrr listens on.";
            };

            checkForUpdates = lib.mkOption {
              type = lib.types.bool;
              default = true;
              description = "Whether autobrr needs to check for updates.";
            };
          };
        };
        default = { };
        example = {
          port = 7654;
          logLevel = "DEBUG";
        };
        description = ''
@@ -61,7 +78,17 @@ in
      }
    ];

    systemd.services.autobrr = {
    systemd = {
      tmpfiles.settings = {
        "10-autobrr" = {
          # DynamicUser uses /var/lib/private/
          "/var/lib/private/autobrr/config.toml"."L+" = {
            argument = "${configFile}";
          };
        };
      };

      services.autobrr = {
        description = "Autobrr";
        after = [
          "syslog.target"
@@ -69,18 +96,22 @@ in
        ];
        wants = [ "network-online.target" ];
        wantedBy = [ "multi-user.target" ];
        restartTriggers = [ configFile ];

        serviceConfig = {
          Type = "simple";
          DynamicUser = true;
          LoadCredential = "sessionSecret:${cfg.secretFile}";
          Environment = [ "AUTOBRR__SESSION_SECRET_FILE=%d/sessionSecret" ];
          StateDirectory = "autobrr";
        ExecStartPre = "${lib.getExe pkgs.bash} -c '${templaterCmd}'";
          ExecStart = "${lib.getExe cfg.package} --config %S/autobrr";
          Restart = "on-failure";
        };
      };
    };

    networking.firewall = lib.mkIf cfg.openFirewall { allowedTCPPorts = [ cfg.settings.port ]; };
  };

  meta.maintainers = with lib.maintainers; [ av-gal ];
}
+32 −8
Original line number Diff line number Diff line
@@ -6,18 +6,42 @@

  nodes.machine =
    { pkgs, ... }:
    let
      # We create this secret in the Nix store (making it readable by everyone).
      # DO NOT DO THIS OUTSIDE OF TESTS!!
      testSecretFile = pkgs.writeText "session_secret" "not-secret";
    in
    {
      services.autobrr = {
        enable = true;
        # We create this secret in the Nix store (making it readable by everyone).
        # DO NOT DO THIS OUTSIDE OF TESTS!!
        secretFile = pkgs.writeText "session_secret" "not-secret";
        secretFile = testSecretFile;
      };

      # Use port other than default to test if settings options work.
      specialisation.settingsPort.configuration = {
        services.autobrr = {
          enable = true;
          secretFile = testSecretFile;
          settings.port = 7777;
        };
      };
    };

  testScript = ''
  testScript =
    { nodes, ... }:
    let
      settingsPort = "${nodes.machine.system.build.toplevel}/specialisation/settingsPort";
    in
    # python
    ''
      def test_webui(port):
        machine.wait_for_unit("autobrr.service")
    machine.wait_for_open_port(7474)
    machine.succeed("curl --fail http://localhost:7474/")
        machine.wait_for_open_port(port)
        machine.wait_until_succeeds(f"curl --fail http://localhost:{port}")

      test_webui(7474)

      machine.succeed("${settingsPort}/bin/switch-to-configuration test")
      test_webui(7777)
    '';
}
+9 −5
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
  fetchFromGitHub,
  stdenvNoCC,
  nix-update-script,
  nixosTests,
  nodejs,
  pnpm_10,
  fetchPnpmDeps,
@@ -86,12 +87,15 @@ buildGoModule (finalAttrs: {
  versionCheckProgram = "${placeholder "out"}/bin/autobrrctl";
  versionCheckProgramArg = "version";

  passthru.updateScript = nix-update-script {
  passthru = {
    updateScript = nix-update-script {
      extraArgs = [
        "--subpackage"
        "autobrr-web"
      ];
    };
    tests.testService = nixosTests.autobrr;
  };

  meta = {
    description = "Modern, easy to use download automation for torrents and usenet";