1 { lib, pkgs, config, generators, ... }:
4 cfg = config.services.grafana-agent;
5 settingsFormat = pkgs.formats.yaml { };
6 configFile = settingsFormat.generate "grafana-agent.yaml" cfg.settings;
10 maintainers = with maintainers; [ flokli zimbatm ];
13 options.services.grafana-agent = {
14 enable = mkEnableOption "grafana-agent";
16 package = mkPackageOption pkgs "grafana-agent" { };
18 credentials = mkOption {
20 Credentials to load at service startup. Keys that are UPPER_SNAKE will be loaded as env vars. Values are absolute paths to the credentials.
22 type = types.attrsOf types.str;
26 logs_remote_write_password = "/run/keys/grafana_agent_logs_remote_write_password";
27 LOGS_REMOTE_WRITE_URL = "/run/keys/grafana_agent_logs_remote_write_url";
28 LOGS_REMOTE_WRITE_USERNAME = "/run/keys/grafana_agent_logs_remote_write_username";
29 metrics_remote_write_password = "/run/keys/grafana_agent_metrics_remote_write_password";
30 METRICS_REMOTE_WRITE_URL = "/run/keys/grafana_agent_metrics_remote_write_url";
31 METRICS_REMOTE_WRITE_USERNAME = "/run/keys/grafana_agent_metrics_remote_write_username";
35 extraFlags = mkOption {
36 type = with types; listOf str;
38 example = [ "-enable-features=integrations-next" "-disable-reporting" ];
40 Extra command-line flags passed to {command}`grafana-agent`.
42 See <https://grafana.com/docs/agent/latest/static/configuration/flags/>
48 Configuration for {command}`grafana-agent`.
50 See <https://grafana.com/docs/agent/latest/configuration/>
53 type = types.submodule {
54 freeformType = settingsFormat.type;
58 defaultText = lib.literalExpression ''
61 wal_directory = "\''${STATE_DIRECTORY}";
62 global.scrape_interval = "5s";
66 agent.scrape_integration = true;
67 node_exporter.enabled = true;
72 metrics.global.remote_write = [{
73 url = "\${METRICS_REMOTE_WRITE_URL}";
74 basic_auth.username = "\${METRICS_REMOTE_WRITE_USERNAME}";
75 basic_auth.password_file = "\${CREDENTIALS_DIRECTORY}/metrics_remote_write_password";
84 labels.job = "systemd-journal";
88 source_labels = [ "__journal__systemd_unit" ];
89 target_label = "systemd_unit";
92 source_labels = [ "__journal__hostname" ];
93 target_label = "nodename";
96 source_labels = [ "__journal_syslog_identifier" ];
97 target_label = "syslog_identifier";
102 positions.filename = "\${STATE_DIRECTORY}/loki_positions.yaml";
104 url = "\${LOGS_REMOTE_WRITE_URL}";
105 basic_auth.username = "\${LOGS_REMOTE_WRITE_USERNAME}";
106 basic_auth.password_file = "\${CREDENTIALS_DIRECTORY}/logs_remote_write_password";
113 config = mkIf cfg.enable {
114 services.grafana-agent.settings = {
115 # keep this in sync with config.services.grafana-agent.settings.defaultText.
117 wal_directory = mkDefault "\${STATE_DIRECTORY}";
118 global.scrape_interval = mkDefault "5s";
121 agent.enabled = mkDefault true;
122 agent.scrape_integration = mkDefault true;
123 node_exporter.enabled = mkDefault true;
127 systemd.services.grafana-agent = {
128 wantedBy = [ "multi-user.target" ];
133 # Load all credentials into env if they are in UPPER_SNAKE form.
134 if [[ -n "''${CREDENTIALS_DIRECTORY:-}" ]]; then
135 for file in "$CREDENTIALS_DIRECTORY"/*; do
136 key=$(basename "$file")
137 if [[ $key =~ ^[A-Z0-9_]+$ ]]; then
139 export "$key=$(< "$file")"
144 # We can't use Environment=HOSTNAME=%H, as it doesn't include the domain part.
145 export HOSTNAME=$(< /proc/sys/kernel/hostname)
147 exec ${lib.getExe cfg.package} -config.expand-env -config.file ${configFile} ${escapeShellArgs cfg.extraFlags}
153 SupplementaryGroups = [
154 # allow to read the systemd journal for loki log forwarding
157 StateDirectory = "grafana-agent";
158 LoadCredential = lib.mapAttrsToList (key: value: "${key}:${value}") cfg.credentials;