Nix 101 Course / Section 7: Advanced packaging topics
Premium

7.5 Nix cross-compilation with pkgsCross

Lesson 51 of 68 · 23:52

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Cross Compilation Overview
  2. 2:00 Using Nixpkgs Cross Systems
  3. 5:30 Splicing and Cross-Compilation Inputs
  4. 10:20 Strict Dependencies and Portable Builds
  5. 14:20 Static Linking and Toolchain Settings
  6. 18:40 Build System Smells and Portability Advice
Transcript

Cross Compilation Overview

0:00 Okay, cross compilation. Even if you don't use cross compilation like daily because you don't do embedded systems or something like that, there's a few things that I want you to know. One thing is how easy cross compilation is on Nix and NixOS. So that even if you let's say you have like VMs somewhere in the cloud where you are on an Intel system and you want to deploy to ARM VMs, that's particularly easy with Nix. It's also very easy to keep your packages in a cross compilable fashion so that others can use your software easily to cross compile that using Nix. I just find it really important that people keep their packages portable, keep their own software as most useful as possible kind of. So we're going to go over a few tricks. So when you know how to use cross compilation as a user, you will most likely also build better packages for others. Let's have a look. In general, we can in the REPL, for example, import Nix packages as we always do. And then there's a sub attribute set packages, lib systems, examples. And from those you can pick for example aarch64 multiplatform, which is just ARM 64 bit and inject that into another Nix packages import with the cross system parameter that we didn't use yet. And then you get packages that naturally create ARM packages. Let's play with that right now. So what we can do

1:34 is we can say Nix REPL and then packages equals import. Typical dance, right? And then packages, lib, systems, examples, contains many, many interesting examples. So for example, we have Windows, NetBSD, VBSD, Cygwin, whatever. We have WebAssembly. We have RISC-V. We have

Using Nixpkgs Cross Systems

2:04 those reMarkable tablets, Raspberry Pi, PowerPC, the musl library instead of the C library for 32 and 64 bit Intel, musl library for Raspberry Pis, Texas Instruments, MSP processors and whatnot, different versions of Android. What's also interesting is we have, I think that's Nvidia Bluefield or Intel Bluefield. I don't know. GNU32 and GNU64 are just the standard Linux targets. We can compile for iPhone. Minimalist GNU environment for Windows and so on and so on. And that's crazy how much choice we have here. Let's say now we say cross-system equals examples, aarch64, multi-platform. Then we say cross-packages equals import nixpkgs, inherit cross-system. Now we can access cross-packages. And here we get just the normal packages. Hello, for example. And we can already see that this is not only just called hello dash and then the version, but hello aarch64 unknown Linux GNU, blah, blah, blah, some string in between that describes the target platform. And when we now build that, we get an output path.

The full transcript comes with the lesson.

About this lesson

Building ARM binaries on an Intel machine, Windows executables on Linux, static binaries with musl: Nix cross-compilation turns all of this into picking a different package set. This lesson shows pkgsCross and the crossSystem parameter, explains the splicing mechanism behind them, and teaches the habits that keep your own packages cross-compilable even if you never touch embedded systems.

In this video

  • A tour of pkgs.lib.systems.examples: ARM, RISC-V, WebAssembly, Windows, Android, Raspberry Pi, even iPhone targets, ready to use
  • Cross-building GNU hello and ripgrep for ARM and Windows live in the REPL, verified with the file utility
  • Splicing explained: how two callPackage invocations combine into one derivation that runs on your host but compiles for the target
  • The one mixup that breaks cross-compilation, and how strictDeps = true turns it into an immediate error instead of a mystery
  • pkgsStatic for static linking without touching your build system, plus the build system smells that quietly kill portability

After this lesson

You can cross-compile packages from nixpkgs for another architecture in one line, and you know the rules that keep your own derivations portable enough that others can do the same with your software.

Builds on: How callPackage works (6.3)

Links: