# Automatically upload built derivations to cache?

**URL:** https://discourse.nixos.org/t/automatically-upload-built-derivations-to-cache/25428
**Category:** Help
**Created:** [February 13, 2023, 1:54pm UTC](https://discourse.nixos.org/t/automatically-upload-built-derivations-to-cache/25428 "2023-02-13T13:54:58Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![SKyletoft](https://discourse.nixos.org/user_avatar/discourse.nixos.org/skyletoft/32/6100_2.png) [@SKyletoft](https://discourse.nixos.org/u/SKyletoft)
#### Post date: [February 13, 2023, 1:54pm UTC](https://discourse.nixos.org/t/automatically-upload-built-derivations-to-cache/25428/1 "2023-02-13T13:54:58Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![wamserma](https://discourse.nixos.org/user_avatar/discourse.nixos.org/wamserma/32/5353_2.png) [@wamserma](https://discourse.nixos.org/u/wamserma)
#### Post date: [February 13, 2023, 4:39pm UTC](https://discourse.nixos.org/t/automatically-upload-built-derivations-to-cache/25428/2 "2023-02-13T16:39:15Z")

</div>

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

---

<div class="post-metadata">

### Author: ![nrdxp](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nrdxp/32/8501_2.png) [@nrdxp](https://discourse.nixos.org/u/nrdxp)
#### Post date: [February 13, 2023, 5:42pm UTC](https://discourse.nixos.org/t/automatically-upload-built-derivations-to-cache/25428/3 "2023-02-13T17:42:13Z")

</div>

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](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](https://github.com/divnix/std-action).

---

<div class="post-metadata">

### Author: ![SKyletoft](https://discourse.nixos.org/user_avatar/discourse.nixos.org/skyletoft/32/6100_2.png) [@SKyletoft](https://discourse.nixos.org/u/SKyletoft)
#### Post date: [February 13, 2023, 6:38pm UTC](https://discourse.nixos.org/t/automatically-upload-built-derivations-to-cache/25428/4 "2023-02-13T18:38:04Z")

</div>

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

---

<div class="post-metadata">

### Author: ![kevinmehall](https://discourse.nixos.org/user_avatar/discourse.nixos.org/kevinmehall/32/7125_2.png) [@kevinmehall](https://discourse.nixos.org/u/kevinmehall)
#### Post date: [February 13, 2023, 7:07pm UTC](https://discourse.nixos.org/t/automatically-upload-built-derivations-to-cache/25428/5 "2023-02-13T19:07:36Z")

</div>

See [Untrusted CI: Using Nix to get automatic trusted caching of untrusted builds](https://www.tweag.io/blog/2019-11-21-untrusted-ci/) for details on using post-build hooks.

---

<div class="post-metadata">

### Author: ![nrdxp](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nrdxp/32/8501_2.png) [@nrdxp](https://discourse.nixos.org/u/nrdxp)
#### Post date: [February 13, 2023, 7:18pm UTC](https://discourse.nixos.org/t/automatically-upload-built-derivations-to-cache/25428/6 "2023-02-13T19:18:03Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![SKyletoft](https://discourse.nixos.org/user_avatar/discourse.nixos.org/skyletoft/32/6100_2.png) [@SKyletoft](https://discourse.nixos.org/u/SKyletoft)
#### Post date: [February 13, 2023, 8:08pm UTC](https://discourse.nixos.org/t/automatically-upload-built-derivations-to-cache/25428/7 "2023-02-13T20:08:38Z")

</div>

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

---

<div class="post-metadata">

### Author: ![nrdxp](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nrdxp/32/8501_2.png) [@nrdxp](https://discourse.nixos.org/u/nrdxp)
#### Post date: [February 13, 2023, 9:01pm UTC](https://discourse.nixos.org/t/automatically-upload-built-derivations-to-cache/25428/8 "2023-02-13T21:01:43Z")

</div>

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.
