2020-07-01 01:15:47 +02:00
|
|
|
# Module that configures CLBot, our Gerrit->IRC info bridge.
|
2021-04-02 14:18:50 +02:00
|
|
|
{ depot, config, lib, pkgs, ... }:
|
2020-07-01 01:15:47 +02:00
|
|
|
|
|
|
|
let
|
2020-08-17 23:03:08 +02:00
|
|
|
inherit (builtins) attrValues concatStringsSep mapAttrs readFile;
|
2022-09-26 19:33:05 +02:00
|
|
|
inherit (pkgs) runCommand;
|
2020-08-17 23:03:08 +02:00
|
|
|
|
2020-07-01 01:15:47 +02:00
|
|
|
inherit (lib)
|
2020-08-17 23:03:08 +02:00
|
|
|
listToAttrs
|
2020-07-01 01:15:47 +02:00
|
|
|
mkEnableOption
|
|
|
|
mkIf
|
|
|
|
mkOption
|
2020-08-17 23:03:08 +02:00
|
|
|
removeSuffix
|
2020-07-01 01:15:47 +02:00
|
|
|
types;
|
|
|
|
|
2020-08-17 23:03:08 +02:00
|
|
|
description = "Bot to forward CL notifications";
|
2020-07-01 01:15:47 +02:00
|
|
|
cfg = config.services.depot.clbot;
|
|
|
|
|
|
|
|
mkFlags = flags:
|
|
|
|
concatStringsSep " "
|
|
|
|
(attrValues (mapAttrs (key: value: "-${key} \"${toString value}\"") flags));
|
2020-08-17 23:03:08 +02:00
|
|
|
|
|
|
|
# Escapes a unit name for use in systemd
|
2022-09-26 19:33:05 +02:00
|
|
|
systemdEscape = name: removeSuffix "\n" (readFile (runCommand "unit-name" { } ''
|
2020-08-17 23:03:08 +02:00
|
|
|
${pkgs.systemd}/bin/systemd-escape '${name}' >> $out
|
|
|
|
''));
|
|
|
|
|
|
|
|
mkUnit = flags: channel: {
|
|
|
|
name = "clbot-${systemdEscape channel}";
|
|
|
|
value = {
|
|
|
|
description = "${description} to ${channel}";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
2021-04-02 14:18:50 +02:00
|
|
|
script = "${depot.fun.clbot}/bin/clbot ${mkFlags (cfg.flags // {
|
2020-08-17 23:03:08 +02:00
|
|
|
irc_channel = channel;
|
|
|
|
})} -alsologtostderr";
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
User = "clbot";
|
2021-12-10 07:58:16 +01:00
|
|
|
EnvironmentFile = cfg.secretsFile;
|
2020-08-17 23:03:08 +02:00
|
|
|
Restart = "always";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2020-07-01 01:15:47 +02:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.depot.clbot = {
|
|
|
|
enable = mkEnableOption description;
|
2020-08-17 23:03:08 +02:00
|
|
|
|
2020-07-01 01:15:47 +02:00
|
|
|
flags = mkOption {
|
|
|
|
type = types.attrsOf types.str;
|
|
|
|
description = "Key value pairs for command line flags";
|
|
|
|
};
|
2020-08-17 23:03:08 +02:00
|
|
|
|
|
|
|
channels = mkOption {
|
|
|
|
type = with types; listOf str;
|
|
|
|
description = "Channels in which to post (generates one unit per channel)";
|
|
|
|
};
|
2021-12-10 07:58:16 +01:00
|
|
|
|
|
|
|
secretsFile = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "EnvironmentFile from which to load secrets";
|
2022-05-22 23:51:49 +02:00
|
|
|
default = config.age.secretsDir + "/clbot";
|
2021-12-10 07:58:16 +01:00
|
|
|
};
|
2020-07-01 01:15:47 +02: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 = {
|
|
|
|
groups.clbot = { };
|
|
|
|
|
|
|
|
users.clbot = {
|
|
|
|
group = "clbot";
|
2021-04-13 12:21:42 +02:00
|
|
|
isSystemUser = true;
|
2020-07-01 01:15:47 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-08-17 23:03:08 +02:00
|
|
|
systemd.services = listToAttrs (map (mkUnit cfg.flags) cfg.channels);
|
2020-07-01 01:15:47 +02:00
|
|
|
};
|
|
|
|
}
|