bashwindows-subsystem-for-linuxssh-agent

Source bashrc in WSL in a non-interactive, non-login shell


I am working on a project, and I have a problem with WSL and bashrc. To elaborate a little bit, I have keychain and I want to run a keychain command every time I use a WSL command.

So I added that command in my ~./bashrc.

If from my Windows Terminal I use wsl bash -ic "command", my command inside ~/.bashrc runs.

My problem now is that I have another program that runs WSL without the bash -ic params, and I can't change that.

Do you know how I can address this problem or find a workaround?

Basically, I want it so that every time I use WSL from my Windows Terminal, it runs the keychain command first.

For example, let's say that I want to run wsl git push. I want my keychain command to execute before the git push runs.


Solution

  • Well, I've noodled on this on-and-off for several days to see if I could come up with a better solution. Nothing I can think of is optimal, but I think I ended up pretty close.

    To restate/frame the problem:

    As a result of this, the git commands in your plugin are likely to fail for the user, since they require key-based authentication.

    I think the above at least captures the spirit of the problem you are trying to solve.

    Unfortunately, there's just no method that I'm aware of to force Bash to load its startup files when it is a non-interactive, non-login shell other than things that would modify the WSL command-line.

    Proposed workaround - $WSLENV chain-loading IntelliJ

    This is "pretty close" to optimal, I believe. Your users would need to launch IntelliJ through WSL and use the $WSLENV feature to pass through the correct environment variables. This could be done interactively or through ~/.bashrc.

    Add the following to ~/.bashrc:

    eval $(keychain --eval ~/.ssh/<keyfile>)
    export WSLENV=$WSLENV:SSH_AGENT_PID:SSH_AUTH_SOCK
    

    With this in place, you should be able to run:

    wsl -e bash -lic /path/to/intellij.exe
    

    It will ask for the key password and (as normal) add the SSH_AGENT_PID/SSH_AUTH_SOCK environment variables to the shell session. It will also add those variable names to $WSLENV, so that they are passed to any Windows process launched from inside WSL.

    When the IntelliJ Windows executable is launched this way, the three variables will also be available in its environment:

    And when IntelliJ then runs wsl git ..., those three variables will also be passed back into WSL, so that git will be able to access the key from ssh-agent.

    While I don't have IntelliJ, I've tested this successfully using:

    wsl -e bash -lic pwsh.exe # or powershell.exe
    

    Then, from PowerShell:

    > $env:WSLENV
    WT_SESSION::WT_PROFILE_ID:SSH_AGENT_PID:SSH_AUTH_SOCK
    
    > $env:SSH_AGENT_PID
    45
    
    > $env:SSH_AUTH_SOCK
    /tmp/ssh-XXXXXXTWbsTa/agent.44
    
    > cd some-dir
    > wsl git clone "git@github.com:NotTheDr01ds/<private_repo>"
    

    It worked as expected.

    Other options

    This is actually a bit easier if your users are using (or open to using) some shells other than Bash:

    I actually started with these as proposed solutions before I found that $WSLENV was a workable solution (that didn't require a shell change). However, if you want to see my write-up on how to do this in Zsh or Fish, just look at the edit history for this answer.