ctucx.git: nixfiles

ctucx' nixfiles

commit adfdacfaa71798388c93180c2b915dc39cac55fc
parent bdbe6062378562d6eb11b22fea582d8955a98137
Author: Leah (ctucx) <leah@ctu.cx>
Date: Sun, 12 Jun 2022 11:20:06 +0200

machines/{blechbuechse,blechkasten}: use hidutil module
4 files changed, 62 insertions(+), 162 deletions(-)
M
machines/blechbuechse/darwin-configuration.nix
|
32
+++++++++++++++++++++++++++++++-
D
machines/blechbuechse/keyboard.nix
|
80
-------------------------------------------------------------------------------
M
machines/blechkasten/darwin-configuration.nix
|
32
+++++++++++++++++++++++++++++++-
D
machines/blechkasten/keyboard.nix
|
80
-------------------------------------------------------------------------------
diff --git a/machines/blechbuechse/darwin-configuration.nix b/machines/blechbuechse/darwin-configuration.nix
@@ -9,6 +9,7 @@ in {
   imports = [
     <home-manager/nix-darwin>
     ../../modules/darwin/syncthing.nix
+    ../../modules/darwin/hidutil.nix
     ../../modules/darwin/quirks.nix
 
     ../../helpers/make-nixpkgs.nix

@@ -30,13 +31,42 @@ in {
     ../../configurations/programs/cli/garminexport.nix
     ./yabai.nix
     ./skhd.nix
-    ./keyboard.nix
     ./speakers.nix
   ];
 
   nix.package = pkgs.nix;
   services.nix-daemon.enable = true;
 
+  hidutil = {
+    enable = true;
+    remapKeys = [
+      {
+        # 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; }
+        ];
+      }
+    ];
+  };
+
   home-manager = {
     useUserPackages = true;
 
diff --git a/machines/blechbuechse/keyboard.nix b/machines/blechbuechse/keyboard.nix
@@ -1,80 +0,0 @@
-{ 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 '{"VendorID":0x${entry.VendorID},"ProductID":0x${entry.ProductID}}' --set '{"UserKeyMapping":${builtins.toJSON entry.UserKeyMapping}}' > /dev/null'');
-
-in {
-
-  launchd.user.agents = builtins.listToAttrs (lib.forEach keyMappings (entry: {
-    name  = "activateUserKeyMapping-${entry.VendorID}:${entry.ProductID}";
-    value.serviceConfig = {
-      Disabled          = if (entry.VendorID != "5ac" && entry.ProductID != "281") then false else true;
-      Label             = "org.nixos.activateUserKeyMapping-${entry.VendorID}:${entry.ProductID}";
-#      StandardErrorPath = "/tmp/activateUserKeyMapping.stderr";
-#      StandardOutPath   = "/tmp/activateUserKeyMapping.stdout";
-      ProgramArguments  = [
-        "${XPCEventStreamHandler}/bin/xpc_set_event_stream_handler"
-        "${pkgs.writeScript "hidutil" ''
-          #!/usr/bin/env bash
-          osascript -e 'display notification "Load UserKeyMapping for ${entry.VendorID}:${entry.ProductID}" with title "hidutil"'
-          hidutil property --matching '{"VendorID":0x${entry.VendorID},"ProductID":0x${entry.ProductID}}' --set '{"UserKeyMapping":${builtins.toJSON entry.UserKeyMapping}}'  > /dev/null
-        ''}"
-      ];
-      LaunchEvents = {
-        "com.apple.iokit.matching" = {
-          "com.apple.device-attach" = {
-            IOMatchLaunchStream = true;
-            IOProviderClass     = "IOUSBDevice";
-            idVendor            = hexToDec entry.VendorID;
-            idProduct           = hexToDec entry.ProductID;
-          };
-        };
-      };
-    };
-  }));
-
-  system.activationScripts.keyboard.text = ''
-    # Configuring keyboard
-    echo "configuring keyboard..." >&2
-    ${lib.concatStringsSep "\n" commandList}
-  '';
-
-}
diff --git a/machines/blechkasten/darwin-configuration.nix b/machines/blechkasten/darwin-configuration.nix
@@ -9,6 +9,7 @@ in {
   imports = [
     <home-manager/nix-darwin>
     ../../modules/darwin/syncthing.nix
+    ../../modules/darwin/hidutil.nix
     ../../modules/darwin/quirks.nix
 
     ../../helpers/make-nixpkgs.nix

@@ -30,13 +31,42 @@ in {
     ../../configurations/programs/cli/garminexport.nix
     ./yabai.nix
     ./skhd.nix
-    ./keyboard.nix
     ./speakers.nix
   ];
 
   nix.package = pkgs.nix;
   services.nix-daemon.enable = true;
 
+  hidutil = {
+    enable = true;
+    remapKeys = [
+      {
+        # 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; }
+        ];
+      }
+    ];
+  };
+
   home-manager = {
     useUserPackages = true;
 
diff --git a/machines/blechkasten/keyboard.nix b/machines/blechkasten/keyboard.nix
@@ -1,80 +0,0 @@
-{ 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 '{"VendorID":0x${entry.VendorID},"ProductID":0x${entry.ProductID}}' --set '{"UserKeyMapping":${builtins.toJSON entry.UserKeyMapping}}' > /dev/null'');
-
-in {
-
-  launchd.user.agents = builtins.listToAttrs (lib.forEach keyMappings (entry: {
-    name  = "activateUserKeyMapping-${entry.VendorID}:${entry.ProductID}";
-    value.serviceConfig = {
-      Disabled          = if (entry.VendorID != "5ac" && entry.ProductID != "281") then false else true;
-      Label             = "org.nixos.activateUserKeyMapping-${entry.VendorID}:${entry.ProductID}";
-#      StandardErrorPath = "/tmp/activateUserKeyMapping.stderr";
-#      StandardOutPath   = "/tmp/activateUserKeyMapping.stdout";
-      ProgramArguments  = [
-        "${XPCEventStreamHandler}/bin/xpc_set_event_stream_handler"
-        "${pkgs.writeScript "hidutil" ''
-          #!/usr/bin/env bash
-          osascript -e 'display notification "Load UserKeyMapping for ${entry.VendorID}:${entry.ProductID}" with title "hidutil"'
-          hidutil property --matching '{"VendorID":0x${entry.VendorID},"ProductID":0x${entry.ProductID}}' --set '{"UserKeyMapping":${builtins.toJSON entry.UserKeyMapping}}'  > /dev/null
-        ''}"
-      ];
-      LaunchEvents = {
-        "com.apple.iokit.matching" = {
-          "com.apple.device-attach" = {
-            IOMatchLaunchStream = true;
-            IOProviderClass     = "IOUSBDevice";
-            idVendor            = hexToDec entry.VendorID;
-            idProduct           = hexToDec entry.ProductID;
-          };
-        };
-      };
-    };
-  }));
-
-  system.activationScripts.keyboard.text = ''
-    # Configuring keyboard
-    echo "configuring keyboard..." >&2
-    ${lib.concatStringsSep "\n" commandList}
-  '';
-
-}