nixos/loki: skip config validation when it's impossible to validate

This is a follow-up to 8d7f3c9dbd and
ae48735c53.

Running the config validation in the build sandbox is impossible and
will fail when using `cfg.configFile` or `-config.expand-env=true`.

`cfg.configFile` is a string of a path which is simply not available to
the build sandbox.

Similarly, one may opt to use `cfg.configuration` with environment
variables in combination with `-config.expand-env=true`.

The environment variables referenced that way are also not available
in the build sandbox.

So we skip the validation when it's impossible (`cfg.configFile`) or
likely impossible (`-config.expand-env=true`).

An alternative approach would be something like nixos/prometheus'
`services.prometheus.checkConfig` that takes a boolean and makes
toggling the config validation user-facing.
This commit is contained in:
emilylange 2024-05-19 04:47:36 +02:00
parent 68ead29211
commit 100c1501e1
No known key found for this signature in database
GPG Key ID: 0AD773CE46FD0F87

View File

@ -97,7 +97,12 @@ in {
serviceConfig = let
conf = if cfg.configFile == null
then prettyJSON cfg.configuration
then
# Config validation may fail when using extraFlags = [ "-config.expand-env=true" ].
# To work around this, we simply skip it when extraFlags is not empty.
if cfg.extraFlags == []
then validateConfig (prettyJSON cfg.configuration)
else prettyJSON cfg.configuration
else cfg.configFile;
validateConfig = file:
pkgs.runCommand "validate-loki-conf" {
@ -108,7 +113,7 @@ in {
'';
in
{
ExecStart = "${cfg.package}/bin/loki --config.file=${validateConfig conf} ${escapeShellArgs cfg.extraFlags}";
ExecStart = "${cfg.package}/bin/loki --config.file=${conf} ${escapeShellArgs cfg.extraFlags}";
User = cfg.user;
Restart = "always";
PrivateTmp = true;