tvl-depot/ops/modules/clbot.nix
Profpatsch 9559ef56e3 feat(fun/clbot,ops/machines/whitby): filter tvix-dev clbot
In #tvix-dev, we want to display only CLs that relate to tvix and
related projects.

So use a pretty dumb allow-list for which CLs to display in that
channel.

Change-Id: I3ef50b64e3d7fbc27a6690be6a10f1b55c04cd6e
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11658
Reviewed-by: flokli <flokli@flokli.de>
Reviewed-by: lukegb <lukegb@tvl.fyi>
Tested-by: BuildkiteCI
2024-06-03 19:35:34 +00:00

83 lines
2.2 KiB
Nix

# Module that configures CLBot, our Gerrit->IRC info bridge.
{ depot, config, lib, pkgs, ... }:
let
inherit (builtins) attrValues concatStringsSep mapAttrs readFile;
inherit (pkgs) runCommand;
inherit (lib)
listToAttrs
mapAttrsToList
mkEnableOption
mkIf
mkOption
removeSuffix
types;
description = "Bot to forward CL notifications";
cfg = config.services.depot.clbot;
mkFlags = flags:
concatStringsSep " "
(attrValues (mapAttrs (key: value: "-${key} \"${toString value}\"") flags));
# Escapes a unit name for use in systemd
systemdEscape = name: removeSuffix "\n" (readFile (runCommand "unit-name" { } ''
${pkgs.systemd}/bin/systemd-escape '${name}' >> $out
''));
mkUnit = channel: channelFlags: {
name = "clbot-${systemdEscape channel}";
value = {
description = "${description} to ${channel}";
wantedBy = [ "multi-user.target" ];
script = "${depot.fun.clbot}/bin/clbot ${mkFlags (cfg.flags // channelFlags // {
irc_channel = channel;
})} -alsologtostderr";
serviceConfig = {
User = "clbot";
EnvironmentFile = cfg.secretsFile;
Restart = "always";
};
};
};
in
{
options.services.depot.clbot = {
enable = mkEnableOption description;
flags = mkOption {
type = types.attrsOf types.str;
description = "Key value pairs for command line flags";
};
channels = mkOption {
type = with types; attrsOf (attrsOf str);
description = "Channels in which to post (generates one unit per channel); nested attrs are used as extra flags to the service, which override the attrs in `flags`";
};
secretsFile = mkOption {
type = types.str;
description = "EnvironmentFile from which to load secrets";
default = config.age.secretsDir + "/clbot";
};
};
config = mkIf cfg.enable {
# This does not use DynamicUser because we need to make some files
# (notably the SSH private key) readable by this user outside of
# the module.
users = {
groups.clbot = { };
users.clbot = {
group = "clbot";
isSystemUser = true;
};
};
systemd.services = listToAttrs (mapAttrsToList mkUnit cfg.channels);
};
}