Cross-compilation issues with CGo, glibc, and aarch64-linux

Figured it out! :tada: The answer:

pkgs.pkgsHostTarget.buildGo119Module.override {
  stdenv = pkgsHostTarget.llvmPackages_14.stdenv;
}

Here is the fixed derivation:

{ pkgs ? import
    (builtins.fetchTarball {
      # nixos-unstable as of Tue, 6 Dec 2022 08:21:00 +0530 (latest revision with libbpf 1.0.1)
      name = "nixos-unstable-2022-12-06";
      url = "https://github.com/nixos/nixpkgs/archive/b10a520017ac319c1e57b07742efd2bcc918d160.tar.gz";
      sha256 = "0jdf78gjrhpj501i0qd79sy7nd2sfwvd5y67ixjggmbj93a6i7kx";
    })
    { crossSystem = crossSystem; }
, crossSystem ? null
, runTests ? false
}:

let
  inherit (pkgs) pkgsBuildHost pkgsHostTarget;
  # bpf = pkgsBuildHost.callPackage ./bpf { pkgs = pkgs; };
  # Dummy BPF program for simplification
  bpf = pkgsHostTarget.writeTextDir "cpu.bpf.o" "";
in
(pkgs.pkgsHostTarget.buildGo119Module.override {
  stdenv = pkgsHostTarget.llvmPackages_14.stdenv;
}) rec {
  pname = "parca-agent";
  version = "26f5194fd9477ec8ccb0e35f9677275c09211d7e";

  src = pkgsBuildHost.fetchFromGitHub {
    owner = "parca-dev";
    repo = pname;
    rev = version;
    sha256 = "sha256-e+VO/BmXSygfyjIlnkFgPW8jsQrcItX0icYI1xOY7pc=";
  };
  vendorSha256 = "sha256-knGgdAY9YNBfV0xkGc0GZq7hyHeR1C6yyGEpZIBzKDo=";

  nativeBuildInputs = with pkgsBuildHost; [
    nukeReferences
  ];

  buildInputs = with pkgsHostTarget; [
    elfutils.dev
    glibc.static
    libbpf
    zlib.static
  ];

  CGO_LDFLAGS = "-lbpf";

  ldflags = [ "-extldflags=-static" ];

  tags = [ "osusergo" "netgo" ];

  subPackages = "cmd/parca-agent";
  # Tests for these packages have additional requirements
  excludedPackages = "internal/pprof pkg/profiler e2e";

  preBuild = ''
    cp -f ${bpf}/cpu.bpf.o ./pkg/profiler/cpu/cpu-profiler.bpf.o
  '';

  # Nuke any references to other Nix store paths
  # https://github.com/NixOS/nixpkgs/blob/ccb52a00240f7bc4db651ac6521d42316361df56/pkgs/build-support/nuke-references/default.nix#L1-L4
  postBuild = ''
    nuke-refs "$GOPATH/bin/parca-agent"
  '';

  # Not necessary and/or desired (e.g. strip, patchelf...)
  # https://nixos.org/manual/nixpkgs/stable/#ssec-fixup-phase
  dontFixup = true;

  doCheck = runTests;
  # Test all packages
  preCheck = ''
    unset subPackages
  '';
}

I am still a bit confused about the pkgs<host><target> attributes, but it works, it just need some more testing to make sure everything is good.

Documentation for reference: Nixpkgs 23.11 manual | Nix & NixOS

1 Like