First, import the disko NixOS module in your NixOS configuration and define disko devices as described in the examples.
Let's assume that your NixOS configuration lives in configuration.nix
and your
target machine is called machine
:
See https://github.com/nix-community/disko-templates/ for more examples:
The example below will work with both UEFI and BIOS-based systems.
curl https://raw.githubusercontent.com/nix-community/disko-templates/main/single-disk-ext4/disko-config.nix > ./disko-config.nix
-
Option 1: If NixOS is not installed, boot into an installer without first installing NixOS.
-
Option 2: Use the kexec tarball method, as described here.
-
Generate Configuration: Run the following command on the target machine:
nixos-generate-config --no-filesystems --dir /tmp/config
This creates the necessary configuration files under /tmp/config/
. Copy
/tmp/config/nixos/hardware-configuration.nix
to your local machine into the
same directory as disko-config.nix
.
# default.nix
let
# replace nixos-24.05 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.05.tar.gz";
in
import (nixpkgs + "/nixos/lib/eval-config.nix") {
modules = [ ./configuration.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.05";
}
Your current directory now should contain the following files from the previous step:
configuration.nix
,default.nix
,disko-config.nix
andhardware-configuration.nix
Run nixos-anywhere
as follows:
nixos-anywhere --store-paths $(nix-build -A config.system.build.disko -A config.system.build.toplevel --no-out-link) root@machine