From b8da5337cc22dc6fedd2591f98da8379232bfd1c Mon Sep 17 00:00:00 2001 From: MrTipson Date: Sun, 15 Dec 2024 00:28:43 +0100 Subject: [PATCH] fish: source event handling functions on shell init Functions that contain event handler switches should be sourced during init, otherwise they become active only after being called manually. --- modules/programs/fish.nix | 12 ++++++ tests/modules/programs/fish/default.nix | 1 + .../modules/programs/fish/source-handlers.nix | 43 +++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 tests/modules/programs/fish/source-handlers.nix diff --git a/modules/programs/fish.nix b/modules/programs/fish.nix index 02141042d..5d47ddd0d 100644 --- a/modules/programs/fish.nix +++ b/modules/programs/fish.nix @@ -228,6 +228,15 @@ let echo "end" echo "setup_hm_session_vars") > $out ''; + sourceHandlersStr = let + handlerAttrs = + [ "onJobExit" "onProcessExit" "onVariable" "onSignal" "onEvent" ]; + isHandler = name: def: + isAttrs def && any (attr: hasAttr attr def) handlerAttrs; + handlerFunctions = filterAttrs isHandler cfg.functions; + sourceFunction = name: def: + "source ${config.xdg.configHome}/fish/functions/${name}.fish"; + in concatStringsSep "\n" (mapAttrsToList sourceFunction handlerFunctions); in { imports = [ @@ -475,6 +484,9 @@ in { source ${translatedSessionVariables} + # Source handler functions + ${sourceHandlersStr} + ${cfg.shellInit} status is-login; and begin diff --git a/tests/modules/programs/fish/default.nix b/tests/modules/programs/fish/default.nix index f81ff971e..8903ee0a1 100644 --- a/tests/modules/programs/fish/default.nix +++ b/tests/modules/programs/fish/default.nix @@ -3,5 +3,6 @@ fish-format-scripts = ./format-scripts.nix; fish-functions = ./functions.nix; fish-no-functions = ./no-functions.nix; + fish-source-handlers = ./source-handlers.nix; fish-plugins = ./plugins.nix; } diff --git a/tests/modules/programs/fish/source-handlers.nix b/tests/modules/programs/fish/source-handlers.nix new file mode 100644 index 000000000..80ab273df --- /dev/null +++ b/tests/modules/programs/fish/source-handlers.nix @@ -0,0 +1,43 @@ +{ config, lib, pkgs, ... }: + +with builtins; { + config = { + programs.fish = { + enable = true; + + functions = { + normal-function = ""; + event-handler = { + body = ""; + onEvent = "test"; + }; + variable-handler = { + body = ""; + onVariable = "test"; + }; + job-handler = { + body = ""; + onJobExit = "10"; + }; + signal-handler = { + body = ""; + onSignal = "10"; + }; + process-handler = { + body = ""; + onProcessExit = "10"; + }; + }; + }; + + nmt.script = '' + assertFileExists home-files/.config/fish/config.fish + assertFileContains home-files/.config/fish/config.fish "source /home/hm-user/.config/fish/functions/event-handler.fish" + assertFileContains home-files/.config/fish/config.fish "source /home/hm-user/.config/fish/functions/variable-handler.fish" + assertFileContains home-files/.config/fish/config.fish "source /home/hm-user/.config/fish/functions/job-handler.fish" + assertFileContains home-files/.config/fish/config.fish "source /home/hm-user/.config/fish/functions/signal-handler.fish" + assertFileContains home-files/.config/fish/config.fish "source /home/hm-user/.config/fish/functions/process-handler.fish" + assertFileNotRegex home-files/.config/fish/config.fish "source /home/hm-user/.config/fish/functions/normal-function.fish" + ''; + }; +}