ctucx.git: nixfiles

ctucx' nixfiles

commit b3e908120edd7ac1a2294be70c7f7d72626dc955
parent f16fadb6c62cf0b5911f61e3e7f28f8b3810e29e
Author: Leah (ctucx) <git@ctu.cx>
Date: Sun, 18 Sep 2022 19:26:57 +0200

modules/darwin/hidutil: add support for location-ids
1 file changed, 46 insertions(+), 29 deletions(-)
M
modules/darwin/hidutil.nix
|
75
++++++++++++++++++++++++++++++++++++++++++++++-----------------------------
diff --git a/modules/darwin/hidutil.nix b/modules/darwin/hidutil.nix
@@ -20,11 +20,18 @@ in {
           options = {
 
             VendorID = mkOption {
-              type = types.str;
+              type    = types.nullOr types.str;
+              default = null;
             };
 
             ProductID = mkOption {
-              type = types.str;
+              type    = types.nullOr types.str;
+              default = null;
+            };
+
+            LocationID = mkOption {
+              type    = types.nullOr types.str;
+              default = null;
             };
 
             UserKeyMapping = mkOption {

@@ -51,38 +58,48 @@ in {
   ###### implementation
   config = mkIf cfg.enable {
 
-    launchd.user.agents = builtins.listToAttrs (lib.forEach cfg.remapKeys (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}";
-
-        ProgramArguments  = [
-          "${pkgs.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;
+    launchd.user.agents = builtins.listToAttrs (
+      lib.remove "skip" (
+        lib.forEach cfg.remapKeys (
+          entry: (if (entry.LocationID != null) then "skip" else {
+            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}";
+
+              ProgramArguments  = [
+                "${pkgs.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" (lib.forEach cfg.remapKeys (entry: "hidutil property --matching '{\"VendorID\":0x${entry.VendorID},\"ProductID\":0x${entry.ProductID}}' --set '{\"UserKeyMapping\":${builtins.toJSON entry.UserKeyMapping}}' > /dev/null"))}
+      ${lib.concatStringsSep "\n" (lib.forEach cfg.remapKeys (entry: (if entry.LocationID != null then
+        "hidutil property --matching '{\"LocationID\":0x${entry.LocationID}}' --set '{\"UserKeyMapping\":${builtins.toJSON entry.UserKeyMapping}}' > /dev/null"
+      else
+        "hidutil property --matching '{\"VendorID\":0x${entry.VendorID},\"ProductID\":0x${entry.ProductID}}' --set '{\"UserKeyMapping\":${builtins.toJSON entry.UserKeyMapping}}' > /dev/null"
+      )))}
     '';
 
   };