Rebuild/rerun upon save of a local source file

I have a flake that exposes derivations that depend on certain local paths - of the src = ./file.ext variety. I’m actively developing the sources in question. What I’m looking for is a way of watching all of the local source files and rebuilding whenever one changes. Perhaps as a way of polling nix to see if the derivation has changed, and if it has, rebuild and rerun.

The use case is as follows. I’ve been using Nix for a while and for a hobby project I’m looking to try out some web development using Nix. The end result will be several docker images (e.g. one for static assets, one for a blog backend, etc) that I pass traffic to. But during active development, web projects tend to favour rapid iterations (specifically - change a file and see the results almost immediately), and I’m looking to recreate that experience - entirely bypassing dockertools’ buildImage, docker load, etc, and simply running binaries natively and port forwarding them for an nginx ingress image to route localhost traffic to them. If I change a style.css file, for instance, I’d like the static file server binary to automatically rebuild, shut down the old one, and relaunch the new one.

In order to do this, I need a way of knowing when/if a derivation has changed, and acting upon that. The ‘ideal’ solution would be if nix had a watch option, such as nix-run --watch .#static-file-server -- --port=1234.

Is there any existing solution for this?

You can use tools like entr for that.

Nix doesn’t give you any developing experience beyond nix-shells.

If you want live reload, live build, etc, you have to use the regular tooling of the target ecosystem.

I guess what I could do is watch the full source tree for a change, then invoke a nix build .#all-binaries whenever anything changes. That all-binaries could be a simple linkFarm that maps binaries to named locations within the result directory.

I could then separately watch the result directory for changes, and reload whatever binary changes. It’s a bit contrived but it could work.

I guess what I’m wondering now is whether there’s any interest in adding a flag to nix build to have it persist, rebuilding upon any file change (which might be within the CWD, but more generally nix should be able to produce a list of all files to watch).

I doubt this because Nix is not meant nor wants to be a build tool: it’s a package manager. As @NobbZ said, you should be able to do this with entr and a standard makefile or something equivalent.