If I'm not mistaken, everything in nix uses bash. So you should be able to use trap
to catch the EXIT
signal and execute a cleanup script. Here's a guide
Nix / NixOS
Main links
Videos
Id never heard of trap. This is awesome! Thank you for sharing
Sounds like the perfect use case for devenv. I use it in a handful of personal projects and it's proved to be very useful when swapping projects especially when they require multiple services (eg. postgres, redis, nginx, etc.)
It can be setup as a flake that you can use with nix develop
.
There's options to start services and you can use scripts if you want some easy ways to tear down environments while in the devenv shell.
Hope this helps.
But why? This only seems practical if people are using the environment once. Otherwise you're just wasting a lot of bandwidth downloading dependencies over and over.
You should really just set up nix garbage collection to run once a week or something and be done with it.
I'm not talking about deleting anything nix related or downloadable, but development artifacts. Running Docker images, temporary data, services that were started during the development, etc.
That is the opposite of using it once, but using it multiple times a day, because developers often need to switch projects.