Cross-compilationIntroduction
"Cross-compilation" means compiling a program on one machine for another type of machine.
For example, a typical use of cross compilation is to compile programs for embedded devices.
These devices often don't have the computing power and memory to compile their own programs.
One might think that cross-compilation is a fairly niche concern, but there are advantages to being rigorous about distinguishing build-time vs run-time environments even when one is developing and deploying on the same machine.
Nixpkgs is increasingly adopting this opinion in that packages should be written with cross-compilation in mind, and nixpkgs should evaluate in a similar way (by minimizing cross-compilation-specific special cases) whether or not one is cross-compiling.
This chapter will be organized in three parts.
First, it will describe the basics of how to package software in a way that supports cross-compilation.
Second, it will describe how to use Nixpkgs when cross-compiling.
Third, it will describe the internal infrastructure supporting cross-compilation.
Packaging in a cross-friendly mannerPlatform parameters
The three GNU Autoconf platforms, build, host, and target, are historically the result of much confusion.
clears this up somewhat but there is more to be said.
An important advice to get out the way is, unless you are packaging a compiler or other build tool, just worry about the build and host platforms.
Dealing with just two platforms usually better matches people's preconceptions, and in this case is completely correct.
In Nixpkgs, these three platforms are defined as attribute sets under the names buildPlatform, hostPlatform, and targetPlatform.
All three are always defined as attributes in the standard environment, and at the top level. That means one can get at them just like a dependency in a function that is imported with callPackage:
{ stdenv, buildPlatform, hostPlatform, fooDep, barDep, .. }: ...buildPlatform..., or just off stdenv:
{ stdenv, fooDep, barDep, .. }: ...stdenv.buildPlatform....
buildPlatform
The "build platform" is the platform on which a package is built.
Once someone has a built package, or pre-built binary package, the build platform should not matter and be safe to ignore.
hostPlatform
The "host platform" is the platform on which a package is run.
This is the simplest platform to understand, but also the one with the worst name.
targetPlatform
The "target platform" is black sheep.
The other two intrinsically apply to all compiled software—or any build process with a notion of "build-time" followed by "run-time".
The target platform only applies to programming tools, and even then only is a good for for some of them.
Briefly, GCC, Binutils, GHC, and certain other tools are written in such a way such that a single build can only compile code for a single platform.
Thus, when building them, one must think ahead about which platforms they wish to use the tool to produce machine code for, and build binaries for each.
There is no fundamental need to think about the target ahead of time like this.
LLVM, for example, was designed from the beginning with cross-compilation in mind, and so a normal LLVM binary will support every architecture that LLVM supports.
If the tool supports modular or pluggable backends, one might imagine specifying a set of target platforms / backends one wishes to support, rather than a single one.
The biggest reason for mess, if there is one, is that many compilers have the bad habit a build process that builds the compiler and standard library/runtime together.
Then the specifying target platform is essential, because it determines the host platform of the standard library/runtime.
Nixpkgs tries to avoid this where possible too, but still, because the concept of a target platform is so ingrained now in Autoconf and other tools, it is best to support it as is.
Tools like LLVM that don't need up-front target platforms can safely ignore it like normal packages, and it will do no harm.
The exact schema these fields follow is a bit ill-defined due to a long and convoluted evolution, but this is slowly being cleaned up.
You can see examples of ones used in practice in lib.systems.examples; note how they are not all very consistent.
For now, here are few fields can count on them containing:
system
This is a two-component shorthand for the platform.
Examples of this would be "x86_64-darwin" and "i686-linux"; see lib.systems.doubles for more.
This format isn't very standard, but has built-in support in Nix, such as the builtins.currentSystem impure string.
config
This is a 3- or 4- component shorthand for the platform.
Examples of this would be "x86_64-unknown-linux-gnu" and "aarch64-apple-darwin14".
This is a standard format called the "LLVM target triple", as they are pioneered by LLVM and traditionally just used for the targetPlatform.
This format is strictly more informative than the "Nix host double", as the previous format could analogously be termed.
This needs a better name than config!
parsed
This is a nix representation of a parsed LLVM target triple with white-listed components.
This can be specified directly, or actually parsed from the config.
[Technically, only one need be specified and the others can be inferred, though the precision of inference may not be very good.]
See lib.systems.parse for the exact representation.
libc
This is a string identifying the standard C library used.
Valid identifiers include "glibc" for GNU libc, "libSystem" for Darwin's Libsystem, and "uclibc" for µClibc.
It should probably be refactored to use the module system, like parse.
is*
These predicates are defined in lib.systems.inspect, and slapped on every platform.
They are superior to the ones in stdenv as they force the user to be explicit about which platform they are inspecting.
Please use these instead of those.
platform
This is, quite frankly, a dumping ground of ad-hoc settings (it's an attribute set).
See lib.systems.platforms for examples—there's hopefully one in there that will work verbatim for each platform that is working.
Please help us triage these flags and give them better homes!
Specifying Dependencies
As mentioned in the introduction to this chapter, one can think about a build time vs run time distinction whether cross-compiling or not.
In the case of cross-compilation, this corresponds with whether a derivation running on the native or foreign platform is produced.
An interesting thing to think about is how this corresponds with the three Autoconf platforms.
In the run-time case, the depending and depended-on package simply have matching build, host, and target platforms.
But in the build-time case, one can imagine "sliding" the platforms one over.
The depended-on package's host and target platforms (respectively) become the depending package's build and host platforms.
This is the most important guiding principle behind cross-compilation with Nixpkgs, and will be called the sliding window principle.
In this manner, given the 3 platforms for one package, we can determine the three platforms for all its transitive dependencies.
Some examples will probably make this clearer.
If a package is being built with a (build, host, target) platform triple of (foo, bar, bar), then its build-time dependencies would have a triple of (foo, foo, bar), and those packages' build-time dependencies would have triple of (foo, foo, foo).
In other words, it should take two "rounds" of following build-time dependency edges before one reaches a fixed point where, by the sliding window principle, the platform triple no longer changes.
Indeed, this happens with cross compilation, where only rounds of native dependencies starting with the second necessarily coincide with native packages.
The depending package's target platform is unconstrained by the sliding window principle, which makes sense in that one can in principle build cross compilers targeting arbitrary platforms.
How does this work in practice? Nixpkgs is now structured so that build-time dependencies are taken from buildPackages, whereas run-time dependencies are taken from the top level attribute set.
For example, buildPackages.gcc should be used at build time, while gcc should be used at run time.
Now, for most of Nixpkgs's history, there was no buildPackages, and most packages have not been refactored to use it explicitly.
Instead, one can use the four attributes used for specifying dependencies as documented in .
We "splice" together the run-time and build-time package sets with callPackage, and then mkDerivation for each of four attributes pulls the right derivation out.
This splicing can be skipped when not cross compiling as the package sets are the same, but is a bit slow for cross compiling.
Because of this, a best-of-both-worlds solution is in the works with no splicing or explicit access of buildPackages needed.
For now, feel free to use either method.
There is also a "backlink" __targetPackages, yielding a package set whose buildPackages is the current package set.
This is a hack, though, to accommodate compilers with lousy build systems.
Please do not use this unless you are absolutely sure you are packaging such a compiler and there is no other way.
Cross-building packages
More information needs to moved from the old wiki, especially , for this section.
Nixpkgs can be instantiated with localSystem alone, in which case there is no cross compiling and everything is built by and for that system,
or also with crossSystem, in which case packages run on the latter, but all building happens on the former.
Both parameters take the same schema as the 3 (build, host, and target) platforms defined in the previous section.
As mentioned above, lib.systems.examples has some platforms which are used as arguments for these parameters in practice.
You can use them programmatically, or on the command line like nix-build <nixpkgs> --arg crossSystem '(import <nixpkgs/lib>).systems.examples.fooBarBaz'.
While one is free to pass both parameters in full, there's a lot of logic to fill in missing fields.
As discussed in the previous section, only one of system, config, and parsed is needed to infer the other two.
Additionally, libc will be inferred from parse.
Finally, localSystem.system is also impurely inferred based on the platform evaluation occurs.
This means it is often not necessary to pass localSystem at all, as in the command-line example in the previous paragraph.
Many sources (manual, wiki, etc) probably mention passing system, platform, along with the optional crossSystem to nixpkgs:
import <nixpkgs> { system = ..; platform = ..; crossSystem = ..; }.
Passing those two instead of localSystem is still supported for compatibility, but is discouraged.
Indeed, much of the inference we do for these parameters is motivated by compatibility as much as convenience.
One would think that localSystem and crossSystem overlap horribly with the three *Platforms (buildPlatform, hostPlatform, and targetPlatform; see stage.nix or the manual).
Actually, those identifiers are purposefully not used here to draw a subtle but important distinction:
While the granularity of having 3 platforms is necessary to properly *build* packages, it is overkill for specifying the user's *intent* when making a build plan or package set.
A simple "build vs deploy" dichotomy is adequate: the sliding window principle described in the previous section shows how to interpolate between the these two "end points" to get the 3 platform triple for each bootstrapping stage.
That means for any package a given package set, even those not bound on the top level but only reachable via dependencies or buildPackages, the three platforms will be defined as one of localSystem or crossSystem, with the former replacing the latter as one traverses build-time dependencies.
A last simple difference then is crossSystem should be null when one doesn't want to cross-compile, while the *Platforms are always non-null.
localSystem is always non-null.
Cross-compilation infrastructureTo be written.
If one explores nixpkgs, they will see derivations with names like gccCross.
Such *Cross derivations is a holdover from before we properly distinguished between the host and target platforms
—the derivation with "Cross" in the name covered the build = host != target case, while the other covered the host = target, with build platform the same or not based on whether one was using its .nativeDrv or .crossDrv.
This ugliness will disappear soon.