noisebell/remote/discord-bot/module.nix
2026-03-10 17:29:29 -07:00

81 lines
2.2 KiB
Nix

self:
{ config, lib, ... }:
let
cfg = config.services.noisebell-discord;
bin = "${self.packages.x86_64-linux.default}/bin/noisebell-discord";
in
{
options.services.noisebell-discord = {
enable = lib.mkEnableOption "noisebell Discord bot";
domain = lib.mkOption {
type = lib.types.str;
description = "Domain for the Caddy virtual host.";
};
port = lib.mkOption {
type = lib.types.port;
default = 3001;
};
discordTokenFile = lib.mkOption {
type = lib.types.path;
description = "Path to file containing the Discord bot token.";
};
channelId = lib.mkOption {
type = lib.types.str;
description = "Discord channel ID to post messages to.";
};
webhookSecretFile = lib.mkOption {
type = lib.types.path;
description = "Path to file containing the webhook secret.";
};
};
config = lib.mkIf cfg.enable {
users.users.noisebell-discord = {
isSystemUser = true;
group = "noisebell-discord";
};
users.groups.noisebell-discord = {};
services.caddy.virtualHosts.${cfg.domain}.extraConfig = ''
reverse_proxy localhost:${toString cfg.port}
'';
systemd.services.noisebell-discord = {
description = "Noisebell Discord bot";
wantedBy = [ "multi-user.target" ];
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
environment = {
NOISEBELL_DISCORD_PORT = toString cfg.port;
NOISEBELL_DISCORD_CHANNEL_ID = cfg.channelId;
RUST_LOG = "info";
};
script = ''
export NOISEBELL_DISCORD_TOKEN="$(cat ${cfg.discordTokenFile})"
export NOISEBELL_DISCORD_WEBHOOK_SECRET="$(cat ${cfg.webhookSecretFile})"
exec ${bin}
'';
serviceConfig = {
Type = "simple";
Restart = "on-failure";
RestartSec = 5;
User = "noisebell-discord";
Group = "noisebell-discord";
NoNewPrivileges = true;
ProtectSystem = "strict";
ProtectHome = true;
PrivateTmp = true;
ProtectKernelTunables = true;
ProtectKernelModules = true;
ProtectControlGroups = true;
RestrictSUIDSGID = true;
};
};
};
}