about summary refs log tree commit diff
path: root/home/shell/fish/functions
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2023-02-20 19:08:57 -0800
committerFranck Cuny <franck@fcuny.net>2023-02-21 12:59:08 -0800
commit1fe21928c76b04775f40b49b74ba91ebec1be40e (patch)
tree063e1ede6fa4c7ed84e51b20164aae13b2e38da0 /home/shell/fish/functions
parentfeat(home/pass): add passage (diff)
downloadworld-1fe21928c76b04775f40b49b74ba91ebec1be40e.tar.gz
feat(home/fish): move fish's code to external files
It's easier to edit / debug / test the code that way.

This also add a new function (`find-ssh-agent') to find or start a new
ssh agent when a shell is started.
Diffstat (limited to 'home/shell/fish/functions')
-rw-r--r--home/shell/fish/functions/find-ssh-agent.fish20
-rw-r--r--home/shell/fish/functions/nix-rebuild-host.fish4
2 files changed, 22 insertions, 2 deletions
diff --git a/home/shell/fish/functions/find-ssh-agent.fish b/home/shell/fish/functions/find-ssh-agent.fish
new file mode 100644
index 0000000..9e2de8d
--- /dev/null
+++ b/home/shell/fish/functions/find-ssh-agent.fish
@@ -0,0 +1,20 @@
+function find-ssh-agent --description "find or run ssh-agent"
+    # let's avoid storing the agent's socket under /tmp
+    set -l ssh_auth_sock $XDG_RUNTIME_DIR/ssh-agent.sock
+
+    if set -q SSH_AGENT_PID; and set -q SSH_AUTH_SOCK
+        # if variables already defined, then try to connect to agent
+        ssh-add -l &>/dev/null
+        test $status -ne 2; and return
+    end
+
+    set -l user_id (id -u)
+    set -l ssh_agent_pid (pgrep --exact --newest --uid $user_id ssh-agent)
+
+    if test -S $ssh_auth_sock
+        set --global --export SSH_AUTH_SOCK $ssh_auth_sock
+        set --global --export SSH_AGENT_PID $ssh_agent_pid
+    else
+        eval (ssh-agent -c -a $ssh_auth_sock)
+    end
+end
diff --git a/home/shell/fish/functions/nix-rebuild-host.fish b/home/shell/fish/functions/nix-rebuild-host.fish
index 1ec7daa..02c0050 100644
--- a/home/shell/fish/functions/nix-rebuild-host.fish
+++ b/home/shell/fish/functions/nix-rebuild-host.fish
@@ -1,4 +1,4 @@
 function nix-rebuild-host --description "rebuild the current host"
-  cd ~/workspace/world
-  sudo nixos-rebuild switch --flake .
+    cd ~/workspace/world
+    sudo nixos-rebuild switch --flake .
 end