Files
tv-discord/nix/module.nix
T

179 lines
5.2 KiB
Nix

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;
};
};
};
}