[flakes] Embedding git commit into the binary

What I’m trying to achieve:
Have the git commit SHA baked into the built binary, passed via ldFlags in buildGoModule.

Before flakes:
I used to read the current commit SHA through an ENV var with builtins.getEnv.
This was useful for Github actions and agnostic of git worktrees (bare repo) vs normal repos.
i.e. in a bare repo checkout where the worktree isn’t necessarily laying flat with the .git folder itself, you can’t just read .git/refs/heads/main.

With Flakes
Given flakes are hermetic, the build can’t read the ENV variable.
Any ideas of how to achieve this with flakes? Is anyone else doing something similar?

self.rev

(Post must be at least 20 characters)

1 Like

While self.rev is the correct answer, the whole answer should probably be self.rev or "no clue". The revision attribute only exists when you are in a clean checkout of the repository, and you’ll get errors if you don’t handle this case.

2 Likes

Thanks both! I’m assuming this should always be OK on the CI builds?
Locally I have a different flake output so can just hardcode version = “development”;

Yes, this should always work in CI, but right now self.rev does not get set correctly in some situations.

That happens for me in GitLab’s CI and this issue is the cause:

I have an open PR ready for review that fixes that, though: