NixOS ❄: tmpfs as root
This post covers both EFI and legacy boot setups.
One fairly unique property of NixOS is the ability to boot with only /boot
and /nix
. Nothing else is actually required. This supports doing all sorts
of weird things with your root file system.
One way is to do like Graham's post "erase your darlings" describes and empty
your root file system each boot using ZFS snapshots. This way have some cool
things that you could do on top of his setup, such as doing snapshots when
it's running and roll-back to empty on boot. That way you actually can go
back to recover files you lost but still have an empty state.
Another way is to go the tmpfs way which is probably why you're here. So I'm
going to walk through the install with tmpfs as root file system.
Step 1 - Partitioning on EFI
I'm going to do the most basic setup when it comes to file systems, just a
/boot
as fat32
and /nix
as ext4
without encryption. If you want to
have another file system or use LUKS or something it should be trivial to
just format the drive differently and mount it.
DISK=/dev/disk/by-id/ata-VENDOR-ID-OF-THE-DRIVE
parted $DISK -- mklabel gpt
parted $DISK -- mkpart ESP fat32 1MiB 512MiB
parted $DISK -- set 1 boot on
parted $DISK -- mkpart Nix 512MiB 100%
Step 1 - Partitioning for legacy boot
I'm going to do the most basic setup when it comes to file systems, just a
/boot
as ext4
and /nix
as ext4
without encryption. If you want to
have another file system or use LUKS or something it should be trivial to
just format the drive differently and mount it.
DISK=/dev/disk/by-id/ata-VENDOR-ID-OF-THE-DRIVE
parted $DISK -- mklabel msdos
parted $DISK -- mkpart primary ext4 1M 512M
parted $DISK -- set 1 boot on
parted $DISK -- mkpart primary ext4 512MiB 100%
Step 2 - Creating the file systems
This is fairly straight forward in my example:
mkfs.vfat $DISK-part1
mkfs.ext4 $DISK-part1
mkfs.ext4 $DISK-part2
Step 3 - Mounting the file systems
Here we do one of the neat tricks, we mount tmpfs instead of a partition and
then we mount the partitions we just created.
mount -t tmpfs none /mnt
mkdir -p /mnt/{boot,nix,etc/nixos,var/log}
mount $DISK-part1 /mnt/boot
mount $DISK-part2 /mnt/nix
mkdir -p /mnt/nix/persist/{etc/nixos,var/log}
mount -o bind /mnt/nix/persist/etc/nixos /mnt/etc/nixos
mount -o bind /mnt/nix/persist/var/log /mnt/var/log
Step 4 - Configuration
Then go ahead and do a nixos-generate-config --root /mnt
to get a basic
configuration for your system.
Step 4.4 - Configure the boot loader
Here you can choose your boot loader.
{
boot.loader.systemd-boot.enable = true;
boot.loader.efi.canTouchEfiVariables = true;
boot.loader.grub.enable = true;
boot.loader.grub.efiSupport = true;
boot.loader.grub.device = "nodev";
boot.loader.grub.version = 2;
boot.loader.grub.device = "/dev/sda";
}
Perform the actual install. We can ignore setting the roots password at the
end of the install since it won't be there after reboot anyway.
nixos-install --no-root-passwd
Date: 2020-05-02 Sat 10:00
Author: Elis Hirwing <elis@hirwing.se>