self: { config, lib, pkgs, ... }: let inherit (lib) mkOption mkEnableOption types; cfg = config.services.bug-tv; system = pkgs.stdenv.hostPlatform.system; credDir = "/run/credentials/bug-tv.service"; appConfig = lib.recursiveUpdate ( { inherit (cfg) host port timeoutMs workers cacheTtlSec providerSettings; enabledProviders = cfg.providers; inherit (cfg) disabledProviders; torrent = { inherit (cfg.torrent) enable maxSizeGB; }; remux = { inherit (cfg.remux) enable; }; filters = cfg.filters; } // ( if cfg.tmdbApiKeyFile != null then { tmdbKeyFile = "${credDir}/tmdb"; } else { tmdbKey = cfg.tmdbApiKey; } ) // lib.optionalAttrs (cfg.secretFile != null) { secretFile = "${credDir}/secret"; } ) cfg.extraConfig; configFile = pkgs.writeText "bug-tv-config.json" (builtins.toJSON appConfig); hardening = { DynamicUser = true; NoNewPrivileges = true; PrivateTmp = true; PrivateDevices = true; ProtectSystem = "strict"; ProtectHome = true; ProtectHostname = true; ProtectClock = true; ProtectProc = "invisible"; ProtectKernelTunables = true; ProtectKernelModules = true; ProtectKernelLogs = true; ProtectControlGroups = true; RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" "AF_NETLINK" ]; RestrictNamespaces = true; RestrictRealtime = true; RestrictSUIDSGID = true; LockPersonality = true; MemoryDenyWriteExecute = false; # node's JIT SystemCallArchitectures = "native"; CapabilityBoundingSet = ""; AmbientCapabilities = ""; UMask = "0077"; Restart = "always"; RestartSec = 5; }; in { options.services.bug-tv = { enable = mkEnableOption "the bug.tv multi-provider stream finder API"; package = mkOption { type = types.package; default = self.packages.${system}.api; defaultText = lib.literalExpression "bug-tv.packages.\${system}.api"; description = "The bug-tv API package."; }; host = mkOption { type = types.str; default = "0.0.0.0"; description = "Interface the API binds to."; }; port = mkOption { type = types.port; default = 8789; description = "API TCP port."; }; tmdbApiKey = mkOption { type = types.str; default = ""; description = "TMDB API key. Ends up world-readable in the Nix store; prefer tmdbApiKeyFile."; }; tmdbApiKeyFile = mkOption { type = types.nullOr types.path; default = null; example = "/run/secrets/tmdb"; description = "File holding the TMDB API key (passed via systemd LoadCredential). Takes precedence over tmdbApiKey."; }; secretFile = mkOption { type = types.nullOr types.path; default = null; description = "File holding the token-signing secret, so proxy/remux URLs survive restarts. Random per boot when null."; }; providers = mkOption { type = types.listOf types.str; default = [ ]; description = "Providers used when a request names none. Empty means every bundled provider except `disabledProviders`."; }; disabledProviders = mkOption { type = types.listOf types.str; default = [ "torrentio" ]; description = "Providers left out of the default set when `providers` is empty (torrent/magnet sources by default)."; }; providerSettings = mkOption { type = types.attrsOf types.json; default = { }; description = "Per-provider settings (config key providerSettings)."; }; timeoutMs = mkOption { type = types.ints.positive; default = 20000; description = "Per-provider scrape timeout in milliseconds."; }; workers = mkOption { type = types.ints.unsigned; default = 0; description = "Scrape worker count (0 = automatic)."; }; cacheTtlSec = mkOption { type = types.ints.unsigned; default = 1800; description = "Result cache TTL in seconds."; }; torrent = { enable = mkOption { type = types.bool; default = true; description = "Whether to stream torrents (cached under /var/cache/bug-tv/torrents)."; }; maxSizeGB = mkOption { type = types.ints.positive; default = 20; description = "Torrent cache size limit in GB."; }; }; remux.enable = mkOption { type = types.bool; default = true; description = "Whether to enable ffmpeg remuxing."; }; filters = mkOption { default = { }; description = "Default stream filters."; type = types.submodule { options = { minQuality = mkOption { type = types.str; default = "any"; description = "Minimum quality (e.g. 1080p, or any)."; }; audioCodec = mkOption { type = types.str; default = "any"; description = "Required audio codec, or any."; }; subtitles = mkOption { type = types.str; default = "any"; description = "Subtitle requirement (any/required/none)."; }; subtitleLang = mkOption { type = types.str; default = ""; description = "Required subtitle language, empty for any."; }; }; }; }; extraConfig = mkOption { type = types.attrsOf types.json; default = { }; description = "Extra keys recursively merged last into the generated config.json."; }; web = { enable = mkEnableOption "the bug.tv static web frontend (static-web-server)"; port = mkOption { type = types.port; default = 8790; description = "Web frontend TCP port (binds to services.bug-tv.host)."; }; apiBase = mkOption { type = types.str; default = "https://api-tv.bug.tools"; description = "API base URL baked into the frontend."; }; package = mkOption { type = types.package; default = self.packages.${system}.web.override { inherit (cfg.web) apiBase; }; defaultText = lib.literalExpression "bug-tv.packages.\${system}.web.override { inherit (cfg.web) apiBase; }"; description = "The web frontend package (a directory of static files)."; }; }; }; config = lib.mkMerge [ (lib.mkIf cfg.enable { assertions = [ { assertion = cfg.tmdbApiKey != "" || cfg.tmdbApiKeyFile != null; message = "services.bug-tv: set tmdbApiKey or tmdbApiKeyFile"; } ]; systemd.services.bug-tv = { description = "bug.tv stream finder API"; wantedBy = [ "multi-user.target" ]; after = [ "network-online.target" ]; wants = [ "network-online.target" ]; environment = { BUG_TV_CONFIG = toString configFile; BUG_TV_CACHE_DIR = "/var/cache/bug-tv/torrents"; NODE_ENV = "production"; }; serviceConfig = hardening // { ExecStart = lib.getExe cfg.package; CacheDirectory = "bug-tv"; WorkingDirectory = "/var/cache/bug-tv"; LoadCredential = lib.optional (cfg.tmdbApiKeyFile != null) "tmdb:${toString cfg.tmdbApiKeyFile}" ++ lib.optional (cfg.secretFile != null) "secret:${toString cfg.secretFile}"; }; }; }) (lib.mkIf cfg.web.enable { systemd.services.bug-tv-web = { description = "bug.tv web frontend"; wantedBy = [ "multi-user.target" ]; after = [ "network.target" ]; serviceConfig = hardening // { ExecStart = lib.escapeShellArgs [ (lib.getExe pkgs.static-web-server) "--host" cfg.host "--port" (toString cfg.web.port) "--root" "${cfg.web.package}" "--page-fallback" "${cfg.web.package}/index.html" "--compression" "true" "--health" "true" "--log-level" "info" ]; RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" ]; MemoryDenyWriteExecute = true; }; }; }) ]; }