mirror of
https://github.com/nix-community/home-manager.git
synced 2025-12-03 07:31:03 +01:00
macos-remap-keys: add (#6605)
A module to remap keys using macos native tool (hidutil)
This commit is contained in:
parent
ef257da52a
commit
6576167e6b
7 changed files with 248 additions and 0 deletions
72
modules/services/macos-remap-keys/default.nix
Normal file
72
modules/services/macos-remap-keys/default.nix
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
{ config, lib, pkgs, ... }:
|
||||
let
|
||||
cfg = config.services.macos-remap-keys;
|
||||
keytables = import ./keytables.nix { inherit lib; };
|
||||
|
||||
keyToHIDCode = table: key: keytables.${table}.${key};
|
||||
|
||||
# Note: hidutil requires HIDKeyboardModifierMapping values to be in hexadecimal
|
||||
# format rather than decimal JSON. Using hex strings instead of numbers will
|
||||
# crash macOS.
|
||||
makeMapping = table: from: to:
|
||||
''
|
||||
{ "HIDKeyboardModifierMappingSrc": ${
|
||||
keyToHIDCode table from
|
||||
}, "HIDKeyboardModifierMappingDst": ${keyToHIDCode table to} }'';
|
||||
|
||||
makeMappingsList = table: mappings:
|
||||
lib.mapAttrsToList (from: to: makeMapping table from to) mappings;
|
||||
|
||||
allMappings = (makeMappingsList "keyboard" (cfg.keyboard or { }))
|
||||
++ (makeMappingsList "keypad" (cfg.keypad or { }));
|
||||
|
||||
allMappingsString = lib.concatStringsSep ", " allMappings;
|
||||
propertyString = ''{ "UserKeyMapping": [ ${allMappingsString} ] }'';
|
||||
in {
|
||||
meta.maintainers = [ lib.maintainers.WeetHet ];
|
||||
|
||||
options.services.macos-remap-keys = {
|
||||
enable = lib.mkEnableOption "macOS key remapping service";
|
||||
|
||||
keyboard = lib.mkOption {
|
||||
type = lib.types.attrsOf lib.types.str;
|
||||
default = { };
|
||||
example = {
|
||||
Capslock = "Escape";
|
||||
SquareBracketOpen = "SquareBracketClose";
|
||||
};
|
||||
description = "Mapping of keyboard keys to remap";
|
||||
};
|
||||
|
||||
keypad = lib.mkOption {
|
||||
type = lib.types.attrsOf lib.types.str;
|
||||
default = { };
|
||||
example = {
|
||||
Enter = "Equal";
|
||||
Plus = "Minus";
|
||||
};
|
||||
description = "Mapping of keypad keys to remap";
|
||||
};
|
||||
};
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
assertions = [
|
||||
(lib.hm.assertions.assertPlatform "services.macos-remap-keys" pkgs
|
||||
lib.platforms.darwin)
|
||||
];
|
||||
home.activation.macosRemapKeys =
|
||||
lib.hm.dag.entryAfter [ "writeBoundary" ] ''
|
||||
run --silence /usr/bin/hidutil property --set '${propertyString}'
|
||||
'';
|
||||
|
||||
launchd.agents.remap-keys = {
|
||||
enable = true;
|
||||
config = {
|
||||
ProgramArguments =
|
||||
[ "/usr/bin/hidutil" "property" "--set" propertyString ];
|
||||
KeepAlive.SuccessfulExit = false;
|
||||
RunAtLoad = true;
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue