Skip to content

Instantly share code, notes, and snippets.

@lmb
Last active November 14, 2023 13:45
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lmb/e4448973daf99f0cc4a182a4e1811f93 to your computer and use it in GitHub Desktop.
Save lmb/e4448973daf99f0cc4a182a4e1811f93 to your computer and use it in GitHub Desktop.
macOS: use ssh-agent from nix-env

This recipe allows you to use the ssh-agent from nix-env with the auto generated SSH_AUTH_SOCK in /private/tmp. It works because disabling com.openssh.ssh-agent doesn't stop launchd from allocating SSH_AUTH_SOCK. com.nix.ssh-agent spawns a shell to remove the socket and then execute the homebrew ssh-agent.

  1. Install openssh from nix: nix-env --install --attr nikpkgs.openssh
  2. Disable the built in macOS agent: launchctl disable gui/$UID/com.openssh.ssh-agent
  3. Copy com.nix.ssh-agent.plist into ~/Library/LaunchAgents
  4. Enable the new agent: launchctl bootstrap gui/$UID ~/Library/LaunchAgents/com.nix.ssh-agent.plist
  5. Ensure that the correct agent is running:
    % launchctl list | grep ssh
    -	0	com.openssh.ssh-agent
    23986	0	com.nix.ssh-agent
    
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.nix.ssh-agent</string>
<key>ProgramArguments</key>
<array>
<string>/bin/sh</string>
<string>-c</string>
<!-- We reuse SSH_AUTH_SOCK from com.openssh.ssh-agent -->
<string>rm -f $SSH_AUTH_SOCK; exec $HOME/.nix-profile/bin/ssh-agent -D -a $SSH_AUTH_SOCK</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
@ahelwer
Copy link

ahelwer commented Nov 9, 2022

I get

/Users/ahelwer/Library/LaunchAgents/com.homebrew.ssh-agent.plist: Invalid property list
Bootstrap failed: 109: Invalid property list

I'm on Ventura 13.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment