How to use $XDG_RUNTIME_DIR in pam.sessionVariables?

I’m using home-manager to manage my local settings

I have the following config to set $XDG_RUNTIME_DIR

home.sessionVariables = {
  XDG_RUNTIME_DIR = "/run/user/$UID";
};

Then, $XDG_RUNTIME_DIR will have the value /run/user/1000. This is expected

I will use $XDG_RUNTIME_DIR to set $SSH_AUTH_SOCK as the following

pam.sessionVariables = {
   SSH_AUTH_SOCK = "${config.home.sessionVariables.XDG_RUNTIME_DIR}/ssh-agent.socket";
};

However, this results in $SSH_AUTH_SOCK has the value /run/user/$UID/ssh-agent.socket. This is not expected.

Is there any way to make it to have the value /run/user/1000/ssh-agent.socket?

1 Like

PAM expects variable references in the form ${UID}, not $UID. I don’t know if the UID variable is set at the time pam_env is run, though. Hopefully it is.

In any case, I believe XDG_RUNTIME_DIR should be set by pam_systemd so you don’t have to do it yourself. I would suggest simply using

pam.sessionVariables = {
   SSH_AUTH_SOCK = "/run/user/${UID}/ssh-agent.socket";
};

When I use ${UID}, home-manager command complains that it cannot find this variable

I came up with the following solution

pam.sessionVariables = {
   SSH_AUTH_SOCK = "${builtins.getEnv "XDG_RUNTIME_DIR"}/ssh-agent.socket";
};
2 Likes