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

+58 -206
View File
@@ -1,228 +1,80 @@
{
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
description = "bug.tv: multi-provider stream finder API + web frontend";
outputs = { nixpkgs, ... }:
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
outputs =
{ self, nixpkgs, ... }:
let
lib = nixpkgs.lib;
inherit (nixpkgs) lib;
systems = [ "x86_64-linux" "aarch64-linux" ];
pkgsFor = system: nixpkgs.legacyPackages.${system};
appFor = pkgs:
let
src = lib.fileset.toSource {
root = ./.;
fileset = lib.fileset.unions [
./rest
./mod
./providers
./package.json
./package-lock.json
];
};
npmDeps = pkgs.fetchNpmDeps {
name = "bug-tv-npm-deps";
inherit src;
hash = "sha256-XzEx5eCnlY99OzURnVNcZalYg69APIT/LCchklPlHJg=";
};
in
pkgs.stdenvNoCC.mkDerivation {
pname = "bug-tv";
version = "0.1.0";
inherit src npmDeps;
nativeBuildInputs = [ pkgs.nodejs pkgs.npmHooks.npmConfigHook ];
dontConfigure = true;
dontBuild = true;
installPhase = ''
mkdir -p $out/lib/bug-tv $out/bin
cp -r rest mod providers package.json node_modules $out/lib/bug-tv/
printf '%s\n' \
'#!${pkgs.bash}/bin/bash' \
"exec ${pkgs.nodejs}/bin/node \"$out/lib/bug-tv/rest/server.js\" \"\$@\"" \
> $out/bin/bug-tv
chmod 755 $out/bin/bug-tv
'';
};
forAllSystems = f: lib.genAttrs systems (system: f nixpkgs.legacyPackages.${system});
in
{
packages = lib.genAttrs systems (system: {
default = appFor (pkgsFor system);
packages = forAllSystems (pkgs: rec {
api = pkgs.callPackage ./nix/package.nix { };
# web.override { apiBase = "https://..."; }
web = pkgs.callPackage ./nix/web.nix { };
default = api;
});
devShells = lib.genAttrs systems (system: {
default =
let pkgs = pkgsFor system;
in pkgs.mkShell {
packages = [ pkgs.nodejs pkgs.nodejs-npm ];
};
apps = forAllSystems (pkgs: {
default = {
type = "app";
program = lib.getExe self.packages.${pkgs.stdenv.hostPlatform.system}.api;
meta.description = "Run the bug.tv API (reads ./config.json or $BUG_TV_CONFIG)";
};
});
nixosModules.default =
{ config, lib, pkgs, ... }:
devShells = forAllSystems (pkgs: {
default = pkgs.mkShell {
packages = [ pkgs.nodejs pkgs.ffmpeg-headless ];
};
});
formatter = forAllSystems (pkgs: pkgs.nixfmt);
checks = forAllSystems (
pkgs:
let
cfg = config.services."bug-tv";
baseFilters = {
minQuality = cfg.filters.minQuality;
audioCodec = cfg.filters.audioCodec;
subtitles = cfg.filters.subtitles;
subtitleLang = cfg.filters.subtitleLang;
};
baseConfig = {
tmdbKey = cfg.tmdbApiKey;
enabledProviders = cfg.providers;
timeoutMs = cfg.timeoutMs;
apiHost = cfg.host;
apiPort = cfg.port;
filters = baseFilters;
};
mergedConfig =
(baseConfig // (lib.removeAttrs cfg.extraConfig [ "filters" ]))
// lib.optionalAttrs (cfg.extraConfig ? "filters") { filters = baseFilters // cfg.extraConfig.filters; };
configJson = pkgs.writeText "bug-tv-config.json" (builtins.toJSON mergedConfig);
app = pkgs.runCommand "bug-tv" { preferLocalBuild = true; } ''
mkdir -p $out
for path in rest mod providers package.json node_modules; do
cp -a ${cfg.package}/lib/bug-tv/''${path} $out/
done
cp ${configJson} $out/config.json
mkdir -p $out/bin
printf '%s\n' \
'#!${pkgs.bash}/bin/bash' \
"exec ${pkgs.nodejs}/bin/node \"$out/rest/server.js\" \"\$@\"" \
> $out/bin/bug-tv
chmod 755 $out/bin/bug-tv
'';
inherit (self.packages.${pkgs.stdenv.hostPlatform.system}) api web;
in
{
options.services."bug-tv" = lib.mkOption {
default = { };
type = lib.types.submodule {
options = {
enable = lib.mkEnableOption "the bug.tv multi-provider stream finder API";
inherit api web;
package = lib.mkOption {
description = "The bug-tv package to run.";
type = lib.types.package;
};
port = lib.mkOption {
description = "TCP port to listen on.";
type = lib.types.port;
default = 8789;
};
host = lib.mkOption {
description = "Interface to bind.";
type = lib.types.str;
default = "0.0.0.0";
};
tmdbApiKey = lib.mkOption {
description = "TMDB API key used for search and details lookups.";
type = lib.types.str;
default = "";
};
providers = lib.mkOption {
description = "Providers enabled by default when none are requested.";
type = lib.types.listOf lib.types.str;
default = [ "cineby" ];
};
timeoutMs = lib.mkOption {
description = "Per-provider scrape timeout in milliseconds.";
type = lib.types.int;
default = 30000;
};
filters = lib.mkOption {
description = "Default stream filters.";
default = { };
type = lib.types.submodule {
options = {
minQuality = lib.mkOption {
description = "Minimum quality (e.g. 1080p, or any).";
type = lib.types.str;
default = "any";
};
audioCodec = lib.mkOption {
description = "Required audio codec, or any.";
type = lib.types.str;
default = "any";
};
subtitles = lib.mkOption {
description = "Subtitle requirement (any/required/none).";
type = lib.types.str;
default = "any";
};
subtitleLang = lib.mkOption {
description = "Required subtitle language, empty for any.";
type = lib.types.str;
default = "";
};
};
};
};
extraConfig = lib.mkOption {
description = "Extra keys merged into the generated config.json.";
type = lib.types.attrsOf lib.types.json;
default = { };
};
};
};
};
config = lib.mkIf cfg.enable {
assertions = [
smoke =
pkgs.runCommand "bug-tv-smoke"
{
assertion = cfg.tmdbApiKey != "";
message = "services.bug-tv.tmdbApiKey must be set";
nativeBuildInputs = [ pkgs.nodejs pkgs.curl pkgs.findutils ];
}
{
assertion = cfg.providers != [ ];
message = "services.bug-tv.providers must not be empty";
}
];
''
cd ${api}/lib/bug-tv
find src -name '*.js' -print0 | xargs -0 -n1 node --check
users.users."bug-tv" = {
isSystemUser = true;
group = "bug-tv";
};
port=$((20000 + RANDOM % 20000))
cat > $TMPDIR/config.json <<JSON
{ "tmdbKey": "dummy", "host": "127.0.0.1", "port": $port }
JSON
export HOME=$TMPDIR BUG_TV_CONFIG=$TMPDIR/config.json BUG_TV_CACHE_DIR=$TMPDIR/cache
users.groups."bug-tv" = { };
${lib.getExe api} > $TMPDIR/server.log 2>&1 &
pid=$!
ok=
for _ in $(seq 60); do
if curl -fsS "http://127.0.0.1:$port/healthz"; then ok=1; break; fi
kill -0 $pid 2>/dev/null || break
sleep 0.5
done
kill $pid 2>/dev/null || true
cat $TMPDIR/server.log
[ -n "$ok" ] || { echo "server did not answer /healthz"; exit 1; }
touch $out
'';
}
);
systemd.services."bug-tv" = {
description = "bug.tv stream finder API";
wantedBy = [ "multi-user.target" ];
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
serviceConfig = {
User = "bug-tv";
Group = "bug-tv";
ExecStart = "${app}/bin/bug-tv";
Restart = "always";
RestartSec = 5;
NoNewPrivileges = true;
PrivateTmp = true;
ProtectSystem = "strict";
ProtectHome = true;
LockPersonality = true;
};
};
};
};
nixosModules.default = import ./nix/module.nix self;
nixosModules.bug-tv = self.nixosModules.default;
};
}