Flakes: accessing self's revision?

How do I access the revision for “self” in a flake?

I want to extend system.nixos.label to include the git hash of the repo that it was built from, which doesn’t seem to be a thing by default (or am I missing something? I thought maybe there was some sort of support for getting “Back to the flake” from a system toplevel, but I can’t find that now.)

It seems like inputs.self.sourceInfo looks like this, and lacks the rev info (and this seems to be the same for different inputs too, that are locked from git repos). I was sure that there’s some way to get to this, right? Maybe I’ve previously accessed it through flake-compat since it parses the lock file?

3 Likes

Don’t you look for this?

1 Like

As far as I can tell that’s not available. It wasn’t there when I was evaluating and when I try to use it in my config it’s just null. For some reason it’s not there even though it seems like it ought to be.

self.rev is only non-null if your working tree is clean. You can use the nix command line flag --no-allow-dirty to prevent yourself from building from a dirty worktree. See also Nix Flakes, Part 3: Managing NixOS systems

5 Likes

Is there a way to get the git rev of the flake anyway? Even if it is dirty, it might be useful to have something like “1f421ec-dirty”.

1 Like

No, not at the moment, see e.g. Should a flake's `self.rev` return something useful in modified repos? · Issue #4682 · NixOS/nix · GitHub.

2 Likes

You can now get exactly that with self.dirtyShortRev.

What I find myself using is if (self ? rev) then self.rev else self.dirtyRev.

Sorry for reviving an old conversation. Maybe you’ve found this answer since you posted! But since this thread came up in my searches on the subject today, I thought it was worth adding a note for anyone else who follows in my footsteps.

3 Likes

Yeah thanks, I use this in my dotfiles:

toString (self.shortRev or self.dirtyShortRev or self.lastModified or "unknown")

Now I have to find out, why nix falls back to lastModified when my flake.nix is in a subdirectory of my larger git repo. It works when I softlink .git into inside the flake.nix dir, but stuff like GIT_DIR seem to mess with nix’s detection of the (dirty)ShortRev.

2 Likes