1 { config, lib, pkgs, ... }:
6 cfg = config.services.telegraf;
8 settingsFormat = pkgs.formats.toml {};
9 configFile = settingsFormat.generate "config.toml" cfg.extraConfig;
14 enable = mkEnableOption "telegraf server";
16 package = mkPackageOption pkgs "telegraf" { };
18 environmentFiles = mkOption {
19 type = types.listOf types.path;
21 example = [ "/run/keys/telegraf.env" ];
23 File to load as environment file. Environment variables from this file
24 will be interpolated into the config file using envsubst with this
25 syntax: `$ENVIRONMENT` or `''${VARIABLE}`.
26 This is useful to avoid putting secrets into the nix store.
30 extraConfig = mkOption {
32 description = "Extra configuration options for telegraf";
33 type = settingsFormat.type;
36 urls = ["http://localhost:8086"];
37 database = "telegraf";
40 service_address = ":8125";
41 delete_timings = true;
50 config = mkIf config.services.telegraf.enable {
51 services.telegraf.extraConfig = {
55 systemd.services.telegraf = let
56 finalConfigFile = if config.services.telegraf.environmentFiles == []
58 else "/var/run/telegraf/config.toml";
60 description = "Telegraf Agent";
61 wantedBy = [ "multi-user.target" ];
62 wants = [ "network-online.target" ];
63 after = [ "network-online.target" ];
64 path = lib.optional (config.services.telegraf.extraConfig.inputs ? procstat) pkgs.procps
65 ++ lib.optional (config.services.telegraf.extraConfig.inputs ? ping) pkgs.iputils;
67 EnvironmentFile = config.services.telegraf.environmentFiles;
68 ExecStartPre = lib.optional (config.services.telegraf.environmentFiles != [])
69 (pkgs.writeShellScript "pre-start" ''
71 ${pkgs.envsubst}/bin/envsubst -i "${configFile}" > /var/run/telegraf/config.toml
73 ExecStart="${cfg.package}/bin/telegraf -config ${finalConfigFile}";
74 ExecReload="${pkgs.coreutils}/bin/kill -HUP $MAINPID";
75 RuntimeDirectory = "telegraf";
78 Restart = "on-failure";
80 AmbientCapabilities = [ "CAP_NET_RAW" ];
84 users.users.telegraf = {
85 uid = config.ids.uids.telegraf;
87 description = "telegraf daemon user";
90 users.groups.telegraf = {};