Retrieving nixos-version derivation path from nix

Hi !

I am trying to configure telegraf to send a measurement to influxdb2 with the exec input. The exec.command must include the binary location, otherwise I get a binary not found in PATH error.

To get the path of a binary, I am trying to retrieve the derivation path of it. It is working great with ${pkgs.libraspberrypi}/bin/vcgencmd for example.

I am stuck for few hours now, trying desperately to get the derivation path of the nixos-version binary (defined here?).

I am new to nix, and I don’t understand how I should import the derivation when it is not already accessible though a variable like pkgs. I tried things like import or calling callPackage to mimic what is done with pkgs.* definition, but like everything nix related, if I don’t understand something it does not work ; and as much as it is tempting to just put /run/current-system/sw/bin/nixos-version, I feel like it is not the right way of doing it.

Here is the full file

{
  config,
  pkgs,
  ...
}: {
  services.telegraf = {
    enable = true;
    environmentFiles = [/run/secrets/services/telegraf/env];
    extraConfig = {
      inputs = {
        cpu = {
          percpu = true;
          totalcpu = true;
          collect_cpu_time = false;
          report_active = false;
        };
        disk = {};
        diskio = {};
        mem = {};
        net = {};
        processes = {};
        swap = {};
        system = {};
        temp = {
          name_override = "temp_cpu";
        };
        exec = [
          {
            commands = ["${pkgs.libraspberrypi}/bin/vcgencmd measure_temp"];
            name_override = "temp_gpu";
            data_format = "grok";
            grok_patterns = ["%{NUMBER:value:float}"];
          }
          {
            commands = ["${???}/bin/nixos-version"];
            name_override = "nixos_version";
          }
        ];
      };
      outputs.influxdb_v2 = {
        urls = ["myhost"];
        token = "$INFLUX_TOKEN";
        organization = "myorg";
        bucket = "mybucket";
      };
    };
  };

  users.users.telegraf.extraGroups = ["video"];

  sops.secrets."services/telegraf/env" = {
    key = "services.telegraf.env";
    owner = config.users.users.telegraf.name;
  };
}

the interesting part being commands = ["${???}/bin/nixos-version"];

I hope I didn’t miss something too obvious, thanks in advance for the time you’ll spend helping me.
Alexis

nixos-version is just a shell script cobbled together by a nixos module. It doesn’t expose it externally in any way but putting it in environment.systemPackages, so I believe the only way of accessing it is via /run/current-system/sw/bin/nixos-version as things currently stand.

Vaguely related: Man caches updating after the tiniest changes due to `nixos-version` implementation · Issue #209835 · NixOS/nixpkgs · GitHub

Thank you for your response.

Maybe I’ve taken the problem the wrong way, maybe I can directly use config.system.nixos.version like commands = ["echo ${config.system.nixos.version}"];.

I’ll try later and update here.

Ah, yes, that sounds like a good approach. Just use the same data that nixos uses to create nixos-version in the first place.

I can confirm it is working

          {
            commands = [''${pkgs.bash}/bin/bash -c "echo ${config.system.nixos.version}"''];
            name_override = "nixos_version";
            data_format = "grok";
            grok_patterns = ["%{NUMBER:value:float}"];
          }

not sure if the bash -c "echo is mandatory vs directly using echo, I’ll try later but it does not really matter for the topic.

Thanks for your help