diff options
| author | David Morgan <djm_uk@protonmail.com> | 2025-09-25 08:58:31 +0100 |
|---|---|---|
| committer | David Morgan <djm_uk@protonmail.com> | 2025-09-25 08:58:31 +0100 |
| commit | 6e628ecd64764548dbd20861a3124ae5b9d26be5 (patch) | |
| tree | 57047191f57d2fc99d5d1c9dc1e747c1fab530d8 | |
| parent | ea2edfcd2ae3108e0bd4af5238541671aaf99c15 (diff) | |
| download | dotfiles-6e628ecd64764548dbd20861a3124ae5b9d26be5.tar.gz | |
Use karabiner for keyboard mappings
| -rw-r--r-- | nix-conf/darwin/configuration.nix | 25 | ||||
| -rw-r--r-- | nix-conf/flake.lock | 6 | ||||
| -rw-r--r-- | nix-conf/home/includes/darwin.nix | 1 | ||||
| -rw-r--r-- | nix-conf/home/includes/karabiner.nix | 176 |
4 files changed, 182 insertions, 26 deletions
diff --git a/nix-conf/darwin/configuration.nix b/nix-conf/darwin/configuration.nix index 70b7855..7a60b88 100644 --- a/nix-conf/darwin/configuration.nix +++ b/nix-conf/darwin/configuration.nix @@ -6,29 +6,6 @@ ]; system.stateVersion = 6; system.primaryUser = "djm"; - system.keyboard.enableKeyMapping = true; - system.keyboard.userKeyMapping = [ - { - HIDKeyboardModifierMappingSrc = 30064771296; - HIDKeyboardModifierMappingDst = 30064771299; - } - { - HIDKeyboardModifierMappingSrc = 30064771299; - HIDKeyboardModifierMappingDst = 30064771296; - } - ]; - #system.defaults.CustomUserPreferences = { - # "com.apple.symbolichotkeys" = { - # AppleSymbolicHotKeys = { - # "60" = { - # enabled = 0; - # }; - # "61" = { - # enabled = 0; - # }; - # }; - # }; - #}; nixpkgs.hostPlatform = "aarch64-darwin"; users.users.djm.home = "/Users/djm"; @@ -88,6 +65,8 @@ casks = [ "ankerwork" "aws-vpn-client" + "karabiner-elements" # services.karabiner-elements.enable = true; causes problems ]; }; + } diff --git a/nix-conf/flake.lock b/nix-conf/flake.lock index 103443c..1ce6f45 100644 --- a/nix-conf/flake.lock +++ b/nix-conf/flake.lock @@ -28,11 +28,11 @@ ] }, "locked": { - "lastModified": 1758719930, - "narHash": "sha256-DgHe1026Ob49CPegPMiWj1HNtlMTGQzfSZQQVlHC950=", + "lastModified": 1758782963, + "narHash": "sha256-8IHJucP64Nyahj5RqiurNAbH1aEcNX070mMI49SP0MQ=", "owner": "nix-community", "repo": "home-manager", - "rev": "142acd7a7d9eb7f0bb647f053b4ddfd01fdfbf1d", + "rev": "ade850153b41793e785e730f812b09302e9fd3b4", "type": "github" }, "original": { diff --git a/nix-conf/home/includes/darwin.nix b/nix-conf/home/includes/darwin.nix index 7ce0d1d..4b75a03 100644 --- a/nix-conf/home/includes/darwin.nix +++ b/nix-conf/home/includes/darwin.nix @@ -31,6 +31,7 @@ in imports = [ ./dev-common.nix ./ghostty.nix + ./karabiner.nix ]; home.packages = with pkgs; [ diff --git a/nix-conf/home/includes/karabiner.nix b/nix-conf/home/includes/karabiner.nix new file mode 100644 index 0000000..9d64fd8 --- /dev/null +++ b/nix-conf/home/includes/karabiner.nix @@ -0,0 +1,176 @@ +{ pkgs, ... }: + +let + json = pkgs.formats.json { }; + + # For things that should only apply to Emacs + emacsCondition = { + type = "frontmost_application_if"; + bundle_identifiers = [ + "^org\\.gnu\\.Emacs$" + "^org\\.gnu\\.Emacs-.*$" + "^net\\.mitsuya\\.Emacs$" + ]; + }; + + disableShortcut = from: { + type = "basic"; + inherit from; + to = [ { key_code = "vk_none"; } ]; + conditions = [ emacsCondition ]; + }; + + # macOS shortcuts to disable (note that this is affected by how we swap ctrl and cmd) + # This was generated by ChatGPT from a diff of com.apple.symbolichotkeys.plist files, so could be wrong + shortcuts = [ + # Spotlight + { + key_code = "spacebar"; + modifiers = { + mandatory = [ "control" ]; + }; + } + { + key_code = "spacebar"; + modifiers = { + mandatory = [ + "control" + "option" + ]; + }; + } + + # Mission Control / App windows / Move between Spaces + { + key_code = "up_arrow"; + modifiers = { + mandatory = [ "command" ]; # TODO control or command? + }; + } + { + key_code = "down_arrow"; + modifiers = { + mandatory = [ "command" ]; # TODO control or command? + }; + } + { + key_code = "left_arrow"; + modifiers = { + mandatory = [ "command" ]; # TODO control or command? + }; + } + { + key_code = "right_arrow"; + modifiers = { + mandatory = [ "command" ]; # TODO control or command? + }; + } + + # Dock toggle + { + key_code = "d"; + modifiers = { + mandatory = [ + "control" + "option" + ]; + }; + } + + # Help menu + { + key_code = "slash"; + modifiers = { + mandatory = [ + "control" + "shift" + ]; + }; + } + + # Quick Note (Fn-Q / Globe-Q) + { + key_code = "q"; + modifiers = { + mandatory = [ "fn" ]; + }; + } + ]; + + # For things that should NOT apply to certain terminals + terminalsCondition = { + type = "frontmost_application_unless"; + bundle_identifiers = [ + "^com\\.mitchellh\\.ghostty$" + "^org\\.alacritty$" + ]; + }; + + swapModifiers = pair: { + type = "basic"; + from = { key_code = pair.from; }; + to = [ { key_code = pair.to; } ]; + conditions = [ terminalsCondition ]; + }; + + modifierPairs = [ + { from = "left_command"; to = "left_control"; } + { from = "left_control"; to = "left_command"; } + { from = "right_command"; to = "right_control"; } + { from = "right_control"; to = "right_command"; } + ]; + + karabiner = { + profiles = [ + { + name = "Default profile"; + selected = true; + virtual_hid_keyboard = { + keyboard_type_v2 = "iso"; + }; + + complex_modifications = { + rules = [ + { + description = "Swap cmd and ctrl (except in ghossty and alacritty"; + manipulators = builtins.map swapModifiers modifierPairs; + } + + { + manipulators = [ + { + description = "Change caps_lock to command+control+option+shift."; + type = "basic"; + from = { + key_code = "caps_lock"; + modifiers = { + optional = [ "any" ]; + }; + }; + to = [ + { + key_code = "left_shift"; + modifiers = [ + "left_command" + "left_control" + "left_option" + ]; + } + ]; + } + ]; + } + + { + description = "Disable various macOS shortcuts in Emacs"; + manipulators = builtins.map disableShortcut shortcuts; + } + ]; + }; + } + ]; + }; +in +{ + xdg.configFile."karabiner/karabiner.json".source = json.generate "karabiner.json" karabiner; +} |