From 486f515d4720ae9a4a7790f4cdcb4686efe5bcf5 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Sat, 8 Jun 2024 18:27:31 +0300 Subject: [PATCH 1/4] nixos/alloy: init module This adds a NixOS module for Grafana Alloy. I started from the grafana-agent one but dropped all settings and config management whatsoever. Grafana Alloy uses its own Alloy config format (similar to HCL), which is not really possible to express in Nix. Simply pointing to a path in `/etc`, and leaving it up to the user to configure it via `environment.etc` allows the user to arrange config files however it makes most sense for them. The module, systemd unit etc is called "alloy", not "grafana-alloy" to follow the way it's packaged on other distros, to follow POLA. --- nixos/modules/module-list.nix | 1 + nixos/modules/services/monitoring/alloy.nix | 80 +++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 nixos/modules/services/monitoring/alloy.nix diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix index c8d485f694cc..7758de32dea8 100644 --- a/nixos/modules/module-list.nix +++ b/nixos/modules/module-list.nix @@ -834,6 +834,7 @@ ./services/misc/zoneminder.nix ./services/misc/zookeeper.nix ./services/monitoring/alerta.nix + ./services/monitoring/alloy.nix ./services/monitoring/apcupsd.nix ./services/monitoring/arbtt.nix ./services/monitoring/below.nix diff --git a/nixos/modules/services/monitoring/alloy.nix b/nixos/modules/services/monitoring/alloy.nix new file mode 100644 index 000000000000..e88de1e484e9 --- /dev/null +++ b/nixos/modules/services/monitoring/alloy.nix @@ -0,0 +1,80 @@ +{ lib, pkgs, config, ... }: +with lib; +let + cfg = config.services.alloy; +in +{ + meta = { + maintainers = with maintainers; [ flokli ]; + }; + + 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 + ''; + }; + }; + + + 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"; + }; + }; + }; +} From c12da487556dd0e1dd6e8b98a072932c1f97881a Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Sat, 8 Jun 2024 18:31:12 +0300 Subject: [PATCH 2/4] nixosTests.alloy: init VM test This adds a VM test, starting up Grafana Alloy and ensuring it comes up healthy. --- nixos/tests/all-tests.nix | 1 + nixos/tests/alloy.nix | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 nixos/tests/alloy.nix diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index ddeeeb298c35..f9e0c6d1fefc 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -115,6 +115,7 @@ in { akkoma = handleTestOn [ "x86_64-linux" "aarch64-linux" ] ./akkoma.nix {}; akkoma-confined = handleTestOn [ "x86_64-linux" "aarch64-linux" ] ./akkoma.nix { confined = true; }; alice-lg = handleTest ./alice-lg.nix {}; + alloy = handleTest ./alloy.nix {}; allTerminfo = handleTest ./all-terminfo.nix {}; alps = handleTest ./alps.nix {}; amazon-init-shell = handleTest ./amazon-init-shell.nix {}; diff --git a/nixos/tests/alloy.nix b/nixos/tests/alloy.nix new file mode 100644 index 000000000000..814a21d1e952 --- /dev/null +++ b/nixos/tests/alloy.nix @@ -0,0 +1,32 @@ +import ./make-test-python.nix ({ lib, pkgs, ... }: + + let + nodes = { + machine = { + services.alloy = { + enable = true; + }; + environment.etc."alloy/config.alloy".text = ""; + }; + }; + in + { + name = "alloy"; + + meta = with lib.maintainers; { + maintainers = [ flokli ]; + }; + + inherit nodes; + + testScript = '' + start_all() + + machine.wait_for_unit("alloy.service") + machine.wait_for_open_port(12345) + machine.succeed( + "curl -sSfN http://127.0.0.1:12345/-/healthy" + ) + machine.shutdown() + ''; + }) From aa52f15fff5ea2e2cd5f2bb2326cf3ec02ade422 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Sat, 8 Jun 2024 18:34:51 +0300 Subject: [PATCH 3/4] grafana-alloy: add nixosTests.alloy to passthru --- pkgs/by-name/gr/grafana-alloy/package.nix | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkgs/by-name/gr/grafana-alloy/package.nix b/pkgs/by-name/gr/grafana-alloy/package.nix index 63de413775e6..2ce1bd8f043c 100644 --- a/pkgs/by-name/gr/grafana-alloy/package.nix +++ b/pkgs/by-name/gr/grafana-alloy/package.nix @@ -8,6 +8,7 @@ , fixup-yarn-lock , nodejs , grafana-alloy +, nixosTests , nix-update-script , installShellFiles , testers @@ -103,6 +104,7 @@ buildGoModule rec { passthru = { tests = { + inherit (nixosTests) alloy; version = testers.testVersion { version = "v${version}"; command = "${lib.getExe grafana-alloy} --version"; From 1fa96ce4dff44aab96401322f997fb78d040dc53 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Mon, 10 Jun 2024 13:22:14 +0300 Subject: [PATCH 4/4] grafana-alloy, nixos/alloy: add hbjydev to maintainers --- nixos/modules/services/monitoring/alloy.nix | 2 +- nixos/tests/alloy.nix | 2 +- pkgs/by-name/gr/grafana-alloy/package.nix | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/nixos/modules/services/monitoring/alloy.nix b/nixos/modules/services/monitoring/alloy.nix index e88de1e484e9..abe8fcd7e1be 100644 --- a/nixos/modules/services/monitoring/alloy.nix +++ b/nixos/modules/services/monitoring/alloy.nix @@ -5,7 +5,7 @@ let in { meta = { - maintainers = with maintainers; [ flokli ]; + maintainers = with maintainers; [ flokli hbjydev ]; }; options.services.alloy = { diff --git a/nixos/tests/alloy.nix b/nixos/tests/alloy.nix index 814a21d1e952..d87492127d5b 100644 --- a/nixos/tests/alloy.nix +++ b/nixos/tests/alloy.nix @@ -14,7 +14,7 @@ import ./make-test-python.nix ({ lib, pkgs, ... }: name = "alloy"; meta = with lib.maintainers; { - maintainers = [ flokli ]; + maintainers = [ flokli hbjydev ]; }; inherit nodes; diff --git a/pkgs/by-name/gr/grafana-alloy/package.nix b/pkgs/by-name/gr/grafana-alloy/package.nix index 2ce1bd8f043c..66d6f632bcbe 100644 --- a/pkgs/by-name/gr/grafana-alloy/package.nix +++ b/pkgs/by-name/gr/grafana-alloy/package.nix @@ -121,7 +121,7 @@ buildGoModule rec { mainProgram = "alloy"; license = licenses.asl20; homepage = "https://grafana.com/oss/alloy"; - maintainers = with maintainers; [ flokli emilylange ]; + maintainers = with maintainers; [ flokli emilylange hbjydev ]; platforms = lib.platforms.unix; }; }