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 (lib.mdDoc "telegraf server");
17 default = pkgs.telegraf;
18 defaultText = literalExpression "pkgs.telegraf";
19 description = lib.mdDoc "Which telegraf derivation to use";
23 environmentFiles = mkOption {
24 type = types.listOf types.path;
26 example = [ "/run/keys/telegraf.env" ];
27 description = lib.mdDoc ''
28 File to load as environment file. Environment variables from this file
29 will be interpolated into the config file using envsubst with this
30 syntax: `$ENVIRONMENT` or `''${VARIABLE}`.
31 This is useful to avoid putting secrets into the nix store.
35 extraConfig = mkOption {
37 description = lib.mdDoc "Extra configuration options for telegraf";
38 type = settingsFormat.type;
41 urls = ["http://localhost:8086"];
42 database = "telegraf";
45 service_address = ":8125";
46 delete_timings = true;
55 config = mkIf config.services.telegraf.enable {
56 systemd.services.telegraf = let
57 finalConfigFile = if config.services.telegraf.environmentFiles == []
59 else "/var/run/telegraf/config.toml";
61 description = "Telegraf Agent";
62 wantedBy = [ "multi-user.target" ];
63 after = [ "network-online.target" ];
65 EnvironmentFile = config.services.telegraf.environmentFiles;
66 ExecStartPre = lib.optional (config.services.telegraf.environmentFiles != [])
67 (pkgs.writeShellScript "pre-start" ''
69 ${pkgs.envsubst}/bin/envsubst -i "${configFile}" > /var/run/telegraf/config.toml
71 ExecStart="${cfg.package}/bin/telegraf -config ${finalConfigFile}";
72 ExecReload="${pkgs.coreutils}/bin/kill -HUP $MAINPID";
73 RuntimeDirectory = "telegraf";
76 Restart = "on-failure";
78 AmbientCapabilities = [ "CAP_NET_RAW" ];
82 users.users.telegraf = {
83 uid = config.ids.uids.telegraf;
85 description = "telegraf daemon user";
88 users.groups.telegraf = {};