Change:
extra-platforms = ${toString (cfg.emulatedSystems ++ lib.optional pkgs.stdenv.hostPlatform.isx86_64 "i686-linux")} ''; nix.sandboxPaths = lib.mkIf (cfg.emulatedSystems != []) <del> ([ "/run/binfmt" ] ++ (map (system: dirOf (dirOf (getEmulator system))) cfg.emulatedSystems)); </del> <ins> ([ "/run/binfmt" "${pkgs.bash}" ] ++ (map (system: dirOf (dirOf (getEmulator system))) cfg.emulatedSystems)); </ins> environment.etc."binfmt.d/nixos.conf".source = builtins.toFile "binfmt_nixos.conf" (lib.concatStringsSep "n" (lib.mapAttrsToList makeBinfmtLine config.boot.binfmt.registrations));

Discussion:
EDIT: See solution Describe the bug Trying to compile aarch64 images on x86_64 with qemu aarch64 binfmt and flake master fails. Somebody helped me debugging this but they couldn't find the underlying issue but suspected the binfmt virtualization to fail somehow. The following error occurs: copying path '/nix/store/x1d645w5lj0civpxhcvf5dak2msv5n94-xauth-' from ''. copying path '/nix/store/a7rh31d9w13i7f5n8717vcc2g4ins25n-xcb-util-' from ''. copying path '/nix/store/098f1qj2556ix1j59klllxy0yyq9i5lr-xz-5.2.5' from ''. copying path '/nix/store/xmp5w9p9jb4r2nq8sa1wyc33s6ymq198-zd1211-firmware-1.5' from ''. copying path '/nix/store/agsxfrv6kqzvw32mhpvnx3284zwa00wc-xz-5.2.5-bin' from ''. copying path '/nix/store/0v9x8nga9djc6vb52f394cndp3pdxfq8-zlib-1.2.11' from ''. copying path '/nix/store/87cpl8czpm28j7ls8yxmj3n3mcvqc4ld-xz-5.2.5-dev' from ''. copying path '/nix/store/hmc262ya9j5lkxpw8an2k295qncxask6-binutils-2.35.1' from ''. copying path '/nix/store/dmjaj3hmgpq38xsb08m5z4cvhlzvakaa-zlib-1.2.11' from ''. copying path '/nix/store/cnqgq8j45d637d61wplmyaa3a4yhcxfx-binutils-2.35.1-info' from ''. copying path '/nix/store/wih74414281kgzvj78j9296zln12c0nb-binutils-2.35.1-man' from ''.

copying path '/nix/store/wpfm6ad9gzgxm8kpr3pga2i5aa5kb4cr-binutils-wrapper-2.35.1' from ''. copying path '/nix/store/g17kkn2jlnnmcdmxjwy0vvjr32c4bkq3-zlib-1.2.11-dev' from ''. copying path '/nix/store/39yvfarcw8xmbi5gwn1x2mz9xfyxs551-bootstrap-stage2-gcc-wrapper-' from ''. copying path '/nix/store/7zgvdwixnfmq411sgljf98xyl2hi7yg4-bootstrap-stage2-stdenv-linux' from ''. building '/nix/store/45yl8f6m620xfmhmhf6ni6lwp74qsyq3-bash-4.4-'. ErroSysErrorexecuting '/nix/store/fj18xk7dwk706mfki61klg69dg84gsim-bootstrap-tools/bin/bash': No such file or directory error: --- Error
---------- nix-build builder for '/nix/store/45yl8f6m620xfmhmhf6ni6lwp74qsyq3-bash-4.4-' failed with exit code 1 building '/nix/store/79in2lallcn63ys01nwiz6521ga3vkm5-libunistring-'. error: --- Error
---------- nix-build 1 dependencies of derivation '/nix/store/83lyqfkdhghfi22855d8zns5vf33jb60-python3-minimal-' failed to build error: --- Error
---------- nix-build 1 dependencies of derivation '/nix/store/5c74i3z46scnfk22hbahsl5hc0njq4r6-glibc-2.32-' failed to build error: --- Error
---------- nix-build 1 dependencies of derivation '/nix/store/00zy36zbplflmvdpcb73i0m4pmri81rv-binutils-wrapper-' failed to build error: --- Error
---------- nix-build 1 dependencies of derivation '/nix/store/wp3xkqah5vkljmqxl3745qjfba9g0b6a-bootstrap-stage3-gcc-wrapper-.

drv' failed to build error: --- Error
---------- nix-build 1 dependencies of derivation '/nix/store/w5995whwivqxpndbi85lj60ylcs2z8mv-bootstrap-stage4-gcc-wrapper-' failed to build error: --- Error
---------- nix-build 1 dependencies of derivation '/nix/store/zbq3kjr264dykf3pq7l19dfwgji3fbhw-cmake-' failed to build error: --- Error
---------- nix-build 1 dependencies of derivation '/nix/store/d1fjncs8agy6lm0d725m0xjyzfr52q7b-cmake-boot-' failed to build error: --- Error
---------- nix-build 1 dependencies of derivation '/nix/store/3vyi2wbc5c841r265jpkhwkmpmbvl1ma-gcc-' failed to build error: --- Error
---------- nix-build 1 dependencies of derivation '/nix/store/5hw1srch09kqnhycgg1ddma74g9q3wf3-gcc-wrapper-' failed to build error: --- Error
---------- nix-build 1 dependencies of derivation '/nix/store/75s0s8w31k053kmmmpcsbb9psbc1k9rq-getent-1003.1-' failed to build error: --- Error
---------- nix-build 1 dependencies of derivation '/nix/store/cq2i0vzksszqk7cgfjmlf0hx96ymxbgz-glibc-iconv-2.32-' failed to build error: --- Error
---------- nix-build 1 dependencies of derivation '/nix/store/gaygvfpzjz1jdcnznhz0m9c2kgs37v0v-glibc-iconv-2.32-' failed to build error: --- Error
---------- nix-build 1 dependencies of derivation '/nix/store/1ip9cx89pdqli9ag41dbrj4nlm4z4cq0-glibc-locales-2.

32-' failed to build error: --- Error
---------- nix-build 1 dependencies of derivation '/nix/store/a4w785413w2pa2cj0phj1yj29p2hbs4r-libapparmor-' failed to build error: --- Error
---------- nix-build 1 dependencies of derivation '/nix/store/05p2ak50d67iyhbv6ydxyr36ra6fby9w-nixos-system-nixos-21.03pre-' failed to build error: --- Error
---------- nix-build 1 dependencies of derivation '/nix/store/7vpcmysv3d8hgai7x96asdssh41jsp92-perl-' failed to build error: --- Error
---------- nix-build 1 dependencies of derivation '/nix/store/ww72vyvxrfd2kamki9dp02wgr1mcbyh2-perl-' failed to build error: --- Error
---------- nix-build 1 dependencies of derivation '/nix/store/34s2baij7m76xrw3a6kiyx077p4cxwk1-python-' failed to build error: --- Error
---------- nix-build 1 dependencies of derivation '/nix/store/j10jsr5g3clm586l7jxc7cy3gprb4qq5-shadow-' failed to build error: --- Error
---------- nix-build 1 dependencies of derivation '/nix/store/hqq6njw91pglk3z4m2gyk0imnpkhivmz-stdenv-' failed to build error: --- Error
---------- nix-build 1 dependencies of derivation '/nix/store/mb1xndiip69kd61r6wyrqvhwd6650cw1-stdenv-' failed to build error: --- Error
---------- nix-build 1 dependencies of derivation '/nix/store/vzar9pfn9qmpvjzvq70r4ss2p7kk3id1-nixos-sd-image-21.

03pre-git-aarch64-' failed to build file /nix/store/fj18xk7dwk706mfki61klg69dg84gsim-bootstrap-tools/bin/bash /nix/store/fj18xk7dwk706mfki61klg69dg84gsim-bootstrap-tools/bin/bash: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /nix/store/fj18xk7dwk706mfki61klg69dg84gsim-bootstrap-tools/lib/ld-linux-, for GNU/Linux 2.6.32, stripped cat /run/binfmt/aarch64-linux #!/usr/bin/env sh exec -- /nix/store/rhl4j0mpxcl9xrh0xi17xc8amzz0w0gf-qemu-5.1.0/bin/qemu-aarch64 "$ /nix/store/rhl4j0mpxcl9xrh0xi17xc8amzz0w0gf-qemu-5.1.0/bin/qemu-aarch64 qemu: no user program specified cat build-users-group = nixbld max-jobs = auto cores = 0 sandbox = true extra-sandbox-paths = /run/binfmt /nix/store/rhl4j0mpxcl9xrh0xi17xc8amzz0w0gf-qemu-5.1.0 substituters = trusted-substituters = trusted-public-keys = auto-optimise-store = false require-sigs = true trusted-users = root allowed-users = * builders = system-features = nixos-test benchmark big-parallel kvm sandbox-fallback = false experimental-features = nix-command flakes extra-platforms = aarch64-linux i686-linux To Reproduce Steps to reproduce the behavior: a server on e.g. with Ubuntu 20.04 of type cpx11 (bigger recommended for faster compilation of nix) the instructions in (take care to not run this on your machine :D) would love to at least get feedback if this is reproducible. Expected behavior I can build an aarch64 sd-image using qemu binfmt.
I think () broke it. would that be a possibility/make sense to you? And how can this be fixed because I don't understand that code at all?

samueldr told me on IRC that something like and needs to be done but I don't think I'm able to implement that.
Fixed with
Is there a way to write a NixOS test for this? Simply testing the build of a NixOS image for aarch64 from x86_64 seems impossible cause you would need network access.
I just encountered a very similar thing again: