1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-12-03 15:41:02 +01:00

ssh-agent: add macOS support

This commit is contained in:
XYenon 2025-11-06 14:10:26 +08:00 committed by Austin Horstman
parent d7b1ece79d
commit c053d701d6
15 changed files with 205 additions and 48 deletions

View file

@ -23,7 +23,8 @@ in
default = "ssh-agent"; default = "ssh-agent";
example = "ssh-agent/socket"; example = "ssh-agent/socket";
description = '' description = ''
The agent's socket; interpreted as a suffix to {env}`$XDG_RUNTIME_DIR`. The agent's socket; interpreted as a suffix to {env}`$XDG_RUNTIME_DIR`
on Linux and `$(getconf DARWIN_USER_TEMP_DIR)` on macOS.
''; '';
}; };
@ -45,26 +46,38 @@ in
enableNushellIntegration = lib.hm.shell.mkNushellIntegrationOption { inherit config; }; enableNushellIntegration = lib.hm.shell.mkNushellIntegrationOption { inherit config; };
}; };
config = lib.mkIf cfg.enable { config = lib.mkIf cfg.enable (
assertions = [ lib.mkMerge [
(lib.hm.assertions.assertPlatform "services.ssh-agent" pkgs lib.platforms.linux) {
];
programs = programs =
let let
socketPath =
if pkgs.stdenv.isDarwin then
"$(${lib.getExe pkgs.getconf} DARWIN_USER_TEMP_DIR)/${cfg.socket}"
else
"$XDG_RUNTIME_DIR/${cfg.socket}";
bashIntegration = '' bashIntegration = ''
if [ -z "$SSH_AUTH_SOCK" ]; then if [ -z "$SSH_AUTH_SOCK" ]; then
export SSH_AUTH_SOCK=$XDG_RUNTIME_DIR/${cfg.socket} export SSH_AUTH_SOCK=${socketPath}
fi fi
''; '';
fishIntegration = '' fishIntegration = ''
if test -z "$SSH_AUTH_SOCK" if test -z "$SSH_AUTH_SOCK"
set -x SSH_AUTH_SOCK $XDG_RUNTIME_DIR/${cfg.socket} set -x SSH_AUTH_SOCK ${socketPath}
end end
''; '';
nushellIntegration = '' nushellIntegration =
if pkgs.stdenv.isDarwin then
''
if "SSH_AUTH_SOCK" not-in $env {
$env.SSH_AUTH_SOCK = $"(${lib.getExe pkgs.getconf} DARWIN_USER_TEMP_DIR)/${cfg.socket}"
}
''
else
''
if "SSH_AUTH_SOCK" not-in $env { if "SSH_AUTH_SOCK" not-in $env {
$env.SSH_AUTH_SOCK = $"($env.XDG_RUNTIME_DIR)/${cfg.socket}" $env.SSH_AUTH_SOCK = $"($env.XDG_RUNTIME_DIR)/${cfg.socket}"
} }
@ -79,7 +92,9 @@ in
nushell.extraConfig = lib.mkIf cfg.enableNushellIntegration nushellIntegration; nushell.extraConfig = lib.mkIf cfg.enableNushellIntegration nushellIntegration;
}; };
}
(lib.mkIf pkgs.stdenv.isLinux {
systemd.user.services.ssh-agent = { systemd.user.services.ssh-agent = {
Install.WantedBy = [ "default.target" ]; Install.WantedBy = [ "default.target" ];
Unit = { Unit = {
@ -92,5 +107,30 @@ in
) " -t ${toString cfg.defaultMaximumIdentityLifetime}" ) " -t ${toString cfg.defaultMaximumIdentityLifetime}"
}"; }";
}; };
})
(lib.mkIf pkgs.stdenv.isDarwin {
launchd.agents.ssh-agent = {
enable = true;
config = {
ProgramArguments = [
(lib.getExe pkgs.bash)
"-c"
''${lib.getExe' cfg.package "ssh-agent"} -D -a "$(${lib.getExe pkgs.getconf} DARWIN_USER_TEMP_DIR)/${cfg.socket}"${
lib.optionalString (
cfg.defaultMaximumIdentityLifetime != null
) " -t ${toString cfg.defaultMaximumIdentityLifetime}"
}''
];
KeepAlive = {
Crashed = true;
SuccessfulExit = false;
}; };
ProcessType = "Background";
RunAtLoad = true;
};
};
})
]
);
} }

View file

@ -50,6 +50,7 @@ let
"feh" "feh"
"fzf" "fzf"
"gallery-dl" "gallery-dl"
"getconf"
"gh" "gh"
"gh-dash" "gh-dash"
"ghostty" "ghostty"
@ -125,6 +126,7 @@ let
"ollama" "ollama"
"onlyoffice-desktopeditors" "onlyoffice-desktopeditors"
"opencode" "opencode"
"openssh"
"openstackclient" "openstackclient"
"papis" "papis"
"patdiff" "patdiff"

View file

@ -0,0 +1,14 @@
{
services.ssh-agent = {
enable = true;
enableBashIntegration = true;
};
programs.bash.enable = true;
nmt.script = ''
assertFileContains \
home-files/.bashrc \
'export SSH_AUTH_SOCK=$(@getconf-system_cmds@/bin/getconf DARWIN_USER_TEMP_DIR)/ssh-agent'
'';
}

View file

@ -0,0 +1,25 @@
<?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>KeepAlive</key>
<dict>
<key>Crashed</key>
<true/>
<key>SuccessfulExit</key>
<false/>
</dict>
<key>Label</key>
<string>org.nix-community.home.ssh-agent</string>
<key>ProcessType</key>
<string>Background</string>
<key>ProgramArguments</key>
<array>
<string>@bash-interactive@/bin/bash</string>
<string>-c</string>
<string>@openssh@/bin/ssh-agent -D -a &quot;$(@getconf-system_cmds@/bin/getconf DARWIN_USER_TEMP_DIR)/ssh-agent&quot;</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>

View file

@ -0,0 +1,14 @@
{ config, ... }:
{
services.ssh-agent = {
enable = true;
package = config.lib.test.mkStubPackage { outPath = "@openssh@"; };
};
nmt.script = ''
assertFileContent \
LaunchAgents/org.nix-community.home.ssh-agent.plist \
${./basic-service-expected.plist}
'';
}

View file

@ -0,0 +1,6 @@
{
ssh-agent-darwin-basic-service = ./basic-service.nix;
ssh-agent-darwin-timeout-service = ./timeout-service.nix;
ssh-agent-darwin-bash-integration = ./bash-integration.nix;
ssh-agent-darwin-nushell-integration = ./nushell-integration.nix;
}

View file

@ -0,0 +1,14 @@
{
services.ssh-agent = {
enable = true;
enableNushellIntegration = true;
};
programs.nushell.enable = true;
nmt.script = ''
assertFileContains \
home-files/.config/nushell/config.nu \
'$env.SSH_AUTH_SOCK = $"(@getconf-system_cmds@/bin/getconf DARWIN_USER_TEMP_DIR)/ssh-agent"'
'';
}

View file

@ -0,0 +1,25 @@
<?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>KeepAlive</key>
<dict>
<key>Crashed</key>
<true/>
<key>SuccessfulExit</key>
<false/>
</dict>
<key>Label</key>
<string>org.nix-community.home.ssh-agent</string>
<key>ProcessType</key>
<string>Background</string>
<key>ProgramArguments</key>
<array>
<string>@bash-interactive@/bin/bash</string>
<string>-c</string>
<string>@openssh@/bin/ssh-agent -D -a &quot;$(@getconf-system_cmds@/bin/getconf DARWIN_USER_TEMP_DIR)/ssh-agent&quot; -t 1337</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>

View file

@ -0,0 +1,15 @@
{ config, ... }:
{
services.ssh-agent = {
enable = true;
defaultMaximumIdentityLifetime = 1337;
package = config.lib.test.mkStubPackage { outPath = "@openssh@"; };
};
nmt.script = ''
assertFileContent \
LaunchAgents/org.nix-community.home.ssh-agent.plist \
${./timeout-service-expected.plist}
'';
}

View file

@ -3,7 +3,5 @@
pkgs, pkgs,
... ...
}: }:
lib.optionalAttrs pkgs.stdenv.hostPlatform.isLinux { (lib.optionalAttrs pkgs.stdenv.hostPlatform.isLinux (import ./linux))
ssh-agent-basic-service = ./basic-service.nix; // (lib.optionalAttrs pkgs.stdenv.hostPlatform.isDarwin (import ./darwin))
ssh-agent-timeout-service = ./timeout-service.nix;
}

View file

@ -0,0 +1,4 @@
{
ssh-agent-basic-service = ./basic-service.nix;
ssh-agent-timeout-service = ./timeout-service.nix;
}