How to provide include path to stblib to clang derived tool?

I have written a flake of binder, a C++ binding generator for python. It uses (lib)clang under the hood and clang args can be passed via --extra-args.

But when running it, it cannot find the C++ standard library. I’ve been trying to add -I/nix/store/... pathes manually and chasing the errors, but I cannot find the right paths. Always still something is missing (for example stddef.h or whatever).

How to provide all required and correct include pathes?

{
  description = "C++ bindings generator for python";

  inputs = {
    nixpkgs.url = "nixpkgs/nixos-unstable";
  };

  outputs = { self, nixpkgs }:
    let
      supportedSystems = [ "x86_64-linux" ];
      forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
      pkgs = forAllSystems (system: import nixpkgs { inherit system; });
    in {

      packages = forAllSystems (system:
        let
          binderDrv = with pkgs.${system}; stdenv.mkDerivation {
          pname = "binder";
          version = "1.3.0";

          src = fetchurl {
            url = "https://github.com/RosettaCommons/binder/archive/refs/tags/v1.3.0.tar.gz";
            hash = "sha256-Iqr0rekqis8VGY6Eep9k/9I59DJSryTOqnzIvcRTELI=";
          };

          nativeBuildInputs = [
            cmake
          ];

          buildInputs = [
            (python3Packages.pybind11.overrideAttrs (old: {
              src = fetchFromGitHub {
                  owner = "RosettaCommons";
                  repo = "pybind11";
                  rev = "5b0a6fc2017fcc176545afe3e09c9f9885283242";
                  hash = "sha256-n7nLEG2+sSR9wnxM+C8FWc2B+Mx74Pan1+IQf+h2bGU=";
                };
            }))

            libxml2
            libffi
            libllvm
            libclang
          ];

          enableParallelBuilding = true;
          cmakeFlags = [ "-DBINDER_ENABLE_TEST=OFF" ];

          meta = {
            desciption = "binder";
            homepage = "https://cppbinder.readthedocs.io/";
            platforms = lib.platforms.linux;
          };
        };
        in {
          default = binderDrv;
        }
      );
    };
}

I can make it work by supplying the following manually (by copying from g++ -v -E -x c++ - < /dev/null, the project itself is using gcc-9):

-isystem '/nix/store/hb947hf5a1xr6w1lg2vn813g5gw93ajy-gcc-9.5.0/lib/gcc/x86_64-unknown-linux-gnu/9.5.0/../../../../include/c++/9.5.0' \
-isystem '/nix/store/hb947hf5a1xr6w1lg2vn813g5gw93ajy-gcc-9.5.0/lib/gcc/x86_64-unknown-linux-gnu/9.5.0/../../../../include/c++/9.5.0/x86_64-unknown-linux-gnu' \
-isystem '/nix/store/hb947hf5a1xr6w1lg2vn813g5gw93ajy-gcc-9.5.0/lib/gcc/x86_64-unknown-linux-gnu/9.5.0/../../../../include/c++/9.5.0/backward' \
-isystem '/nix/store/hb947hf5a1xr6w1lg2vn813g5gw93ajy-gcc-9.5.0/lib/gcc/x86_64-unknown-linux-gnu/9.5.0/include' \
-isystem '/nix/store/hb947hf5a1xr6w1lg2vn813g5gw93ajy-gcc-9.5.0/include' \
-isystem '/nix/store/hb947hf5a1xr6w1lg2vn813g5gw93ajy-gcc-9.5.0/include/c++/9.5.0/' \
-isystem '/nix/store/hb947hf5a1xr6w1lg2vn813g5gw93ajy-gcc-9.5.0/lib/gcc/x86_64-unknown-linux-gnu/9.5.0/include-fixed' \
-isystem '/nix/store/9bldnbh7kcxyq8y0g1bifd5ywz7m3bq9-glibc-2.37-8-dev/include' \

But how to do it properly?

I ended up doing it via a wrapper using the clang provided cflags.

In the derivation:

          postFixup = ''
            # We need to tell binder where to find the standard library headers.

            standard_library_includes="$(<${clang}/nix-support/cc-cflags) $(<${clang}/nix-support/libc-cflags) $(<${clang}/nix-support/libcxx-cxxflags)"

            export standard_library_includes

            wrapped=".binder-wrapped"
            export wrapped

            mv $out/bin/binder $out/bin/$wrapped
            substituteAll ${./wrapper} $out/bin/binder
            chmod --reference=$out/bin/$wrapped $out/bin/binder
          '';

wrapper

#! @shell@ -e

read -a extraArgsArray <<<"@standard_library_includes@"

if [ "${NIX_CFLAGS_COMPILE}" != "" ]; then
	read -a cflags_array <<<${NIX_CFLAGS_COMPILE}
	extraArgsArray+=("${cflags_array[@]}")
fi

extraArgs=$(printf ' --extra-arg-before=%s' "${extraArgsArray[@]}")

exec -a "$0" "@out@/bin/@wrapped@" $extraArgs $@