Unable to set up a C++ dev environment because of conflicting stdlibs

I’m using the following dev flake:

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
  };
  outputs = {nixpkgs, ...}: let
    pkgsLinux = import nixpkgs { system = "x86_64-linux"; };
  in {
      devShells."x86_64-linux".default = pkgsLinux.mkShell.override { stdenv = pkgsLinux.llvmPackages_19.stdenv; } {
        system = "x86_64-linux";
        packages = with pkgsLinux; [ 
          ninja
          cmake
          llvmPackages_19.clang-tools
        ];
        shellHook = ''
          export CPLUS_INCLUDE_PATH="${pkgsLinux.llvmPackages_19.libcxx.dev}/include/c++/v1:$CPLUS_INCLUDE_PATH"
          export CMAKE_CXX_FLAGS="-stdlib=libc++ -I${pkgsLinux.llvmPackages_19.libcxx.dev}/include/c++/v1"
        '';
      };
    };
}

This works on Nix Darwin but on my NixOS machine, when I run make I get an error like:

[ 16%] Building CXX object CMakeFiles/lox.dir/src/main.cpp.o
In file included from /home/dotuncle/projects/lox/src/main.cpp:1:
In file included from /nix/store/qdknxw57cwy1jkrhq7fzmiis73j42jv6-gcc-14.3.0/include/c++/14.3.0/cstdlib:81:
/nix/store/qdknxw57cwy1jkrhq7fzmiis73j42jv6-gcc-14.3.0/include/c++/14.3.0/bits/std_abs.h:56:3: error: declaration conflicts with target of using declaration already in scope
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^
/nix/store/f8dggj2ls5nfwyglvk9mqn418z9g1a6z-libcxx-19.1.7-dev/include/c++/v1/stdlib.h:113:53: note: target of using declaration
  113 | _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI long abs(long __x) _NOEXCEPT { return __builtin_labs(__x); }
      |                                                     ^
/nix/store/qdknxw57cwy1jkrhq7fzmiis73j42jv6-gcc-14.3.0/include/c++/14.3.0/bits/std_abs.h:52:11: note: using declaration
   52 |   using ::abs;
      |           ^
/nix/store/qdknxw57cwy1jkrhq7fzmiis73j42jv6-gcc-14.3.0/include/c++/14.3.0/bits/std_abs.h:61:3: error: declaration conflicts with target of using declaration already in scope
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^
/nix/store/f8dggj2ls5nfwyglvk9mqn418z9g1a6z-libcxx-19.1.7-dev/include/c++/v1/stdlib.h:114:58: note: target of using declaration
  114 | _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI long long abs(long long __x) _NOEXCEPT { return __builtin_llabs(__x); }
      |                                                          ^
/nix/store/qdknxw57cwy1jkrhq7fzmiis73j42jv6-gcc-14.3.0/include/c++/14.3.0/bits/std_abs.h:52:11: note: using declaration
   52 |   using ::abs;
      |           ^
...

For some reason, gcc headers are being included and this is causing a conflict. I tried asking AI for help but it was unfruitful other than helping me find where the gcc headers are included, which is -isystem and -fmacro-prefix-map.

I can’t really do any C++ development right now so any help would be appreciated. Thanks!

And remove the funny envvars.

I tried updating it to

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
  };
  outputs = {nixpkgs, ...}: let
    pkgsLinux = import nixpkgs { system = "x86_64-linux"; };
  in {
      devShells."x86_64-linux".default = pkgsLinux.mkShell.override { stdenv = pkgsLinux.llvmPackages_19.stdenv; } {
        system = "x86_64-linux";
        packages = with pkgsLinux; [ 
          cmake
        ];
      };
    };
}

But when I do it without the funny envvars it’s unable to find stdlib:

dotuncle@kagerou:~/projects/lox/build/ > make
[ 16%] Building CXX object CMakeFiles/lox.dir/src/main.cpp.o
In file included from /home/dotuncle/projects/lox/src/main.cpp:11:
In file included from /home/dotuncle/projects/lox/src/interpreter/Parser.hpp:1:
In file included from /home/dotuncle/projects/lox/src/interpreter/Stmt.hpp:6:
/home/dotuncle/projects/lox/src/interpreter/Expr.hpp:31:8: error: no template named 'unique_ptr' in namespace 'std'
   31 |   std::unique_ptr<Expr> value;
      |   ~~~~~^
/home/dotuncle/projects/lox/src/interpreter/Expr.hpp:32:28: error: no template named 'unique_ptr' in namespace 'std'
   32 |   Assign(Token &name, std::unique_ptr<Expr> &value) : name { name }, value { std::move(value) } {};
      |                       ~~~~~^
/home/dotuncle/projects/lox/src/interpreter/Expr.hpp:40:8: error: no template named 'unique_ptr' in namespace 'std'
   40 |   std::unique_ptr<Expr> expr;
      |   ~~~~~^
/home/dotuncle/projects/lox/src/interpreter/Expr.hpp:41:17: error: no template named 'unique_ptr' in namespace 'std'
   41 |   Grouping(std::unique_ptr<Expr> &expr) : expr { std::move(expr) } {};
      |            ~~~~~^
...

Nevermind: I was misinterpreting the error. This solved it. Thanks!

Actually, this solution only works when I build with make. When I use ninja, it can’t find the standard library:

dotuncle@kagerou:~/projects/lox/build/ > ninja
[1/12] Scanning /home/dotuncle/projects/lox/src/main.cpp for CXX dependencies
FAILED: [code=1] CMakeFiles/lox.dir/src/main.cpp.o.ddi 
"/nix/store/mdh9zj32f7s2xhg683g8x5sg9qba4rxb-clang-19.1.7/bin/clang-scan-deps" -format=p1689 -- clang++  -isystem /nix/store/fbfcll570w9vimfbh41f9b4rrwnp33f3-clang-wrapper-19.1.7/include/c++/v1 -isystem "/nix/store/fbfcll570w9vimfbh41f9b4rrwnp33f3-clang-wrapper-19.1.7/lib/clang/*/include" -Werror -Wall -Weffc++ -Wextra -Wconversion -Wsign-conversion -std=gnu++23 -x c++ /home/dotuncle/projects/lox/src/main.cpp -c -o CMakeFiles/lox.dir/src/main.cpp.o -resource-dir "/nix/store/fbfcll570w9vimfbh41f9b4rrwnp33f3-clang-wrapper-19.1.7/resource-root" -MT CMakeFiles/lox.dir/src/main.cpp.o.ddi -MD -MF CMakeFiles/lox.dir/src/main.cpp.o.ddi.d > CMakeFiles/lox.dir/src/main.cpp.o.ddi.tmp && mv CMakeFiles/lox.dir/src/main.cpp.o.ddi.tmp CMakeFiles/lox.dir/src/main.cpp.o.ddi
Error while scanning dependencies for /home/dotuncle/projects/lox/src/main.cpp:
/home/dotuncle/projects/lox/src/main.cpp:1:10: fatal error: 'cstdlib' file not found
[2/12] Scanning /home/dotuncle/projects/lox/src/interpreter/Environment.cpp for CXX dependencies
FAILED: [code=1] CMakeFiles/lox.dir/src/interpreter/Environment.cpp.o.ddi
...

Also, clangd in my editor isn’t able to resolve the standard library from compile_commands.json. Am I missing something obvious?

Mind updating with the exact shell you’re using now?

Also for the compile_commands.json bit, see

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
  };
  outputs = {nixpkgs, ...}: let
    pkgsLinux = import nixpkgs { system = "x86_64-linux"; };
  in {
      devShells."x86_64-linux".default = pkgsLinux.mkShell.override { stdenv = pkgsLinux.llvmPackages_19.stdenv; } {
        system = "x86_64-linux";
        packages = with pkgsLinux; [ 
          ninja
          cmake
        ];
      };
    };
}

is it.
Those additions to CMakeLists.txt fixed both issues! Thanks again for the help.