Flake build: how do I get GIT SHA for HEAD?

I wish that my application can inform me what was the GIT SHA of the commit it was build with.

I enter development shell with nix develop and build the application successfully. However, nix build fails. It nails down to cmake executing command git rev-parse HEAD. The comand fails since fatal: not a git repository.

I can locate the source in nix store with nix flake metadata run in the git repo dir. The source in nix store is not a git repo. Not even a shallow clone. Thus git cannot work with the stored source. But flakes are intended to work with version control. Yet I cannot query that version control.

Can I fix it?

1 Like

The git commit hash is in self.rev, where self is the argument to your outputs function in flake.nix.

The rev attribute is only set if your git tree is clean. So you should use it like self.rev or "dirty".