From ebeaeeea4671628e90e669d94aca9c3dd23358be Mon Sep 17 00:00:00 2001 From: Sukant Hajra Date: Sat, 20 Jan 2024 20:58:12 -0600 Subject: [PATCH] Switch Cmd/Option via Karabiner instead of keyboards --- home/modules/base/gui/all/default.nix | 25 +- home/modules/base/tui/darwin/default.nix | 2 + .../tui/darwin/home/activation/default.nix | 20 +- .../tui/darwin/xdg/configFile/default.nix | 3 + .../tui/darwin/xdg/configFile/karabiner.json | 283 ++++++++++++++++++ 5 files changed, 318 insertions(+), 15 deletions(-) create mode 100644 home/modules/base/tui/darwin/xdg/configFile/default.nix create mode 100644 home/modules/base/tui/darwin/xdg/configFile/karabiner.json diff --git a/home/modules/base/gui/all/default.nix b/home/modules/base/gui/all/default.nix index f2c3df9d..fc88efb6 100644 --- a/home/modules/base/gui/all/default.nix +++ b/home/modules/base/gui/all/default.nix @@ -17,25 +17,22 @@ in { fonts.fontconfig.enable = true; - # DESIGN: Allows VSCode to override settings, but everything will be lost - # upon a home-manager switch. - # - # REVISIT: Might want to complicate logic to more easily catch changes or - # maybe save them off as backups. - # - home.activation.removeVscodeMutableUserSettings = + # DESIGN: Allows VSCode to override settings, but we catch changes when + # performing a home-manager switch. + home.activation.restoreImmutableVscodeUserSettings = lib.hm.dag.entryBefore [ "checkLinkTargets" ] '' - $DRY_RUN_CMD rm -f "${vscodeSettings}" + DEST="${vscodeSettings}" + if test -e "$DEST" && test -e "$DEST.home-manager" \ + && "${pkgs.diffutils}/bin/diff" -q "$DEST" "$DEST.home-manager" + then $DRY_RUN_CMD mv "$DEST.home-manager" "$DEST" + fi ''; home.activation.makeVscodeUserSettingsMutable = lib.hm.dag.entryAfter [ "writeBoundary" ] '' - $DRY_RUN_CMD rm -f "${vscodeSettings}" - $DRY_RUN_CMD cat \ - ${(pkgs.formats.json {}).generate - "vscode-settings.json" - config.programs.vscode.userSettings} \ - > "${vscodeSettings}" + DEST="${vscodeSettings}" + $DRY_RUN_CMD mv "$DEST" "$DEST.home-manager" + $DRY_RUN_CMD cat "$DEST.home-manager" > "$DEST" ''; home.extraPackages = build.pkgs.lists.base.gui.all; diff --git a/home/modules/base/tui/darwin/default.nix b/home/modules/base/tui/darwin/default.nix index 14e497aa..ac29f123 100644 --- a/home/modules/base/tui/darwin/default.nix +++ b/home/modules/base/tui/darwin/default.nix @@ -12,4 +12,6 @@ # REVISIT: 23-12-21: pinned to stable because broot built was broken programs.broot.package = build.infra.np.nixpkgs.stable.broot; + + xdg.configFile = import xdg/configFile; } diff --git a/home/modules/base/tui/darwin/home/activation/default.nix b/home/modules/base/tui/darwin/home/activation/default.nix index 04b3ecbc..8f34e856 100644 --- a/home/modules/base/tui/darwin/home/activation/default.nix +++ b/home/modules/base/tui/darwin/home/activation/default.nix @@ -1,4 +1,4 @@ -config: lib: nixpkgs: +config: lib: pkgs: let home = config.home.homeDirectory; @@ -25,4 +25,22 @@ in $DRY_RUN_CMD [ -e "$app_path" ] && rm -r "$app_path" $DRY_RUN_CMD mv "$tmp_path" "$app_path" ''; + + # DESIGN: Allows Karabiner to override settings, but we catch changes when + # performing a home-manager switch. + restoreImmutableKarabinerJson = + lib.hm.dag.entryBefore [ "checkLinkTargets" ] '' + DEST="${config.xdg.configHome}/karabiner/karabiner.json" + if test -e "$DEST" && test -e "$DEST.home-manager" \ + && "${pkgs.diffutils}/bin/diff" -q "$DEST" "$DEST.home-manager" + then $DRY_RUN_CMD mv "$DEST.home-manager" "$DEST" + fi + ''; + + makeKarabinerJsonSettingsMutable = + lib.hm.dag.entryAfter [ "writeBoundary" ] '' + DEST="${config.xdg.configHome}/karabiner/karabiner.json" + $DRY_RUN_CMD mv "$DEST" "$DEST.home-manager" + $DRY_RUN_CMD cat "$DEST.home-manager" > "$DEST" + ''; } diff --git a/home/modules/base/tui/darwin/xdg/configFile/default.nix b/home/modules/base/tui/darwin/xdg/configFile/default.nix new file mode 100644 index 00000000..6c96783f --- /dev/null +++ b/home/modules/base/tui/darwin/xdg/configFile/default.nix @@ -0,0 +1,3 @@ +{ + "karabiner/karabiner.json".source = ./karabiner.json; +} diff --git a/home/modules/base/tui/darwin/xdg/configFile/karabiner.json b/home/modules/base/tui/darwin/xdg/configFile/karabiner.json new file mode 100644 index 00000000..b3a33009 --- /dev/null +++ b/home/modules/base/tui/darwin/xdg/configFile/karabiner.json @@ -0,0 +1,283 @@ +{ + "global": { + "ask_for_confirmation_before_quitting": true, + "check_for_updates_on_startup": true, + "show_in_menu_bar": true, + "show_profile_name_in_menu_bar": false, + "unsafe_ui": false + }, + "profiles": [ + { + "complex_modifications": { + "parameters": { + "basic.simultaneous_threshold_milliseconds": 50, + "basic.to_delayed_action_delay_milliseconds": 500, + "basic.to_if_alone_timeout_milliseconds": 1000, + "basic.to_if_held_down_threshold_milliseconds": 500, + "mouse_motion_to_scroll.speed": 100 + }, + "rules": [] + }, + "devices": [ + { + "disable_built_in_keyboard_if_exists": false, + "fn_function_keys": [], + "identifiers": { + "is_keyboard": true, + "is_pointing_device": false, + "product_id": 833, + "vendor_id": 1452 + }, + "ignore": false, + "manipulate_caps_lock_led": true, + "simple_modifications": [], + "treat_as_built_in_keyboard": false + }, + { + "disable_built_in_keyboard_if_exists": false, + "fn_function_keys": [], + "identifiers": { + "is_keyboard": false, + "is_pointing_device": true, + "product_id": 833, + "vendor_id": 1452 + }, + "ignore": true, + "manipulate_caps_lock_led": false, + "simple_modifications": [], + "treat_as_built_in_keyboard": false + }, + { + "disable_built_in_keyboard_if_exists": false, + "fn_function_keys": [], + "identifiers": { + "is_keyboard": false, + "is_pointing_device": true, + "product_id": 407, + "vendor_id": 6780 + }, + "ignore": true, + "manipulate_caps_lock_led": false, + "simple_modifications": [], + "treat_as_built_in_keyboard": false + }, + { + "disable_built_in_keyboard_if_exists": false, + "fn_function_keys": [], + "identifiers": { + "is_keyboard": true, + "is_pointing_device": false, + "product_id": 6, + "vendor_id": 13462 + }, + "ignore": false, + "manipulate_caps_lock_led": true, + "simple_modifications": [ + { + "from": { + "key_code": "left_command" + }, + "to": [ + { + "key_code": "left_option" + } + ] + }, + { + "from": { + "key_code": "left_option" + }, + "to": [ + { + "key_code": "left_command" + } + ] + }, + { + "from": { + "key_code": "right_command" + }, + "to": [ + { + "key_code": "right_option" + } + ] + }, + { + "from": { + "key_code": "right_option" + }, + "to": [ + { + "key_code": "right_command" + } + ] + } + ], + "treat_as_built_in_keyboard": false + }, + { + "disable_built_in_keyboard_if_exists": false, + "fn_function_keys": [], + "identifiers": { + "is_keyboard": false, + "is_pointing_device": true, + "product_id": 6, + "vendor_id": 13462 + }, + "ignore": true, + "manipulate_caps_lock_led": false, + "simple_modifications": [], + "treat_as_built_in_keyboard": false + }, + { + "disable_built_in_keyboard_if_exists": false, + "fn_function_keys": [], + "identifiers": { + "is_keyboard": true, + "is_pointing_device": false, + "product_id": 34304, + "vendor_id": 1452 + }, + "ignore": false, + "manipulate_caps_lock_led": true, + "simple_modifications": [], + "treat_as_built_in_keyboard": false + } + ], + "fn_function_keys": [ + { + "from": { + "key_code": "f1" + }, + "to": [ + { + "consumer_key_code": "display_brightness_decrement" + } + ] + }, + { + "from": { + "key_code": "f2" + }, + "to": [ + { + "consumer_key_code": "display_brightness_increment" + } + ] + }, + { + "from": { + "key_code": "f3" + }, + "to": [ + { + "apple_vendor_keyboard_key_code": "mission_control" + } + ] + }, + { + "from": { + "key_code": "f4" + }, + "to": [ + { + "apple_vendor_keyboard_key_code": "spotlight" + } + ] + }, + { + "from": { + "key_code": "f5" + }, + "to": [ + { + "consumer_key_code": "dictation" + } + ] + }, + { + "from": { + "key_code": "f6" + }, + "to": [ + { + "key_code": "f6" + } + ] + }, + { + "from": { + "key_code": "f7" + }, + "to": [ + { + "consumer_key_code": "rewind" + } + ] + }, + { + "from": { + "key_code": "f8" + }, + "to": [ + { + "consumer_key_code": "play_or_pause" + } + ] + }, + { + "from": { + "key_code": "f9" + }, + "to": [ + { + "consumer_key_code": "fast_forward" + } + ] + }, + { + "from": { + "key_code": "f10" + }, + "to": [ + { + "consumer_key_code": "mute" + } + ] + }, + { + "from": { + "key_code": "f11" + }, + "to": [ + { + "consumer_key_code": "volume_decrement" + } + ] + }, + { + "from": { + "key_code": "f12" + }, + "to": [ + { + "consumer_key_code": "volume_increment" + } + ] + } + ], + "name": "Default", + "parameters": { + "delay_milliseconds_before_open_device": 1000 + }, + "selected": true, + "simple_modifications": [], + "virtual_hid_keyboard": { + "country_code": 0, + "indicate_sticky_modifier_keys_state": true, + "mouse_key_xy_scale": 100 + } + } + ] +}