tv-discord: Discord bot + Activity for watching bug.tv together

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
4DBugandClaude Opus 5.5 committed 2026-10-27 08:54:57 -05:00
commit ef7b44f074
36 files changed
+5841

No files matched your search

+67
View File
@@ -0,0 +1,67 @@
{
lib,
buildNpmPackage,
importNpmLock,
nodejs_22,
esbuild,
# bug-tv's static web package: provides js/ui/player.js and vendor/hls.min.js
bugTvWeb,
}:
let
src = lib.fileset.toSource {
root = ../.;
fileset = lib.fileset.unions [
(lib.fileset.difference ../activity (
lib.fileset.unions [
(lib.fileset.maybeMissing ../activity/dist)
(lib.fileset.maybeMissing ../activity/vendor/bugtv)
]
))
(lib.fileset.maybeMissing ../scripts)
../package.json
../package-lock.json
];
};
pkgJson = lib.importJSON ../package.json;
in
buildNpmPackage {
pname = "bug-tv-discord-activity";
inherit (pkgJson) version;
inherit src;
nodejs = nodejs_22;
# same lockfile as the bot; @discord/embedded-app-sdk is resolved from node_modules by esbuild
npmDeps = importNpmLock { npmRoot = src; };
npmConfigHook = importNpmLock.npmConfigHook;
npmFlags = [ "--ignore-scripts" ];
# npm's esbuild needs its postinstall binary download; use nixpkgs' instead
nativeBuildInputs = [ esbuild ];
buildPhase = ''
runHook preBuild
# same layout as a dev checkout: activity/vendor/bugtv/{js/ui/player.js,js/config.js,vendor/hls.min.js}
bash scripts/vendor-bugtv.sh ${bugTvWeb} activity
esbuild activity/js/main.js --bundle --format=esm --target=es2022 \
--minify --sourcemap --outfile=activity/dist/main.js
runHook postBuild
'';
installPhase = ''
runHook preInstall
cp -r activity $out
runHook postInstall
'';
meta = {
description = "bug.tv Discord Activity (static files)";
homepage = "https://git.bug.tools/bug/tv-discord";
platforms = lib.platforms.all;
};
}
+178
View File
@@ -0,0 +1,178 @@
self:
{
config,
lib,
pkgs,
...
}:
let
inherit (lib) mkOption mkEnableOption types;
cfg = config.services.bug-tv-discord;
system = pkgs.stdenv.hostPlatform.system;
credDir = "/run/credentials/bug-tv-discord.service";
appConfig = lib.recursiveUpdate (
{
inherit (cfg) host port apiUrl applicationId guildIds providers;
activityDir = "${cfg.activityPackage}";
tokenFile = "${credDir}/token";
clientSecretFile = "${credDir}/client-secret";
}
// (
if cfg.tmdbApiKeyFile != null then
{ tmdbKeyFile = "${credDir}/tmdb"; }
else
{ tmdbKey = cfg.tmdbApiKey; }
)
) cfg.extraConfig;
configFile = pkgs.writeText "bug-tv-discord-config.json" (builtins.toJSON appConfig);
# ordered after the local bug-tv API when it runs on the same host
bugTv = lib.optional (config.systemd.services ? bug-tv) "bug-tv.service";
in
{
options.services.bug-tv-discord = {
enable = mkEnableOption "the bug.tv Discord bot and Activity server";
package = mkOption {
type = types.package;
default = self.packages.${system}.default;
defaultText = lib.literalExpression "bug-tv-discord.packages.\${system}.default";
description = "The bot package.";
};
activityPackage = mkOption {
type = types.package;
default = self.packages.${system}.activity;
defaultText = lib.literalExpression "bug-tv-discord.packages.\${system}.activity";
description = "Static Activity files served by the bot's HTTP server.";
};
host = mkOption {
type = types.str;
default = "127.0.0.1";
description = "Interface the HTTP/WS server binds to (cloudflared connects locally).";
};
port = mkOption {
type = types.port;
default = 8791;
description = "HTTP/WS server TCP port.";
};
apiUrl = mkOption {
type = types.str;
default = "http://127.0.0.1:8789";
description = "bug-tv API base URL used server-side.";
};
applicationId = mkOption {
type = types.str;
example = "123456789012345678";
description = "Discord application id (not secret).";
};
guildIds = mkOption {
type = types.listOf types.str;
default = [ ];
description = "Guilds to register slash commands in (instant). Empty registers them globally.";
};
tokenFile = mkOption {
type = types.path;
description = "File holding the Discord bot token (passed via systemd LoadCredential).";
};
clientSecretFile = mkOption {
type = types.path;
description = "File holding the Discord OAuth2 client secret (passed via systemd LoadCredential).";
};
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;
description = "File holding the TMDB API key (passed via systemd LoadCredential). Takes precedence over tmdbApiKey.";
};
providers = mkOption {
type = types.listOf types.str;
default = [ ];
description = "bug-tv providers to query. Empty means bug-tv's defaults.";
};
extraConfig = mkOption {
type = types.attrsOf types.json;
default = { };
description = "Extra keys recursively merged last into the generated config JSON.";
};
};
config = lib.mkIf cfg.enable {
assertions = [
{
assertion = cfg.tmdbApiKey != "" || cfg.tmdbApiKeyFile != null;
message = "services.bug-tv-discord: set tmdbApiKey or tmdbApiKeyFile";
}
{
assertion = cfg.applicationId != "";
message = "services.bug-tv-discord: set applicationId";
}
];
systemd.services.bug-tv-discord = {
description = "bug.tv Discord bot and Activity server";
wantedBy = [ "multi-user.target" ];
after = [ "network-online.target" ] ++ bugTv;
wants = [ "network-online.target" ] ++ bugTv;
environment = {
BUG_TV_DISCORD_CONFIG = toString configFile;
NODE_ENV = "production";
};
serviceConfig = {
ExecStart = lib.getExe cfg.package;
LoadCredential = [
"token:${toString cfg.tokenFile}"
"client-secret:${toString cfg.clientSecretFile}"
]
++ lib.optional (cfg.tmdbApiKeyFile != null) "tmdb:${toString cfg.tmdbApiKeyFile}";
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" ];
RestrictNamespaces = true;
RestrictRealtime = true;
RestrictSUIDSGID = true;
LockPersonality = true;
MemoryDenyWriteExecute = false; # node's JIT
SystemCallArchitectures = "native";
CapabilityBoundingSet = "";
AmbientCapabilities = "";
UMask = "0077";
Restart = "always";
RestartSec = 5;
};
};
};
}
+69
View File
@@ -0,0 +1,69 @@
{
lib,
buildNpmPackage,
importNpmLock,
nodejs_22,
makeWrapper,
# linked to lib/bug-tv-discord/activity, config.js's default activityDir
activity ? null,
}:
let
nodejs = nodejs_22;
src = lib.fileset.toSource {
root = ../.;
fileset = lib.fileset.unions [
../src
../package.json
../package-lock.json
];
};
pkgJson = lib.importJSON ../package.json;
in
buildNpmPackage {
pname = "bug-tv-discord";
inherit (pkgJson) version;
inherit src nodejs;
# deps are fetched per-package from package-lock.json, no hash to keep in sync
npmDeps = importNpmLock { npmRoot = src; };
npmConfigHook = importNpmLock.npmConfigHook;
# esbuild's postinstall is only needed for the activity bundle (built with pkgs.esbuild)
npmFlags = [ "--ignore-scripts" ];
dontNpmBuild = true;
nativeBuildInputs = [ makeWrapper ];
installPhase = ''
runHook preInstall
# the activity SDK and esbuild are dev deps; the bot only needs discord.js and ws
npm prune --omit=dev --ignore-scripts --offline --no-audit --no-fund
find node_modules -mindepth 1 -maxdepth 1 -type d -empty -delete
mkdir -p $out/lib/bug-tv-discord
cp -r src node_modules package.json $out/lib/bug-tv-discord/
makeWrapper ${lib.getExe nodejs} $out/bin/bug-tv-discord \
--add-flags $out/lib/bug-tv-discord/src/index.js \
--set-default NODE_ENV production
'' + lib.optionalString (activity != null) ''
ln -s ${activity} $out/lib/bug-tv-discord/activity
'' + ''
runHook postInstall
'';
passthru = { inherit activity; };
meta = {
inherit (pkgJson) description;
homepage = "https://git.bug.tools/bug/tv-discord";
mainProgram = "bug-tv-discord";
platforms = lib.platforms.linux;
};
}