Skip to content

Instantly share code, notes, and snippets.

@pete3n
Created January 27, 2024 20:38
Show Gist options
  • Save pete3n/97608771e835ffcd715506b70f918bea to your computer and use it in GitHub Desktop.
Save pete3n/97608771e835ffcd715506b70f918bea to your computer and use it in GitHub Desktop.
Statically linking cross-compiled Nix packages

Combining staticPkgs with crossPkgs

Downloading nixpkgs allows you to easily cross-compile Nix packages with

nix build .#pkgsCross.architecture.packageName

Where architecture is the target architecture you are cross-compiling to (like aarch64-multiplatform), and packageName is the package you are building (traceroute in this example).

This will output the package output to ./result which should include your output binary in ./result/bin . This output binary typically dynamically linked, but what if we need to cross-compile AND statically link the output binary? We can use pkgsStatic to achieve this:

nix build .#pkgsCross.aarch64-multiplatform.pkgsStatic.traceroute

We could also write a small flake like static-traceroute.nix as shown below and build it with:

nix build -f ./static-traceroute.nix
{ pkgs ? import <nixpkgs> {} }:
let
crossPkgs = pkgs.pkgsCross.aarch64-multiplatform;
staticPkgs = crossPkgs.pkgsStatic;
staticTraceroute = staticPkgs.traceroute.overrideAttrs (oldAttrs: {
# Additional configurations or overrides for static building
});
in
staticTraceroute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment