NixOS: config flake store path for /run/current-system

Is there a way to get the flake store path that was used to generate a system, e.g. the one pointed to by /run/current-system and others?

Specifically I mean the store path that you can get from nix flake metadata in the flake repo itself.

I’m trying to write some tooling around giving myself a reminder to update the flake (to keep nixpkgs up to date), and currently I just look at the flake.lock file in the checked out flake repo itself, but that only tracks whether the flake repo has been updated, but not whether it’s been applied or not, so I’d like to be able to inspect the lock file that was stored in the Nix store when the system was built.

My first thought was to use nix show-derivation -r /run/current-system and parse the output, but I don’t seem to be able to find the source path. It’s very likely I’m looking in the wrong part of the output, but I’m hoping someone can point me in the right direction.

Thanks!

Not by default, but you can include a reference to it somewhere in the generation by interpolating self into a string that makes it somewhere you can find: "${self}"

1 Like

Oh good point. I could just put it in a file that gets dropped into /etc or similar. I’m trying to make things more complicated than they have to be.

Thanks!

Did you ever write that tooling? Stumbled upon your post when researching for how to do the same tooling and don’t want to reinvent the wheel.

Yes, but it’s nothing amazing. Originally I had some shell scripts, then rewrote it in Crystal just to play around with that language, and finally rewrote it in Rust to get some experience with that language. Here’s the repo if you want to look at the code and try to figure out what it’s doing. I haven’t touched it in a few years (it just works for me) and while I made an effort to document the code, it might not be the most idiomatic.

Basically, I have this code in my host configuration:

# Drop a link to the current system configuration flake in to /etc.
# That way we can tell what configuration built the current
# system version.
environment.etc."current-system-flake".source = inputs.self;

Since that’s the contents of the flake repo that was used to generate the system, it has a flake.lock file in it, and I parse that and use the last updated timestamp for the “nixpkgs” input as a reference for how old it is.

Then I have a check command that compares that to the current date, and prints a message based off of that, and I have that in my shell login script.

Thank you for the link. I Tool a Look at the Code and I will propably reuse the idea.

For anyone who plans to use this - do note that depending on self like this will cause rebuilds anytime any file in the flake is changed, even if it’s just a reformatting/refactoring that would otherwise not imply a rebuild.

Thanks, that’s good to know. I can see how that might be a problem for some setups even though it hasn’t been a problem for me yet.