Dual-boot Windows and NixOS

Learning about computers

I always presumed there was a BIOS that did stuff that triggered OS software to run. Turns out things are much more complicated than that.

New technology obsoletes others. After a strong dominance, BIOS is now being deprecated, and being replaced by UEFI. In most modern motherboards, you should already have UEFI installed.

Speaking of new technology, MBR, short for Master Boot Record is also being deprecated in favor of GPT, or GUID Partition Table. Reason? MBR didn’t think consumer-grade 4TB hard drives were possible then.

Installing windows

Installing windows using UEFI should be pretty easy by following window’s own instructions. The entire installation process should take roughly half an hour if you skip updates.

Installing nixos

Installing nixos is slightly more complicated. First, you should download nixos through their official page. Now using that loader, make sure it is booted in UEFI mode by going into your UEFI BIOS and specifically triggering it.

A rather simple screen should pop up with a whole bunch of output. Wait for the command line to appear.

Understanding linux file systems In short, disks are ordered alphabetically and prefixed with /dev/sd. So your first disk will be /dev/sda and second /dev/sdb and so on.

Partitions are then ordered numerically, so the first partition in the first disk will be dev/sda1.

Partitioning NixOS We will be creating two partitions, a file system, and a swap partition. A swap partition is used for increasing your amount of virtual RAM. Although I have 8Gb RAM sticks, machine learning can be quite taxing, and hence I will be partitioning 8Gb here, 4Gb should be sufficient for most workloads.

Run the following using gdisk:

Command: o ↵
This option deletes all partitions and creates a new protective MBR.
Proceed? (Y/N): y ↵

Command: n ↵
Partition Number: ↵
First sector: ↵
Last sector: 4G ↵
Hex Code: 8200 ↵

Command: n ↵
Partition Number: ↵
First sector: ↵
Last sector: ↵
Hex Code: ↵

Command: w ↵
Do you want to proceed? (Y/N): Y ↵

You can check if everything went smoothly via fdisk --list

Now we need to set them to the correct type of partitions with the following

mkfs.ext4 -L nixos /dev/sdb2
mkswap -L swap /dev/sdb1

change drive ids as needed

Mounting Mount NixOS on the partition we just labeled as nixos with

mount /dev/disk/by-label/nixos /mnt

Remember the boot drive windows installed for you? If you don’t, just run fdisk --list again and find the one with the type EFI system.

mkdir /mnt/boot
mount /dev/sda2 /mnt/boot

NixOS config The great thing about NixOS is everything from the kernel to the desktop environment is config driven. Generate and modify the config via

nixos-generate-config --root /mnt
nano /mnt/etc/nixos/configuration.nix

You should double-check if the following are present, absence of them means you installed in BIOS mode instead of UEFI!

boot.loader.systemd-boot.enable = true;
boot.loader.efi.canTouchEfiVariables = true;

You should also add the following line if your EFI partition isn’t /dev/sda1:

boot.loader.efi.efibootmgr.efiDisk = "/dev/sda";
boot.loader.efi.efibootmgr.efiPartition = "2";

Things you should enable are definitely the services.xserver sets, as those are desktop environment related settings. If you don’t trigger those, you’ll be looking at a console on reboot.

You should also add a user:

users.extraUsers.myName = {
  isNormaluser = true;
  initialPassword = "password";
  uid = 1000;
  home = "/home/myName";
  description = "My Full Name";
  extraGroups = [ "wheel" "networkmanager" ];
};

Installing

Run nixos-install and pray. Don’t worry if you screwed up. NixOS configuration is stateless and can always be changed with nixos-rebuild switch.

If everything went well, reboot should lead you to the EFI loader, and you can now switch between the two OSes. Congratulations!

Resources

https://nixos.wiki/wiki/Dual_Booting_NixOS_and_Windows https://zimbatm.com/journal/2016/09/09/nixos-window-dual-boot/ https://github.com/jethrokuan/nix-config/blob/master/base.nix https://nixos.org/nixos/manual/index.html