Skip to content

Instantly share code, notes, and snippets.

@mlaventure
Forked from chris-martin/nixos-from-ubuntu.md
Last active September 16, 2021 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mlaventure/6620708452143cab8eec6311d33dec34 to your computer and use it in GitHub Desktop.
Save mlaventure/6620708452143cab8eec6311d33dec34 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.

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.

export NIXOS_INSTALL=$(find /nix/store -path '*-nixos-install/bin/nixos-install')
export NIX_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=$(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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment