From 0a5bba0f15bea325bf5d316da979b126e645da67 Mon Sep 17 00:00:00 2001 From: Bouke van der Bijl Date: Mon, 24 Mar 2025 11:27:15 +0100 Subject: [PATCH] nixos/opentelemetry-collector: validate config --- .../manual/release-notes/rl-2511.section.md | 2 + .../monitoring/opentelemetry-collector.nix | 56 +++++++++++-------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/nixos/doc/manual/release-notes/rl-2511.section.md b/nixos/doc/manual/release-notes/rl-2511.section.md index 848fa1eb9b52..8dc6829d7182 100644 --- a/nixos/doc/manual/release-notes/rl-2511.section.md +++ b/nixos/doc/manual/release-notes/rl-2511.section.md @@ -435,3 +435,5 @@ - The `open-webui` package's postgres support have been moved to optional dependencies to comply with upstream changes in 0.6.26. - `prl-tools` has been moved out of `linuxPackages` because Parallels Guest Tools become driverless since 26.1.0. + +- `services.opentelemetry-collector` has a new option `validateConfigFile` option that checks the configuration file during build. It is enabled by default if the configuration file is in the Nix store. diff --git a/nixos/modules/services/monitoring/opentelemetry-collector.nix b/nixos/modules/services/monitoring/opentelemetry-collector.nix index 125dec00b6cf..a722c7a88b5f 100644 --- a/nixos/modules/services/monitoring/opentelemetry-collector.nix +++ b/nixos/modules/services/monitoring/opentelemetry-collector.nix @@ -13,12 +13,27 @@ let mkOption types getExe + isStorePath + literalMD ; cfg = config.services.opentelemetry-collector; opentelemetry-collector = cfg.package; settingsFormat = pkgs.formats.yaml { }; + generatedConf = + if cfg.configFile == null then + settingsFormat.generate "config.yaml" cfg.settings + else + cfg.configFile; + conf = + if cfg.validateConfigFile then + pkgs.runCommandLocal "config.yaml" { inherit generatedConf; } '' + cp $generatedConf $out + ${getExe opentelemetry-collector} validate --config=file:$out + '' + else + generatedConf; in { options.services.opentelemetry-collector = { @@ -43,6 +58,11 @@ in Specify a path to a configuration file that Opentelemetry Collector should use. ''; }; + + validateConfigFile = lib.mkEnableOption "Validate configuration file" // { + defaultText = literalMD "`true` if `configFile` is a store path"; + default = isStorePath cfg.configFile; + }; }; config = mkIf cfg.enable { @@ -61,28 +81,20 @@ in description = "Opentelemetry Collector Service Daemon"; wantedBy = [ "multi-user.target" ]; - serviceConfig = - let - conf = - if cfg.configFile == null then - settingsFormat.generate "config.yaml" cfg.settings - else - cfg.configFile; - in - { - ExecStart = "${getExe opentelemetry-collector} --config=file:${conf}"; - DynamicUser = true; - Restart = "always"; - ProtectSystem = "full"; - DevicePolicy = "closed"; - NoNewPrivileges = true; - WorkingDirectory = "%S/opentelemetry-collector"; - StateDirectory = "opentelemetry-collector"; - SupplementaryGroups = [ - # allow to read the systemd journal for opentelemetry-collector - "systemd-journal" - ]; - }; + serviceConfig = { + ExecStart = "${getExe opentelemetry-collector} --config=file:${conf}"; + DynamicUser = true; + Restart = "always"; + ProtectSystem = "full"; + DevicePolicy = "closed"; + NoNewPrivileges = true; + WorkingDirectory = "%S/opentelemetry-collector"; + StateDirectory = "opentelemetry-collector"; + SupplementaryGroups = [ + # allow to read the systemd journal for opentelemetry-collector + "systemd-journal" + ]; + }; }; }; }