nixos/telegraf: add environmentFile option

This commit is contained in:
Jörg Thalheim 2020-08-22 11:31:34 +01:00
parent 9750813b89
commit 157d7354d6
No known key found for this signature in database
GPG Key ID: 003F2096411B5F92

@ -26,6 +26,19 @@ in {
type = types.package;
};
environmentFile = mkOption {
type = types.nullOr types.path;
default = null;
example = "/run/keys/telegraf.env";
description = ''
File to load as environment file. Environment variables
from this file will be interpolated into the config file
using envsubst with this syntax:
<literal>$ENVIRONMENT ''${VARIABLE}</literal>
This is useful to avoid putting secrets into the nix store.
'';
};
extraConfig = mkOption {
default = {};
description = "Extra configuration options for telegraf";
@ -51,15 +64,23 @@ in {
###### implementation
config = mkIf config.services.telegraf.enable {
systemd.services.telegraf = {
systemd.services.telegraf = let
finalConfigFile = if config.services.telegraf.environmentFile == null
then configFile
else "/tmp/config.toml";
in {
description = "Telegraf Agent";
wantedBy = [ "multi-user.target" ];
after = [ "network-online.target" ];
serviceConfig = {
ExecStart=''${cfg.package}/bin/telegraf -config "${configFile}"'';
EnvironmentFile = config.services.telegraf.environmentFile;
ExecStartPre = lib.optional (config.services.telegraf.environmentFile != null)
''${pkgs.envsubst}/bin/envsubst -o /tmp/config.toml -i "${configFile}"'';
ExecStart=''${cfg.package}/bin/telegraf -config ${finalConfigFile}'';
ExecReload="${pkgs.coreutils}/bin/kill -HUP $MAINPID";
User = "telegraf";
Restart = "on-failure";
PrivateTmp = true;
# for ping probes
AmbientCapabilities = [ "CAP_NET_RAW" ];
};