this post was submitted on 24 Jan 2025
13 points (100.0% liked)

Nix / NixOS

1932 readers
1 users here now

Main links

Videos

founded 2 years ago
MODERATORS
 

I'm working on small nix flake to standardize the developer environments at my job.

What I'm still missing, however, is a way to clean up after leaving the shell. Some hook to call a shell script, when the shell is closed.

Is there something like this? I thought about wrapping the actual nix develop call inside a bash script and waiting for nix to terminate, but that seems rather hacky.

top 7 comments
sorted by: hot top controversial new old
[–] onlinepersona 5 points 1 week ago (1 children)

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

Anti Commercial-AI license

[–] [email protected] 4 points 1 week ago

Id never heard of trap. This is awesome! Thank you for sharing

[–] [email protected] 2 points 1 week ago (1 children)

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.

[–] [email protected] 1 points 1 week ago (1 children)

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.

[–] [email protected] 1 points 1 week ago* (last edited 1 week ago) (1 children)

In this case what I would recommend is to provide a command (perhaps inside that nix develop) that would set up all the images/services/data, and a second one to tear it down, and allow devs to run those commands themselves. This allows for more flexibility, e.g. multiple nix develop shells of the same project, but only one does the stateful stuff, coming back to the same shell later in the day without waiting for the setup. Most importantly it would allow for easy clean-up if the shell is shutdown unexpectedly and doesn't have time to clean up itself, e.g. SIGKILL or power loss. It can be as simple as using writeShellScriptBin to string together commands (instead of doing it in the shellHook as you presumably are doing right now), or as complex and flexible as https://github.com/svanderburg/nix-processmgmt .

Then, if you really want to automate it, you can simply add your-setup-command and trap "your-cleanup-command" EXIT to your shellHook.

[–] [email protected] 1 points 1 week ago

I would externalize it into a separate script/command anyway (the skeleton already exists).

However, knowing myself, I would certainly forget cleaning up manually once in a while. My goal was to create truly self-contained, stupid-simple dev environments - or get as close as possible to it.

[–] [email protected] 2 points 1 week ago

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.