NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
17.46k stars 13.66k forks source link

Add `--target-host` to `nixos-generate-config` and `nixos-install` #217891

Open roberth opened 1 year ago

roberth commented 1 year ago

Project description

Adding a --target-host option (like nixos-rebuild has) to these tools will make remote installations much easier, and allows ad hoc installation on lightweight hardware that may not be big enough to evaluate its config.

Example work flow:

  1. Write an installer image and boot it on the target host
  2. Prepare and mount the file systems
  3. nixos-generate-config --target-host HOST --root /mnt --dir .
    • connects with ssh to HOST
    • runs the local copy of nixos-generate-config with --root /mnt --dir $tmp
    • copies the files back to the local directory from --dir. --dir is mandatory when --target-host is specified
  4. Write a flake locally to import that configuration.nix into nixosConfigurations.host
  5. nixos-install --target-host HOST --root /mnt --flake .#host
    • builds the configuration locally
    • copies a closure including nixos-install script. nixos-install need not be part of toplevel for this to work!
    • invokes the installation process over ssh

Nice to have:

Majiir commented 1 year ago

As a workaround, I have done this manually:

  1. nixos-rebuild build --flake <flake>
  2. nix-copy-closure --to <host> result
  3. (on remote) nixos-install --system <system-closure>

Of course, that requires nixos-install on the target!

roberth commented 1 year ago

I think that's nixos-rebuild build in (1)?

Of course, that requires nixos-install on the target!

Ideally we shouldn't even need nixos-rebuild on the target. I think it only needs to copy the closure, set the profile and call activate. On the target, nix should suffice, even just a store-only one:

Such minimalism is up to the final configuration though. It seems like a good tradeoff to keep some of these commands on the installation media configurations:

  • runs the local copy of nixos-generate-config