From 5fdec143aebd05b6c0cc8928eae292806c565c6d Mon Sep 17 00:00:00 2001 From: alexion Date: Sat, 10 Oct 2026 14:17:22 -0400 Subject: [PATCH] refactor(hyprland): simplify keybind action schema --- configs/nixos/registry/desktop-keybind.nix | 116 ++++++--------------- 1 file changed, 30 insertions(+), 86 deletions(-) diff --git a/configs/nixos/registry/desktop-keybind.nix b/configs/nixos/registry/desktop-keybind.nix index 78eb351..e148a73 100644 --- a/configs/nixos/registry/desktop-keybind.nix +++ b/configs/nixos/registry/desktop-keybind.nix @@ -1,12 +1,20 @@ { lib, ... }: let - actionMatchesType = + actionType = lib.types.addCheck (lib.types.attrsOf lib.types.anything) isAction; + + isAction = action: - let - required = actionFields.${action.type}; - in - lib.all (field: action.${field} != null) required - && lib.all (field: lib.elem field required || action.${field} == null) actionOptions; + builtins.hasAttr "type" action + && builtins.hasAttr action.type actionFields + && ( + let + fields = builtins.getAttr action.type actionFields; + allowedFields = [ "type" ] ++ fields; + in + lib.all (field: builtins.hasAttr field action) fields + && lib.all (field: lib.elem field allowedFields) (builtins.attrNames action) + && lib.all (field: actionFieldChecks.${field} action.${field}) fields + ); actionFields = { exec = [ "command" ]; @@ -25,14 +33,21 @@ let "move-to-workspace" = [ "workspace" ]; }; - actionOptions = [ - "command" - "direction" - "x" - "y" - "workspace" - "message" - ]; + actionFieldChecks = { + command = builtins.isString; + direction = + direction: + lib.elem direction [ + "left" + "down" + "up" + "right" + ]; + x = builtins.isInt; + y = builtins.isInt; + workspace = builtins.isInt; + message = builtins.isString; + }; in { checks = [ @@ -40,10 +55,6 @@ in assertion = entries: lib.all (entry: entry.value.keys != [ ]) entries; message = "Desktop keybinds must set at least one key."; } - { - assertion = entries: lib.all (entry: actionMatchesType entry.value.action) entries; - message = "Desktop keybind action fields must match their type."; - } ]; type = lib.types.submodule { @@ -58,74 +69,7 @@ in }; action = lib.mkOption { - type = lib.types.submodule { - options = { - type = lib.mkOption { - type = lib.types.enum [ - "exec" - "focus" - "move-window" - "resize-window" - "toggle-floating" - "fullscreen" - "layout" - "close-window" - "kill-window" - "focus-workspace" - "move-to-workspace" - ]; - description = "Selects the desktop action this keybind triggers."; - }; - - command = lib.mkOption { - type = lib.types.nullOr lib.types.str; - default = null; - example = "ghostty"; - description = "Command executed by an exec action."; - }; - - direction = lib.mkOption { - type = lib.types.nullOr ( - lib.types.enum [ - "left" - "down" - "up" - "right" - ] - ); - default = null; - description = "Direction used by focus and move-window actions."; - }; - - x = lib.mkOption { - type = lib.types.nullOr lib.types.int; - default = null; - example = 40; - description = "Horizontal delta used by resize-window actions."; - }; - - y = lib.mkOption { - type = lib.types.nullOr lib.types.int; - default = null; - example = 40; - description = "Vertical delta used by resize-window actions."; - }; - - workspace = lib.mkOption { - type = lib.types.nullOr lib.types.int; - default = null; - example = 1; - description = "Workspace number used by workspace actions."; - }; - - message = lib.mkOption { - type = lib.types.nullOr lib.types.str; - default = null; - example = "togglesplit"; - description = "Layout message sent by layout actions."; - }; - }; - }; + type = actionType; description = "Declares the desktop action for this keybind."; }; };