From d4c5b1c6674151295f2b9de1ce515f3ef13f2ae7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9ana=20=E6=B1=9F?= Date: Thu, 29 May 2025 14:29:00 +0200 Subject: [PATCH 01/11] packages/tmux-sessionizer: register-session --- .../by-name/tmux-sessionizer/package.nix | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 nix/packages/by-name/tmux-sessionizer/package.nix diff --git a/nix/packages/by-name/tmux-sessionizer/package.nix b/nix/packages/by-name/tmux-sessionizer/package.nix new file mode 100644 index 00000000..121ee8db --- /dev/null +++ b/nix/packages/by-name/tmux-sessionizer/package.nix @@ -0,0 +1,23 @@ +{ + writeShellApplication, + tmux, + symlinkJoin, +}: let + tmux-register-session = writeShellApplication { + name = "__tmux-register-session"; + runtimeInputs = [tmux]; + text = '' + last=/tmp/TMUX_LAST + this="$(tmux display-message -p '#S')" + if [ ! -f "$last" ] || [ "$(cat "$last")" != "$this" ]; then + echo "$this" >"$last" + fi + ''; + }; +in + symlinkJoin { + name = "tmux-sessionizer"; + paths = [ + tmux-register-session + ]; + } From ddeb5859b0ff0be88d79fe21167792c55fba96d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9ana=20=E6=B1=9F?= Date: Thu, 29 May 2025 14:43:37 +0200 Subject: [PATCH 02/11] packages/tmux-sessionizer: maybe-create --- nix/packages/by-name/tmux-sessionizer/package.nix | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/nix/packages/by-name/tmux-sessionizer/package.nix b/nix/packages/by-name/tmux-sessionizer/package.nix index 121ee8db..90a99068 100644 --- a/nix/packages/by-name/tmux-sessionizer/package.nix +++ b/nix/packages/by-name/tmux-sessionizer/package.nix @@ -1,6 +1,7 @@ { writeShellApplication, tmux, + procps, symlinkJoin, }: let tmux-register-session = writeShellApplication { @@ -14,10 +15,24 @@ fi ''; }; + + tmux-maybe-create = writeShellApplication { + name = "__tmux-maybe-create"; + runtimeInputs = [procps tmux]; + text = '' + session_name="$1" + session_dir="$2" + + if ! pgrep tmux >/dev/null 2>&1 || ! tmux has -t="$session_name" 2>/dev/null; then + tmux new-session -ds "$session_name" -c "$session_dir" + fi + ''; + }; in symlinkJoin { name = "tmux-sessionizer"; paths = [ tmux-register-session + tmux-maybe-create ]; } From b69c80497f791f96e9478cece7a6004c2648cd04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9ana=20=E6=B1=9F?= Date: Thu, 29 May 2025 14:49:21 +0200 Subject: [PATCH 03/11] packages/tmux-sessionizer: attach-or-switch --- .../by-name/tmux-sessionizer/package.nix | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/nix/packages/by-name/tmux-sessionizer/package.nix b/nix/packages/by-name/tmux-sessionizer/package.nix index 90a99068..c6e22fa9 100644 --- a/nix/packages/by-name/tmux-sessionizer/package.nix +++ b/nix/packages/by-name/tmux-sessionizer/package.nix @@ -28,11 +28,27 @@ fi ''; }; + + tmux-attach-or-switch = writeShellApplication { + name = "__tmux-attach-or-switch"; + runtimeInputs = [tmux]; + text = '' + session_name="$1" + TMUX=''${TMUX:-} + + if [ -z "$TMUX" ]; then + tmux attach-session -t "$session_name" + else + tmux switch-client -t "$session_name" + fi + ''; + }; in symlinkJoin { name = "tmux-sessionizer"; paths = [ tmux-register-session tmux-maybe-create + tmux-attach-or-switch ]; } From db5a9ce92d8ca9e4581f35839625abd328e2fa82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9ana=20=E6=B1=9F?= Date: Thu, 29 May 2025 15:00:21 +0200 Subject: [PATCH 04/11] packages/tmux-sessionizer: tmux-last --- .../by-name/tmux-sessionizer/package.nix | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/nix/packages/by-name/tmux-sessionizer/package.nix b/nix/packages/by-name/tmux-sessionizer/package.nix index c6e22fa9..d222db0b 100644 --- a/nix/packages/by-name/tmux-sessionizer/package.nix +++ b/nix/packages/by-name/tmux-sessionizer/package.nix @@ -2,6 +2,7 @@ writeShellApplication, tmux, procps, + lib, symlinkJoin, }: let tmux-register-session = writeShellApplication { @@ -9,6 +10,12 @@ runtimeInputs = [tmux]; text = '' last=/tmp/TMUX_LAST + + # bail if tmux not running + if ! pgrep tmux >/dev/null 2>&1; then + exit 1 + fi + this="$(tmux display-message -p '#S')" if [ ! -f "$last" ] || [ "$(cat "$last")" != "$this" ]; then echo "$this" >"$last" @@ -43,6 +50,28 @@ fi ''; }; + + tmux-last = writeShellApplication { + name = "tmux-last"; + text = '' + tmux_last=/tmp/TMUX_LAST + if [ ! -f $tmux_last ]; then + echo "Last session is not yet set" + return 1 + fi + + session_name="$(cat $tmux_last)" + session_path="$session_name" + if [ ! -d "$session_path" ]; then + session_path="/tmp" + fi + + # bail if not in tmux, nothing to register + ${lib.getExe tmux-register-session} || : + ${lib.getExe tmux-maybe-create} "$session_name" "$session_path" + ${lib.getExe tmux-attach-or-switch} "$session_name" + ''; + }; in symlinkJoin { name = "tmux-sessionizer"; @@ -50,5 +79,6 @@ in tmux-register-session tmux-maybe-create tmux-attach-or-switch + tmux-last ]; } From c16d84e75e43a4e5c48bf6742b7bd93b82d8c547 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9ana=20=E6=B1=9F?= Date: Thu, 29 May 2025 15:14:39 +0200 Subject: [PATCH 05/11] packages/tmux-sessionizer: tmux-sessionizer --- .../by-name/tmux-sessionizer/package.nix | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/nix/packages/by-name/tmux-sessionizer/package.nix b/nix/packages/by-name/tmux-sessionizer/package.nix index d222db0b..6cdf7d1b 100644 --- a/nix/packages/by-name/tmux-sessionizer/package.nix +++ b/nix/packages/by-name/tmux-sessionizer/package.nix @@ -2,6 +2,8 @@ writeShellApplication, tmux, procps, + fzf, + gnused, lib, symlinkJoin, }: let @@ -72,6 +74,46 @@ ${lib.getExe tmux-attach-or-switch} "$session_name" ''; }; + + tmux-sessionizer = writeShellApplication { + name = "tmux-sessionizer"; + runtimeInputs = [fzf gnused]; + text = '' + selected=$( + { + fd . "$REPO_PATH" --exact-depth 2 --hidden --type d + fd . "$WORKTREE_PATH" --exact-depth 4 --hidden --type d + fd . "$PLAYGROUND_PATH" --exact-depth 1 --hidden --type d + echo "dotfiles" + echo "password-store" + } 2> /dev/null | sed -e "s|^$HOME|~|" | fzf) + selected=''${selected//\~/$HOME} + + if [ -z "$selected" ]; then + return 0 + fi + + # derive session name based on selected path + # dots are meaningful in tmux, remove them + session_name=''${selected//./_} + + # fixup special cases, override derived session_name if necessary + # this should be id for non-special cases + case "$selected" in + dotfiles) + selected=~/.dotfiles + ;; + password-store) + selected="$PASSWORD_STORE_DIR" + ;; + esac + + # effects + ${lib.getExe tmux-register-session} || : + ${lib.getExe tmux-maybe-create} "$session_name" "$selected" + ${lib.getExe tmux-attach-or-switch} "$session_name" + ''; + }; in symlinkJoin { name = "tmux-sessionizer"; @@ -80,5 +122,6 @@ in tmux-maybe-create tmux-attach-or-switch tmux-last + tmux-sessionizer ]; } From 3e2045260ec54e2d62146510ced1048e82dc7511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9ana=20=E6=B1=9F?= Date: Thu, 29 May 2025 15:21:51 +0200 Subject: [PATCH 06/11] fish: filter out fish sessionizer functions to test --- nix/homeModules/common/fish/default.nix | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/nix/homeModules/common/fish/default.nix b/nix/homeModules/common/fish/default.nix index 3ac5f313..c3b15848 100644 --- a/nix/homeModules/common/fish/default.nix +++ b/nix/homeModules/common/fish/default.nix @@ -53,11 +53,21 @@ in { # # Scripts and functions # - xdg.configFile = - lib.mapAttrs' - (path: _: - lib.nameValuePair "fish/functions/${path}" {source = "${./functions}/${path}";}) - (builtins.readDir ./functions); + xdg.configFile = let + allFunctions = + lib.mapAttrs' + (path: _: + lib.nameValuePair "fish/functions/${path}" {source = "${./functions}/${path}";}) + (builtins.readDir ./functions); + in + builtins.removeAttrs allFunctions [ + "fish/functions/__tmux-attach-or-switch.fish" + "fish/functions/__tmux-maybe-create.fish" + "fish/functions/__tmux-register-session.fish" + "fish/functions/tmux-home.fish" + "fish/functions/tmux-last.fish" + "fish/functions/tmux-sessionizer.fish" + ]; programs.fish = { interactiveShellInit = builtins.readFile ./shellInit.fish; From 77c43acfe3cd8218cd0df47fc97651972b288593 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9ana=20=E6=B1=9F?= Date: Thu, 29 May 2025 15:24:19 +0200 Subject: [PATCH 07/11] vanadium: +tmux-sessionizer --- nix/configurations/vanadium/home/programs.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/nix/configurations/vanadium/home/programs.nix b/nix/configurations/vanadium/home/programs.nix index 2836b1af..0ba0f0ac 100644 --- a/nix/configurations/vanadium/home/programs.nix +++ b/nix/configurations/vanadium/home/programs.nix @@ -23,6 +23,7 @@ pkgs.hutils pkgs.miniserve pkgs.agenix + pkgs.tmux-sessionizer # pdf pkgs.poppler_utils # pdfseparate, pdfunite From 664873d9a8a39a3406c27020672cce11b0d7571c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9ana=20=E6=B1=9F?= Date: Thu, 29 May 2025 15:24:27 +0200 Subject: [PATCH 08/11] fish: remove the concept of tmux-home for now --- nix/homeModules/common/fish/default.nix | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nix/homeModules/common/fish/default.nix b/nix/homeModules/common/fish/default.nix index c3b15848..c47a18d7 100644 --- a/nix/homeModules/common/fish/default.nix +++ b/nix/homeModules/common/fish/default.nix @@ -24,10 +24,9 @@ in { programs.tmux.extraConfig = lib.mkBefore '' # sessionizer binds bind -n C-f run-shell "tmux new-window ${fishExe} -c tmux-sessionizer" - bind -n C-g run-shell "${fishExe} -c tmux-home" bind s run-shell "${fishExe} -c tmux-last" ''; - programs.kitty.settings.shell = ''${fishExe} --command="tmux-home"''; + programs.kitty.settings.shell = ''${fishExe}''; # # Script dependencies From 31023ba3d8726da6494e024905a71705cc575cf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9ana=20=E6=B1=9F?= Date: Thu, 29 May 2025 15:30:11 +0200 Subject: [PATCH 09/11] tree-wide: change my shell to bash --- nix/nixosModules/extra/leana.nix | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nix/nixosModules/extra/leana.nix b/nix/nixosModules/extra/leana.nix index 57920d83..0f884580 100644 --- a/nix/nixosModules/extra/leana.nix +++ b/nix/nixosModules/extra/leana.nix @@ -6,7 +6,6 @@ # # My user # - programs.fish.enable = true; nix.settings.trusted-users = ["leana"]; users.users."leana" = { isNormalUser = true; @@ -14,7 +13,7 @@ description = "Leana"; group = "leana"; extraGroups = ["wheel"]; - shell = pkgs.fish; + shell = pkgs.bash; openssh.authorizedKeys.keys = config.users.users.root.openssh.authorizedKeys.keys; }; users.groups.leana = {}; From f5db31a4a981af4793c1431fbffa6afc6cf97487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9ana=20=E6=B1=9F?= Date: Thu, 29 May 2025 15:37:01 +0200 Subject: [PATCH 10/11] fish: make bindings shell agnostic --- nix/homeModules/common/fish/default.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nix/homeModules/common/fish/default.nix b/nix/homeModules/common/fish/default.nix index c47a18d7..d5ce080e 100644 --- a/nix/homeModules/common/fish/default.nix +++ b/nix/homeModules/common/fish/default.nix @@ -23,8 +23,8 @@ in { programs.direnv.config.whitelist.prefix = [(config.home.sessionVariables.REPO_PATH + "/leana8959")]; programs.tmux.extraConfig = lib.mkBefore '' # sessionizer binds - bind -n C-f run-shell "tmux new-window ${fishExe} -c tmux-sessionizer" - bind s run-shell "${fishExe} -c tmux-last" + bind -n C-f run-shell "tmux new-window tmux-sessionizer" + bind s run-shell "tmux-last" ''; programs.kitty.settings.shell = ''${fishExe}''; From eb29ed15dde74334b2841f24c9d6185a70ae43f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9ana=20=E6=B1=9F?= Date: Thu, 29 May 2025 16:35:19 +0200 Subject: [PATCH 11/11] tmux-sessionizer: fix fd search depth --- nix/packages/by-name/tmux-sessionizer/package.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nix/packages/by-name/tmux-sessionizer/package.nix b/nix/packages/by-name/tmux-sessionizer/package.nix index 6cdf7d1b..c00e3747 100644 --- a/nix/packages/by-name/tmux-sessionizer/package.nix +++ b/nix/packages/by-name/tmux-sessionizer/package.nix @@ -82,7 +82,7 @@ selected=$( { fd . "$REPO_PATH" --exact-depth 2 --hidden --type d - fd . "$WORKTREE_PATH" --exact-depth 4 --hidden --type d + fd . "$WORKTREE_PATH" --exact-depth 3 --hidden --type d fd . "$PLAYGROUND_PATH" --exact-depth 1 --hidden --type d echo "dotfiles" echo "password-store"