Cross Compiling: Difference between revisions

imported>Symphorien
How to obtain a shell with a cross compiler: mention using callPackage to have native build inputs
imported>Symphorien
add snippet using lib.systems.example
Line 26: Line 26:
and then use it to obtain a shell:
and then use it to obtain a shell:
{{commands|nix-shell crossShell.nix}}
{{commands|nix-shell crossShell.nix}}
The resulting shell will have <code>$CC</code> etc. set to the right compiler.
The resulting shell contains a cross toolchain and zlib in this example. Note that contrary to native shells, the compiler and some other tools are prefixed: there is no <code>gcc</code> but a <code>aarch64-unknown-linux-gnu-gcc</code>. Some convenience environment variables expand to the prefixed version of tools: <code>$CC</code>, <code>$LD</code>...


Examples of how to specify your target system can be found in [https://github.com/NixOS/nixpkgs/blob/master/lib/systems/examples.nix lib/systems/examples.nix].
Examples of how to specify your target system can be found in [https://github.com/NixOS/nixpkgs/blob/master/lib/systems/examples.nix lib/systems/examples.nix]. If the exact system you are targeting is available in this file then you can use the existing definition as in the following example:
<syntaxhighlight lang="nix">
let pkgs = import <nixpkgs> {
    crossSystem = (import <nixpkgs/lib>).systems.examples.armv7l-hf-multiplatform;
};
in
...
</syntaxhighlight>


The example above does not work as is with build dependencies (<code>nativeBuildInputs</code>). A solution is to use <code>callPackage</code> to enable splicing:
The examples above do not work as is with build dependencies (<code>nativeBuildInputs</code>). A solution is to use <code>callPackage</code> to enable splicing:
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
let pkgs = import <nixpkgs> {
let pkgs = import <nixpkgs> {