Refer to prometheus.yml in NixOS config.nix

Hi,

I’ve setup Prometheus scrapeConfigs in NixOS but now I have to switch from Prometheus to VictoriaMetrics.

VictoriaMetrics uses the same scrape config but I have to pass the prometheus.yml file. This file is built by the Prometheus nix module and placed in the nix store here.

Can I refer to this generated file in the nix store? Something like this:

  services.victoriametrics = {
    enable = true;
    extraOptions = [
      "-promscrape.config=${config.prometheus.prometheusYml}"
    ];
  };

I couldn’t find out myself where/if this prometheusYml is exposed anywhere…

There may be a nicer way but I think you can use what’s in the let block to generate the correct path

I think you could do something like

  services.victoriametrics = {
    enable = true;
    extraOptions = [
      "-promscrape.config=/var/lib/${config.services.prometheus.stateDir}/prometheus-substituted.yaml"
    ];
  };

That should regenerate promethesYmlOut, though I don’t see that used elsewhere in the module so I’m not 100% sure that’ll work. The default stateDir is prometheus2. So maybe check to see if /var/lib/prometheus2/prometheus-substitued.yml exists on your system as a sanity check first.

1 Like

TL;DR

It would have be nice to have NixOS/Prometheus build the scrape config because of syntax checks at nix build time. But it doesn’t work like that: The config file is only built when Prometheus is enabled – however it does not make sense to enable Prometheus and VictoriaMetrics with the same scrape config.

My attempts

Thank you aljenadroooo! I tested your suggestion but turns out that prometheus.yaml is actually in /etc/prometheus/, not /var/lib/ (on my NixOS 22.11). That would be good enough – but see above, I tried to walk the wrong way ^^

I also tested

"-promscrape.config=${config.services.prometheus.environment.etc."prometheus/prometheus.yaml".source}"

from here but that causes

error: attribute 'environment' missing

although services.prometheus.enableReload is true. Don’t know why.

I wonder how to implement such a sanity check in config.nix? Is it something with pathExists and abort?

My solution now

  services.victoriametrics = {
    enable = true;
    extraOptions = let
      scrapeConfigFile = builtins.toFile "prometheus-scrape-config.yml" ''
        # configure scrape targets here
        # https://github.com/VictoriaMetrics/VictoriaMetrics#how-to-scrape-prometheus-exporters-such-as-node-exporter
      '';
    in [
      "-promscrape.config.strictParse=false" # required for victoriametrics to parse the config
      "-promscrape.config=${scrapeConfigFile}"
   ];
  };