Developing clang tools within nix-shell. How to wrap the program within a nix shell?

Hi everybody,

I’m at my first steps trying to set up a reproducible development environment for developing Clang tools.
In my case my clang tool performs source-code level transformation.

So far I’ve created a flake-based shell.nix and inside nix develop my program compiles fine. But, from what I got from these two threads [1] [2], just like other unwrapped clang tools, my program can’t find header files which are typically found in places like /usr/include.
I tried to use buildFHSUserEnv but I can only get the libraries end up in /usr/.... It seems that the header files are not copied to FHS, which maybe relates this issue.

I think my problem is similar to Python + Qt woes, because within a nix shell I’m developing something that needs to be wrapped. But the proposed solution to set up the environment variables in shellHooks doesn’t work in my case because the environment variables set by the wrapper script for clang tools make compilation fail, probably because CPATH and CPLUS_INCLUDE_PATH also influence the compiler. So I guess I need to wrap my program, so that the environment is only applied to my program, rather than the whole shell.

So fundamentally I guess my question is: is there a neat way to wrap a program that is compiled inside a nix shell on-the-fly, without too much developer intervention each time the program is recompiled?

I don’t think there is a way to wrap the unknown. Wrapping is taking something which is known, and modifying how it will be executed by applying other known values. (E.g. take clang, and wrap it with some env vars that point to headers).

I think my case is something between the known and unknown. I can specify in the derivation of my application how it should be wrapped. Then I can use nix-build and have my application finely wrapped. But, as I develop, I don’t want to use nix-build at the tiniest change to the project, because AFAIK nix-build rebuilds the application from scratch. I want to develop inside a nix-shell (or nix develop), with the reproducible dev environment that I can specify using Nix. But the problem is that building inside nix-shell (using the project-specific build system, e.g. CMake, Makefile, Cargo, etc) doesn’t do the wrapping for me. I thought that I could incorporate the wrapping process in the project build system (CMake, Makefile, etc), but this means that there are two places that contain the wrapping logic: the Nix derivation and the project build system. I could only do the wrapping in the project build system and avoid unnecessarily repeating the wrapping logic in Nix, but that doesn’t feel right. Wrapping should be done at the Nix level, because it’s something that Nix requires… Somebody could build my project without using Nix (and just having all the required dev environment installed in their non-Nix system).
Actually I posted a similar question also on Reddit (see here). And I discovered that I can invoke the phases of a derivation manually inside a nix-shell. That may be useful.

I think the Use a clang compiled from source section from C - NixOS Wiki may help.

Hosted by Flying Circus.