FEX as binfmt to build Nix derivations

I have an aarch64 machine and I want to run some x86_64 proprietary software on it. QEMU is slow so I turn to projects that do JIT: Box64 and FEX. Box64 have some more complex problems so now I will only talk about FEX.

My NixOS config for using FEX as binfmt interpreter is flakes/nixos/fragments/fex.nix at 9e3898fda575e98343787a5f77254e1d59313ae6 · shirok1/flakes · GitHub (which is basically nix-config/modules/fex.nix at 465bb75594935e8735a70055cf05151c04ece6db · Liamolucko/nix-config · GitHub ). TLDR:

boot.binfmt.registrations.FEX-x86_64 = {
  inherit mask;
  wrapInterpreterInShell = false;
  interpreter = "${cfg.package}/bin/FEXInterpreter";
  offset = 0;
  matchCredentials = true;
  fixBinary = true;
  preserveArgvZero = true;
  magicOrExtension = x86_64Magic;
};
nix.settings.extra-sandbox-paths = [
  "/run/binfmt"
  "${cfg.package}"
];
nix.settings.extra-platforms = [
  "i686-linux"
  "x86_64-linux"
];

And when I try to build my derivation with binfmt set:

patching script interpreter paths in /nix/store/f9x1i83r0bgqfpnwbpwv768465vr2q9w-stata-19.5+2025-05-07
Traceback (most recent call last):
  File "/nix/store/8z6ap8amdlqlxp8djjmbp0w3j069ilc8-auto-patchelf-0-unstable-2024-08-14/bin/auto-patchelf", line 17, in <module>
    from elftools.common.exceptions import ELFError  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'elftools'

Using QEMU as binfmt (boot.binfmt.emulatedSystems = [ “x86_64-linux” ];) doesn’t have this problem.

My packaging code is in flakes/pkgs/stata.nix at 9e3898fda575e98343787a5f77254e1d59313ae6 · shirok1/flakes · GitHub , but basically any derivation with nativeBuildInputs = [ autoPatchelfHook ]; can reproduce this I think.

Running auto-patchelf outside Nix sandbox works just fine:

shiroki@nixo6n ~> /nix/store/8z6ap8amdlqlxp8djjmbp0w3j069ilc8-auto-patchelf-0-unstable-2024-08-14/bin/auto-patchelf
automatically fixing dependencies for ELF files
usage: auto-patchelf [-h] [--ignore-missing [IGNORE_MISSING ...]] [--no-recurse] --paths [PATHS ...] [--libs [LIBS ...]] [--runtime-dependencies [RUNTIME_DEPENDENCIES ...]] [--append-rpaths [APPEND_RPATHS ...]]
                     [--keep-libc] [--preserve-origin] [--ignore-existing] [--extra-args ...]
auto-patchelf: error: the following arguments are required: --paths
shiroki@nixo6n ~ [2]> head /nix/store/8z6ap8amdlqlxp8djjmbp0w3j069ilc8-auto-patchelf-0-unstable-2024-08-14/bin/auto-patchelf
#!/nix/store/sxyir85k5zifkss58lq0nr6wy8hlc1qc-python3-3.13.11-env/bin/python3

import argparse
import os
import pprint
import subprocess
import sys
import json
from fnmatch import fnmatch
from collections import defaultdict
shiroki@nixo6n ~> nix run nixpkgs#file /nix/store/sxyir85k5zifkss58lq0nr6wy8hlc1qc-python3-3.13.11-env/bin/python3.13
/nix/store/sxyir85k5zifkss58lq0nr6wy8hlc1qc-python3-3.13.11-env/bin/python3.13: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /nix/store/wb6rhpznjfczwlwx23zmdrrw74bayxw4-glibc-2.42-47/lib/ld-linux-x86-64.so.2, for GNU/Linux 3.10.0, not stripped
shiroki@nixo6n ~> /nix/store/sxyir85k5zifkss58lq0nr6wy8hlc1qc-python3-3.13.11-env/bin/python3.13
Python 3.13.11 (main, Dec  5 2025, 16:06:33) [GCC 15.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>