about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--ops/nixos/clbot.nix49
-rw-r--r--ops/nixos/whitby/default.nix5
2 files changed, 41 insertions, 13 deletions
diff --git a/ops/nixos/clbot.nix b/ops/nixos/clbot.nix
index adcbebd57fd0..0c45badd2b3e 100644
--- a/ops/nixos/clbot.nix
+++ b/ops/nixos/clbot.nix
@@ -2,26 +2,59 @@
 { config, lib, pkgs, ... }:
 
 let
-  inherit (builtins) concatStringsSep attrValues mapAttrs;
+  inherit (builtins) attrValues concatStringsSep mapAttrs readFile;
+  inherit (pkgs) runCommandNoCC;
+
   inherit (lib)
+    listToAttrs
     mkEnableOption
     mkIf
     mkOption
+    removeSuffix
     types;
 
-  description = "CLBot forwards Gerrit notifications to IRC";
+  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 (runCommandNoCC "unit-name" {} ''
+    ${pkgs.systemd}/bin/systemd-escape '${name}' >> $out
+  ''));
+
+  mkUnit = flags: channel: {
+    name = "clbot-${systemdEscape channel}";
+    value = {
+      description = "${description} to ${channel}";
+      wantedBy = [ "multi-user.target" ];
+
+      script = "${config.depot.fun.clbot}/bin/clbot ${mkFlags (cfg.flags // {
+        irc_channel = channel;
+      })} -alsologtostderr";
+
+      serviceConfig = {
+        User = "clbot";
+        EnvironmentFile = "/etc/secrets/clbot";
+        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; listOf str;
+      description = "Channels in which to post (generates one unit per channel)";
+    };
   };
 
   config = mkIf cfg.enable {
@@ -37,16 +70,6 @@ in {
       };
     };
 
-    systemd.services.clbot = {
-      inherit description;
-      script = "${config.depot.fun.clbot}/bin/clbot ${mkFlags cfg.flags} -alsologtostderr";
-      wantedBy = [ "multi-user.target" ];
-
-      serviceConfig = {
-        User = "clbot";
-        EnvironmentFile = "/etc/secrets/clbot";
-        Restart = "always";
-      };
-    };
+    systemd.services = listToAttrs (map (mkUnit cfg.flags) cfg.channels);
   };
 }
diff --git a/ops/nixos/whitby/default.nix b/ops/nixos/whitby/default.nix
index 456dac8a46d4..c05cb324f5ec 100644
--- a/ops/nixos/whitby/default.nix
+++ b/ops/nixos/whitby/default.nix
@@ -218,6 +218,11 @@ in lib.fix(self: {
       # populated from /etc/secrets/clbot
       irc_pass = "$CLBOT_PASS";
     };
+
+    channels = [
+      "##tvl"
+      "##tvl-dev"
+    ];
   };
 
   services.depot = {