NixOS; what's in a rebuild? Continued.

Published 2020-09-08 on Farid Zakaria's Blog

This is part 2 of a series on nixos-rebuild. You can read part 1 here.

We previously broke down that one of the first tasks done by nixos-rebuild is to build the system attribute.

What happens next for switch ? Let’s go back to the source.

...
copyToTarget "$pathToConfig"
targetHostCmd nix-env -p "$profile" --set "$pathToConfig"
...

It will attempt to copy the system /nix/store path if a target host is set & also change the profile (/nix/var/nix/profiles/system) to the new system.

❯ tree /nix/var/nix/profiles/
/nix/var/nix/profiles/
...
├── system -> system-18-link
...
├── system-8-link -> /nix/store/3n0563wd5c0jxppaaj2y4nlw2ybvikl3-nixos-system-unnamed-20.03post-git
└── system-9-link -> /nix/store/9y3gp7llvinvra8qcx9n52s0kpvvyzh8-nixos-system-unnamed-20.03post-git

Finally the system is activated; by calling the generated switch-to-configuration script created within the system store entry.

...
if ! targetHostCmd $pathToConfig/bin/switch-to-configuration "$action";
...
❯ tree /nix/store/rpfin018i0s2bdvmsikkrdvd0wvwg287-nixos-system-altaria-20.03post-git/bin
/nix/store/rpfin018i0s2bdvmsikkrdvd0wvwg287-nixos-system-altaria-20.03post-git/bin
└── switch-to-configuration

These 4 tasks; are the basic steps that nixos-rebuild will perform.

  1. Build the system attribute
  2. Copy the /nix/store transitive closure
  3. Set the /nix/var/nix/profiles/system to the new version
  4. Run the switch-to-configuration script

Many1 2 3 have already written about these simple steps; simple scripts can be found on GitHub or even simple wrappers such as nix-simple-deploy; however nixos-rebuild natively supports remote machines.

nixos-rebuild switch --target-host $REMOTE_HOST

Is there a need for these small wrappers given that nixos-rebuild can do it?

There is a simplicity to running the minimal number of commands needed for your workflow; avoiding nixos-rebuild.sh which is ~500 LOC.

Linux 🎮 NixOS

I admitted earlier that I have just recently finally started running NixOS after having run Nix on my Debian system for a while now.

I am only running NixOS on my personal server and not my laptop; which continues to be Debian. The non-NixOS Nix distribution does not come with nixos-rebuild;

I use my laptop primarily for my work which does not support NixOS.

I would like to however continue hacking on my laptop; building NixOS VMs or running nixos-rebuild with --target-host set to my server. Can this be achieved?

With the help of Infinisil; he helped demonstrate how I can easily install not only nixos-rebuild; but even the manpages.

Here is an example of adding the two if you are using home-manager.

home.packages = with pkgs; [
  # I want the NixOS manpages even when not on nixos
  ((import <nixpkgs/nixos> {
    configuration = { };
  }).config.system.build.manual.manpages)
  # I want NixOS tooling even when not on NixOS
  (nixos { }).nixos-rebuild
];

Bonus; the above actually includes all NixOS manpages including configuration.nix

Now I can continue to play around with NixOS even while on my Linux distribution! Hurray?

I would love to dig deeper or understand the active & init scripts within the system target that take care of making NixOS bootable; if you have a good write-up please share!