This repository contains the accompanying code for the Nixcademy blog post: Cross-Compiling NixOS images
This project demonstrates how to set up a clean and scalable cross-compilation architecture for NixOS images using Nix Flakes.
Instead of hardcoding build targets, this repository shows how to:
- Strictly separate the system configuration from the build metadata.
- Use
nixpkgs.buildPlatformandnixpkgs.hostPlatformto define cross-builds. - Support cross-compilation on macOS (via the Linux builder).
It serves as the practical example for the fourth part of the Nixcademy series on building minimal, self-inflating, and auto-updating NixOS appliance images.
The article goes a step further to automatically generate the Cartesian product of all possible build and target platforms (e.g., building an aarch64 image from x86_64, or vice versa).
For a detailed explanation of the logic behind extendModules, how to generalize cross-compilation functions, and how to set up the flake to build "anything from anywhere," please read the full article:
π Cross-Compiling NixOS images @ Nixcademy
You can see all available build targets (combinations of build platform and target image) by running:
nix flake showYou will see output similar to this, showing that you can build images for different architectures from your current system:
ββββpackages
β ββββaarch64-linux
β β ββββimage-aarch64
β β ββββimage-x86_64
β ββββx86_64-linux
β ββββimage-aarch64
β ββββimage-x86_64
To cross-compile an AArch64 (ARM64) image while running on an x86_64 machine:
$ nix build .#image-aarch64You can load the flake into the Nix REPL to inspect specific configuration values without building the entire image:
nix repl .
# Check the system version
nix-repl> nixosConfigurations.image.config.system.nixos.version
# Check where specific options are defined
nix-repl> :p nixosConfigurations.image.options.networking.hostName.definitionsWithLocations