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);
|
2021-12-14 17:06:19 +01:00
|
|
|
|
|
|
|
# Right now, merging configuration file with secrets and running the main
|
|
|
|
# application needs to happen both in ExecStart=, due to
|
|
|
|
# https://github.com/systemd/systemd/issues/19604#issuecomment-989279884
|
2021-12-10 15:10:50 +01:00
|
|
|
mergeAndLaunch = pkgs.writeShellScript "merge-irccat-config" ''
|
|
|
|
if [ ! -f "$CREDENTIALS_DIRECTORY/secrets" ]; 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 15:10:50 +01:00
|
|
|
${pkgs.jq}/bin/jq -s '.[0] * .[1]' ${configJson} "$CREDENTIALS_DIRECTORY/secrets" \
|
2020-11-08 02:41:34 +01:00
|
|
|
> /var/lib/irccat/irccat.json
|
2021-12-10 15:10:50 +01:00
|
|
|
|
|
|
|
exec ${depot.third_party.irccat}/bin/irccat
|
2020-11-08 02:41:34 +01:00
|
|
|
'';
|
2022-01-30 17:06:58 +01:00
|
|
|
in
|
|
|
|
{
|
2020-11-08 02:41:34 +01:00
|
|
|
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";
|
2022-05-22 23:51:49 +02:00
|
|
|
default = config.age.secretsDir + "/irccat";
|
2021-12-10 13:49:11 +01:00
|
|
|
};
|
2020-11-08 02:41:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf cfg.enable {
|
|
|
|
systemd.services.irccat = {
|
|
|
|
inherit description;
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
|
|
|
serviceConfig = {
|
2021-12-10 15:10:50 +01:00
|
|
|
ExecStart = "${mergeAndLaunch}";
|
2020-11-08 02:41:34 +01:00
|
|
|
DynamicUser = true;
|
|
|
|
StateDirectory = "irccat";
|
|
|
|
WorkingDirectory = "/var/lib/irccat";
|
2021-12-10 15:10:50 +01:00
|
|
|
LoadCredential = "secrets:${cfg.secretsFile}";
|
2020-11-08 02:41:34 +01:00
|
|
|
Restart = "always";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|