Files
nixos/scripts/hakase-scripts.nix
T
2025-12-24 23:03:49 -06:00

69 lines
2.2 KiB
Nix

{pkgs, ...}: let
# 1. The Brain: Logic to find a window or launch a new one
hakase-launch-or-focus = pkgs.writeShellScriptBin "hakase-launch-or-focus" ''
if (($# == 0)); then
echo "Usage: hakase-launch-or-focus [window-pattern] [launch-command]"
exit 1
fi
WINDOW_PATTERN="$1"
# Escaped Nix interpolation for shell default value logic
LAUNCH_COMMAND="''${2:-"uwsm-app -- $WINDOW_PATTERN"}"
# Locate window address via hyprctl and jq
WINDOW_ADDRESS=$(${pkgs.hyprland}/bin/hyprctl clients -j | ${pkgs.jq}/bin/jq -r --arg p "$WINDOW_PATTERN" \
'.[] | select((.class | test("\\b" + $p + "\\b"; "i")) or (.title | test("\\b" + $p + "\\b"; "i"))) | .address' | head -n1)
if [[ -n $WINDOW_ADDRESS ]]; then
# If found, focus the existing window
${pkgs.hyprland}/bin/hyprctl dispatch focuswindow "address:$WINDOW_ADDRESS"
else
# If not found, execute the launch command
eval exec setsid $LAUNCH_COMMAND
fi
'';
hakase-launch-tui = pkgs.writeShellScriptBin "hakase-launch-tui" ''
APP_NAME=$(basename "$1")
# Using org.hakase prefix so the focus script can find it via class name
exec setsid uwsm-app -- xdg-terminal-exec --app-id=org.hakase."$APP_NAME" -e "$@"
'';
hakase-focus-wrapper = pkgs.writeShellScriptBin "hakase-focus-wrapper" ''
APP_NAME=$(basename "$1")
WINDOW_PATTERN="org.hakase.$APP_NAME"
LAUNCH_CMD="hakase-launch-tui $*"
exec hakase-launch-or-focus "$WINDOW_PATTERN" "$LAUNCH_CMD"
'';
hakase-launch-wifi = pkgs.writeShellScriptBin "hakase-launch-wifi" ''
# Unblock the WiFi radio (requires appropriate user groups/permissions)
${pkgs.util-linux}/bin/rfkill unblock wifi
# Use the focus wrapper to launch or switch to Impala
exec hakase-focus-wrapper impala
'';
hakase-launch-bluetooth =
pkgs.writeShellScriptBin "hakase-launch-bluetooth"
''
${pkgs.util-linux}/bin/rfkill unblock wifi
exec hakase-focus-wrapper bluetui
'';
in {
environment.systemPackages = [
pkgs.jq
pkgs.util-linux # Provides rfkill
pkgs.bluetui
pkgs.impala
pkgs.wiremix
pkgs.pamixer
hakase-launch-or-focus
hakase-launch-tui
hakase-focus-wrapper
hakase-launch-wifi
hakase-launch-bluetooth
];
}