How to get stack info / module info from builtins or the module system

Hi,

I’m writing an external tool that does nix eval '.#nixosConfigurations.someconfig.config.some.options' --json under the hood. It does that to get some information about nix configurations in my flake. Now, I want to set an option that has the file and line in which that option was set as value. So similar how error messages display in what file and line an error happened. E.g. builtins.abort displays where it was called. The only difference is, that I want that information without exiting and failing the evaluation. Ideally, I’d like to assign it as value to a NixOS option, I defnied.

To give an example: what I’m looking for is <some mechanism> where I can do this:

# File: someconfig.nix (corresponds to the flake outputs.nixosConfigurations.someconfig)
{
  some.options = {
    currFileLine = <some mechanism>; # <--- where <some mechanism> would set currFileLine to `someconfig.nix:4`
    anotherOption = "aValue";
    yetAnotherOption = "anotherValue";
  };
  ...
}

So that nix eval '.#nixosConfigurations.someconfig.config.some.options' --json results in the output:

{
  "currentFile": "someconfig.nix:4",     <--- store path of someconfig.nix would be okay here, too, of course
  "anotherOption": "aValue",
  "yetAnotherOption": "anotherValue"
}

Is that at all possible and if so how? Cloud there be something in _module that might have that info?

It sounds like nixos-option .might already achieve what you want? It offers information on where an option was defined in the module system and where it was declared in a config. I’m not completely certain on how it is implemented, so if you need more, it still might be a good place to check.

1 Like

Nevermind, I found what I needed:

{
  some.options.fileLine = "${__curPos.file}:${__curPos.line}";
}

:partying_face: :piñata: :tada:

But thank you very much @withakay ! If you (or anyone) finds out, do absolutely tell me, please!

Options in the options module argument now have the declarationPositions attribute containing the position where the option was declared:

$ nix-repl -f '<nixpkgs/nixos>' [...]
nix-repl> :p options.environment.systemPackages.declarationPositions
 [ {
   column = 7;
   file = "/nix/store/vm9zf9wvfd628cchj0hdij1g4hzjrcz9-source/nixos/modules/config/system-path.nix";
   line = 62;
 } ]

Not to be confused with definitionsWithLocations

Quote from the changelog.

I feel like I’m falling down a rabbit hole, because, I’ve also discovered the pkgs.lib.options.show* functions, and I have no idea where and if they are documented and what they’re used for.

1 Like