Skip to content

Instantly share code, notes, and snippets.

@ivan
Last active December 22, 2019 23:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivan/dd3666048eb9f5c36e1ea6f418197b07 to your computer and use it in GitHub Desktop.
Save ivan/dd3666048eb9f5c36e1ea6f418197b07 to your computer and use it in GitHub Desktop.
Replace Debian install with NixOS on MBR/DHCP machine e.g. on SoYouStart

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment