From 3b5c031619b3afdb638f0019ac63527f0214bb30 Mon Sep 17 00:00:00 2001 From: OlivierChiasson Date: Mon, 22 Jun 2026 12:28:25 -0300 Subject: [PATCH] Add rbw SSH socket support in home-manager modules - Introduced a new module `rbw-ssh-socket.nix` to manage SSH_AUTH_SOCK for rbw. - Updated `default.nix` to utilize the new SSH socket variable for session management. - Refactored Bash and Fish shell configurations to use the new module for setting SSH_AUTH_SOCK, improving maintainability and consistency across shell environments. --- modules/lib/rbw-ssh-socket.nix | 30 ++++++++++++++++++++++++++++ modules/ssh/home-manager/default.nix | 4 ++-- modules/wisdom/shells/bash.nix | 23 ++++----------------- modules/wisdom/shells/fish.nix | 13 +++--------- 4 files changed, 39 insertions(+), 31 deletions(-) create mode 100644 modules/lib/rbw-ssh-socket.nix diff --git a/modules/lib/rbw-ssh-socket.nix b/modules/lib/rbw-ssh-socket.nix new file mode 100644 index 0000000..79b6458 --- /dev/null +++ b/modules/lib/rbw-ssh-socket.nix @@ -0,0 +1,30 @@ +{ ... }: { + flake.lib.rbwSshSocket = + let + bashSnippet = '' + if [ -z "''${SSH_AUTH_SOCK:-}" ]; then + if [ -n "''${XDG_RUNTIME_DIR:-}" ]; then + export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/rbw/ssh-agent-socket" + else + export SSH_AUTH_SOCK="/run/user/$(id -u)/rbw/ssh-agent-socket" + fi + fi + ''; + fishSnippet = + pkgs: '' + if test -z "$SSH_AUTH_SOCK" + if test -n "$XDG_RUNTIME_DIR" + set -gx SSH_AUTH_SOCK "$XDG_RUNTIME_DIR/rbw/ssh-agent-socket" + else + set -l _hm_uid (${pkgs.coreutils}/bin/id -u) + set -gx SSH_AUTH_SOCK "/run/user/$_hm_uid/rbw/ssh-agent-socket" + end + end + ''; + in + { + inherit bashSnippet fishSnippet; + sessionVariable = "$XDG_RUNTIME_DIR/rbw/ssh-agent-socket"; + activationPath = "/run/user/$(id -u)/rbw/ssh-agent-socket"; + }; +} diff --git a/modules/ssh/home-manager/default.nix b/modules/ssh/home-manager/default.nix index 5711b66..ec2c6a2 100644 --- a/modules/ssh/home-manager/default.nix +++ b/modules/ssh/home-manager/default.nix @@ -56,14 +56,14 @@ } { home.packages = with pkgs; [ rbw pinentry-gtk2 ]; - home.sessionVariables.SSH_AUTH_SOCK = "$XDG_RUNTIME_DIR/rbw/ssh-agent-socket"; + home.sessionVariables.SSH_AUTH_SOCK = self.lib.rbwSshSocket.sessionVariable; home.file = inventory.mkIdentityFiles selectedHosts; programs.ssh.enable = lib.mkIf cfg.manageSshConfig false; home.activation.rbwSshConfig = lib.mkIf cfg.manageSshConfig (lib.hm.dag.entryAfter [ "writeBoundary" ] '' mkdir -p "$HOME/.ssh" chmod 700 "$HOME/.ssh" - RBW_SSH_SOCK="/run/user/$(id -u)/rbw/ssh-agent-socket" + RBW_SSH_SOCK="${self.lib.rbwSshSocket.activationPath}" cat > "$HOME/.ssh/config" <<'EOF' ${sshConfigTemplate} EOF diff --git a/modules/wisdom/shells/bash.nix b/modules/wisdom/shells/bash.nix index 85405af..35d819b 100644 --- a/modules/wisdom/shells/bash.nix +++ b/modules/wisdom/shells/bash.nix @@ -1,9 +1,10 @@ -{ ... }: { +{ self, ... }: { flake.homeManagerModules.wisdomShellBash = { config, lib, ... }: let root = config.chiasson.home; cfg = config.chiasson.home.shell.bash; + rbw = self.lib.rbwSshSocket; in { options.chiasson.home.shell.bash.enable = lib.mkEnableOption "Bash + rbw SSH socket env in profile/init." // { @@ -13,24 +14,8 @@ config = lib.mkIf (root.enable && cfg.enable) { programs.bash = { enable = true; - profileExtra = '' - if [ -z "''${SSH_AUTH_SOCK:-}" ]; then - if [ -n "''${XDG_RUNTIME_DIR:-}" ]; then - export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/rbw/ssh-agent-socket" - else - export SSH_AUTH_SOCK="/run/user/$(id -u)/rbw/ssh-agent-socket" - fi - fi - ''; - initExtra = '' - if [ -z "''${SSH_AUTH_SOCK:-}" ]; then - if [ -n "''${XDG_RUNTIME_DIR:-}" ]; then - export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/rbw/ssh-agent-socket" - else - export SSH_AUTH_SOCK="/run/user/$(id -u)/rbw/ssh-agent-socket" - fi - fi - ''; + profileExtra = rbw.bashSnippet; + initExtra = rbw.bashSnippet; }; }; }; diff --git a/modules/wisdom/shells/fish.nix b/modules/wisdom/shells/fish.nix index 5e5fb83..582e0dd 100644 --- a/modules/wisdom/shells/fish.nix +++ b/modules/wisdom/shells/fish.nix @@ -1,9 +1,10 @@ -{ ... }: { +{ self, ... }: { flake.homeManagerModules.wisdomShellFish = { config, lib, pkgs, ... }: let root = config.chiasson.home; cfg = config.chiasson.home.shell.fish; + rbw = self.lib.rbwSshSocket; in { options.chiasson.home.shell.fish = { @@ -19,21 +20,13 @@ }; config = lib.mkIf (root.enable && cfg.enable) { - # `fishPlugins.grc` only installs the plugin; the wrapper invokes the `grc` binary. home.packages = [ pkgs.grc ]; programs.fish = { enable = true; interactiveShellInit = '' set fish_greeting "" - if test -z "$SSH_AUTH_SOCK" - if test -n "$XDG_RUNTIME_DIR" - set -gx SSH_AUTH_SOCK "$XDG_RUNTIME_DIR/rbw/ssh-agent-socket" - else - set -l _hm_uid (${pkgs.coreutils}/bin/id -u) - set -gx SSH_AUTH_SOCK "/run/user/$_hm_uid/rbw/ssh-agent-socket" - end - end + ${rbw.fishSnippet pkgs} ''; plugins = [ {