How to override stdenv for all packages in mkShell

key information was left out so there came to your confusion. To contextualize it, I’m using the following flake.nix for a project that requires some features from libc++. The is no problem with building the project. The problem is clangd always points to headers file during my daily editing job. Then, I found those unwanted header files come from gcc which results from stdenv.

Without using pkgs.pkgsLLVM, the clang dependency always searches at least from gcc’s c++/v1/include directory. And, this extra unwanted gcc causes a problem. Those gcc dependencies actually have no such an impact. I guess this ambiguity of different gcc dependencies caused confusion :sweat_smile:

Still I have no idea why config.replaceStdenv won’t solve the problem. :sweat_smile: And it’s not needed for my flake.nix; pkgs.pkgsLLVM suffices.

{
  description = "cracking the coding interview";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, flake-utils, ... }:
    flake-utils.lib.eachDefaultSystem (
      system:
        let
          pkgs = import nixpkgs {
            inherit system;
            # XXX: There is no easy way to avoid libstdc++ include files. See
            # https://discourse.nixos.org/t/use-clang-without-gccs-c-standard-library/9884
            # https://discourse.nixos.org/t/how-to-override-stdenv-for-all-packages-in-mkshell/10368/10
            # https://discourse.nixos.org/t/gcc11stdenv-and-clang/17734
            # overlays = [ (_: super: { stdenvNoCC = super.llvmPackages_latest.libcxxStdenv; }) ];
            # config.replaceStdenv = { pkgs, ...}: pkgs.llvmPackages_latest.stdenv;
          };
          llvm = pkgs.pkgsLLVM.llvmPackages_latest;
          lib = nixpkgs.lib;

        in
          {
            devShell = llvm.stdenv.mkDerivation {
              name = "shell";
              nativeBuildInputs = [
                # builder
                # p.gnumake
                # p.bear
                pkgs.cmake  # for discovering libraries
                pkgs.pkg-config
                pkgs.meson
                pkgs.ninja
                # debugger
                # llvm.lldb
                # pkgs.gdb

                pkgs.gtest
                pkgs.fmt

                # pkgs.leetcode-cli

                llvm.bintools
                pkgs.clang-tools_14  # don't use clangd from llvm.clang
              ] ++ lib.optionals pkgs.stdenv.isLinux [ llvm.lld ]
              ;
              shellHook = lib.optionalString pkgs.stdenv.isLinux ''
                export CC_LD="lld"
                export CXX_LD="lld"
              '';
              LD_LIBRARY_PATH = lib.strings.makeLibraryPath [ pkgs.fmt pkgs.gtest llvm.libcxx ];
              LLVM_PROFILE_FILE="/tmp/test.profraw";
            };
          }
    );
}
2 Likes