-
-
Notifications
You must be signed in to change notification settings - Fork 80
Description
The --build-host option in nh does not behave the same way as in nixos-rebuild, which makes it confusing and arguably useless in its current state.
Behavior in nh
When using --build-host, nh simply forwards the host to nix build --builders:
Line 195 in 8bf3234
.builder(self.build_host.clone()) Lines 664 to 666 in 8bf3234
Some(host) => { vec!["--builders".to_string(), format!("ssh://{host} - - - 100")] }, Lines 813 to 815 in 8bf3234
if let Some(ref builders) = self.builders { args.push("--builders".into()); args.push(builders.clone());
This means that the SSH connection is initiated by the nix-daemon (running as root), in a non-interactive environment.
As a consequence, if the SSH key used to connect to the builder requires a passphrase, it must already be unlocked and available in root’s SSH agent. Since nix-daemon does not have access to the user’s SSH agent, this often fails unless you use a passphrase-less key or preload it into an agent that root can access.
See also: nix.dev distributed builds
Behavior in nixos-rebuild
In contrast, nixos-rebuild’s --build-host works differently:
- It opens an SSH connection as the user running the command, in an interactive environment (so it has access to the user’s SSH agent, or the user can even type the passphrase if necessary).
- It then transfers the system closure to the build host via that connection and launches the build remotely.
Relevant code:
- https://github.com/NixOS/nixpkgs/blob/b71d5fb26af9c12377192522fc79f89e50554c22/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/nix.py#L165-L177
- https://github.com/NixOS/nixpkgs/blob/b71d5fb26af9c12377192522fc79f89e50554c22/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/process.py#L111-L122
Why this is problematic
- In
nh,--build-hostis effectively just an alias for--builders. - This is misleading, because it gives the impression that it behaves like
nixos-rebuild --build-host, when in reality it does not. - For many users, this makes
nh --build-hostunusable unless they set up special conditions (e.g. SSH keys without passphrases available toroot).
Conclusion
There are two possible ways forward:
- Remove the
--build-hostoption fromnhto avoid confusion. - Reimplement it so that it matches the expected semantics from
nixos-rebuild, i.e. opening the SSH connection as the user in an interactive environment, transferring the system closure, and building remotely.
Related discussions
nh os switch --build-host <HOST>still builds locally #308 (comment)--buildersdoes not use identities from ssh-agent when key has a passphrase NixOS/nix#14125 (comment)
Maintainers
Add 👍 to issues you find important.