How to get C++ headers included in NIX_CFLAGS_COMPILE?

Here is a simple shell.nix:

with (import <nixpkgs> {});

(mkShell.override {stdenv = llvmPackages_latest.stdenv;}) {
    buildInputs = [
      cmake
      gtest
    ];
}

which produces the following NIX_CFLAGS_COMPILE

-frandom-seed=8nzawrykna
-isystem /nix/store/b4zvm3271fldf2pn3yj03vichy0k28kz-compiler-rt-libc-13.0.1-dev/include
-isystem /nix/store/d1mprylqwl2lgjg7ng7svz3ryvf5m563-gtest-1.11.0-dev/include
-isystem /nix/store/b4zvm3271fldf2pn3yj03vichy0k28kz-compiler-rt-libc-13.0.1-dev/include
-isystem /nix/store/d1mprylqwl2lgjg7ng7svz3ryvf5m563-gtest-1.11.0-dev/include

It would be very convenient for developing cmake projects if there was an easy way to have

"-isystem ${gccForLibs}/include/c++/${gccForLibs.version}"
"-isystem ${gccForLibs}/include/c++/${gccForLibs.version}/${stdenv.targetPlatform.config}"

also included in NIX_CFLAGS_COMPILE. This functionality already exists in cc-wrapper.sh:

but as far as I can tell, the environment variable isCxx isn’t an attribute of stdenv which i can modify. Is there a way to trigger isCxx that i am missing?

1 Like

Sorted all of this out this weekend. Firstly, a PR to stop the flag duplication in NIX_CFLAGS_COMPILE: cc-wrapper/setup-hook.sh: remove duplicate flags in NIX_CFLAGS_COMPILE by danielbarter · Pull Request #191724 · NixOS/nixpkgs · GitHub.

Not super important, but it was bugging me. Secondly, reading the cc-wrapper.sh code more closely, all of the command line arguments are being written to files in nix-support. We can read those. For example, here is a shell.nix with a clang standard environment that exposes all the foundational compilation flags + flags for dependencies:

with (import <nixpkgs> {});
let llvm = llvmPackages_latest;
in (mkShell.override {stdenv = llvm.stdenv;}) {
    buildInputs = [
      cmake
      gtest
    ];

    CXXFLAGS = builtins.concatStringsSep " " [
      (lib.removeSuffix "\n" (builtins.readFile "${llvm.clang}/nix-support/cc-cflags"))
      (lib.removeSuffix "\n" (builtins.readFile "${llvm.clang}/nix-support/libc-cflags"))
      (lib.removeSuffix "\n" (builtins.readFile "${llvm.clang}/nix-support/libcxx-cxxflags"))
    ];


    shellHook = "export CXXFLAGS+=$NIX_CFLAGS_COMPILE";
}

CXXFLAGS is used by cmake specifically, but you could use any environment variable you want.

Hosted by Flying Circus.