How to install NixOS from /nix/store?

Hello,

how can I install NixOS from running Ubuntu using /nix/store from the NixOS live iso image?
I have prepared the $target partition – fdisk, cryptsetup, btrfs, mounted volumes and system directories, generated configuration.nix, copied /nix/store from the iso image,…
I can even chroot to bash, but…
How to continue to install NixOS from nix/store, please?
See current installation steps bellow.

Expected steps to do: generate root file system, generate uki kernel+initrd (dracut) with kernel parameters for crypt open and mount btrfs root volume, sign it, copy kernel to EFI partition, create user hans (in wheel/sudo group), set password for hans, reboot with (current) systemd-boot, “install packages”,…

Thank you in advance,
Hans

# Update + Prerequisites
# Download Graphical ISO image from https://nixos.org/download/.
sudo -i
apt update
apt upgrade
apt install cryptsetup btrfs-progs vim #nix

# Partitioning
lsblk -f
device=/dev/nvme0n1
fdisk $device
# …

# Parameters
partition=${device}p7
efi_partition=${device}p1
luks_label=F4TB_CryptLinux_32GB
btrfs_label=F4TB_NixOS_32GB
crypt_name=crypt_nixos
mapper=/dev/mapper/$crypt_name
target=/mnt/p7
iso=/home/hans/Downloads/nixos-graphical-25.11.10134.26ef669cffa9-x86_64-linux.iso
iso_dir=/mnt/iso

# File systems
cryptsetup luksFormat -v --label $luks_label $partition
cryptsetup open $partition $crypt_name
mkfs.btrfs -L $btrfs_label $mapper
mkdir -p $target
mount $mapper $target
btrfs subvolume create $target/@ $target/@home $target/@var $target/@snapshots $target/@nix
umount $target
# https://forums.unraid.net/bug-reports/prereleases/consider-using-compress-force-instead-of-compress-for-btrfs-compression-r2326/
# https://docs.google.com/spreadsheets/d/1x9-3OQF4ev1fOCrYuYWt1QmxYRmPilw_nLik5H_2_qA
mount $mapper -odefaults,noatime,compress=zstd:6,discard=async,subvol=@ $target
cd $target
mkdir -p nix var home .snapshots boot/efi
mount $mapper -odefaults,noatime,compress=zstd:6,discard=async,subvol=@nix       nix
mount $mapper -odefaults,noatime,compress=zstd:6,discard=async,subvol=@var       var
mount $mapper -odefaults,noatime,compress=zstd:6,discard=async,subvol=@home      home
mount $mapper -odefaults,noatime,compress=zstd:6,discard=async,subvol=@snapshots .snapshots
mount $efi_partition boot/efi
lsblk -f $partition
findmnt -R $target

# Installation
mkdir -p $iso_dir
mount -o ro,loop $iso $iso_dir
mkdir -p proc sys dev run etc
unsquashfs -d nix/store $iso_dir/nix-store.squashfs '*'
mount --bind  /proc proc
mount --rbind /sys  sys
mount --make-rslave sys
mount --rbind /dev  dev
mount --make-rslave dev
#mount --bind /run  run
mount -t tmpfs tmpfs run
export NIX_CONFIG="experimental-features = nix-command flakes"
find nix/store/*-nixos-generate-config -name nixos-generate-config
ls -l `!!`
nix/store/31y7hr6q3y32hf6d1ka1qizgbc3pg1dc-nixos-generate-config/bin/nixos-generate-config
nixos-generate-config --root .
#writing /mnt/p7/etc/nixos/hardware-configuration.nix...
#writing /mnt/p7/etc/nixos/configuration.nix...
#For more hardware-specific settings, see https://github.com/NixOS/nixos-hardware.
ls -l etc/nixos
vi -o etc/nixos/{,hardware-}configurationconfiguration.nix
cp /etc/{resolv.conf,hostname} etc
find nix/store/*-bash-interactive* -name bash
chroot . nix/store/nilkcchyj1l9ix3ww4p1fby7v00dwkpc-bash-interactive-5.3p3/bin/bash
bash-5.3# ls
bash: ls: command not found

The only supported way to do this is to run nixos-install after booting from the iso. I’m guessing your next question is what are the unsupported ways to do it … but they rely on things that are specific to nix, and you will potentially have a LOT to learn (no matter how much you know about linux already). If you really want to go there, you can look at these links:

but I don’t recommend it.

1 Like

nixos-anywhere can also kexec into the installer from a running Linux system for you if that’s an option, you’ll need a second machine to execute it from, though.

1 Like

Seems very relevant to your question:

1 Like

This metod does not use the already downloaded iso image file, resp. files on it, /nix/store/

No, on NixOS you don’t manually generate a UKI or manually run dracut, and things like the rootfs and initrd LUKS encryption aren’t (normally) specified with kernel params. The boot process is managed by a NixOS module that sets up the boot loader and keeps all the files in sync with the system state. You cannot use NixOS normally without just letting a NixOS boot loader module manage this, which is what the typical nixos-install-style tools are for. Trying to set it up manually is not supported.

Similarly, you can’t just copy /nix out of an ISO and expect that to be usable. The nix store is managed by Nix, and Nix maintains a database of metadata about the store. The way the ISO sets up its store, and in particular that database, is nontrivial, so you can’t just copy out of it.

I would strongly recommend against trying to use the ISO as a file source rather than as an actual bootable installer. There’s just no particular reason to do that. As others have pointed out, there are documented methods to install NixOS from an existing installation of a different linux distro, and those will use all the same binaries that the ISO is made of.

2 Likes

Adding to the comments above

chroot . nix/store/nilkcchyj1l9ix3ww4p1fby7v00dwkpc-bash-interactive-5.3p3/bin/bash
bash-5.3# ls
bash: ls: command not found

is expected to fail, as bash cannot see its dependencies (which live outside the chroot).

Instead of unsquashfs-ing the nix store simply mount it and nix copy the paths you want.[1] This may save some downloads on the subsequent nixos-install run.

[1] Local Store - Nix 2.34.7 Reference Manual