Getting ssh-agent to work with Nushell and Hyprland

Getting ssh-agent to work with Nushell and Hyprland

Could not open a connection to your authentication agent

ยท

2 min read

๐Ÿค—
NOTE: This tutorial is for those who are using Hyprland and Nushell

You tried adding your newly generated ssh key and ssh-add throws the following error:

> ssh-add ~/.ssh/id_ed25519
Could not open a connection to your authentication agent

To fix this issue, first add this option to the top of your ~/.ssh/config file:

AddKeysToAgent yes

Now we need to setup an ssh-agent service that is enabled and starts whenever you boot your system. We will be using systemd for this task and create a systemd user service like so:

touch ~/.config/systemd/user/ssh-agent.service

Edit the above file using your favorite editor and paste the following contents:

[Unit]
Description=SSH key agent

[Service]
Type=simple
Environment=SSH_AUTH_SOCK=%t/ssh-agent.socket
# DISPLAY required for ssh-askpass to work
Environment=DISPLAY=:0
ExecStart=/usr/bin/ssh-agent -D -a $SSH_AUTH_SOCK

[Install]
WantedBy=default.target

Then edit your Hyprland's user preferences file:

vim ~/.config/hypr/userprefs.conf

And set the SSH_AUTH_SOCK environment variable to $XDG_RUNTIME_DIR/ssh-agent.socket. To do that, add the following line to end of the userprefs.conf file:

env = SSH_AUTH_SOCK,$XDG_RUNTIME_DIR/ssh-agent.socket

Now enable the systemd service you just created:

systemctl enable --user ssh-agent.service
systemctl start --user ssh-agent.service

You can check if the agent is running by executing the following command in Nushell:

> ps | grep ssh-agent
โ”‚ 261 โ”‚ 1569552 โ”‚    1168 โ”‚ /usr/bin/ssh-agent โ”‚ Sleeping   โ”‚ ... โ”‚

But we are not done yet. We need to define the environment variable in $nu.env-path as well. Use your favorite editor to make changes. I'll be using lunarvim:

> lvim $nu.env-path

Add this line to the end of the file:

$env.SSH_AUTH_SOCK = $"($env.XDG_RUNTIME_DIR)/ssh-agent.socket"

Now ssh-add should work:

> ssh-add -l
The agent has no identities.
ย