Check, in a performant way, if nixos-rebuild changes are necessary

I’d like to check - in a script - whether nixos-rebuild switch (or boot or whatever) actually does something, and in a performant kind of way. Is this possible at all? I know about dry-build and dry-activate, but those take time (around 10 seconds on my machine). Is there anything faster that just outputs a boolean value?

1 Like

The minimum amount of work that would need to be done is computing a hash over all inputs to the system configuration to see if anything changed. In nix, that is equivalent to computing the deviation or the out path (i.e. the directory your system will be built in). You can compute the out path as follows:

echo '(import <nixpkgs/nixos> {}).system.outPath' | nix repl

You’d then need to compare that with the path of the current system (readlink -f /run/current-system). I don’t think that’s much faster than nixos-rebuild dry-run though.

1 Like

dry-build also checks what paths are available from the binary cache and what needs to be built, so you can save this time by just evaluating the system closure as shown above. Evaluating still takes quite a bit of time though.

nix eval '(expr here)' seems fine too (parens required).

Oh, forgot about nix eval. There’s also --raw which strips the quotes, so

nix eval --raw '(import <nixpkgs/nixos> {}).system.outPath'

should be much more convenient for scripting.

1 Like

Thanks for the tips guys!