How to check current flake config of NixOS?

I know when running nixos-rebuild switch, the flake repo is copied in to nix store with something like /nix/store/somehashhhhhh-source. How can I find the one corresponding the current state of nixos and let me do a nix repl?

E.g. If I accidentally removed the local flake repo containing nixos setup and I forgot the URL to git clone again for the time being, how can I find the corresponding nixos flake setup in the current system and let me do a nix repl?

Thanks

You can probably find it by checking what /run/current-system depends on
But this will return multiple -source things, one of which is your flake.
EDIT: turns out this isn’t always true

Instead of having to do this, from next time, you can symlink your flake to some location -

{ inputs, ... }:

{
  # place the flake that built the current configuration
  # in /etc/current-flake, for ease-of-use with tools
  # like nixos-enter, in case the flake contains
  # changes that have not been staged yet
  environment.etc."current-flake".source = inputs.self;
}

Which is what I’ve adopted after quite some time c:

No, unless you’ve done something to explicitly link it in, normally the copy of the flake in the nix store will not be in the closure of your system generation.

Interesting, maybe its because I do link it as shown?

Yeah. If you’ve set something like what you posted, then it would show up in the closure.

1 Like

Another useful thing to mention here is that even if you don’t want to keep the flake itself in the closure of your system, perhaps for disk space reasons or something else, you can still keep the traceability by setting:

system.configurationRevision = lib.mkIf (self ? rev) self.rev;

using self presumably passed through specialArgs. (The mkIf is necessary as rev won’t exist when building from a dirty git tree, which you probably want to do sometimes without erroring out.)

This lets you find out the commit of your flake the current system was built with by running nixos-version --configuration-revision. It’s also useful even if you do keep the actual flake contents as it’s not straightforward to determine a commit id that matches a set of files.

I it more like this today:

system.configurationRevision = lib.mkIf (self ? rev || self ? dirtyRev) (self.rev or self.dirtyRev)

This still collects enough of info if you built from dirty, while at the same time will still just ignore the revision if being built from a path flake or whatever.

1 Like

Ah. I didn’t know about dirtyRev, but that is better, yeah. As long as it still clearly marks dirty builds differently than clean ones, which seems to be the case.

1 Like

As a bonus, if you also want to track the version of Nixpkgs you’re using, you could use nix registry for that with the nixpkgs.flake.setFlakeRegistry option! That way commands like nix shell nixpkgs#hello and nix repl nixpkgs refer to the actual inputs.nixpkgs you’ve used for that generation. (it also means nixpkgs will not get garbage collected if the nixos generation that uses it is still alive)