Stuck trying to build a nixos bootable USB

Hello,

I’m trying to build a nixos usb stick that only contains firefox running in cage.

The goal is to have firefox pointed at a specific url so that it can work like a kiosk.

I have the following nix

{ pkgs, ... }: {
  imports = [ 
    <nixpkgs/nixos/modules/profiles/image-based-appliance.nix>
  ];

  boot.initrd.includeDefaultModules = false;
  boot.initrd.kernelModules = [ "ext4" ];

  system.stateVersion = "24.11";

  disabledModules = [
    <nixpkgs/nixos/modules/profiles/all-hardware.nix>
    <nixpkgs/nixos/modules/profiles/base.nix>
  ];

  environment.defaultPackages = [ ];
  environment.systemPackages = with pkgs; [ firefox cage ];

  services.xserver = { enable = true; };

  services = {
    displayManager.sddm.enable = true;
    libinput.enable = true; # for touchpad support on many laptops
  };

  time.timeZone = "Europe/London"; 
  i18n.defaultLocale = "en_GB.UTF-8";
  console.keyMap = "uk"; 

  users.users.root.initialHashedPassword = "";

  systemd.services.cage-firefox = {
    description = "Run Cage with Firefox";
    wantedBy = [ "default.target" ];
    serviceConfig = {
      ExecStart = "${pkgs.cage}/bin/cage ${pkgs.firefox}/bin/firefox";
      Restart = "always";
      Environment = "XDG_RUNTIME_DIR=/run/user/1000";
    };
  };
}

I use the following command to build

nixos-generate -f raw -o result/ -c configuration.nix

and i get the following error in build

error: builder for '/nix/store/rk8jc4kh6rw8z1sbcnqms3ybvxbajwfq-nixos-disk-image.drv' failed with exit code 127

Any help would be much appreciated, i’ve been trying for a couple days now and just about to give up.

Try nix log /nix/store/rk8jc4kh6rw8z1sbcnqms3ybvxbajwfq-nixos-disk-image.drv and check build logs.

Attempting to reproduce on my side gives the error:

chroot: failed to run command '/nix/var/nix/profiles/system/bin/switch-to-confi
guration': No such file or directory

This is caused by

So if you wish to keep the image-based-appliance.nix, you might want to add

system.switch.enable = lib.mkForce true;

Ah amazing thank you, that has managed to build the project again now.

I am back to the original issue I have which is that it doesnt boot the system.

This is what happens when i run the img. Are there any additional configurations i need to add?

This is how I’m running the image

qemu-system-x86_64 -drive file=nixos.img,format=raw -m 16G -enable-kvm

Thanks for this btw that was really helpful.

Knowing to add flags to my ~/.config/nix/nix.conf is so useful!

I’ll suggest that you start by switching to the serial console with Ctrl-Alt-3, which leads you to detailed boot messages from systemd.

On the other hand, I don’t recommend attempting to reducing image size with options like boot.initrd.includeDefaultModules without having a working image first.

For this specific setup, if you allow default modules to be included, then it will boot up normally, so you should figure out a list of modules to add before disabling those modules again.