Passing Git commit hash and tag to build with Flakes

It’s helpful to have a build ID compiled into the binary that can be included as part of the binary’s outputs. For example, a C++ program that runs FEA simulations might include the latest git tag and git commit hash that it was built from to help identify itself.

Prior to Nix Flakes, we did this by having Meson run git describe --always and saving the output to a file which was included into the compiled binary.

This isn’t possible with Nix Flakes since builds are done in a sandbox where all of the git metadata is gone.

Is there a new suggested way to do pass this information into the binary? This is a useful feature to have and does not affect reproducibility.

4 Likes

You can use

if (self ? rev) then self.rev else "dirty"

to determine the revision of the flake or an alternative string if the flake is dirty.

Don’t know how to aquire other git metadate though.

6 Likes

Thank you, it would be nice to acquire other git metadata but self.rev in combination with builtins.substring is good enough for now. Thanks again!

1 Like

If your tree is clean, self.shortRev is also exposed. So no need for substrings.
Additionally, sourceInfo (on all flakes) exposes added “metadata” you might be interested in:

  • lastModified
  • lastModifiedDate
  • narHash
  • outPath
  • rev
  • revCount
  • shortRev
  • submodules
2 Likes

Shorter yet, self.rev or "dirty"

4 Likes

Hi,
Is there anyway to grab the git tagged name instead of the commit hash? self does not seem to include this info.