Clangd issues on nixos: missing headers

Hi all,

I use clangd while working on neovim. The compile-commands generated on other distributions work fine, but I’m getting missing library issues on nix. Has anyone solved this issue?

If you need an editor + language client to try this out, you can use neovim’s minimal init.lua here: https://github.com/neovim/nvim-lspconfig/blob/86b316140a5fdc902e89ff585ce429357ce4d5df/test/minimal_init.lua (with clangd substituted for pyright)

  1. git clone https://github.com/neovim/neovim && cd neovim
  2. nix develop contrib/
  3. cmakeConfigurePhase
  4. cp compile-commands.json ..
  5. cd ..
  6. nvim src/nvim/main.c

I get many errors of the form:

 'assert.h' file not found with <angled> include; use "quotes" instead (fix available)`
 
 In included file: 'inttypes.h' file not found

which don’t occur on traditional distributions.

3 Likes

I’ve seen the same behavior without investigating. I wonder if changing the llvm version fixes it.

I tried llvm_latest and llvm_{8,9,10}. 7 is too old. All of the above still show the issue :confused:

Try add

CPATH = lib.makeSearchPathOutput "dev" "include" buildInputs;

as an attribute in your derivation, that might fix your problem.

Unfortunately that didn’t seem to do it, still getting

6  Diagnostics:
     7  1. 'assert.h' file not found with <angled> include; use "quotes" instead (fix available)
     8  2. In included file: 'auto/config.h' file not found

Does this help? Clang tooling woes 2 - #4 by bjornfor

Maybe I’m using it incorrectly, but I get the same issue with nix develop ./contrib when I make the following modifications to the flake:

diff --git a/contrib/flake.nix b/contrib/flake.nix
index d18534215..a21e0466a 100644
--- a/contrib/flake.nix
+++ b/contrib/flake.nix
@@ -13,14 +13,35 @@
           pkgs = nixpkgs.legacyPackages.${prev.system};
         in
         rec {
+          clang-tools = let
+          in
+            prev.clang-tools.overrideAttrs (
+              o: {
+                pname = o.pname + "-with-fixed-cpath";
+
+                installPhase =
+                  builtins.replaceStrings [ "export clang" ] [
+                    ''
+                      libcpp_includes+=":$libc_includes"
+                      libc_includes=""
+                      export clang''
+                  ]
+                    o.installPhase;
+
+              }
+            );
+
           neovim = pkgs.neovim-unwrapped.overrideAttrs (oa: {
             version = "master";
             src = ../.;

             buildInputs = oa.buildInputs ++ ([
               pkgs.tree-sitter
+              final.clang-tools
             ]);

+            # CPATH = prev.lib.makeSearchPathOutput "dev" "include" oa.buildInputs;
+
             cmakeFlags = oa.cmakeFlags ++ [
               "-DUSE_BUNDLED=OFF"
             ];

I fixed this in our flake contrib: add clang-tools to nix flake by mjlbach · Pull Request #14340 · neovim/neovim · GitHub

Just needed to add clang-tools to native buildinputs

3 Likes

Like so:

          # C++
          # Clangd from clang-tools must come first.
          (hiPrio clang-tools.override {
            llvmPackages = llvmPackages_16;
            enableLibcxx = false;
          })
          # Do not use the clangd from this package as it does not work correctly with
          # stdlib headers.
          llvmPackages_16.libstdcxxClang
2 Likes

This worked, thank you!