$ nix-build -A ext4

The ext4 derivation, defined as, creates a rootfs image in the ext4 format. It contains the closures of toplevel and a registration file.

The rootfs is also available as a squashfs image.

To explore the result, we can extract the content of the rootfs in a temporary directory as follow:

$ mkdir -p tmp/ext4-root ; cd tmp
$ sudo mount $(readlink -fn ../result) ext4-root
$ find -maxdepth 2

The derivation is defined as a call to nixpkgs/nixos/lib/make-ext4-fs.nix, passing toplevel as argument. The closure is constructed by nixpkgs/build-support/closure-info.nix. The ext4 file system is populated using the debugfs command-line tool provided by e2fsprogs. The beauty of using debugfs is that no root privilege nor loop device are required.

A call to nix-store --load-db with the registration file found in the rootfs is done in a runit script. I wonder if it could be done directly when the rootfs is mounted.