Skip to content

Instantly share code, notes, and snippets.

@elbart
Forked from mlaventure/nixos-from-ubuntu.md
Last active September 16, 2021 14:55
Show Gist options
  • Save elbart/d4f9c2ffbd2ba922711547c4cca5837f to your computer and use it in GitHub Desktop.
Save elbart/d4f9c2ffbd2ba922711547c4cca5837f 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: ext2
    size: 512 MB
  • name: swap
    file system: linux-swap
  • name: root
    file system: ext4
  • name: home
    file system: luks

Encrypt your home:

cryptsetup luksFormat /dev/sdaX
cryptsetup luksOpen /dev/sda2 cryptedpool

pvcreate /dev/mapper/cryptedpool
vgcreate home /dev/mapper/cryptedpool
vcreate -l '100%FREE' -n home cryptedpool

mkfs.ext4 -L home /dev/mapper/cryptedpool-home

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

Obtain the ISO.

apt install curl
ISO_URL=$(curl -s https://nixos.org/download.html | grep minimal | grep x86 | grep -v SHA | 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.

export NIXOS_INSTALL=$(find /nix/store -path '*-nixos-install/bin/nixos-install')
export NIXOS_INSTANTIATE=$(find /nix/store -path '*-nix-*/bin/nix-instantiate')
export NIXOS_GENERATE_CONFIG=$(find /nix/store -path '*-nixos-generate-config/bin/nixos-generate-config')
export PATH="$(dirname $NIXOS_INSTALL):$(dirname $NIXOS_INSTANTIATE):$(dirname $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=21.05
wget https://github.com/nixos/nixpkgs/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment