This is not a direct solution, rather a workaround, but I must admit it is handy in more complex cases.
For fish there is fenv tool, which evaluates bash scripts and uses produced variables. I am using it myself for the Nix initialization workaround.
# while in .config/fish/config.fish
fenv source '$HOME/ssh_gnupg_support.sh'
with ssh_gnupg_support.sh keeping the mentioned content for the bash.
As for direct correct solution, I do not know fish syntax well, but would place something like this in .config/fish/config.fish:
# warning: draft, might contain errors; not tested!
set -e SSH_AGENT_PID # unset
if test {$gnupg_SSH_AUTH_SOCK_by} -ne $fish_pid;
set -x SSH_AUTH_SOCK (gpgconf --list-dirs agent-ssh-socket)
end
Currently we only handle environment variables that are added or modified. If a variable is unset (removed from environment) it will not be removed from fish.
Does that mean that unset SSH_AGENT_PID won’t work?
Here’s a slightly modified (direct) version of @szszszsz’s script, that’s working for me:
set -e SSH_AGENT_PID
if not set -q gnupg_SSH_AUTH_SOCK_by or test $gnupg_SSH_AUTH_SOCK_by -ne $fish_pid
set -gx SSH_AUTH_SOCK (gpgconf --list-dirs agent-ssh-socket)
end