Cleanup NixOS service after switching to a config where it is disabled

I intend to create a Systemd service that simply runs a docker compose -f <file> up for a program that is mainly distributed this way. I don’t want, however, to just leave all the created resources (volumes, containers, networks) floating around if I ever disable this service from my config.

I’m kind of a noob for NixOS modules, so I want to know if there’s a way to define an automatic cleanup for a service once I disable it in my config (in this case it’d just run docker compose down -v and possibly prune images), or is this cleanup generally done manually?

Sadly not possible, NixOS can’t really manage state.

The generic solution is preservation & co., but it takes a bit of effort to set up, and I don’t think it really works for docker.

1 Like