Clang, clang++, and clangd can't find headers, even with compile_commands.json

Hello @joralebl @xiugaze ,

Good news!

I found a fix that does not involve flakes, CMake, or even a nix-shell – just using my configuration.nix file.

Before, I had the same issue with clangd, where an example output would look like

I[13:54:21.663] Indexing headers... E[13:54:21.664] [pp_file_not_found] Line 1: 'unordered_map' file not found

I ran some diagnostics. In particular, we know that nixpkgs provide a wrapper around clangd, so we can read the clang-tools wrapper, along with finding out what my libcxx flags are (formatted for readability):

[nix-shell:~/temp]$ cat $(dirname $(dirname $(which clang)))/nix-support/libcxx-cxxflags 2>/dev/null || echo "file not found"

-cxx-isystem /nix/store/...-gcc-14.3.0/include/c++/14.3.0 -fmacro-prefix-map=/nix/store/...-gcc-14.3.0/include/c++/14.3.0=/nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-gcc-14.3.0/include/c++/14.3.0 ...


[nix-shell:~/temp]$ cat $(which clangd)
#!/nix/store/rlq03x4cwf8zn73hxaxnx0zn5q9kifls-bash-5.3p3/bin/sh

buildcpath() {
  local path after
  while (( $# )); do
    case $1 in
        -isystem)
...

So, clang uses -cxx-isystem for libcxx, but the clangd wrapper only parsed -isystem. We have an issue!


Here is the link to the clang-tools wrapper. Early in September 2025, PR 445095 makes libc++ headers always available in sysroot where clang searches, and in November PR 462747 introduced a hotfix that directly addresses clang-tools.

Both updates are now backported to 25.11. All I had to do was update my system:

$ sudo nix-channel --update

# To clear out cache? Unsure if this is needed
$ nix-collect garbage -d

$ sudo nixos-rebuild switch --upgrade

Verify that you did update and clean cache correctly:

$ nix-shell -p clang-tools --run "cat \$(which clangd) | grep cxx-isystem"

And voila.


FYI, I use clangd with helix, but it works with command invocation as well:

[svi@nixos:~/temp]$ clangd --check=test.cpp
I[15:06:16.513] clangd version 21.1.2
...
I[15:06:17.147] All checks completed, 0 errors

My relevant portion of configuration.nix is as follows:

environment.systemPackages = with pkgs; [
	# c development
	clang-tools
	clang
	gcc
	glibc
	libgcc
	gdb
	gnumake
	cmake
1 Like