Clang-tooling woes on NixOS


#1

I’ve been trying to get a good working C++ project environment in NixOS. My project requires that I use clang and cmake. These work great, with the help of the clang wrapper which sets the stdlib paths that make libraries like iostream available. The trouble comes with using clang-tooling like clang-tidy. These aren’t wrapped, and are unable to find these system libraries. The compile_commands.json file produced by cmake also doesn’t contain reference to the system library paths, because that’s encapsulated within the clang binary wrapper. I couldn’t find any reference C++ projects that are built with nix. Any suggestions on how I can get it to clang-tidy to also locate stdlibs during compilation?


#2

This is definitely something that I think we should try to figure out. Right now the experience is pretty bad. You can do something like this:

CFLAGS=$(cat $(nix-build '<nixpkgs>' -A stdenv.cc)/nix-support/{cc,libc}-cflags)
LDFLAGS=$(cat $(nix-build '<nixpkgs>' -A stdenv.cc)/nix-support/cc-ldflags))

and pass these into clang-tidy. But ideally the user wouldn’t have to set this up.


#3

Thanks for getting back. Could you illustrate how you’d pass them to clang-tidy? I’ve tried it with clang-tidy -p=build/ path/to/srcfile.cpp -- -DCFLAGS=$CFLAGS -DLDFLAGS=$LDFLAGS to no avail.


#5

Would clang-tidy -extra-arg="$CFLAGS" work? In the end this should become a wrapper we can have in nixpkgs similar to what we do with ccls/cquery.


#6
[nix-shell:~/projects/SPA]$ clang-tidy -extra-arg="$CFLAGS" src/spa/src/simple_parser/parser.cpp 
Error while trying to load a compilation database:
Could not auto-detect compilation database for file "src/spa/src/simple_parser/parser.cpp"
No compilation database found in /home/jethro/projects/SPA/src/spa/src/simple_parser or any parent directory
json-compilation-database: Error while opening JSON database: No such file or directory
Running without flags.
1 warning and 1 error generated.
Error while processing /home/jethro/projects/SPA/src/spa/src/simple_parser/parser.cpp.
warning:  -B/nix/store/hlnxw4k6931bachvg5sv0cyaissimswb-gcc-7.4.0-lib/lib
-B/nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/ -idirafter /nix/store/sr4253np2gz2bpha4gn8gqlmiw604155-glibc-2.27-dev/include -idirafter /nix/store/d4n93jn9fdq8fkmkm1q8f32lfagvibjk-gcc-7.4.0/lib/gcc/*/*/include-fixed: 'linker' input unused [clang-diagnostic-unused-command-line-argument]
/home/jethro/projects/SPA/src/spa/src/simple_parser/parser.cpp:1:10: error: 'simple_parser/exceptions.h' file not found [clang-diagnostic-error]
#include "simple_parser/exceptions.h"
         ^

[nix-shell:~/projects/SPA]$ echo $CFLAGS
-B/nix/store/hlnxw4k6931bachvg5sv0cyaissimswb-gcc-7.4.0-lib/lib -B/nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/ -idirafter /nix/store/sr4253np2gz2bpha4gn8gqlmiw604155-glibc-2.27-dev/include -idirafter /nix/store/d4n93jn9fdq8fkmkm1q8f32lfagvibjk-gcc-7.4.0/lib/gcc/x86_64-unknown-linux-gnu/7.4.0/include-fixed

unfortunately not ): here it is missing project dependencies. If I pass it the cmake build directory:

[nix-shell:~/projects/SPA]$ clang-tidy -p=build/ -extra-arg="$CFLAGS" src/spa/src/simple_parser/parser.cpp 
1 warning and 1 error generated.
Error while processing /home/jethro/projects/SPA/src/spa/src/simple_parser/parser.cpp.
warning:  -B/nix/store/hlnxw4k6931bachvg5sv0cyaissimswb-gcc-7.4.0-lib/lib
-B/nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/ -idirafter /nix/store/sr4253np2gz2bpha4gn8gqlmiw604155-glibc-2.27-dev/include -idirafter /nix/store/d4n93jn9fdq8fkmkm1q8f32lfagvibjk-gcc-7.4.0/lib/gcc/*/*/include-fixed: 'linker' input unused [clang-diagnostic-unused-command-line-argument]
/home/jethro/projects/SPA/src/spa/src/simple_parser/exceptions.h:10:10: error: 'stdexcept' file not found [clang-diagnostic-error]
#include <stdexcept>