Rework colorsync file-watching using fswatch instead of watchman

This commit is contained in:
Martin Larsson 2025-08-28 23:00:23 +02:00
parent f74c830e88
commit de289309f7
5 changed files with 72 additions and 85 deletions

View file

@ -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.";
})
]

View file

@ -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.";
})
]

View file

@ -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; };

View file

@ -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";
};
};
};