main
  1{
  2  lib,
  3  pkgs,
  4  config,
  5  ...
  6}: let
  7  cfg = config.services.sub-store;
  8in {
  9  options.services.sub-store = {
 10    enable = lib.mkEnableOption "Enable sub-store, an advanced subscription manager";
 11
 12    package = lib.mkPackageOption pkgs "sub-store" {};
 13
 14    address = lib.mkOption {
 15      default = "127.0.0.1";
 16      type = lib.types.str;
 17      description = "Host the sub-store backend listen on";
 18    };
 19
 20    port = lib.mkOption {
 21      default = 3000;
 22      type = lib.types.port;
 23      description = "Port the sub-store backend listen on";
 24    };
 25
 26    frontend = {
 27      enable = lib.mkEnableOption "Enable sub-store's local web frontend";
 28      package = lib.mkPackageOption pkgs "sub-store-frontend" {};
 29      address = lib.mkOption {
 30        default = "127.0.0.1";
 31        type = lib.types.str;
 32        description = "Host the sub-store frontend listen on";
 33      };
 34      port = lib.mkOption {
 35        default = 3001;
 36        type = lib.types.port;
 37        description = "Port the sub-store frontend listen on";
 38      };
 39      backendPath = lib.mkOption {
 40        default = "/2cXaAxRGfddmGz2yx1wA";
 41        type = lib.types.str;
 42        description = "Path of backend, see <https://hub.docker.com/r/xream/sub-store/>";
 43      };
 44    };
 45
 46    environment = {
 47      extra = lib.mkOption {
 48        default = {};
 49        type = lib.types.attrsOf lib.types.envVar;
 50        description = "Extra environment variables to pass to sub-store systemd service";
 51      };
 52      file = lib.mkOption {
 53        default = null;
 54        type = lib.types.nullOr lib.types.path;
 55        description = "Environment file to pass to sub-store systemd service";
 56      };
 57    };
 58  };
 59
 60  config = lib.mkIf (cfg.enable) {
 61    systemd.services.sub-store = {
 62      enable = cfg.enable;
 63      after = ["network-online.target"];
 64      wants = ["network-online.target"];
 65      wantedBy = ["multi-user.target"];
 66      environment =
 67        {
 68          SUB_STORE_DATA_BASE_PATH = "%S/sub-store";
 69          SUB_STORE_BACKEND_API_HOST = cfg.address;
 70          SUB_STORE_BACKEND_API_PORT = toString cfg.port;
 71        }
 72        // (lib.optionalAttrs (cfg.frontend.enable) {
 73          SUB_STORE_FRONTEND_PATH = "${cfg.frontend.package}";
 74          SUB_STORE_FRONTEND_HOST = cfg.frontend.address;
 75          SUB_STORE_FRONTEND_BACKEND_PATH = cfg.frontend.backendPath;
 76        })
 77        // (
 78          if (cfg.port == cfg.frontend.port)
 79          then {
 80            SUB_STORE_BACKEND_MERGE = "true";
 81          }
 82          else {
 83            SUB_STORE_FRONTEND_PORT = toString cfg.frontend.port;
 84          }
 85        )
 86        // cfg.environment.extra;
 87
 88      serviceConfig =
 89        {
 90          StateDirectory = "sub-store";
 91          StateDirectoryMode = "0700";
 92          ExecStart = "${lib.getExe cfg.package}";
 93          Restart = "on-failure";
 94          DynamicUser = true;
 95        }
 96        // (lib.optionalAttrs (cfg.environment.file != null) {
 97          EnvironmentFile = cfg.environment.file;
 98        });
 99    };
100  };
101}