ctucx.git: nixfiles

ctucx' nixfiles

commit e534f9857c6b39c444ab13c06f9d27d9bfcc3c6d
parent c099bc8bdc95a0fb20bb823cd0984eb53b408035
Author: Leah (ctucx) <leah@ctu.cx>
Date: Sun, 20 Mar 2022 18:24:09 +0100

machines/blechbuechse: remap keys on external keyboard too
2 files changed, 125 insertions(+), 10 deletions(-)
M
machines/blechbuechse/darwin-configuration.nix
|
22
++++++++++++----------
A
machines/blechbuechse/keyboard.nix
|
113
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
diff --git a/machines/blechbuechse/darwin-configuration.nix b/machines/blechbuechse/darwin-configuration.nix
@@ -28,6 +28,7 @@ in {
     ../../configurations/programs/cli/utilities.nix
     ../../configurations/programs/cli/network-utilities.nix
     ../../configurations/programs/cli/scripts.nix
+    ./keyboard.nix
   ];
 
   nix.package = pkgs.nix;

@@ -56,16 +57,17 @@ in {
   networking.hostName     = "blechbuechse";
   networking.computerName = config.networking.hostName;
 
-  system.keyboard = {
-    enableKeyMapping = true;
-    userKeyMapping   = [
-      # Remap tilde on non-US keyboards
-      { HIDKeyboardModifierMappingSrc = 30064771172; HIDKeyboardModifierMappingDst = 30064771125; }
-      # Swap right command and option keys
-      { HIDKeyboardModifierMappingSrc = 30064771303; HIDKeyboardModifierMappingDst = 30064771302; }
-      { HIDKeyboardModifierMappingSrc = 30064771302; HIDKeyboardModifierMappingDst = 30064771303; }
-    ];
-  };
+#  system.keyboard = {
+#    enableKeyMapping = true;
+#    userKeyMapping   = [
+#      # Remap tilde on non-US keyboards
+#      { HIDKeyboardModifierMappingSrc = 30064771172; HIDKeyboardModifierMappingDst = 30064771125; }
+#      # Swap right command and option keys
+#      { HIDKeyboardModifierMappingSrc = 30064771303; HIDKeyboardModifierMappingDst = 30064771302; }
+#      { HIDKeyboardModifierMappingSrc = 30064771302; HIDKeyboardModifierMappingDst = 30064771303; }
+#    ];
+#  };
+
   system.activationScripts.postActivation.text = ''
     # Set the default shell to bash. MacOS doesn't do this like nixOS does
     sudo chsh -s ${pkgs.bashInteractive}/bin/bash leah
diff --git a/machines/blechbuechse/keyboard.nix b/machines/blechbuechse/keyboard.nix
@@ -0,0 +1,113 @@
+{ config, pkgs, lib, ... }:
+
+let
+  XPCEventStreamHandler = pkgs.callPackage ../../pkgs/XPCEventStreamHandler {};
+
+  keyMappings = [
+    {
+      # Internal keyboard
+      VendorID       = "5ac";
+      ProductID       = "281";
+      UserKeyMapping = [
+        # Remap tilde on non-US keyboards
+        { HIDKeyboardModifierMappingSrc = 30064771172; HIDKeyboardModifierMappingDst = 30064771125; }
+        # Swap right_command and right_option
+        { HIDKeyboardModifierMappingSrc = 30064771303; HIDKeyboardModifierMappingDst = 30064771302; }
+        { HIDKeyboardModifierMappingSrc = 30064771302; HIDKeyboardModifierMappingDst = 30064771303; }
+      ];
+    }
+    {
+      # Trackpoint Keyboard 2
+      VendorID       = "17ef";
+      ProductID      = "60ee";
+      UserKeyMapping = [
+        # Swap left_command and left_option
+        { HIDKeyboardModifierMappingSrc = 30064771299; HIDKeyboardModifierMappingDst = 30064771298; }
+        { HIDKeyboardModifierMappingSrc = 30064771298; HIDKeyboardModifierMappingDst = 30064771299; }
+        # Remap PrintScreen to left_command
+        { HIDKeyboardModifierMappingSrc = 30064771142; HIDKeyboardModifierMappingDst = 30064771299; }
+      ];
+    }
+  ];
+
+  # convert hex to int with nix!
+  pow      = base: exp: lib.foldl' (a: x: x * a) 1 (lib.genList (_: base) exp);
+  hexToDec = v:
+    let
+      hexToInt = {
+        "0" = 0; "1" = 1;  "2" = 2;
+        "3" = 3; "4" = 4;  "5" = 5;
+        "6" = 6; "7" = 7;  "8" = 8;
+        "9" = 9; "a" = 10; "b" = 11;
+        "c" = 12;"d" = 13; "e" = 14;
+        "f" = 15;
+      };
+      chars = lib.stringToCharacters v;
+      charsLen = lib.length chars;
+    in
+      lib.foldl
+        (a: v: a + v)
+        0
+        (lib.imap0
+          (k: v: hexToInt."${v}" * (pow 16 (charsLen - k - 1)))
+          chars);
+
+  commandList  = lib.forEach keyMappings (entry: ''hidutil property --matching '{"ProductID":0x${entry.ProductID}}' --set '{"UserKeyMapping":${builtins.toJSON entry.UserKeyMapping}}' > /dev/null'');
+  launchAgents = lib.forEach keyMappings (entry: {
+    name         = "org.nixos.activateUserKeyMapping-${entry.VendorID}:${entry.ProductID}.plist";
+    value.enable = if (entry.VendorID != "5ac" && entry.ProductID != "281") then true else false;
+    value.text   = ''
+      <?xml version="1.0" encoding="UTF-8"?>
+      <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+      <plist version="1.0">
+      <dict>
+        <key>StandardErrorPath</key>
+        <string>/tmp/spoofmac.stderr</string>
+        <key>StandardOutPath</key>
+        <string>/tmp/spoofmac.stdout</string>
+
+        <key>Label</key>
+        <string>org.nixos.activateUserKeyMapping-${entry.VendorID}:${entry.ProductID}</string>
+
+        <key>ProgramArguments</key>
+        <array>
+          <string>${XPCEventStreamHandler}/bin/xpc_set_event_stream_handler</string>
+          <string>${pkgs.writeScript "hidutil" ''
+            #!/usr/bin/env bash
+            osascript -e 'display notification "Loaded UserKeyMapping for ${entry.VendorID}:${entry.ProductID}" with title "hidutil"'
+            hidutil property --matching '{"ProductID":0x${entry.ProductID}}' --set '{"UserKeyMapping":${builtins.toJSON entry.UserKeyMapping}}'
+          ''}</string>
+        </array>
+
+        <key>LaunchEvents</key>
+        <dict>
+          <key>com.apple.iokit.matching</key>
+          <dict>
+            <key>com.apple.device-attach</key>
+            <dict>
+              <key>idVendor</key>
+              <integer>${builtins.toString (hexToDec entry.VendorID)}</integer>
+              <key>idProduct</key>
+              <integer>${builtins.toString (hexToDec entry.ProductID)}</integer>
+              <key>IOProviderClass</key>
+              <string>IOUSBDevice</string>
+              <key>IOMatchLaunchStream</key>
+              <true/>
+            </dict>
+          </dict>
+        </dict>
+      </dict>
+      </plist>
+    '';
+  });
+
+in {
+
+  environment.userLaunchAgents           = builtins.listToAttrs launchAgents;
+  system.activationScripts.keyboard.text = ''
+    # Configuring keyboard
+    echo "configuring keyboard..." >&2
+    ${lib.concatStringsSep "\n" commandList}
+  '';
+
+}