From de289309f7ae77652c6eea3799ad6114f21b28df Mon Sep 17 00:00:00 2001 From: Martin Larsson Date: Thu, 28 Aug 2025 23:00:23 +0200 Subject: [PATCH] Rework colorsync file-watching using fswatch instead of watchman --- nix/home/common/colorsync_services.nix | 36 +++++++++++ .../common/watchman_colorsync_services.nix | 39 ----------- nix/home/default.nix | 4 +- nix/utils.nix | 64 ++++++++++--------- tmux/tmux-watchman-statuscolor-trigger.sh | 14 ---- 5 files changed, 72 insertions(+), 85 deletions(-) create mode 100644 nix/home/common/colorsync_services.nix delete mode 100644 nix/home/common/watchman_colorsync_services.nix delete mode 100755 tmux/tmux-watchman-statuscolor-trigger.sh diff --git a/nix/home/common/colorsync_services.nix b/nix/home/common/colorsync_services.nix new file mode 100644 index 0000000..87c5c37 --- /dev/null +++ b/nix/home/common/colorsync_services.nix @@ -0,0 +1,36 @@ +{ + pkgs, + lib, + config, + isLinux, + isDarwin, + ... +}: +let + utils = import ../../utils.nix; + + name = "colorsync-scriptrunner"; + colorsyncScriptWrapper = pkgs.writeShellScriptBin "${name}-wrapper" '' + set -euo pipefail + + echo colorsync-scriptrunner is executing + "$HOME/.config/tmux/tmux-statusbar-color.sh" + "$HOME/.config/ghostty/ghostty-change-theme.sh" + ''; + root = "$HOME/.local/state/colorsync/current"; +in +lib.mkMerge [ + (utils.mkFswatchService { + inherit + pkgs + lib + config + isLinux + isDarwin + ; + name = name; + scriptPath = "${colorsyncScriptWrapper}/bin/${name}-wrapper"; + root = root; + description = "Run colorsync-scriptrunner after ${root} changes."; + }) +] diff --git a/nix/home/common/watchman_colorsync_services.nix b/nix/home/common/watchman_colorsync_services.nix deleted file mode 100644 index 1f58daf..0000000 --- a/nix/home/common/watchman_colorsync_services.nix +++ /dev/null @@ -1,39 +0,0 @@ -{ - pkgs, - lib, - config, - isLinux, - isDarwin, - ... -}: -let - utils = import ../../utils.nix; -in -lib.mkMerge [ - (utils.mkWatchmanTrigger { - inherit - pkgs - lib - config - isLinux - isDarwin - ; - name = "ghostty"; - triggerName = "ghostty_theme"; - scriptPath = "$HOME/.config/ghostty/ghostty-change-theme.sh"; - description = "Register watchman trigger for Ghostty themes."; - }) - (utils.mkWatchmanTrigger { - inherit - pkgs - lib - config - isLinux - isDarwin - ; - name = "tmux"; - triggerName = "tmux_statusbar_color"; - scriptPath = "$HOME/.config/tmux/tmux-statusbar-color.sh"; - description = "Register watchman trigger for Tmux statusbar colors."; - }) -] diff --git a/nix/home/default.nix b/nix/home/default.nix index b8eeb75..87917fc 100644 --- a/nix/home/default.nix +++ b/nix/home/default.nix @@ -67,7 +67,7 @@ let in { imports = [ - ./common/watchman_colorsync_services.nix + ./common/colorsync_services.nix ./common/firefox.nix ]; @@ -144,8 +144,8 @@ in rainfrog atac p7zip - watchman colorsync + fswatch ]; file = utils.mk_symlinks { inherit config dotfiles; }; diff --git a/nix/utils.nix b/nix/utils.nix index a7a1a89..5581e78 100644 --- a/nix/utils.nix +++ b/nix/utils.nix @@ -47,7 +47,7 @@ rec { done ''; - mkWatchmanTrigger = + mkFswatchService = { pkgs, lib, @@ -56,51 +56,55 @@ rec { isDarwin, name, - triggerName, scriptPath, - - root ? "$HOME/.local/state/colorsync", - description ? "Register watchman trigger for ${name}.", - watchExpr ? "current", + root, + description ? "Register ${name}.", }: let - serviceName = "${name}-watchman"; - # e.g. tmux-watchman-statuscolor, ghostty-watchman-theme - binName = - let - trig = lib.replaceStrings [ "_" ] [ "-" ] triggerName; - in - "${serviceName}-${trig}"; - - scriptPkg = pkgs.writeShellScriptBin binName '' + scriptPkg = pkgs.writeShellScriptBin "${name}" '' set -euo pipefail ROOT="${root}" - TRIGGER_NAME="${triggerName}" SCRIPT="${scriptPath}" - WATCHMAN="${pkgs.watchman}/bin/watchman" + FSWATCH="${pkgs.fswatch}/bin/fswatch" - "$WATCHMAN" -- watch-project "$ROOT" >/dev/null - "$WATCHMAN" -- trigger-del "$ROOT" "$TRIGGER_NAME" >/dev/null 2>&1 || true - "$WATCHMAN" -- trigger "$ROOT" "$TRIGGER_NAME" ${watchExpr} -- bash "$SCRIPT" + echo root:"$ROOT" + echo script:"$SCRIPT" + echo fswatch:"$FSWATCH" + echo home:"$HOME" + + "$FSWATCH" --latency=0.2 -o "$ROOT" | xargs -n1 "$SCRIPT" ''; - pathForService = lib.makeBinPath [ - pkgs.watchman + baseBinPath = lib.makeBinPath [ + pkgs.findutils + pkgs.fswatch pkgs.bash pkgs.coreutils ]; + pathForService = lib.concatStringsSep ":" [ + baseBinPath + "${config.home.profileDirectory}/bin" + "/etc/profiles/per-user/${config.home.username}/bin" + "/opt/homebrew/bin" + "/usr/local/bin" + "/usr/bin" + "/bin" + ]; + linuxAttrs = lib.optionalAttrs isLinux { systemd.user.startServices = "sd-switch"; - systemd.user.services."${serviceName}" = { + systemd.user.services."${name}" = { Unit = { Description = description; After = [ "graphical-session.target" ]; }; Service = { - Type = "oneshot"; - ExecStart = "${scriptPkg}/bin/${binName}"; + Type = "simple"; + Restart = "always"; + RestartSec = "5s"; + ExecStart = "${scriptPkg}/bin/${name}"; Environment = [ "PATH=${pathForService}" ]; StandardOutput = "journal"; StandardError = "journal"; @@ -112,17 +116,17 @@ rec { }; darwinAttrs = lib.optionalAttrs isDarwin { - launchd.agents."${serviceName}" = { + launchd.agents."${name}" = { enable = true; config = { - ProgramArguments = [ "${scriptPkg}/bin/${binName}" ]; + ProgramArguments = [ "${scriptPkg}/bin/${name}" ]; RunAtLoad = true; - KeepAlive = false; + KeepAlive = true; EnvironmentVariables = { PATH = pathForService; }; - StandardOutPath = "${config.home.homeDirectory}/Library/Logs/${serviceName}.log"; - StandardErrorPath = "${config.home.homeDirectory}/Library/Logs/${serviceName}.err"; + StandardOutPath = "${config.home.homeDirectory}/Library/Logs/${name}.log"; + StandardErrorPath = "${config.home.homeDirectory}/Library/Logs/${name}.err"; }; }; }; diff --git a/tmux/tmux-watchman-statuscolor-trigger.sh b/tmux/tmux-watchman-statuscolor-trigger.sh deleted file mode 100755 index 5b295da..0000000 --- a/tmux/tmux-watchman-statuscolor-trigger.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail - -ROOT="$HOME/.local/state/colorsync" -TRIGGER_NAME="tmux_statusbar_color" -SCRIPT="$HOME/.config/tmux/tmux-statusbar-color.sh" -WATCHMAN="watchman" - -"$WATCHMAN" watch-project "$ROOT" >/dev/null -"$WATCHMAN" -- trigger-del "$ROOT" "$TRIGGER_NAME" >/dev/null 2>&1 || true -"$WATCHMAN" -- trigger "$ROOT" "$TRIGGER_NAME" 'current' -- bash "$SCRIPT" -# watchman -- trigger "$HOME/.local/state/colorsync" tmux_statusbar_color 'current' -- bash "$HOME/.config/tmux/tmux-statusbar-color.sh" - -# watchman watch-project "$HOME/.local/state/colorsync"