mirror of
https://github.com/nix-community/home-manager.git
synced 2025-11-08 19:46:05 +01:00
ssh-agent: add macOS support
This commit is contained in:
parent
af119feb17
commit
a9f496ead9
15 changed files with 205 additions and 48 deletions
|
|
@ -23,7 +23,8 @@ in
|
|||
default = "ssh-agent";
|
||||
example = "ssh-agent/socket";
|
||||
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,52 +46,91 @@ in
|
|||
enableNushellIntegration = lib.hm.shell.mkNushellIntegrationOption { inherit config; };
|
||||
};
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
assertions = [
|
||||
(lib.hm.assertions.assertPlatform "services.ssh-agent" pkgs lib.platforms.linux)
|
||||
];
|
||||
|
||||
programs =
|
||||
let
|
||||
bashIntegration = ''
|
||||
if [ -z "$SSH_AUTH_SOCK" ]; then
|
||||
export SSH_AUTH_SOCK=$XDG_RUNTIME_DIR/${cfg.socket}
|
||||
fi
|
||||
'';
|
||||
|
||||
fishIntegration = ''
|
||||
if test -z "$SSH_AUTH_SOCK"
|
||||
set -x SSH_AUTH_SOCK $XDG_RUNTIME_DIR/${cfg.socket}
|
||||
end
|
||||
'';
|
||||
|
||||
nushellIntegration = ''
|
||||
if "SSH_AUTH_SOCK" not-in $env {
|
||||
$env.SSH_AUTH_SOCK = $"($env.XDG_RUNTIME_DIR)/${cfg.socket}"
|
||||
}
|
||||
'';
|
||||
in
|
||||
config = lib.mkIf cfg.enable (
|
||||
lib.mkMerge [
|
||||
{
|
||||
bash.initExtra = lib.mkIf cfg.enableBashIntegration bashIntegration;
|
||||
programs =
|
||||
let
|
||||
socketPath =
|
||||
if pkgs.stdenv.isDarwin then
|
||||
"$(${lib.getExe pkgs.getconf} DARWIN_USER_TEMP_DIR)/${cfg.socket}"
|
||||
else
|
||||
"$XDG_RUNTIME_DIR/${cfg.socket}";
|
||||
|
||||
zsh.initContent = lib.mkIf cfg.enableZshIntegration bashIntegration;
|
||||
bashIntegration = ''
|
||||
if [ -z "$SSH_AUTH_SOCK" ]; then
|
||||
export SSH_AUTH_SOCK=${socketPath}
|
||||
fi
|
||||
'';
|
||||
|
||||
fish.interactiveShellInit = lib.mkIf cfg.enableFishIntegration fishIntegration;
|
||||
fishIntegration = ''
|
||||
if test -z "$SSH_AUTH_SOCK"
|
||||
set -x SSH_AUTH_SOCK ${socketPath}
|
||||
end
|
||||
'';
|
||||
|
||||
nushell.extraConfig = lib.mkIf cfg.enableNushellIntegration 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 {
|
||||
$env.SSH_AUTH_SOCK = $"($env.XDG_RUNTIME_DIR)/${cfg.socket}"
|
||||
}
|
||||
'';
|
||||
in
|
||||
{
|
||||
bash.initExtra = lib.mkIf cfg.enableBashIntegration bashIntegration;
|
||||
|
||||
systemd.user.services.ssh-agent = {
|
||||
Install.WantedBy = [ "default.target" ];
|
||||
Unit = {
|
||||
Description = "SSH authentication agent";
|
||||
Documentation = "man:ssh-agent(1)";
|
||||
};
|
||||
Service.ExecStart = "${lib.getExe' cfg.package "ssh-agent"} -D -a %t/${cfg.socket}${
|
||||
lib.optionalString (
|
||||
cfg.defaultMaximumIdentityLifetime != null
|
||||
) " -t ${toString cfg.defaultMaximumIdentityLifetime}"
|
||||
}";
|
||||
};
|
||||
};
|
||||
zsh.initContent = lib.mkIf cfg.enableZshIntegration bashIntegration;
|
||||
|
||||
fish.interactiveShellInit = lib.mkIf cfg.enableFishIntegration fishIntegration;
|
||||
|
||||
nushell.extraConfig = lib.mkIf cfg.enableNushellIntegration nushellIntegration;
|
||||
};
|
||||
}
|
||||
|
||||
(lib.mkIf pkgs.stdenv.isLinux {
|
||||
systemd.user.services.ssh-agent = {
|
||||
Install.WantedBy = [ "default.target" ];
|
||||
Unit = {
|
||||
Description = "SSH authentication agent";
|
||||
Documentation = "man:ssh-agent(1)";
|
||||
};
|
||||
Service.ExecStart = "${lib.getExe' cfg.package "ssh-agent"} -D -a %t/${cfg.socket}${
|
||||
lib.optionalString (
|
||||
cfg.defaultMaximumIdentityLifetime != null
|
||||
) " -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;
|
||||
};
|
||||
};
|
||||
})
|
||||
]
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,6 +50,7 @@ let
|
|||
"feh"
|
||||
"fzf"
|
||||
"gallery-dl"
|
||||
"getconf"
|
||||
"gh"
|
||||
"gh-dash"
|
||||
"ghostty"
|
||||
|
|
@ -125,6 +126,7 @@ let
|
|||
"ollama"
|
||||
"onlyoffice-desktopeditors"
|
||||
"opencode"
|
||||
"openssh"
|
||||
"openstackclient"
|
||||
"papis"
|
||||
"patdiff"
|
||||
|
|
|
|||
14
tests/modules/services/ssh-agent/darwin/bash-integration.nix
Normal file
14
tests/modules/services/ssh-agent/darwin/bash-integration.nix
Normal 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'
|
||||
'';
|
||||
}
|
||||
|
|
@ -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 "$(@getconf-system_cmds@/bin/getconf DARWIN_USER_TEMP_DIR)/ssh-agent"</string>
|
||||
</array>
|
||||
<key>RunAtLoad</key>
|
||||
<true/>
|
||||
</dict>
|
||||
</plist>
|
||||
14
tests/modules/services/ssh-agent/darwin/basic-service.nix
Normal file
14
tests/modules/services/ssh-agent/darwin/basic-service.nix
Normal 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}
|
||||
'';
|
||||
}
|
||||
6
tests/modules/services/ssh-agent/darwin/default.nix
Normal file
6
tests/modules/services/ssh-agent/darwin/default.nix
Normal 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;
|
||||
}
|
||||
|
|
@ -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"'
|
||||
'';
|
||||
}
|
||||
|
|
@ -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 "$(@getconf-system_cmds@/bin/getconf DARWIN_USER_TEMP_DIR)/ssh-agent" -t 1337</string>
|
||||
</array>
|
||||
<key>RunAtLoad</key>
|
||||
<true/>
|
||||
</dict>
|
||||
</plist>
|
||||
15
tests/modules/services/ssh-agent/darwin/timeout-service.nix
Normal file
15
tests/modules/services/ssh-agent/darwin/timeout-service.nix
Normal 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}
|
||||
'';
|
||||
}
|
||||
|
|
@ -3,7 +3,5 @@
|
|||
pkgs,
|
||||
...
|
||||
}:
|
||||
lib.optionalAttrs pkgs.stdenv.hostPlatform.isLinux {
|
||||
ssh-agent-basic-service = ./basic-service.nix;
|
||||
ssh-agent-timeout-service = ./timeout-service.nix;
|
||||
}
|
||||
(lib.optionalAttrs pkgs.stdenv.hostPlatform.isLinux (import ./linux))
|
||||
// (lib.optionalAttrs pkgs.stdenv.hostPlatform.isDarwin (import ./darwin))
|
||||
|
|
|
|||
4
tests/modules/services/ssh-agent/linux/default.nix
Normal file
4
tests/modules/services/ssh-agent/linux/default.nix
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
ssh-agent-basic-service = ./basic-service.nix;
|
||||
ssh-agent-timeout-service = ./timeout-service.nix;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue