Per file derivations with C++

I have a science experiment to share that I imagine some of you will enjoy.

Recently I’ve been trying out writing derivations directly, and limiting my reliance on Nixpkgs ( within reason ). Largely this is motivated by a need for fine grain caching at work and also making Nix more approachable to coworkers by avoiding the complexity of setup.sh.

I wanted to try getting real pedantic with derivations, and use them to process commands like cc, ln, ar, and cp directly. What I cooked up was a minimal set of helpers for C++ builds.

I took it for a spin on a relatively simple set of packages to produce static and shared libraries, as well as dynamic and static executables.

These depend on a mixture of Nixpkgs and “from scratch” libs.

The scale here is too small to make any sweeping performance claims, but because it’s this fine grained, changes to sources only trigger rebuilds of the effected files. It would be easy to extend this to mark individual headers as inputs as well using cpp traces, and to library linkage by using traces in ld ( there’s potentially a large performance advantage for linkage ).

The README in the repo covers limitations and possible extensions, but overall I’m really pleased with this little experiment.

Here’s the helper file:

And my example package :

3 Likes

You may be interested in GitHub - edolstra/nix-ccache: A flake to remotely build and/or cache C/C++ compilation, using recursive Nix, which provides a gcc wrapper that runs the input through the preprocessor and then uses a Nix derivation to perform the actual compilation. This enables caching and distributed builds similar to ccache (hence the name - it doesn’t actually use ccache).

There is also the ancient https://github.com/edolstra/nix-make which provides functions for building C/C++ source files, using some import-from-derivation magic to figure out the header file dependencies automatically.

5 Likes

Thanks for pointing these out. I’ve seen nix-ccache floating around the repo list for ages but never checked it out, and this is the first time I’ve seen nix-make.

I’ll take the dive into those soon. I imagine some of my symbol table and linkage analysis scripts could fit right in with nix-ccache.

What I really want one day is for Nix to dry run cpp and ld to create derivations - and it sounds like nix-ccache has cpp already covered. :slightly_smiling_face: