Creating a static rootfs image for a VM/container

I’m trying to create a nixos.rootfs.ext4 file that when mounted contains a nixos rootfs to be used by a VMM - it will load an unrelated kernel elf, optional initrd and boot a vm to the nixos rootfs mounted on /dev/vda and execute init. So far this works trivially with Alpine.

But I’m running into problems when trying to build the nixos rootfs.

The configuration.nix I’m using starts with:

{ config, pkgs, ... }:

	boot.isContainer = true;
	boot.tmpOnTmpfs = true;

I have tried:

nix-build '<nixpkgs/nixos>' --argstr system x86_64-linux -A system -I nixos-config=./configuration.nix

The result did not contain a /nix store, and it contains symlinks to an existing /nix store on the non-nixos system, they weren’t even relative symlinks. Non functional rootfs.

nix-build -E 'let eval = import <nixpkgs/nixos> { configuration = ./configuration.nix; }; pkgs = import <nixpkgs> {}; in pkgs.callPackage <nixpkgs/nixos/lib/make-system-tarball.nix> { storeContents = [ { object =; symlink = "/toplevel"; } ]; contents = []; }'

This appears to correctly produce a /nix store in the result but the structure of the rootfs is odd, it contains a /toplevel symlink.

Is there a way to produce a static functional nixos rootfs that can be booted by a VMM or bind mounted via namespaces for a container?

1 Like

new solution with nix flakes microvm.nix/disk-image.nix at main · astro/microvm.nix · GitHub

old solution nixos-generators/nixos-generate.nix at master · nix-community/nixos-generators · GitHub

1 Like