Cross Compiling: Difference between revisions
Improve examples |
Link to Nixcademy post Cross-Compiling NixOS images |
||
| (3 intermediate revisions by 3 users not shown) | |||
| Line 20: | Line 20: | ||
</syntaxhighlight>You can perform the same operations using the CLI, and Nix will correctly evaluate the <code>localSystem</code> based on your current system:<syntaxhighlight lang="bash"> | </syntaxhighlight>You can perform the same operations using the CLI, and Nix will correctly evaluate the <code>localSystem</code> based on your current system:<syntaxhighlight lang="bash"> | ||
nix-build '<nixpkgs>' -A pkgsCross.aarch64-multiplatform.hello # nix-legacy | nix-build '<nixpkgs>' -A pkgsCross.aarch64-multiplatform.hello # nix-legacy | ||
nix-build '<nixpkgs>' --arg crossSystem '(import <nixpkgs> {}).lib.systems.examples.aarch64-multiplatform' -A hello # alternative way | |||
nix build nixpkgs#pkgsCross.aarch64-multiplatform.hello # nix3 | nix build nixpkgs#pkgsCross.aarch64-multiplatform.hello # nix3 | ||
</syntaxhighlight>All of the above snippets will resolve to the exact same derivation result, which will provide a binary for GNU Hello that can execute only on an <code>aarch64</code> system. There are many other systems <code>pkgsCross</code> has defined, you can see an exhaustive list of all of them on your system:<syntaxhighlight lang="bash"> | </syntaxhighlight>All of the above snippets will resolve to the exact same derivation result, which will provide a binary for GNU Hello that can execute only on an <code>aarch64</code> system. There are many other systems <code>pkgsCross</code> has defined, you can see an exhaustive list of all of them on your system:<syntaxhighlight lang="bash"> | ||
| Line 159: | Line 160: | ||
} | } | ||
) { } | ) { } | ||
</syntaxhighlight> | |||
=== Using cross-compiled packages in the host NixOS config === | |||
First, configure your builder as a trusted [[Binary Cache|binary cache]]. | |||
Then copy the cross-compiled package to the host using <code>nix copy</code>. | |||
Finally modify your <code>configuration.nix</code> as follows: | |||
<syntaxhighlight lang="nix"> | |||
let pkgsCross = import <nixpkgs> { | |||
localSystem = "x86_64-linux"; # <-- put your builder's platform here and below | |||
hostSystem = "x86_64-linux"; | |||
crossSystem = "aarch64-linux"; # <-- put your host's platform here | |||
}; | |||
in | |||
{ | |||
environment.systemPackages = [ | |||
pkgsCross.hello # <-- put your cross-compiled package here | |||
]; | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 172: | Line 194: | ||
* [https://logs.nix.samueldr.com/nixos/2018-08-03#1533327247-1533327971; 2018-08-03 discussion on #nixos] ([https://matrix.to/#/!AinLFXQRxTuqNpXyXk:matrix.org/$15333274371713496LOAor:matrix.org Mirror of chat on Matrix.org]) | * [https://logs.nix.samueldr.com/nixos/2018-08-03#1533327247-1533327971; 2018-08-03 discussion on #nixos] ([https://matrix.to/#/!AinLFXQRxTuqNpXyXk:matrix.org/$15333274371713496LOAor:matrix.org Mirror of chat on Matrix.org]) | ||
* [https://nixcademy.com/posts/cpp-with-nix-in-2023-part-2-package/ C++ with Nix in 2023, Part 2: Package Generation and Cross-Compilation, Nixcademy] | |||
* [https://nixcademy.com/posts/cross-compilation-with-nix/ Separation of Concerns in Cross-Compilation, Nixcademy] | |||
* [https://nixcademy.com/posts/cross-compile-nixos-images/ Cross-Compiling NixOS images, Nixcademy] | |||
[[Category:nix]] | [[Category:nix]] | ||
[[Category:Development]] | [[Category:Development]] | ||