From d0e97fa1ed17e71c1ab9d08bc7e530414f52590a Mon Sep 17 00:00:00 2001 From: Mikael Voss Date: Fri, 29 Nov 2024 16:41:23 +0100 Subject: [PATCH] builders: Enable user mode emulation for RISC-V and ARM --- pkgs/build-worker-oci/default.nix | 3 ++- pkgs/build-worker-oci/entrypoint.sh | 12 ++++++++++++ pkgs/overlay.nix | 6 +++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/pkgs/build-worker-oci/default.nix b/pkgs/build-worker-oci/default.nix index 958a6ae..06ce68f 100644 --- a/pkgs/build-worker-oci/default.nix +++ b/pkgs/build-worker-oci/default.nix @@ -6,6 +6,7 @@ bash, gnused, util-linux, + qemu-user }: dockerTools.buildLayeredImage { @@ -20,7 +21,7 @@ dockerTools.buildLayeredImage { enableFakechroot = true; - contents = [ openssh util-linux bash gnused ]; + contents = [ openssh util-linux bash gnused qemu-user ]; config.Cmd = [ "/entrypoint.sh" ]; diff --git a/pkgs/build-worker-oci/entrypoint.sh b/pkgs/build-worker-oci/entrypoint.sh index 541bd13..4bf14fc 100644 --- a/pkgs/build-worker-oci/entrypoint.sh +++ b/pkgs/build-worker-oci/entrypoint.sh @@ -26,4 +26,16 @@ cp /root/nix.conf /etc/nix/nix.conf /bin/mount -t overlay overlay -o lowerdir=/nix,upperdir=/mnt/data/nix-store,workdir=/mnt/data/workdir /nix /bin/mount --bind /mnt/data/tmp /tmp +# Register QEMU binaries for user mode emulation +aarch64_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00' +aarch64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff' + +riscv64_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00' +riscv64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff' + +execPath() { readlink -f "$(which "$1")" } + +echo ":qemu-aarch64:M::$aarch64_magic:$aarch64_mask:$(execPath qemu-aarch64):POCF" >/proc/sys/fs/binfmt_misc/register +echo ":qemu-riscv64:M::$riscv64_magic:$riscv64_mask:$(execPath qemu-riscv64):POCF" >/proc/sys/fs/binfmt_misc/register + /root/.nix-profile/bin/sshd -D -f /root/sshd_config diff --git a/pkgs/overlay.nix b/pkgs/overlay.nix index 5a40d24..e941e12 100644 --- a/pkgs/overlay.nix +++ b/pkgs/overlay.nix @@ -2,7 +2,11 @@ final: prev: { nyastodon = final.callPackage ./nyastodon/default.nix {}; upgrade-system = final.callPackage ./upgrade-system/default.nix {}; update-nixfiles = final.callPackage ./update-nixfiles/default.nix {}; - build-worker-oci = final.callPackage ./build-worker-oci/default.nix {}; + build-worker-oci = final.callPackage ./build-worker-oci/default.nix { + qemu-user = final.pkgsStatic.qemu-user.override { + hostCpuTargets = [ "aarch64-linux-user" "riscv64-linux-user" ]; + }; + }; librespeed-rust = final.callPackage ./librespeed-rust/default.nix {}; librespeed-go = final.callPackage ./librespeed-go/default.nix {}; akkoma-fe-domi = final.callPackage ./akkoma-fe-domi/default.nix {};