### Controlling Installation Phases Source: https://context7.com/nix-community/nixos-anywhere/llms.txt Run only specific phases of the installation pipeline, useful for debugging or split deployments. The default phases are kexec, disko, install, and reboot. ```bash # Default phases (all): kexec → disko → install → reboot nix run github:nix-community/nixos-anywhere -- \ --flake .#myconfig --target-host root@192.0.2.1 # Only kexec (boot into NixOS installer) and disko (partition disks), skip install + reboot nix run github:nix-community/nixos-anywhere -- \ --phases kexec,disko \ --flake .#myconfig --target-host root@192.0.2.1 # Repair/re-install without reformatting: mount existing filesystems, then install nix run github:nix-community/nixos-anywhere -- \ --disko-mode mount \ --flake .#myconfig \ --target-host root@192.0.2.1 ``` -------------------------------- ### Installing on a Machine with No OS Source: https://context7.com/nix-community/nixos-anywhere/llms.txt Target a machine already booted from a NixOS USB/netboot installer; nixos-anywhere skips kexec automatically. This is useful for low-RAM targets or targets without kexec support. ```bash # On the target (booted NixOS installer): set SSH password passwd # sets password for the `nixos` user # Find the installer's IP ip addr # look for inet addresses on eth0 / enp0s3 # From source machine: deploy — nixos-anywhere detects the NixOS installer # and skips kexec (useful for low-RAM targets or targets without kexec support) nix run github:nix-community/nixos-anywhere -- \ --flake '.#myconfig' \ --target-host nixos@fec0::5054:ff:fe12:3456 ``` -------------------------------- ### Nixos-Anywhere Flake URI Fragment Example Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/reference.md If the error 'Please specify the name of the NixOS configuration to be installed' occurs, append the configuration name as a URI fragment to your flake. For instance, to use `nixosConfigurations.foo`, append '#foo' to the flake URI. ```bash #foo ``` -------------------------------- ### Use --extra-files with nixos-anywhere Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/howtos/extra-files.md This is an example of how to invoke nixos-anywhere with the --extra-files option, passing the prepared directory containing the files to be copied to the target host. ```console nixos-anywhere --flake ".#" --extra-files $root --target-host root@newhost ``` -------------------------------- ### Command Line Usage Help Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/reference.md Displays the help message for the nixos-anywhere command-line tool, outlining available options for system installation. ```bash Usage: nixos-anywhere [options] [] Options: * -f, --flake set the flake to install the system from. i.e. nixos-anywhere --flake .#mymachine Also supports variants: nixos-anywhere --flake .#nixosConfigurations.mymachine.config.virtualisation.vmVariant * --target-host set the SSH target host to deploy onto. * -i selects which SSH private key file to use. * -p, --ssh-port set the ssh port to connect with * --ssh-option set one ssh option, no need for the '-o' flag, can be repeated. for example: '--ssh-option UserKnownHostsFile=./known_hosts' * -L, --print-build-logs print full build logs * --env-password set a password used by ssh-copy-id, the password should be set by the environment variable SSHPASS * -s, --store-paths set the store paths to the disko-script and nixos-system directly if this is given, flake is not needed * --kexec use another kexec tarball to bootstrap NixOS * --kexec-extra-flags extra flags to add into the call to kexec, e.g. "--no-sync" * --ssh-store-setting ssh store settings appended to the store URI, e.g. "compress true". needs to be URI encoded. * --post-kexec-ssh-port after kexec is executed, use a custom ssh port to connect. Defaults to 22 * --copy-host-keys copy over existing /etc/ssh/ssh_host_* host keys to the installation * --extra-files contents of local are recursively copied to the root (/) of the new NixOS installation. Existing files are overwritten Copied files will be owned by root unless specified by --chown option. See documentation for details. * --chown change ownership of recursively. Recommended to use uid:gid as opposed to username:groupname for ownership. Option can be specified more than once. * --disk-encryption-keys copy the contents of the file or pipe in local_path to remote_path in the installer environment, after kexec but before installation. Can be repeated. * --no-substitute-on-destination disable passing --substitute-on-destination to nix-copy * --debug enable debug output * --show-trace show nix build traces * --option nix option to pass to every nix related command * --from URL of the source Nix store to copy the nixos and disko closure from * --build-on-remote build the closure on the remote machine instead of locally and copy-closuring it * --vm-test build the system and test the disk configuration inside a VM without installing it to the target. * --generate-hardware-config nixos-facter|nixos-generate-config generate a hardware-configuration.nix file using the specified backend and write it to the specified path. The backend can be either 'nixos-facter' or 'nixos-generate-config'. * --phases comma separated list of phases to run. Default is: kexec,disko,install,reboot kexec: kexec into the nixos installer disko: first unmount and destroy all filesystems on the disks we want to format, then run the create and mount mode install: install the system reboot: unmount the filesystems, export any ZFS pools and reboot the machine * --disko-mode disko|mount|format set the disko mode to format, mount or destroy. Default is disko. disko: first unmount and destroy all filesystems on the disks we want to format, then run the create and mount mode * --no-disko-deps This will only upload the disko script and not the partitioning tools dependencies. Installers usually have dependencies available. Use this option if your target machine has not enough RAM to store the dependencies in memory. * --build-on auto|remote|local sets the build on settings to auto, remote or local. Default is auto. auto: tries to figure out, if the build is possible on the local host, if not falls back gracefully to remote build local: will build on the local host remote: will build on the remote host ``` -------------------------------- ### Run NixOS Anywhere for Remote Installation Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/howtos/no-os.md Initiate the NixOS installation using `nixos-anywhere`. This command targets a remote host and assumes a flake configuration named `myconfig` in the current directory. ```bash nix run github:nix-community/nixos-anywhere -- --flake '.#myconfig' --target-host nixos@fec0::5054:ff:fe12:3456 ``` -------------------------------- ### Prepare Extra Files for Copying Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/howtos/extra-files.md This example shows how to prepare a directory with specific files and directories to be copied using --extra-files. It demonstrates copying SSH host keys and a persistent data directory. ```console cd /tmp sudo cp --verbose --archive --parents /etc/ssh/ssh_host_* ${root} cp --verbose --archive --link /persist ${root} ``` -------------------------------- ### Deploy NixOS System with nixos-rebuild Source: https://github.com/nix-community/nixos-anywhere/blob/main/terraform/nixos-rebuild.md This example demonstrates how to use the nixos-rebuild module to deploy a NixOS system to a remote host. It requires the output from a nix-build module and specifies the target host. ```hcl 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 "deploy" { source = "github.com/nix-community/nixos-anywhere//terraform/nixos-rebuild" nixos_system = module.system-build.result.out target_host = local.ipv4 } ``` -------------------------------- ### Use Local kexec Image File Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/howtos/ipv6.md Alternatively, reference a local kexec installer tarball. This file will be uploaded to the target machine via SFTP. ```bash nixos-anywhere \ --kexec ./nixos-kexec-installer-noninteractive-x86_64-linux.tar.gz \ ... ``` -------------------------------- ### Nixos-Anywhere Path Specification Example Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/reference.md When '`` and `` must be existing store-paths' is shown, ensure that the paths provided via the `-s` switch for the disko script and NixOS system are valid and accessible store paths. ```bash -s ``` -------------------------------- ### Basic nixos-anywhere Installation Source: https://context7.com/nix-community/nixos-anywhere/llms.txt Invoke nixos-anywhere directly from GitHub to install NixOS on a target host. Specify the flake path and target host. Supports explicit SSH identity files and password authentication via environment variables. ```bash # Minimal invocation — flake in local directory, target at given IP nix run github:nix-community/nixos-anywhere -- \ --flake /home/mydir/test#hetzner-cloud \ --target-host root@37.27.18.135 # With explicit SSH identity file nix run github:nix-community/nixos-anywhere -- \ --flake .#myconfig \ -i ~/.ssh/id_ed25519 \ --target-host root@192.0.2.1 # Using SSH password (via environment variable) SSHPASS=mysecretpassword nix run github:nix-community/nixos-anywhere -- \ --env-password \ --flake .#myconfig \ --target-host myuser@192.0.2.1 # Expected output on success: # Installation finished. No error reported. # Warning: Permanently added '37.27.18.135' (ED25519) to the list of known hosts ``` -------------------------------- ### Install Terraform Providers for NixOS Anywhere Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/howtos/terraform.md Installs the necessary null and external Terraform providers using nix-shell. Add this to your devshell's package list in flake.nix or shell.nix. ```nix nix-shell -p "(pkgs.terraform.withPlugins (p: [ p.null p.external ]))" ``` -------------------------------- ### Install with Limited RAM using --no-disko-deps Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/howtos/limited-ram.md Use this command when installing NixOS on systems with limited RAM to reduce memory consumption during the installation process. This option omits disko dependencies, relying on target system tools for partitioning. ```bash nix run github:nix-community/nixos-anywhere -- --no-disko-deps --flake # --target-host root@ ``` -------------------------------- ### Example Disk Layout Identification Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/quickstart.md Use the 'lsblk' command to identify the correct disk name on your target machine. Replace 'sda' in your disk configuration accordingly. ```bash NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS nvme0n1 259:0 0 1.8T 0 disk ``` -------------------------------- ### Mount Existing Filesystems with Nixos-Anywhere Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/howtos/disko-modes.md Use this command to boot into a NixOS installer, mount existing disks using disko, and run nixos-install without reformatting. This is useful for recovering from misconfigurations. ```bash nix run github:nix-community/nixos-anywhere -- --disko-mode mount --flake # --target-host root@ ``` -------------------------------- ### Custom kexec Image Source: https://context7.com/nix-community/nixos-anywhere/llms.txt Override the default x86-64 kexec tarball for aarch64 or other architectures. This example shows how to build and use an aarch64 kexec installer. ```bash # Build and use the aarch64 kexec installer from nixos-images nix run github:nix-community/nixos-anywhere -- \ --kexec "$(nix build --print-out-paths \ github:nix-community/nixos-images#packages.aarch64-linux.kexec-installer-nixos-unstable-noninteractive \ )/nixos-kexec-installer-noninteractive-aarch64-linux.tar.gz" \ --flake 'github:your-user/your-repo#your-system' \ root@yourip # Enable aarch64 emulation via qemu on x86-64 NixOS source machine # Add to your NixOS configuration.nix: # { boot.binfmt.emulatedSystems = [ "aarch64-linux" ]; } ``` -------------------------------- ### Connect to NixOS Installer via SSH Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/howtos/no-os.md Use this SSH command to test connectivity to the NixOS installer. Replace the IP address with the one found using `ip addr`. ```bash ssh -v nixos@fec0::5054:ff:fe12:3456 ``` -------------------------------- ### Download Disko Configuration Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/howtos/use-without-flakes.md Fetches a default disko configuration for single-disk ext4 setups. This can be used as a starting point for your disk partitioning and formatting needs. ```bash curl https://raw.githubusercontent.com/nix-community/disko-templates/main/single-disk-ext4/disko-config.nix > ./disko-config.nix ``` -------------------------------- ### Hardware Configuration Generation with nixos-anywhere Source: https://context7.com/nix-community/nixos-anywhere/llms.txt Generate hardware configuration files during installation using either `nixos-generate-config` or `nixos-facter`. Specify the output file path for the generated configuration. ```bash # Option A: nixos-generate-config (writes hardware-configuration.nix) nix run github:nix-community/nixos-anywhere -- \ --generate-hardware-config nixos-generate-config ./hardware-configuration.nix \ --flake .#myconfig \ --target-host root@192.0.2.1 # Option B: nixos-facter (writes facter.json — includes drivers, firmware, kernel modules) nix run github:nix-community/nixos-anywhere -- \ --generate-hardware-config nixos-facter ./facter.json \ --flake .#myconfig \ --target-host root@192.0.2.1 ``` -------------------------------- ### Display Network Interfaces and IP Addresses Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/howtos/no-os.md Execute this command on the NixOS installer to view network interface details and identify the installer's IP address. ```bash $ ip addr ``` -------------------------------- ### Nixos-Anywhere Installation Success Message Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/quickstart.md This message indicates that the installation has finished successfully without errors. Note that the server's public SSH key will have changed. ```bash Installation finished. No error reported. Warning: Permanently added '' (ED25519) to the list of known hosts ``` -------------------------------- ### Copy Persistent Data with Hard Links Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/howtos/extra-files.md This example demonstrates using `cp` with the `--link` option to create hard links for persistent data. This efficiently copies large directory trees without duplicating the underlying data, suitable for use with --extra-files. ```sh cd /tmp root=$(mktemp -d) cp --verbose --archive --link --parents /persist/home/myuser ${root} ``` -------------------------------- ### Set Password for NixOS Installer User Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/howtos/no-os.md Run this command on the NixOS installer to set a password for the `nixos` user, enabling SSH access. ```bash passwd ``` -------------------------------- ### Third-party Remote Deployment Tools Source: https://context7.com/nix-community/nixos-anywhere/llms.txt Examples of third-party remote deployment tools that work with the same NixOS flake configuration. These include deploy-rs, colmena, and nixinate. ```bash # Third-party remote deployment tools that work with the same flake: # deploy-rs: nix run github:serokell/deploy-rs -- .#myconfig # colmena: colmena apply --on myconfig # nixinate: nix run .#apps.nixinate.myconfig ``` -------------------------------- ### Nixos-Anywhere Flake Configuration Example Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/reference.md When encountering 'Flake does not provide attribute', verify that the configuration name specified in the URI fragment exists as a NixOS configuration within your flake, typically defined as `nixosConfigurations.`. ```nix nixosConfigurations.myconfig ``` -------------------------------- ### Copying Extra Files to Target Source: https://context7.com/nix-community/nixos-anywhere/llms.txt Recursively copy a local directory tree to the root of the new NixOS installation, preserving permissions. Use `--chown` to set ownership on paths that need a specific uid:gid. ```bash # Prepare a staging directory mirroring target filesystem layout temp=$(mktemp -d) trap "rm -rf $temp" EXIT install -d -m755 "$temp/etc/ssh" cp --archive /etc/ssh/ssh_host_ed25519_key "$temp/etc/ssh/" cp --archive /etc/ssh/ssh_host_ed25519_key.pub "$temp/etc/ssh/" chmod 600 "$temp/etc/ssh/ssh_host_ed25519_key" # Pass the staging dir — its contents overwrite target / nix run github:nix-community/nixos-anywhere -- \ --extra-files "$temp" \ --flake .#myconfig \ --target-host root@192.0.2.1 # Set ownership on paths that need a specific uid:gid nix run github:nix-community/nixos-anywhere -- \ --extra-files "$temp" \ --chown /home/alice/.ssh 1000:100 \ --flake .#myconfig \ --target-host root@192.0.2.1 # Programmatic multi-host deployment for host in host1 host2 host3; do root="target/${host}" install -d -m755 "${root}/etc/ssh" ssh-keygen -A -C "root@${host}" -f "${root}" nixos-anywhere --extra-files "${root}" --flake ".#${host}" --target-host "root@${host}" done ``` -------------------------------- ### Include Hardware Configuration in NixOS System Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/quickstart.md Integrate a generated hardware configuration into your NixOS system modules. This is recommended for non-VM installations to ensure proper driver configuration. ```nix nixosConfigurations.generic = nixpkgs.lib.nixosSystem { system = "x86_64-linux"; modules = [ disko.nixosModules.disko ./configuration.nix ./hardware-configuration.nix ]; }; ``` -------------------------------- ### Deploy NixOS to Low-RAM Targets Source: https://context7.com/nix-community/nixos-anywhere/llms.txt Use this option to reduce memory usage on targets with limited RAM by omitting disko tool dependencies from the upload. The partitioning tools will be from the target installer. ```bash # Only the partitioning script is uploaded; tools come from the target installer nix run github:nix-community/nixos-anywhere -- \ --no-disko-deps \ --flake .#myconfig \ --target-host root@192.0.2.1 ``` -------------------------------- ### Copy Existing SSH Host Keys Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/howtos/secrets.md Instruct `nixos-anywhere` to copy existing trusted SSH host keys and certificates from `/etc/ssh/ssh_host_*` to the target system's `/mnt` directory before installation. Existing files in the destination are ignored. ```bash nixos-anywhere --copy-host-keys --flake '.#your-host' root@yourip ``` -------------------------------- ### Generate Hardware Configuration with Nixos-Facter Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/quickstart.md Alternatively, use nixos-facter to generate hardware configuration. Ensure the path to facter.json is correct. ```bash nix run github:nix-community/nixos-anywhere -- --generate-hardware-config nixos-facter ./facter.json --flake # --target-host root@ ``` -------------------------------- ### Build and Deploy with NixOS Anywhere Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/howtos/use-without-flakes.md Builds the disko script and deploys the NixOS configuration to the target machine. This command assumes your configuration files (`configuration.nix`, `default.nix`, `disko-config.nix`, `hardware-configuration.nix`) are in the current directory. ```bash nixos-anywhere --store-paths $(nix-build -A config.system.build.diskoScript -A config.system.build.toplevel --no-out-link) root@machine ``` -------------------------------- ### Using a Custom kexec Image Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/howtos/custom-kexec.md Provide a custom kexec image path using the `--kexec` switch. The image will be uploaded before execution. Replace the placeholder Flake URL with your actual configuration. ```bash nix run github:nix-community/nixos-anywhere -- \ --kexec "$(nix build --print-out-paths github:nix-community/nixos-images#packages.aarch64-linux.kexec-installer-nixos-unstable-noninteractive)/nixos-kexec-installer-noninteractive-aarch64-linux.tar.gz" \ --flake 'github:your-user/your-repo#your-system' \ root@yourip ``` -------------------------------- ### Terraform Install-only Modules for NixOS Source: https://context7.com/nix-community/nixos-anywhere/llms.txt These Terraform modules separate the NixOS system build, disk partitioning, and installation steps. They are useful for infrastructure-as-code workflows where build and install are distinct phases. ```hcl # Install-only module (separate build + install steps) module "system-build" { source = "github.com/nix-community/nixos-anywhere//terraform/nix-build" attribute = ".#nixosConfigurations.mymachine.config.system.build.toplevel" } module "disko" { source = "github.com/nix-community/nixos-anywhere//terraform/nix-build" attribute = ".#nixosConfigurations.mymachine.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 } ``` -------------------------------- ### Injecting Secrets During Installation Source: https://context7.com/nix-community/nixos-anywhere/llms.txt Inject SSH host keys, sops secrets, or disk encryption keys into the installer environment before NixOS is written to disk. Disk encryption keys are not copied to the final system. ```bash #!/usr/bin/env bash # Bootstrap sops-nix / agenix by seeding the machine's SSH host key from `pass` temp=$(mktemp -d) trap "rm -rf $temp" EXIT install -d -m755 "$temp/etc/ssh" pass ssh_host_ed25519_key > "$temp/etc/ssh/ssh_host_ed25519_key" chmod 600 "$temp/etc/ssh/ssh_host_ed25519_key" nixos-anywhere \ --extra-files "$temp" \ --flake '.#your-host' \ --target-host root@yourip ``` ```bash # Supply disk encryption keys (file or pipe) — not copied to final system echo "my-super-safe-password" > /tmp/disk-1.key nixos-anywhere \ --disk-encryption-keys /tmp/disk-1.key /tmp/disk-1.key \ --disk-encryption-keys /tmp/disk-2.key <(pass my-disk-encryption-password) \ --flake '.#your-host' \ root@yourip # Copy existing trusted SSH host keys from the running target into the install nixos-anywhere --copy-host-keys --flake '.#your-host' root@yourip ``` -------------------------------- ### NixOS Installation Configuration Source: https://github.com/nix-community/nixos-anywhere/blob/main/terraform/install.md This Terraform configuration defines the NixOS system build, disk partitioning script, and the target host for installation. It utilizes local variables for IP addresses and modules for Nix build results. ```hcl 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 } ``` -------------------------------- ### Programmatic Deployment with --extra-files Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/howtos/extra-files.md This script automates the deployment process for multiple hosts. For each host, it prepares a target directory, generates SSH host keys within that directory, and then calls nixos-anywhere with the --extra-files option. ```sh for host in host1 host2 host3; do root="target/${host}" install -d -m755 ${root}/etc/ssh ssh-keygen -A -C root@${host} -f ${root} nixos-anywhere --extra-files "${root}" --flake ".#${host}" --target-host "root@${host}" done ``` -------------------------------- ### Define NixOS Version and Configuration Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/howtos/use-without-flakes.md Sets up the NixOS version to be used by fetching the specified Nixpkgs channel. This Nix expression imports your local configuration and evaluates it. ```nix let # replace nixos-24.11 with your preferred nixos version or revision from here: https://status.nixos.org/ nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/archive/refs/heads/nixos-24.11.tar.gz"; in import (nixpkgs + "/nixos/lib/eval-config.nix") { modules = [ ./configuration.nix ]; } ``` -------------------------------- ### Manually Add and Update Nix Channel Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/howtos/nix-path.md Add the nixos-unstable channel and update it on the installed machine. This is a manual alternative to configuring NIX_PATH via NixOS configuration. ```shell nix-channel --add https://nixos.org/channels/nixos-unstable nixos nix-channel --update ``` -------------------------------- ### Enter Development Environment Source: https://github.com/nix-community/nixos-anywhere/blob/main/terraform/tests/README.md Enter the development environment for Terraform testing. ```bash nix develop .#terraform ``` -------------------------------- ### Generate Hardware Configuration with Nixos-generate-config Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/quickstart.md Use the --generate-hardware-config flag with nixos-anywhere to automatically create a hardware-configuration.nix file. Specify the output path for the generated file. ```bash --generate-hardware-config nixos-generate-config ./hardware-configuration.nix ``` -------------------------------- ### Generate Hardware Configuration with Nixos-facter Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/quickstart.md Use the --generate-hardware-config flag with nixos-facter to create a hardware report. Specify the output path for the facter.json file. ```bash --generate-hardware-config nixos-facter ./facter.json ``` -------------------------------- ### Build and Deploy NixOS Without Flakes Source: https://context7.com/nix-community/nixos-anywhere/llms.txt This command builds the necessary NixOS configuration and deploys it to a remote host. Ensure the NixOS configuration is accessible. ```bash nixos-anywhere \ --store-paths $(nix-build -A config.system.build.diskoScript \ -A config.system.build.toplevel \ --no-out-link) \ root@machine ``` -------------------------------- ### Run Nixos-Anywhere from Repository Source: https://github.com/nix-community/nixos-anywhere/blob/main/CONTRIBUTING.md Use this command to execute the nixos-anywhere tool directly from the source code repository. ```console nix run . -- --help ``` -------------------------------- ### Format Code with Nix Source: https://github.com/nix-community/nixos-anywhere/blob/main/CONTRIBUTING.md Run this command to ensure the project's code adheres to the defined formatting standards. ```console nix fmt ``` -------------------------------- ### Use Nixos-facter for Hardware Configuration Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/quickstart.md Incorporate the nixos-facter module for more comprehensive hardware reporting and configuration. Specify the path for the facter.json report. ```nix nixosConfigurations.generic-nixos-facter = nixpkgs.lib.nixosSystem { system = "x86_64-linux"; modules = [ disko.nixosModules.disko ./configuration.nix { hardware.facter.reportPath = ./facter.json; } ]; }; ``` -------------------------------- ### Initialize and Run Validation Tests Source: https://github.com/nix-community/nixos-anywhere/blob/main/terraform/tests/README.md Initialize OpenTofu and run all validation tests within the terraform/tests directory. ```bash cd terraform/tests tofu init tofu test ``` -------------------------------- ### Terraform All-in-one Module Configuration Source: https://github.com/nix-community/nixos-anywhere/blob/main/terraform/all-in-one.md Configure the all-in-one Terraform module for NixOS deployment. This example shows how to specify system attributes, target host, instance ID, and scripts for decrypting secrets. Ensure scripts are executable. ```hcl locals { ipv4 = "192.0.2.1" } module "deploy" { source = "github.com/nix-community/nixos-anywhere//terraform/all-in-one" # with flakes nixos_system_attr = ".#nixosConfigurations.mymachine.config.system.build.toplevel" nixos_partitioner_attr = ".#nixosConfigurations.mymachine.config.system.build.diskoScript" # without flakes # file can use (pkgs.nixos []) function from nixpkgs #file = "${path.module}/../.." #nixos_system_attr = "config.system.build.toplevel" #nixos_partitioner_attr = "config.system.build.diskoScript" target_host = local.ipv4 # when instance id changes, it will trigger a reinstall instance_id = local.ipv4 # useful if something goes wrong # debug_logging = true # build the closure on the remote machine instead of locally # build_on_remote = true # script is below extra_files_script = "${path.module}/decrypt-ssh-secrets.sh" disk_encryption_key_scripts = [{ path = "/tmp/secret.key" # script is below script = "${path.module}/decrypt-zfs-key.sh" }] # Optional, arguments passed to special_args here will be available from a NixOS module in this example the `terraform` argument: # { terraform, ... }: { # networking.interfaces.enp0s3.ipv4.addresses = [{ address = terraform.ip; prefixLength = 24; }]; # } # Note that this will means that your NixOS configuration will always depend on terraform! # Skip to `Pass data persistently to the NixOS` for an alternative approach #special_args = { # terraform = { # ip = "192.0.2.0" # } #} } ``` -------------------------------- ### Flake Configuration for nixos-facter Integration Source: https://context7.com/nix-community/nixos-anywhere/llms.txt Integrate `nixos-facter` into your `flake.nix` by specifying the `hardware.facter.reportPath`. This allows automatic configuration based on system facts. ```nix # flake.nix — nixos-facter integration nixosConfigurations.generic-nixos-facter = nixpkgs.lib.nixosSystem { system = "x86_64-linux"; modules = [ disko.nixosModules.disko ./configuration.nix { hardware.facter.reportPath = ./facter.json; } # auto-configured by nixos-facter ]; }; ``` -------------------------------- ### Test NixOS and Disko Configuration with VM Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/quickstart.md Run your NixOS and Disko configuration within a virtual machine for testing. Ensure your flake.nix and disk-config.nix are correctly referenced. ```bash nix run github:nix-community/nixos-anywhere -- --flake # --vm-test ``` -------------------------------- ### Generate Hardware Configuration with Nixos-Anywhere Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/quickstart.md Amend flags to nixos-generate-config when you need to generate hardware configuration. Adjust the path to hardware-configuration.nix accordingly. ```bash nix run github:nix-community/nixos-anywhere -- --generate-hardware-config nixos-generate-config ./hardware-configuration.nix --flake # --target-host root@ ``` -------------------------------- ### Terraform All-in-one Module for NixOS Deployment Source: https://context7.com/nix-community/nixos-anywhere/llms.txt This Terraform module installs NixOS and keeps it updated using nixos-rebuild. It requires specifying NixOS system and partitioner attributes, target host, and optionally extra files and disk encryption keys. ```hcl # All-in-one: install NixOS and keep it updated with nixos-rebuild locals { ipv4 = "192.0.2.1" } module "deploy" { source = "github.com/nix-community/nixos-anywhere//terraform/all-in-one" nixos_system_attr = ".#nixosConfigurations.mymachine.config.system.build.toplevel" nixos_partitioner_attr = ".#nixosConfigurations.mymachine.config.system.build.diskoScript" target_host = local.ipv4 instance_id = local.ipv4 # reinstall triggers when this changes extra_files_script = "${path.module}/decrypt-ssh-secrets.sh" disk_encryption_key_scripts = [{ path = "/tmp/secret.key" script = "${path.module}/decrypt-zfs-key.sh" }] } ``` -------------------------------- ### Run Nixos-Anywhere with a Flake Configuration Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/quickstart.md Use this command to run nixos-anywhere, specifying the path to your flake configuration and the target host IP address. The configuration name must match the definition in your flake. ```bash nix run github:nix-community/nixos-anywhere -- --flake # --target-host root@ ``` ```bash nix run github:nix-community/nixos-anywhere -- --flake /home/mydir/test#hetzner-cloud --target-host root@37.27.18.135 ``` -------------------------------- ### Enabling QEMU Emulation for aarch64 Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/howtos/custom-kexec.md Configure NixOS to emulate the aarch64 architecture using QEMU. This is useful if your local machine cannot build for aarch64 natively or via a remote builder. ```nix { boot.binfmt.emulatedSystems = [ "aarch64-linux" ]; } ``` -------------------------------- ### Set Hetzner Cloud Token and Run Tests Source: https://github.com/nix-community/nixos-anywhere/blob/main/terraform/tests/README.md Set the Hetzner Cloud API token as an environment variable and run the Hetzner Cloud integration tests. ```bash # Set your Hetzner Cloud token export TF_VAR_hcloud_token="your-64-character-hcloud-token" # Run Hetzner Cloud tests tofu test -filter hcloud-deployment.tftest.hcl ``` -------------------------------- ### Nixos-Anywhere CLI Syntax Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/reference.md This is the general command-line syntax for using nixos-anywhere with a flake. Ensure your flake path and configuration name are correctly specified. ```bash nix run github:nix-community/nixos-anywhere -- --flake # root@ ``` -------------------------------- ### Set DigitalOcean Token and Run Tests Source: https://github.com/nix-community/nixos-anywhere/blob/main/terraform/tests/README.md Set the DigitalOcean API token as an environment variable and run the DigitalOcean integration tests. ```bash # Set your DigitalOcean token export TF_VAR_digitalocean_token="your-digitalocean-api-token" # Run DigitalOcean tests tofu test -filter digitalocean-deployment.tftest.hcl ``` -------------------------------- ### Basic Disk Configuration with Disko Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/quickstart.md This is a basic disk configuration for disko, compatible with EFI and BIOS systems. Adjust '/dev/sda' to match your target machine's disk. ```nix # /etc/nixos/disk-config.nix { disko.devices = { disk.sda = { type = "disk"; device = "/dev/sda"; content = { type = "gpt"; partitions = { boot = { size = "1M"; type = "ef02"; }; root = { size = "100%"; type = "linux"; }; }; }; திகளை = { boot = { fs = { type = "vfat"; }; }; root = { fs = { type = "ext4"; }; }; }; }; }; } ``` -------------------------------- ### NixOS Configuration with Disko Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/howtos/use-without-flakes.md Configures the NixOS system, importing the disko module and your custom disk configuration. Ensure `disko.devices.disk.main.device` and `system.stateVersion` are set appropriately for your target system. ```nix # configuration.nix { imports = [ "${fetchTarball "https://github.com/nix-community/disko/tarball/master"}/module.nix" ./disko-config.nix ./hardware-configuration.nix ]; # Replace this with the system of the installation target you want to install!!! disko.devices.disk.main.device = "/dev/sda"; # Set this to the NixOS version that you have set in the previous step. # For more information, see `man configuration.nix` or https://nixos.org/manual/nixos/stable/options#opt-system.stateVersion . system.stateVersion = "24.11"; } ``` -------------------------------- ### Populate NIX_PATH via NixOS Configuration Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/howtos/nix-path.md Configure NixOS to populate the NIX_PATH with a specified nixpkgs input. This ensures NIX_PATH is automatically updated when flakes are updated. ```nix { 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 ]; }; }; } ``` -------------------------------- ### Deploying with Traditional Nix Expressions Source: https://context7.com/nix-community/nixos-anywhere/llms.txt Deploy using traditional Nix expressions instead of flakes. This involves defining your NixOS configuration in `default.nix` and `configuration.nix`. ```nix # default.nix let nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/archive/refs/heads/nixos-24.11.tar.gz"; in import (nixpkgs + "/nixos/lib/eval-config.nix") { modules = [ ./configuration.nix ]; } ``` ```nix # configuration.nix { imports = [ "${fetchTarball "https://github.com/nix-community/disko/tarball/master"}/module.nix" ./disko-config.nix ./hardware-configuration.nix ]; disko.devices.disk.main.device = "/dev/sda"; system.stateVersion = "24.11"; } ``` -------------------------------- ### Generate Hardware Configuration Source: https://github.com/nix-community/nixos-anywhere/blob/main/docs/howtos/use-without-flakes.md Generates a base NixOS configuration on the target machine, excluding filesystem configurations. This is a prerequisite for obtaining the hardware-specific details needed for the main NixOS configuration. ```bash nixos-generate-config --no-filesystems --dir /tmp/config ``` -------------------------------- ### List and Delete Hetzner Cloud Test Resources Source: https://github.com/nix-community/nixos-anywhere/blob/main/terraform/tests/README.md List test resources in Hetzner Cloud and force cleanup by deleting servers and SSH keys. ```bash # List test resources hcloud server list | grep tftest-nixos-anywhere hcloud ssh-key list | grep tftest-nixos-anywhere # Force cleanup hcloud server delete hcloud ssh-key delete ``` -------------------------------- ### Run Individual Nix Test Source: https://github.com/nix-community/nixos-anywhere/blob/main/CONTRIBUTING.md Use this command to build and test a specific check, identified by its attribute path. The -vL flags provide verbose logging. ```console nix build .#checks.x86_64-linux.from-nixos -vL ``` -------------------------------- ### Run Specific Test File Source: https://github.com/nix-community/nixos-anywhere/blob/main/terraform/tests/README.md Filter and run a specific test file, such as nixos-anywhere.tftest.hcl. ```bash tofu test -filter=nixos-anywhere.tftest.hcl ```