I found ctypes.sh to be legitimately useful for managing resources in nix-shell.
E.g. flock a lock file and set CLOEXEC so that subprocesses don't hold the lock open after the shell exits.
E.g. Use memfd_create to create a temp file and write a key. Then pass /proc/$$/fd/$FD to programs that need the key as a file. When the shell exits, the file can no longer be opened.
You can do similar things with traps, but they aren't guaranteed to execute, whereas these OS primitives will always be cleaned up.
https://github.com/taviso/ctypes.sh/wiki
There are some little demos here:
https://github.com/taviso/ctypes.sh/tree/master/test
I even ported the GTK+3 Hello World to bash as a demo:
https://github.com/taviso/ctypes.sh/blob/master/test/gtk.sh