Introspect nix-shell?


#1

From inside a running nix-shell, is there a way to inspect the derivation that launched it?

For example, if I launched it from a file, I could check the version attr with:

nix-instantiate --eval --attr version /path/to/file

But that’s assuming I know /path/to/file, and assuming there even is a file. I could’ve launched the shell using --packages instead…

Is there a way you can reliably reference the shell’s derivation from within that shell?


#2

No. Derivation know nothing about expression it was generated from.

However, not all is lost. For example, many things are exposed as envvar!

$ # $version would work too if it had been defined 
$ nix-shell '<nixpkgs>' -A mercurial --run 'echo $name'
mercurial-4.7.1

mkDerivation attributes are exposed as envvars. Like this:

$ nix-shell -p mercurial git --run 'echo $buildInputs'
/nix/store/0x53zl7psb03352nhwlpyd8bqvqsd97b-mercurial-4.7.1 /nix/store/qcn9ck2zfk9bj5m3g1jv0lcc9yicjq2j-git-2.19.1

I’d wish realized derivation know it’s originating .drv file. In some cases it is possible to get this information from nix DB:

$ nix-shell -p sqlite

[nix-shell] $ sudo sqlite3 "/nix/var/nix/db/db.sqlite" \
      "select deriver from ValidPaths where path = '$buildInputs'"
/nix/store/x5g0bbjf00x8944mna1rwj1bpjk95rjv-sqlite-3.24.0.drv

And drv can be inspected:

$ nix-shell -p jq
$ nix show-derivation /nix/store/x5g0bbjf00x8944mna1rwj1bpjk95rjv-sqlite-3.24.0.drv \
  | jq ....

Unfortunately, nix DB doesn’t contain all backwards mappings store path -> derivation, so this method isn’t reliable too.


#3

Awesome! I totally overlooked that, and it’s a lot simpler to just echo $version