This is based on nixos-infect.
These instructions assume you have a machine using MBR to boot and DHCP for networking.
Run manually:
apt update
apt install curl rsync libfile-slurp-perl xz-utils
mkdir -p -m 0755 /nix
# Add nix build users
groupadd nixbld -g 30000 || true
for i in {1..10}; do
useradd -c "Nix build user $i" -d /var/empty -g nixbld -G nixbld -M -N -r -s "$(which nologin)" nixbld$i || true
done
curl -JLO https://nixos.org/nix/install
chmod +x install
./install
. ~/.nix-profile/etc/profile.d/nix.sh
nix-channel --remove nixpkgs
nix-channel --add "https://nixos.org/channels/nixos-unstable" nixos
nix-channel --update
# View hardware configuration
perl $(find /nix/store | grep /nixpkgs/nixos/modules/installer/tools/nixos-generate-config.pl | head -n 1) --show-hardware-config
# !!! Copy boot.initrd.availableKernelModules and boot.kernelModules to configuration
# !!! rsync configuration
export NIXOS_CONFIG=/root/configuration/hosts/$(hostname).nix
nix-env --set \
-I nixpkgs=$HOME/.nix-defexpr/channels/nixos \
-f '<nixpkgs/nixos>' \
-p /nix/var/nix/profiles/system \
-A system
# Remove nix installed by ./install
rm -fv /nix/var/nix/profiles/default*
/nix/var/nix/profiles/system/sw/bin/nix-collect-garbage
# Use the lustrate mechanism to move almost all files to /old-root on next boot
touch /etc/NIXOS /etc/NIXOS_LUSTRATE
echo etc/nixos >> /etc/NIXOS_LUSTRATE
echo root/.nix-defexpr/channels >> /etc/NIXOS_LUSTRATE
echo var/secrets >> /etc/NIXOS_LUSTRATE
rm -rf /boot/*
/nix/var/nix/profiles/system/bin/switch-to-configuration boot
shutdown -r now