# How to make \`nix-shell\` clean up after itself?

**URL:** https://discourse.nixos.org/t/how-to-make-nix-shell-clean-up-after-itself/3469
**Category:** Help
**Created:** [July 15, 2019, 11:34pm UTC](https://discourse.nixos.org/t/how-to-make-nix-shell-clean-up-after-itself/3469 "2019-07-15T23:34:18Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![toraritte](https://discourse.nixos.org/user_avatar/discourse.nixos.org/toraritte/32/6777_2.png) [@toraritte](https://discourse.nixos.org/u/toraritte)
#### Post date: [July 15, 2019, 11:34pm UTC](https://discourse.nixos.org/t/how-to-make-nix-shell-clean-up-after-itself/3469/1 "2019-07-15T23:34:18Z")

</div>

For example, if PostgreSQL is started for a dev environment, it will keep running when exiting the shell, or when arbitrary files are created in `shellHook` that are not needed afterwards.

Somewhat perplexed that I was only able to find [Killing background processes started in nix-shell](https://unix.stackexchange.com/questions/464106/killing-background-processes-started-in-nix-shell), after 15 minutes of searching. Maybe I was looking at the wrong places.

Are there better/more canonical ways to do this?

Thank you!

UPDATE: See an annotated version with inline bash commands to `trap` [here](https://elixirforum.com/t/nix-the-package-manager/23231/5?u=toraritte).

* * *

```
shellHook =

  let
    # NOTE These are equivalent.
    #
    # shellHook = shellHook =  
    # '' ''         
    # trap \ trap \   
    # " "        
    # '' echo lofa
    # + '' sleep 2  
    # echo lofa echo miez  
    # sleep 2 " \      
    # echo miez EXIT     
    # '' ''         
    # + '' ;            
    # " \
    # EXIT
    # ''
    # ;

    # Helper function to achieve the same
    cleanUp =
      shell_commands:
        ''
          trap \
          "
          ${ builtins.concatStringsSep "" shell_commands }
          " \
          EXIT
        ''
    ;

  in

    cleanUp [
      ''
        echo -n "POSTGRES CLEANUP START..."
      ''
      ( builtins.readFile ./postgres/clean-up.sh )
      ''
        echo "END"
      ''
    ]
  ;

```

---

<div class="post-metadata">

### Author: ![abathur](https://discourse.nixos.org/user_avatar/discourse.nixos.org/abathur/32/2719_2.png) [@abathur](https://discourse.nixos.org/u/abathur)
#### Post date: [July 17, 2019, 7:43pm UTC](https://discourse.nixos.org/t/how-to-make-nix-shell-clean-up-after-itself/3469/2 "2019-07-17T19:43:06Z")

</div>

AFAIK, the answer is manually, or custom scripting.

I’m not sure it’s canonical, but your example is roughly how I do it. I agree it could have more obvious idioms, which would help make it easier to find.

I find a lot of high-level/general searches for anything nix-related somewhat frustrating, so I often end up doing code searches (like this one on github [https://github.com/search?q=PGDATA+pg\_ctl+language%3Anix&type=Code](https://github.com/search?q=PGDATA+pg_ctl+language%3Anix&type=Code)) to hunt down how others are handling something. I see a lot of variations on the general theme, there.

When I have to roll-my-own scripts for handling for a package that has a canonical service definition in order to tailor it for development work in some shell.nix, I can’t help but think it might be nice if the service/program module abstraction had simple support for running multiple instances with scoping/namespacing.

Reflecting on it now, I imagine this is because nix is cross-platform, but module definitions are platform-idiomatic. Improving the nix-shell experience might be a nice side-benefit to cleaner cross-platform module abstractions…
