Skip to content

Instantly share code, notes, and snippets.

@chris-martin
Last active February 17, 2024 18:17
Show Gist options
  • Save chris-martin/4ead9b0acbd2e3ce084576ee06961000 to your computer and use it in GitHub Desktop.
Save chris-martin/4ead9b0acbd2e3ce084576ee06961000 to your computer and use it in GitHub Desktop.
How to install NixOS from an Ubuntu liveCD

These are instructions for booting from an Ubuntu liveCD and installing NixOS on a machine. I needed to do this because the NixOS liveCD doesn't work on my machine (NixOS/nixpkgs#5829), so I'm just using the Ubuntu installation media as something to boot into.

Much of this is from discussion at NixOS/nixpkgs#14680.


Get the Ubuntu ISO: http://releases.ubuntu.com/16.04.1/ubuntu-16.04.1-desktop-amd64.iso

Write it to a USB drive with unetbootin

Boot from the USB drive

Open GParted

Create partitions:

  • name: boot
    file sytem: fat32
    size: 500 MB
  • name: swap
    file system: linux-swap
  • name: root
    file system: ext4

Open a terminal and sudo bash. You'll stay in this shell for the rest of the instructions.

Obtain the ISO.

wget https://nixos.org/nixos/download.html
ISO_URL=$(cat download.html | grep minimal | grep x86 | sed -r 's/.*"(.*\.iso)".*/\1/')
wget $ISO_URL -O nixos.iso

Mount the ISO.

mkdir nixos
mount -o loop nixos.iso nixos

Extract the Nix store. We won't fully install Nix in Ubuntu, but we'll use a few of the executables from the store.

mkdir /nix
unsquashfs -d /nix/store nixos/nix-store.squashfs

Find the relevant programs in the nix store and add them to PATH.

NIXOS_INSTALL=$(find /nix/store -path '*-nixos-install/bin/nixos-install')
NIX_INSTANTIATE=$(find /nix/store -path '*-nix-*/bin/nix-instantiate')
NIXOS_GENERATE_CONFIG=$(find /nix/store -path '*-nixos-generate-config/bin/nixos-generate-config')
export PATH="$(dirname $NIXOS_INSTALL):$(dirname NIXOS_INSTANTIATE):$(NIXOS_GENERATE_CONFIG):$PATH"

Create the build group and a build user.

groupadd --system nixbld
useradd --system --home-dir /var/empty --shell $(which nologin) -g nixbld -G nixbld nixbld0

Obtain a copy of the Nix package repo and construct a NIX_PATH.

NIX_REV=$(echo $ISO_URL | sed -r 's~.*\.([0-9a-f]{7}).*~\1~')
wget https://github.com/nixos/nixpkgs-channels/archive/$NIX_REV.zip -O nixpkgs.zip
unzip nixpkgs.zip
mv nixpkgs-* nixpkgs
export NIX_PATH=nixpkgs=$HOME/nixpkgs

Mount the partitions you created earlier. Note that your device names may differ from these.

mount /dev/sda3 /mnt
mkdir /mnt/boot
mount /dev/sda1 /mnt/boot

Copy the nix store from the ISO into where the nix store will be on your new install. (This step isn't necessary, and I'm not sure if it actually helps, but it may spare you from needing to download some things.)

mkdir /mnt/nix
unsquashfs -d /mnt/nix/store nixos/nix-store.squashfs

Generate config files and install.

nixos-generate-config --root /mnt
nixos-install --root /mnt
@jacoboneill
Copy link

Hey, still going through it but there seemed to be an error with your env variables. Here it is fixed:

NIXOS_INSTALL=$(find /nix/store -path '*-nixos-install/bin/nixos-install')
NIX_INSTANTIATE=$(find /nix/store -path '*-nix-*/bin/nix-instantiate')
NIXOS_GENERATE_CONFIG=$(find /nix/store -path '*-nixos-generate-config/bin/nixos-generate-config')
export PATH="$(dirname $NIXOS_INSTALL):$(dirname $NIX_INSTANTIATE):$(dirname $NIXOS_GENERATE_CONFIG):$PATH"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment