This commit is contained in:
4DBug committed 2026-10-27 05:49:38 -05:00
1 parent bcf05e8063
commit f37985b4c8
109 files changed
+22810 -6143

No files matched your search

+275
View File
@@ -0,0 +1,275 @@
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;
};
};
})
];
}
+62
View File
@@ -0,0 +1,62 @@
{
lib,
buildNpmPackage,
importNpmLock,
nodejs,
ffmpeg-headless,
makeWrapper,
}:
let
root = ../.;
src = lib.fileset.toSource {
inherit root;
fileset = lib.fileset.unions [
../src
../providers
../package.json
../package-lock.json
];
};
pkgJson = lib.importJSON ../package.json;
in
assert lib.assertMsg (lib.versionAtLeast nodejs.version "22") "bug-tv needs node >= 22";
buildNpmPackage {
pname = "bug-tv";
inherit (pkgJson) version;
inherit src nodejs;
# no lockfile hash to keep in sync: deps are fetched per-package from package-lock.json
npmDeps = importNpmLock { npmRoot = src; };
npmConfigHook = importNpmLock.npmConfigHook;
# webtorrent's optional native deps (bufferutil, utf-8-validate, node-datachannel) are not needed
npmFlags = [ "--ignore-scripts" ];
dontNpmBuild = true;
nativeBuildInputs = [ makeWrapper ];
installPhase = ''
runHook preInstall
mkdir -p $out/lib/bug-tv
cp -r src providers node_modules package.json $out/lib/bug-tv/
makeWrapper ${lib.getExe nodejs} $out/bin/bug-tv \
--add-flags $out/lib/bug-tv/src/server.js \
--prefix PATH : ${lib.makeBinPath [ ffmpeg-headless ]}
runHook postInstall
'';
meta = {
description = pkgJson.description;
homepage = "https://git.bug.tools/bug/tv";
mainProgram = "bug-tv";
platforms = lib.platforms.linux;
};
}
+42
View File
@@ -0,0 +1,42 @@
{
lib,
stdenvNoCC,
# baked into <meta name="bug-tv-api" content="..."> of every top-level page
apiBase ? "https://api-tv.bug.tools",
}:
let
sedExpr = ''s#(<meta[[:space:]]+name="bug-tv-api"[[:space:]]+content=")[^"]*(")#\1${
lib.escape [ "\\" "&" "#" ] apiBase
}\2#'';
in
stdenvNoCC.mkDerivation {
pname = "bug-tv-web";
version = (lib.importJSON ../package.json).version;
src = ../web;
dontConfigure = true;
dontBuild = true;
installPhase = ''
runHook preInstall
cp -r . $out
chmod -R u+w $out
# no-op on pages without the tag
for page in $out/*.html; do
sed -i -E ${lib.escapeShellArg sedExpr} "$page"
done
runHook postInstall
'';
passthru = { inherit apiBase; };
meta = {
description = "bug.tv static web frontend (API: ${apiBase})";
platforms = lib.platforms.all;
};
}