diary at Telent Netowrks

NixOS copy closure#

Sat, 20 Aug 2022 22:09:10 +0000

Breaking blog silence with a short entry, because maybe short entries are easier to write than the long ones I have signally failed to write at any time so far in 2022.

Scenario: you have a computer running NixOS which is too small or too slow for you to really enjoy running nixos-rebuild on, especially if you think it might start trying to build things instead of just copying prebuilt binaries from elsewhere.

(I do not know why, but none of my computers ever seem able to install notmuch without building from source. But that's not important right now)

How can you build its configuration.nix on another machine and then copy everything across? You do something like this:

$ nix-build -E "(import <nixpkgs> {}); (pkgs.nixos (import ./configuration.nix)).config.system.build.toplevel"
$ nix-copy-closure --to root@${TARGET} --include-outputs ./result \
 && ssh root@${TARGET} `readlink result`/bin/switch-to-configuration switch

What's it doing?

  1. build the toplevel derivation (the same one as is used by nixos-rebuild
  2. copy it and anything else it needs to the target system using the magic of nix-copy-closure
  3. run switch-to-configuration on the target system, which does the imperative setup - making files in /etc and /boot and all that stuff - that nixos-rebuild does when you ask it to switch

But Daniel, why is this interesting to you? Do you have such small computers? Yes, reader, I have two mobile phones[*] and one of them - albeit not yet one of them that I'm actually using for telephony or communication purposes - is almost running NixOS

[*] technically 3 but one of them doesn't recognise its SIM any more after I dropped it in the toilet. Until recently I had a Pinephone too, but we parted ways citing creative differences