This guide is based on the great nixos-utm helper.
Before starting ensure you have UTM installed with nix or homebrew.
sed is different in different platforms. Following steps only work on MacOS.
# Create new nix configuration for UTM vm
$ nix flake new -t github:ciderale/nixos-utm my-utm-vm
$ cd my-utm-vm
# Generate new passwordless ssh key to be used just for this VM
$ ssh-keygen -t ed25519 -f ~/.ssh/utm-vm-nixos-builder -P "" -C "$USER@utm-vm-nixos-builder"
# Replace the VM ssh key
$ sed -i'' -e "/ssh-ed25519/ s/.*/ \"$(cat ~/.ssh/utm-vm-nixos-builder.pub)\"/" configuration.nix
# Enable rosetta in the VM
$ sed -i '' '/services.openssh.enable = true;/i\
\ \ virtualisation.rosetta.enable = true;\
' configuration.nix
# Run the UTM installer
VM_NAME=nixos nix run github:ciderale/nixos-utm#nixosCreate .#utmAvahi enables you want your host machine to find the VM with utm-vm-nixos-builder.local address.
You can enable it by adding following lines to the configuration.nix
{
networking.hostName = "utm-vm-nixos-builder";
services = {
avahi = {
enable = true;
nssmdns4 = true;
openFirewall = true;
publish = {
enable = true;
userServices = true;
addresses = true;
};
};
};
}According to this great guide nix will use root user for the remote builds and thus the root user needs to trust the builder VM.
It's probably easiest to just symlink your own ssh known hosts to the root user:
sudo mkdir /var/root/.ssh
sudo ln -s ~/.ssh/known_hosts /var/root/.ssh/Then add the x86_64-linux capable remote builder to your nix config
mkdir -p ~/.config/nix/
echo -e "\nbuilders = ssh://root@utm-vm-nixos-builder.local?ssh-key=$HOME/.ssh/utm-vm-nixos-builder x86_64-linux" >> ~/.config/nix/nix.confAfter you have done all of this steps you should be able to use the remote builder in VM:
nix build --impure --expr '(with import <nixpkgs> { system = "x86_64-linux"; }; runCommand "foo" {} "uname > $out")'
cat resultIf the file outputs Linux everything is working properly.
VM_NAME=nixos nixos nix run github:ciderale/nixos-utm#nixosIPssh root@$(VM_NAME=nixos nix run github:ciderale/nixos-utm#nixosIP)nix run nixpkgs#nixos-rebuild -- switch --fast --flake .#utm --target-host root@utm-vm-nixos-builder.local --build-host root@utm-vm-nixos-builder.localISC