What is the best dev workflow around nix-shell?

I use this dirty hack to save my direnv “use nix” environments from being garbage collected:

savedrv () {
  if [ "$out" ]
  then
    drv="$(nix show-derivation "$out" | perl -ne 'if(/"(.*\.drv)"/){print$1;exit}')"
    if [ "$drv" ] && [ -e "$drv" ]
    then
      ln -fs "$drv" .drv
      ln -fs "$PWD/.drv" "/nix/var/nix/gcroots/per-user/$LOGNAME/$(basename "$PWD")"
    fi
  fi
}

Works fine with shell.nix, default.nix and also “use nix -p …”. Comments welcome, I don’t know a more elegant way to get the derivation from $out than by show-derivation.