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?
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:
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.
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.
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.