How to create a uefi bootable usb stick of Nixos?

It is easy to create a mbr bootable USB stick, but seems not so straight forward to create a uefi one.

Uefi bootloader exists not only on the target usb stick, but also in the system’s from in the form of booting record.

But how did the nixos installation iso image do that ? Is it possible to create a usb stick with uefi bootloader, that is able to boot on any machine with uefi support?

Following is how I managed to create a mbr bootable USB stick:

  1. On a nixos machine, create the dos partition table on the USB key and create the boot and / fs.
  2. mount / to /mnt, mount boot to /mnt/boot
  3. nixos-generate-config --root=/mnt to create necessary .nix file in /mnt/etc/nixos and modify them.
  4. nixos-install --root=/mnt

Then unplug this USB key and it can be used to boot any machine confined to mbr bootable.

In the POST progress press some key (e.g., F12) to get the boot order menu and select the USB key, then the nixos will boot.

It does not work for me if I want to create a uefi bootable USB key.

Have you looked at NixOS 23.11 manual | Nix & NixOS ?

The building nixos live iso section seems not apply to my question.

After some googling, I think the solution is related to BOOTX64.EFI, which enables the uefi bios regard the disk to be bootable, even though it is not registered in the nvram.

1 Like

The typical way of making a bootable USB is to burn a live ISO to a USB using dd, as the NxOS installer does. Is there a reason why this isn’t what you’re looking for?

If you have a UEFI partition set up, in theory the motherboard should go look for bootable files in the efi partition unless one of the various fast boot modes are configured. BOOTX64.EFI is a fallback supported by most firmwares, creating it should let you boot the stick from most motherboards if you select the USB.

Because I’m trying to make a fully functional os disk, rather than a live cd, which is not writable (am I right?), after the os is boot up.

appended:
it seems that

boot.loader.grub.efiInstallAsRemovable

applies to this question.

1 Like

Well, if you’re dumping a raw filesystem onto your USB, you can make it do whatever you want it to do when it’s booted. There’s no magic difference between installing to a USB vs a raw file system and then dumping that file system to a USB.

That said, the NixOS installer by default sets up a tmpfs at /, and recreates it from scratch on every reboot, so what you store in it is deleted after a reboot: nixpkgs/nixos/modules/installer/cd-dvd/iso-image.nix at e45d9ac91242645775df11a6bf334e5662822dfe · NixOS/nixpkgs · GitHub

That doesn’t mean yours has to do the same thing. You can pretty easily set up your own volume for writing data to, and use something like GitHub - nix-community/impermanence: Modules to help you handle persistent state on systems with ephemeral root storage [maintainer=@talyz] to keep some persistent directories.

But you can also create an ISO that, when booted, will just act as a normal NixOS desktop. I haven’t actually tried it, but I don’t see anything in nixos-generators/formats/raw-efi.nix at 898edde476232843da50c9a855efaf5dd8caadaa · nix-community/nixos-generators · GitHub, or nixpkgs/nixos/lib/make-disk-image.nix at e45d9ac91242645775df11a6bf334e5662822dfe · NixOS/nixpkgs · GitHub to suggest it would not be persistent, so using the raw-efi template with nixos-generate probably would do the trick?

Even if you don’t decide to use nixos-generate, its efi image setup should be helpful: nixos-generators/formats/raw-efi.nix at 898edde476232843da50c9a855efaf5dd8caadaa · nix-community/nixos-generators · GitHub

3 Likes

Since this thread is the number one result on google for how make nixos boot usb

Here is the normal people answer

Go to Download Nix / NixOS | Nix & NixOS

download either Gnome based installer boot ISO or the KDE based one

https://channels.nixos.org/nixos-23.05/latest-nixos-gnome-x86_64-linux.iso

https://channels.nixos.org/nixos-23.05/latest-nixos-plasma5-x86_64-linux.iso

Go to rufus’ website Rufus - Create bootable USB drives the easy way

download latest rufus https://github.com/pbatard/rufus/releases/download/v4.2/rufus-4.2p.exe

Start rufus

Drag and drop your nixos ISO into rufus

Click device, make SURE you have selected the right device

Hit start

image

then hit OK

image

Hit YES

image

Hit OK to confirm you have the right drive and it will be deleted

You now have a bootable USB nixos installer

3 Likes

Kind of amusing how google does that, given that that wasn’t even remotely the question.

Rufus works, but in case you’re not on Windows, the official instructions are here: NixOS 23.11 manual | Nix & NixOS

That will also have up-to-date links, the above post will be outdated 2-3 months from now.

1 Like

FWIW, Ventoy also works

5 Likes