about summary refs log tree commit diff
path: root/nixos/modules/services/monitoring/alloy.nix
blob: abe8fcd7e1beb70d60f9f71491a16516de48cc74 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
{ lib, pkgs, config, ... }:
with lib;
let
  cfg = config.services.alloy;
in
{
  meta = {
    maintainers = with maintainers; [ flokli hbjydev ];
  };

  options.services.alloy = {
    enable = mkEnableOption "Grafana Alloy";

    package = mkPackageOption pkgs "grafana-alloy" { };

    configPath = mkOption {
      type = lib.types.path;
      default = "/etc/alloy";
      description = ''
        Alloy configuration file/directory path.

        We default to `/etc/alloy` here, and expect the user to configure a
        configuration file via `environment.etc."alloy/config.alloy"`.

        This allows config reload, contrary to specifying a store path.
        A `reloadTrigger` for `config.alloy` is configured.

        Other `*.alloy` files in the same directory (ignoring subdirs) are also
        honored, but it's necessary to manually extend
        `systemd.services.alloy.reloadTriggers` to enable config reload
        during nixos-rebuild switch.

        This can also point to another directory containing `*.alloy` files, or
        a single Alloy file in the Nix store (at the cost of reload).

        Component names must be unique across all Alloy configuration files, and
        configuration blocks must not be repeated.

        Alloy will continue to run if subsequent reloads of the configuration
        file fail, potentially marking components as unhealthy depending on
        the nature of the failure. When this happens, Alloy will continue
        functioning in the last valid state.
      '';
    };

    extraFlags = mkOption {
      type = with lib.types; listOf str;
      default = [ ];
      example = [ "--server.http.listen-addr=127.0.0.1:12346" "--disable-reporting" ];
      description = ''
        Extra command-line flags passed to {command}`alloy run`.

        See <https://grafana.com/docs/alloy/latest/reference/cli/run/>
      '';
    };
  };


  config = mkIf cfg.enable {
    systemd.services.alloy = {
      wantedBy = [ "multi-user.target" ];
      reloadTriggers = [ config.environment.etc."alloy/config.alloy".source or null ];
      serviceConfig = {
        Restart = "always";
        DynamicUser = true;
        RestartSec = 2;
        SupplementaryGroups = [
          # allow to read the systemd journal for loki log forwarding
          "systemd-journal"
        ];
        ExecStart = "${lib.getExe cfg.package} run ${cfg.configPath} ${escapeShellArgs cfg.extraFlags}";
        ExecReload = "${pkgs.coreutils}/bin/kill -SIGHUP $MAINPID";
        ConfigurationDirectory = "alloy";
        StateDirectory = "alloy";
        WorkingDirectory = "%S/alloy";
        Type = "simple";
      };
    };
  };
}