nixos-anywhere
repository·main·Indexed 25 days ago
https://github.com/nix-community/nixos-anywhereA tool for installing NixOS on remote machines via SSH. It automates disk partitioning and formatting using disko, handles software installation, and supports unattended provisioning for cloud servers, bare metal, and local LAN servers. Features include kexec booting for installers, Terraform modules for deployment management, and the ability to copy extra files to the target host.
What's inside nixos-anywhere
- nixos-anywhere is a tool that allows you to pre-configure the entire NixOS installation process and execute the installation remotely over SSH with a single CLI command.
Overview of nixos-anywhere
mainnixos-anywhere is a tool used to install NixOS on remote machines via SSH. It automates the entire provisioning process, including disk partitioning, formatting, NixOS installation, and the installation of additional software or files. It is designed for unattended installations, meaning once the command is initiated, the process runs automatically without manual intervention.
Key capabilities:
- Connect to remote servers via SSH.
- Use
kexecto boot into a NixOS installer if one is not present. - Use disko for disk partitioning and formatting.
- Install Nix packages and additional files.
- Support for cloud servers, bare metal (e.g., Hetzner), and local LAN servers.
Overview of NixOS-Anywhere Terraform Modules
mainThe
nixos-anywhereTerraform modules enable the use of Terraform to install and update NixOS. There are four primary modules available depending on your deployment needs:- All-in-One: A consolidated module that performs an initial installation using
nixos-anywhereand subsequently manages updates usingnixos-rebuild. This is a complete replacement for other deployment tools like Colmena. - Install: A specialized module dedicated exclusively to the initial installation of NixOS via
nixos-anywhere. - NixOS-Rebuild: A module designed for remote updates of existing NixOS machines using
nixos-rebuild. - Nix-Build: A helper module used to build a Nix flake attribute or an attribute from a Nix file.
- All-in-One: A consolidated module that performs an initial installation using
Configure storage with Disko
mainDefine your disk layout in adisk-config.nixfile using the disko tool. This file tellsnixos-anywherehow to partition, format, and mount disks. Ensure the device path (e.g.,/dev/sdaor/dev/nvme0n1) matches your target hardware, which can be identified using thelsblkcommand.Generate hardware-configuration.nix on target machine
mainTo obtain the necessary hardware configuration for a target machine, you must run the configuration generator on that machine.
Options for accessing the target:
- Boot into a NixOS installer without completing the installation.
- Use the
kexectarball method.
Command: Run the following on the target machine to generate the files in
/tmp/config/. You must then copy/tmp/config/nixos/hardware-configuration.nixto your local machine's configuration directory.nixos-generate-config --no-filesystems --dir /tmp/configSet NIX_PATH via NixOS configuration (Recommended)
mainTo avoid errors when using tools like
nix-shellornix-env(e.g.,error: file 'nixpkgs' was not found in the Nix search path), you can populate theNIX_PATHdirectly within your NixOS flake configuration. This is the recommended approach because it is stateless and automatically stays in sync with your flake updates.Add a module to your
nixosConfigurationsthat setsnix.nixPathusing your flake inputs.{ inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; # ... other inputs outputs = inputs@{ nixpkgs, ... }: { nixosConfigurations.yoursystem = nixpkgs.lib.nixosSystem { system = "x86_64-linux"; # adapt to your actual system modules = [ # This line will populate NIX_PATH { nix.nixPath = [ "nixpkgs=${inputs.nixpkgs}" ]; } # ... other modules and your configuration.nix ]; }; }; }Repair NixOS installations using --disko-mode mount
mainTo recover from a misconfigured NixOS installation without wiping existing data, use the
--disko-mode mountflag. This mode avoids reformatting disks and instead mounts the existing filesystems of the target machine to runnixos-installbased on your provided flake.Workflow:
- Boots into a
nixos-installer. - Mounts existing disks using
disko. - Runs
nixos-installusing the specified flake configuration. - Reboots the machine.
nix run github:nix-community/nixos-anywhere -- --disko-mode mount --flake <path to configuration>#<configuration name> --target-host root@<ip address>- Boots into a
Install NixOS using Terraform modules
mainYou can automate NixOS installation using
nixos-anywhereTerraform modules. This involves building the NixOS system and partitioner (e.g., using Disko) as separate modules, then passing their outputs to theinstallmodule.Using Flakes
When using Flakes, specify the
attributepointing to the NixOS configuration path (e.g.,.#nixosConfigurations.NAME.config.system.build.toplevel).Without Flakes
When not using Flakes, specify the
filepath to your NixOS configuration and theattributepath (e.g.,config.system.build.toplevel). Thefilecan use the(pkgs.nixos [])function fromnixpkgs.locals { ipv4 = "192.0.2.1" } module "system-build" { source = "github.com/nix-community/nixos-anywhere//terraform/nix-build" # with flakes attribute = ".#nixosConfigurations.mymachine.config.system.build.toplevel" # without flakes # file can use (pkgs.nixos []) function from nixpkgs #file = "${path.module}/../.." #attribute = "config.system.build.toplevel" } module "disko" { source = "github.com/nix-community/nixos-anywhere//terraform/nix-build" # with flakes attribute = ".#nixosConfigurations.mymachine.config.system.build.diskoScript" # without flakes # file can use (pkgs.nixos []) function from nixpkgs #file = "${path.module}/../.." #attribute = "config.system.build.diskoScript" } module "install" { source = "github.com/nix-community/nixos-anywhere//terraform/install" nixos_system = module.system-build.result.out nixos_partitioner = module.disko.result.out target_host = local.ipv4 }Update NixOS configuration remotely
mainTo apply configuration changes to a remote machine, ensure you have an SSH server enabled on the target and your SSH key is added to the
rootuser'sopenssh.authorizedKeys.keys. Use the--target-hostflag withnixos-rebuildto specify the remote connection.nixos-rebuild switch --flake <URL to your flake> --target-host "root@<ip address>"Install Terraform with required providers for nixos-anywhere
mainThe
nixos-anywhereTerraform modules require thenullandexternalproviders. You can obtain a Terraform installation with these plugins pre-configured usingnix-shellwith the following expression:nix-shell -p '(pkgs.terraform.withPlugins (p: [ p.null p.external ]))'Alternatively, you can add this expression to the
packageslist in yourdevShellwithin aflake.nixorshell.nixfile to ensure the providers are available in your development environment.Destination machine requirements for nixos-anywhere
mainThe destination machine must be reachable over the public internet or a local network. Note:
nixos-anywheredoes not support wifi networks.Boot Options
- Direct Boot: The machine must already be running a NixOS installer.
- Alternative Boot (kexec): If not booting from a NixOS installer image, the machine must meet these requirements:
- Architecture: Must be an x86-64 or aarch64 Linux system with
kexecsupport. For other architectures, you may need to specify a custom kexec image manually. - Memory: At least 1.5 GB of RAM (excluding swap space).
- Architecture: Must be an x86-64 or aarch64 Linux system with
Network Connectivity
If a VPN is required to reach the destination machine, you must define a custom installer using the
--kexecflag to connect to your VPN.Configure the NixOS installer user password
mainWhen booting from a NixOS installer, you must set a password for thenixosuser to enable SSH access fornixos-anywhere. Run this command directly in the terminal of the machine running the installer:passwd