C header includes in NixOS

I’m trying to build a C library on a build of NixOS running on WSL2. It requires the expat.h header.

In the NixOS build, I have included expat in environment.systemPackages, but gcc still cannot find the header.

Essentially, I wonder if GCC in NixOS has something similar to withPackages.

I have read the C - NixOS Wiki but still cannot figure it out.

I figured out a way, not sure if the best:

  • Set environment.extraOutputsToInstall = [ "dev" ] - This makes include and I guess .o, .so files available for all packages listed in environment.systemPackages. See [1]
  • Set environment.variables.C_INCLUDE_PATH = "${nixpkgs.expat.dev}/include" - This makes them available for GCC

[1] Nixpkgs 23.11 manual | Nix & NixOS

1 Like

I usually use nix-locate to find the package/output in which the header lives. Including it in buildInputs is usually enough for the compiler to find it.

2 Likes