Automatically upload built derivations to cache?

I’m currently uploading my built packages to my binary cache manually after I’ve built them (overridden emacs, gnome, patched kernel). This is annoying and easy to forget. Is there a way to configure my machines to always upload derivations it builds (but not copies) to a binary cache?

1 Like

cachix watch-exec mycache nix-build or cachix watch-store mycache

While the above answer does work for Cachix, the OP didn’t specifically mention the use of Cachix so just in case you are using something else, you can more generally define a post-build-hook to run after a build.

There are some caveats with this method that I tried to outline in this feature suggestion:
https://github.com/NixOS/nix/issues/7141

But if you set the hook in your flake.nix as nixConfig.post-build-hook then it will only run for a given flake, so that approach is decent for project specific caching.

I will say though, that I no longer rely on the post-build-hook myself due to some of the pain points, and instead rely on CI to upload to project specific caches using std-action.

4 Likes

Is there some equivalent for non-flake derivations and overrides?

See Untrusted CI: Using Nix to get automatic trusted caching of untrusted builds for details on using post-build hooks.

The post-build-hook is a nix.conf setting so its not specific to flakes in general, but if you set it globally you might end up uploading too much to your cache (since absolutely everything you build will trigger the hook).

If you are not using flakes and you are using a recent enough Nix binary, you can alternatively set it in your project shell (if you are using nix-shell or nix develop for your project) by exporting something like:
NIX_CONFIG="post-build-hook = /path/to/hook.sh" in the shell.

Uploading everything I build was the goal. Or does build include just downloading?

I set it globally for now, I have a couple of hundred gigs to spare on my server. Worst case I’ll just have to GC a bit more often.

Thanks

the post-build-hook only runs on local builds, so nothing substituted will trigger it. So if you want to just set it globally then yeah, it sounds like that would be a decent solution for your use case.

1 Like