Recently, I’ve been trying to understand just what exactly is happening if I am doing nixos-rebuild switch
.
The relevant source files are
- nixos-rebuild bash script: nixpkgs/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh at 63dacb46bf939521bdc93981b4cbb7ecb58427a0 · NixOS/nixpkgs · GitHub
- switch-to-configuration perl script: nixpkgs/nixos/modules/system/activation/switch-to-configuration.pl at 63dacb46bf939521bdc93981b4cbb7ecb58427a0 · NixOS/nixpkgs · GitHub (FYI, there’s now a Rust version of it as well)
- systemd-boot-builder Python script nixpkgs/nixos/modules/system/boot/loader/systemd-boot/systemd-boot-builder.py at 63dacb46bf939521bdc93981b4cbb7ecb58427a0 · NixOS/nixpkgs · GitHub
My current understanding is that roughly the following happens during switch:
- First,
nix build
is run to, roughly, create a./result
file system tree with everything that is a part of a new version of the system. - Then,
./result/bin/switch-to-configuration switch
. This is beautiful: we are using the binary from the freshly build system, running under the old system, to assimilate it. That switch-to-configuration does two things (well, maybe it does more things, it’s fairer to say that I understand two things):
- poke systemd to essentially live-reload the system, using the new stuff
- run that bootloader-updating script to persist the new configuration in the bootloader
Now we are coming close to the actual question I have — my understanding is the source of truth for the bootloader script are the nix-env
profiles. So, before running the script we should add a corresponding symlink
under /nix/var/nix/profiles/
using nix-env -p /nix/var/nix/profiles/system --set ./result
.
It seems to me that that’s basically the single place in my system where I use nix-env
, as I don’t use profiles anywhere else, because I don’t like how stateful nix-env
is. Is there some way to install the bootloader bypassing the profiles infrastruture? Perhaps using existing /boot/loader/entries
and explicit CLI arguments to reconstruct the state?