Skip to content

Commit

Permalink
home-manager/remap-keys: allow customization of mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagokokada committed May 23, 2024
1 parent 8750506 commit ae14f97
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 17 deletions.
6 changes: 5 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@
homePath = "/Users";
extraModules = [{
home-manager = {
darwin.remapKeys.enable = true;
darwin.remapKeys.mappings = {
# '§±' <-> '`~'
"0x700000035" = "0x700000064";
"0x700000064" = "0x700000035";
};
editor.jetbrains.enable = true;
dev = {
clojure.enable = false;
Expand Down
17 changes: 4 additions & 13 deletions home-manager/darwin/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,18 @@ let
cfg = config.home-manager.darwin;
in
{
imports = [ ./trampoline-apps.nix ];
imports = [
./remap-keys.nix
./trampoline-apps.nix
];

options.home-manager.darwin = {
enable = lib.mkEnableOption "Darwin (macOS) config" // {
default = pkgs.stdenv.isDarwin;
};
# https://gist.github.com/paultheman/808be117d447c490a29d6405975d41bd
remapKeys.enable = lib.mkEnableOption "remap internal Macbook keyboard keys from '§±' to '`~' (EU -> US, requires root)";
};

config = lib.mkIf cfg.enable {
home.activation.remapKeys = lib.mkIf cfg.remapKeys.enable
(lib.hm.dag.entryAfter [ "writeBoundary" ] /* bash */ ''
source="${./remapkeys.plist}"
destination="/Library/LaunchDaemons/com.nix.remakeys.plist"
if ! ${lib.getExe' pkgs.diffutils "diff"} "$source" "$destination"; then
$DRY_RUN_CMD /usr/bin/sudo ${lib.getExe' pkgs.coreutils "install"} -m 644 "$source" "$destination"
$DRY_RUN_CMD /usr/bin/sudo /bin/launchctl load -w "$destination"
fi
'');

home-manager = {
dev.enable = true;
desktop = {
Expand Down
52 changes: 52 additions & 0 deletions home-manager/darwin/remap-keys.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{ config, lib, pkgs, ... }:

let
cfg = config.home-manager.darwin.remapKeys;
in
{
options.home-manager.darwin.remapKeys = {
# https://gist.github.com/paultheman/808be117d447c490a29d6405975d41bd
mappings = lib.mkOption {
description = "Remap keyboard keys (requires sudo).";
type = with lib.types; attrsOf str;
default = { };
example = {
# '§±' <-> '`~'
"0x700000035" = "0x700000064";
"0x700000064" = "0x700000035";
};
};
productID = lib.mkOption {
description = "Product ID to remap (use `hidutil list` to get all of them).";
type = lib.types.str;
# Apple Internal Keyboard / Trackpad
default = "0x343";
};
vendorID = lib.mkOption {
description = "Vendor ID to remap (use `hidutil list` to get all of them).";
type = lib.types.str;
# Apple
default = "0x5ac";
};
};

config = lib.mkIf (cfg.mappings != { }) {
home.activation.remapKeys = lib.hm.dag.entryAfter [ "writeBoundary" ] /* bash */ ''
source="${pkgs.substituteAll {
inherit (cfg) productID vendorID;
src = ./remap-keys.plist;
remappings = lib.pipe cfg.mappings [
(lib.mapAttrsToList
(src: dst: /* json */
''{"HIDKeyboardModifierMappingSrc": ${src}, "HIDKeyboardModifierMappingDst": ${dst}}''))
(lib.concatStringsSep ", ")
];
}}"
destination="/Library/LaunchDaemons/com.nix.remapkeys.${cfg.vendorID}-${cfg.productID}.plist"
if [[ ! -L "$destination" ]]; then
/usr/bin/sudo ${lib.getExe' pkgs.coreutils "ln"} -s "$source" "$destination"
/usr/bin/sudo /bin/launchctl load -w "$destination"
fi
'';
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.nix.remapkeys</string>
<string>com.nix.remapkeys.@productID@-@vendorID@</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/hidutil</string>
<string>property</string>
<string>--set</string>
<string>{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000035, "HIDKeyboardModifierMappingDst":0x700000064}, {"HIDKeyboardModifierMappingSrc":0x700000064, "HIDKeyboardModifierMappingDst":0x700000035}]}</string>
<string>{"UserKeyMapping":[@remappings@]}</string>
<string>property</string>
<string>--matching</string>
<string>{"ProductID":0x343, "VendorID":0x5ac}</string>
<string>{"ProductID":@productID@, "VendorID":@vendorID@}</string>
</array>
<key>RunAtLoad</key>
<true/>
Expand Down

0 comments on commit ae14f97

Please sign in to comment.