# rbw (Bitwarden) SSH-agent socket wiring, shared across shells and the
# outbound-rbw Home Manager module. Exposed as `flake.lib.rbwSshSocket`.
{ ... }: {
  flake.lib.rbwSshSocket =
    let
      socketPath = "$XDG_RUNTIME_DIR/rbw/ssh-agent-socket";

      # Bash init snippet — used by wisdom/shells/bash.nix (profile + interactive).
      bashSnippet = ''
        if [ -z "''${SSH_AUTH_SOCK:-}" ]; then
          if [ -n "''${XDG_RUNTIME_DIR:-}" ]; then
            export SSH_AUTH_SOCK="${socketPath}"
          else
            export SSH_AUTH_SOCK="/run/user/$(id -u)/rbw/ssh-agent-socket"
          fi
        fi
      '';

      # Fish init snippet — used by wisdom/shells/fish.nix.
      fishSnippet = pkgs: ''
        if test -z "$SSH_AUTH_SOCK"
          if test -n "$XDG_RUNTIME_DIR"
            set -gx SSH_AUTH_SOCK "${socketPath}"
          else
            set -l _hm_uid (${pkgs.coreutils}/bin/id -u)
            set -gx SSH_AUTH_SOCK "/run/user/$_hm_uid/rbw/ssh-agent-socket"
          end
        end
      '';
    in
    {
      inherit bashSnippet fishSnippet;
      # Used by home-manager modules to expose SSH_AUTH_SOCK to GUI apps
      # and HM-managed shells declaratively (no activation script needed).
      sessionVariable = socketPath;
    };
}


