I’d like to use RISC-V binaries using the binfmt wrappers from within a chroot. However, I have problems to get the wrappers working from inside. I suppose that the QEMU wrapper binaries need to be accessible from within the chroot? That’s why I just bind-mounted the whole nix store into the chroot, so that everything should be there, but to no avail. Probably I’m misunderstanding something fundamentally about that stuff. Here’s what I tried:
I’ve setup the binfmt wrappers using boot.binfmt.emulatedSystems = [ "riscv64-linux" ];
. Rebooted. Tested some RISC-V binary, and it works. Everything fine and dandy so far:
$ uname -srvmo
Linux 6.5.5 #1-NixOS SMP PREEMPT_DYNAMIC Sat Sep 23 09:14:39 UTC 2023 x86_64 GNU/Linux
$ file /nix/store/2jmhsa7wmywnx3ihmv0z5s0p1057m7a8-hello-static-riscv64-unknown-linux-musl-2.12.1/bin/hello
/nix/store/2jmhsa7wmywnx3ihmv0z5s0p1057m7a8-hello-static-riscv64-unknown-linux-musl-2.12.1/bin/hello: ELF 64-bit LSB executable, UCB RISC-V, RVC, double-float ABI, version 1 (SYSV), statically linked, not stripped
$ /nix/store/2jmhsa7wmywnx3ihmv0z5s0p1057m7a8-hello-static-riscv64-unknown-linux-musl-2.12.1/bin/hello
Hello, world!
I then try to enter a chroot and do the same:
$ mkdir -p /tmp/chroot/nix/store
$ sudo mount --bind /nix/store /tmp/chroot/nix/store
$ sudo chroot --userspec=$(id -u):$(id -g) /tmp/chroot $(realpath $(which bash)) -l
bash-5.2$ /nix/store/2jmhsa7wmywnx3ihmv0z5s0p1057m7a8-hello-static-riscv64-unknown-linux-musl-2.12.1/bin/hello
bash: /nix/store/2jmhsa7wmywnx3ihmv0z5s0p1057m7a8-hello-static-riscv64-unknown-linux-musl-2.12.1/bin/hello: cannot execute: required file not found
However, calling the QEMU wrapper manually inside the chroot works:
bash-5.2$ /nix/store/bd362svdsz3giwf3dr5gn6xw64ccb2rd-qemu-8.0.4/bin/qemu-riscv64 /nix/store/2jmhsa7wmywnx3ihmv0z5s0p1057m7a8-hello-static-riscv64-unknown-linux-musl-2.12.1/bin/hello
Hello, world!
Initially, I had a bubblewrap container for RISC-V where I had the exact same problem, but it was easier to get a minimal reproducer using chroot. Hope that soemone can point me into the right direction.