Help with nix flake shell for LLVM

For LLVM engineers out there, how do you set up the nix flake shell? I want to be able to build a clang that can run on nixos, and also a clang that can run on Linux systems.

The way I’m setting it up now means that everytime I make a change, the whole tree is built afresh.

        clang_dev = pkgs.wrapCC ( pkgs.stdenv.mkDerivation rec {
          pname = "llvm-project";
          version = "dev";

          src = builtins.path { path = "...../src/llvm-project/build"; };
          dontStrip = true;

          installPhase = ''
            mkdir -p $out/bin
            cp bin/clang $out/bin
            cp bin/opt $out/bin
            cp bin/llc $out/bin
            cp -r lib $out/lib
          '';

          passthru.isClang = true;
        });

and later

        devShell = with pkgs; mkShell {
          nativeBuildInputs = [
            clang_dev
          ];
}

I’ve found nix develop nixpkgs#llvmPackages.llvm does a good job. Maybe we’ll come up with a better approach in the future that covers all of LLVM but right now, this has been a setup which has worked for me.

Thanks for your prompt reply. Using this devshell I am able to build clang, but when I’m using it to compile any C files, it couldn’t find libc file headers. I know that even after complication there might still be issue with finding dynamic libraries.

I know I probably need to use pkgs.wrapCC, but what’s the best way to instrument it? Or if your setup is different, how is yours done?

As to be expected, unwrapped compilers don’t have the injected flags.

I don’t use pkgs.wrapCC for clang testing. I build it inside of Nix and then call clang. Right now, clang requires quite a bit of magic to wrap it properly. You probably could write a shell script which generates a derivation which then produces a wrapped clang but it’ll be messy. The other option is to cat the wrapped clang and pull the shell script out and edit in your built clang.

I build it inside of Nix and then call clang.
Can you elaborate on your workflow? I think it will be helpful for the community if you can share your flake file for your LLVM setup.

I don’t have a Flake file for LLVM development, like I mentioned before I just do nix develop nixpkgs#llvmPackages.llvm. I don’t really work on Clang, I’ve worked on Bolt but not the other components.