2021-04-02 14:18:50 +02:00
|
|
|
{ depot, config, lib, pkgs, ... }:
|
2020-11-08 02:41:34 +01:00
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.depot.irccat;
|
|
|
|
description = "irccat - forward messages to IRC";
|
|
|
|
|
|
|
|
# irccat expects to read its configuration from the *current
|
|
|
|
# directory*, and its configuration contains secrets.
|
|
|
|
#
|
|
|
|
# To make this work we construct the JSON configuration file and
|
|
|
|
# then recursively merge it with an on-disk secret using jq on
|
|
|
|
# service launch.
|
|
|
|
configJson = pkgs.writeText "irccat.json" (builtins.toJSON cfg.config);
|
|
|
|
configMerge = pkgs.writeShellScript "merge-irccat-config" ''
|
2021-12-10 13:49:11 +01:00
|
|
|
if [ ! -f "${cfg.secretsFile}" ]; then
|
2020-11-08 02:41:34 +01:00
|
|
|
echo "irccat secrets file is missing"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# jq's * is the recursive merge operator
|
2021-12-10 13:49:11 +01:00
|
|
|
${pkgs.jq}/bin/jq -s '.[0] * .[1]' ${configJson} ${cfg.secretsFile} \
|
2020-11-08 02:41:34 +01:00
|
|
|
> /var/lib/irccat/irccat.json
|
|
|
|
'';
|
|
|
|
in {
|
|
|
|
options.services.depot.irccat = {
|
|
|
|
enable = lib.mkEnableOption description;
|
|
|
|
|
|
|
|
config = lib.mkOption {
|
|
|
|
type = lib.types.attrs; # varying value types
|
|
|
|
description = "Configuration structure (unchecked!)";
|
|
|
|
};
|
2021-12-10 13:49:11 +01:00
|
|
|
|
|
|
|
secretsFile = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "Path to the secrets file to be merged";
|
|
|
|
default = "/run/agenix/irccat";
|
|
|
|
};
|
2020-11-08 02:41:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf cfg.enable {
|
|
|
|
systemd.services.irccat = {
|
|
|
|
inherit description;
|
|
|
|
preStart = "${configMerge}";
|
2021-04-02 14:18:50 +02:00
|
|
|
script = "${depot.third_party.irccat}/bin/irccat";
|
2020-11-08 02:41:34 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
DynamicUser = true;
|
2021-12-10 13:49:11 +01:00
|
|
|
Group = "irccat";
|
2020-11-08 02:41:34 +01:00
|
|
|
StateDirectory = "irccat";
|
|
|
|
WorkingDirectory = "/var/lib/irccat";
|
|
|
|
Restart = "always";
|
|
|
|
};
|
|
|
|
};
|
2021-12-10 13:49:11 +01:00
|
|
|
|
|
|
|
# Create a real group to grant access to secrets to.
|
|
|
|
users.groups.irccat = {};
|
2020-11-08 02:41:34 +01:00
|
|
|
};
|
|
|
|
}
|