2020-09-30 03:42:59 +00:00
|
|
|
# Module that configures CLBot, our Gerrit->IRC info bridge.
|
2022-05-19 14:39:52 +00:00
|
|
|
{ depot, config, lib, pkgs, ... }:
|
2020-09-30 03:42:59 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
inherit (builtins) attrValues concatStringsSep mapAttrs readFile;
|
2022-10-02 21:23:44 +00:00
|
|
|
inherit (pkgs) runCommand;
|
2020-09-30 03:42:59 +00:00
|
|
|
|
|
|
|
inherit (lib)
|
|
|
|
listToAttrs
|
|
|
|
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
|
2022-10-02 21:23:44 +00:00
|
|
|
systemdEscape = name: removeSuffix "\n" (readFile (runCommand "unit-name" { } ''
|
2020-09-30 03:42:59 +00:00
|
|
|
${pkgs.systemd}/bin/systemd-escape '${name}' >> $out
|
|
|
|
''));
|
|
|
|
|
|
|
|
mkUnit = flags: channel: {
|
|
|
|
name = "clbot-${systemdEscape channel}";
|
|
|
|
value = {
|
|
|
|
description = "${description} to ${channel}";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
2022-05-19 14:39:52 +00:00
|
|
|
script = "${depot.fun.clbot}/bin/clbot ${mkFlags (cfg.flags // {
|
2020-09-30 03:42:59 +00:00
|
|
|
irc_channel = channel;
|
|
|
|
})} -alsologtostderr";
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
User = "clbot";
|
2022-05-19 14:39:52 +00:00
|
|
|
EnvironmentFile = cfg.secretsFile;
|
2020-09-30 03:42:59 +00:00
|
|
|
Restart = "always";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2022-05-19 14:39:52 +00:00
|
|
|
in
|
|
|
|
{
|
2020-09-30 03:42:59 +00:00
|
|
|
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; listOf str;
|
|
|
|
description = "Channels in which to post (generates one unit per channel)";
|
|
|
|
};
|
2022-05-19 14:39:52 +00:00
|
|
|
|
|
|
|
secretsFile = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "EnvironmentFile from which to load secrets";
|
|
|
|
default = "/run/agenix/clbot";
|
|
|
|
};
|
2020-09-30 03:42:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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 = {
|
2022-05-19 14:39:52 +00:00
|
|
|
groups.clbot = { };
|
2020-09-30 03:42:59 +00:00
|
|
|
|
|
|
|
users.clbot = {
|
|
|
|
group = "clbot";
|
2022-05-19 14:39:52 +00:00
|
|
|
isSystemUser = true;
|
2020-09-30 03:42:59 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services = listToAttrs (map (mkUnit cfg.flags) cfg.channels);
|
|
|
|
};
|
|
|
|
}
|