Trouble with C++ headers in editors inside nix-shell

When I run nix-shell in the project root, I can compile and run C++ code from the terminal, including standard headers like <iostream>, <vector>, and libraries like Boost and spdlog—everything works.
I have a shell.nix for C++ development with GCC, Boost, CMake, etc.
shell.nix:

  buildInputs = with pkgs; [
    stdenv.cc.cc.lib
    boost
    cmake
    ninja
    spdlog
    yaml-cpp
    openssl
    protobuf
    gnulib
    nlohmann_json
    catch2_3
  ];

Problem: When I open VSCodium or LunarVim (even from inside the nix-shell), the language server shows missing headers like <iostream>.

I’ve tried exporting PATH in shellHook, but the editor still doesn’t see the standard headers.

    export CPATH=${pkgs.stdenv.cc.cc.lib}/include/c++/${pkgs.stdenv.cc.version}:$CPATH
    export LIBRARY_PATH=${pkgs.stdenv.cc.cc.lib}/lib:$LIBRARY_PATH
    export LD_LIBRARY_PATH=${pkgs.stdenv.cc.cc.lib}/lib:$LD_LIBRARY_PATH

Also, My codium setup:

{config, pkgs, ...}: {
  programs.vscode = {
    enable = true;
    package = pkgs.vscodium;
    extensions = with pkgs.vscode-extensions; [
      vscodevim.vim
      jnoortheen.nix-ide
      llvm-vs-code-extensions.vscode-clangd # I'm using this one for C/C++ Completion
      github.github-vscode-theme
      rust-lang.rust-analyzer
      pylyzer.pylyzer
      usernamehw.errorlens
      christian-kohler.path-intellisense
      formulahendry.code-runner
      pkief.material-icon-theme
      zguolee.tabler-icons
    ];
  };
}

Questions:

  1. How can I make editors fully recognize C++ headers from a nix-shell?
  2. Is there a preferred NixOS workflow for GUI editors + nix-shell for C++ development?

Thanks for any tips!

I assume you’re using clangd. It needs a compile_commands.json file to be able to pick up these headers. You can write (or generate) one depending on your environment. Check some sources for it online, it’s been discussed very heavily in programmer forums.

1 Like

Thanks for the suggestion regarding compile_commands.json. While that’s the standard approach, I found a simpler solution for my environment: installing clang-tools directly via shell.nix resolved the code completion issues immediately.