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 applicationId; activityDir = "${cfg.activityPackage}"; tokenFile = "${credDir}/token"; clientSecretFile = "${credDir}/client-secret"; } cfg.extraConfig; configFile = pkgs.writeText "bug-tv-discord-config.json" (builtins.toJSON appConfig); in { options.services.bug-tv-discord = { enable = mkEnableOption "the bug.tv Discord Activity server (static site + watch-party relay)"; package = mkOption { type = types.package; default = self.packages.${system}.default; defaultText = lib.literalExpression "bug-tv-discord.packages.\${system}.default"; description = "The server package."; }; activityPackage = mkOption { type = types.package; default = self.packages.${system}.activity; defaultText = lib.literalExpression "bug-tv-discord.packages.\${system}.activity"; description = "Static Activity files (bug.tv site + overlay) served by the 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."; }; applicationId = mkOption { type = types.str; example = "123456789012345678"; description = "Discord application id (not secret)."; }; tokenFile = mkOption { type = types.path; description = "File holding the Discord bot token, used to check Activity instance membership (passed via systemd LoadCredential)."; }; clientSecretFile = mkOption { type = types.path; description = "File holding the Discord OAuth2 client secret (passed via systemd LoadCredential)."; }; 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.applicationId != ""; message = "services.bug-tv-discord: set applicationId"; } ]; systemd.services.bug-tv-discord = { description = "bug.tv Discord Activity server"; wantedBy = [ "multi-user.target" ]; after = [ "network-online.target" ]; wants = [ "network-online.target" ]; 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}" ]; 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; }; }; }; }