Skip to content

Commit 2cea106

Browse files
committed
README.md: update cross compiling section
1 parent 72a931d commit 2cea106

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

README.md

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,52 @@ NIX_PATH=nixpkgs=../nixpkgs nixos-generate -f do
9898

9999
## Cross Compiling
100100

101-
To cross compile nixos images for other system you have
102-
to configure `boot.binfmtMiscRegistrations` on your host system.
101+
To cross compile nixos images for other architectures you have to configure
102+
`boot.binfmt.emulatedSystems` or `boot.binfmt.registrations` on your host system.
103103

104-
For more details about this have a look at :
104+
In your system `configuration.nix`:
105+
```nix
106+
{
107+
# Enable binfmt emulation of aarch64-linux.
108+
boot.binfmt.emulatedSystems = [ "aarch64-linux" ];
109+
}
110+
```
111+
112+
Alternatively, if you want to target other architectures:
113+
```nix
114+
# Define qemu-arm-static source.
115+
let qemu-arm-static = pkgs.stdenv.mkDerivation {
116+
name = "qemu-arm-static";
117+
src = builtins.fetchurl {
118+
url = "https://github.com/multiarch/qemu-user-static/releases/download/v6.1.0-8/qemu-arm-static";
119+
sha256 = "06344d77d4f08b3e1b26ff440cb115179c63ca8047afb978602d7922a51231e3";
120+
};
121+
dontUnpack = true;
122+
installPhase = "install -D -m 0755 $src $out/bin/qemu-arm-static";
123+
};
124+
in {
125+
# Enable binfmt emulation of extra binary formats (armv7l-linux, for exmaple).
126+
boot.binfmt.registrations.arm = {
127+
interpreter = "${qemu-arm-static}/bin/qemu-arm-static";
128+
magicOrExtension = ''\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00'';
129+
mask = ''\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\x00\xff\xfe\xff\xff\xff'';
130+
};
131+
132+
# Define additional settings for nix.
133+
nix.extraOptions = ''
134+
extra-platforms = armv7l-linux
135+
'';
136+
nix.sandboxPaths = [ "/run/binfmt/arm=${qemu-arm-static}/bin/qemu-arm-static" ];
137+
}
138+
```
139+
140+
For more details on configuring `binfmt`, have a look at:
141+
[binfmt options](https://search.nixos.org/options?channel=unstable&query=boot.binfmt),
142+
[binfmt.nix](https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/system/boot/binfmt.nix),
143+
[this comment](https://github.com/NixOS/nixpkgs/issues/109661#issuecomment-762629438) and
105144
[clevers qemu-user](https://github.com/cleverca22/nixos-configs/blob/master/qemu.nix).
106145

107-
Once you've run `nixos-rebuild` with theses options,
146+
Once you've run `nixos-rebuild` with these options,
108147
you can use the `--system` option to create images for other architectures.
109148

110149
## Using in a Flake

0 commit comments

Comments
 (0)