From 1fe21928c76b04775f40b49b74ba91ebec1be40e Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Mon, 20 Feb 2023 19:08:57 -0800 Subject: 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. --- home/shell/fish/default.nix | 21 +++------------------ home/shell/fish/functions/find-ssh-agent.fish | 20 ++++++++++++++++++++ home/shell/fish/functions/nix-rebuild-host.fish | 4 ++-- home/shell/fish/interactive.fish | 8 ++++++++ home/shell/fish/login.fish | 10 ++++++++++ 5 files changed, 43 insertions(+), 20 deletions(-) create mode 100644 home/shell/fish/functions/find-ssh-agent.fish create mode 100644 home/shell/fish/interactive.fish create mode 100644 home/shell/fish/login.fish (limited to 'home/shell') diff --git a/home/shell/fish/default.nix b/home/shell/fish/default.nix index d000cd9..f3e41d9 100644 --- a/home/shell/fish/default.nix +++ b/home/shell/fish/default.nix @@ -1,7 +1,6 @@ { config, lib, pkgs, ... }: let cfg = config.my.home.shell; - swayEnabled = config.my.home.wm.windowManager == "sway"; aliases = config.my.home.shell.aliases; in { @@ -9,24 +8,10 @@ in programs.fish = { enable = true; shellAliases = aliases; - interactiveShellInit = '' - set fish_greeting - - # Tmux on terminal start, unless we're in a SSH connection - if status is-interactive - if test -z "$SSH_CONNECTION" - if not tmux has-session 2>/dev/null; or test -z "$TMUX" - exec tmux new-session -A -s 0 - end - end - end - ''; - loginShellInit = lib.mkIf swayEnabled '' - if test -z "$DISPLAY"; and test (tty) = "/dev/tty1" - exec sway - end - ''; + interactiveShellInit = builtins.readFile ./interactive.fish; + loginShellInit = builtins.readFile ./login.fish; }; + xdg.configFile."fish/functions" = { source = ./functions; recursive = true; 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 diff --git a/home/shell/fish/interactive.fish b/home/shell/fish/interactive.fish new file mode 100644 index 0000000..4adcba8 --- /dev/null +++ b/home/shell/fish/interactive.fish @@ -0,0 +1,8 @@ +# Tmux on terminal start, unless we're in a SSH connection +if status is-interactive + if test -z "$SSH_CONNECTION" + if not tmux has-session 2>/dev/null; or test -z "$TMUX" + exec tmux new-session -A -s 0 + end + end +end diff --git a/home/shell/fish/login.fish b/home/shell/fish/login.fish new file mode 100644 index 0000000..8f29553 --- /dev/null +++ b/home/shell/fish/login.fish @@ -0,0 +1,10 @@ +# disable greeting +set -U fish_greeting '' + +# set up ssh-agent +find-ssh-agent + +# start sway +if test -z "$DISPLAY"; and test (tty) = "/dev/tty1" + exec sway +end -- cgit 1.4.1