From 991cc8fc88279c640b34fd4f1f64590b37603019 Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Tue, 5 Mar 2024 13:10:08 +0100 Subject: [PATCH] add input remapping module with easier configuration --- hosts/catbook-j/configuration.nix | 17 ++++- modules/desktop/default.nix | 29 +------- modules/desktop/evremap.nix | 39 ----------- modules/desktop/input/default.nix | 15 ++++ modules/desktop/input/evremap.nix | 110 ++++++++++++++++++++++++++++++ modules/desktop/x.nix | 18 +++++ other/remaps-catbook-j.toml | 22 ------ 7 files changed, 161 insertions(+), 89 deletions(-) delete mode 100644 modules/desktop/evremap.nix create mode 100644 modules/desktop/input/default.nix create mode 100644 modules/desktop/input/evremap.nix create mode 100644 modules/desktop/x.nix delete mode 100644 other/remaps-catbook-j.toml diff --git a/hosts/catbook-j/configuration.nix b/hosts/catbook-j/configuration.nix index 9379ded..406f484 100644 --- a/hosts/catbook-j/configuration.nix +++ b/hosts/catbook-j/configuration.nix @@ -26,7 +26,22 @@ social.enable = true; mail.enable = true; gaming.enable = true; - evremap.enable = true; + }; + input.remapping = { + enable = true; + devices."AT Translated Set 2 keyboard" = { + swapKeys = [ + ["KEY_Y" "KEY_Z"] + ["KEY_LEFTALT" "KEY_LEFTMETA"] + ]; + dual_role = [ + { + input = "KEY_CAPSLOCK"; + hold = ["KEY_LEFTCTRL"]; + tap = ["KEY_ESC"]; + } + ]; + }; }; terminal.enable = true; }; diff --git a/modules/desktop/default.nix b/modules/desktop/default.nix index cfc104f..2e519bd 100644 --- a/modules/desktop/default.nix +++ b/modules/desktop/default.nix @@ -30,10 +30,11 @@ in ./social.nix ./mail.nix ./specific-hardware - ./evremap.nix ./xmonad.nix ./fonts.nix ./firefox.nix + ./x.nix + ./input ]; i18n.inputMethod = { @@ -52,32 +53,6 @@ in services = { printing.enable = true; gnome.gnome-keyring.enable = true; - - xserver = { - enable = true; - - xkb = { - layout = "us"; - variant = "altgr-intl"; - }; - - libinput = { - enable = true; - }; - - desktopManager = { - xterm.enable = false; - }; - - displayManager = { - defaultSession = "none+xmonad"; - gdm.enable = true; - }; - - windowManager.xmonad = { - enable = true; - }; - }; }; programs.xss-lock = { diff --git a/modules/desktop/evremap.nix b/modules/desktop/evremap.nix deleted file mode 100644 index cbb72a2..0000000 --- a/modules/desktop/evremap.nix +++ /dev/null @@ -1,39 +0,0 @@ -{ - pkgs, - config, - lib, - ... -}: let - cfg = config.jade.desktop.evremap; - evremap = pkgs.rustPlatform.buildRustPackage { - pname = "evremap"; - version = "0.1.0"; - src = pkgs.fetchFromGitHub { - owner = "wez"; - repo = "evremap"; - rev = "4480c4eda223b98899b0fbd926bc34f7bd0e1a18"; - sha256 = "sha256-BxSrphgW1n465FX6bKVkq6O0XE2JqanfSYlsGwWUWkQ="; - }; - cargoHash = ""; - cargoLock.lockFile = ../../other/evremap.Cargo.lock; - postPatch = '' - cp ${../../other/evremap.Cargo.lock} Cargo.lock - ''; - nativeBuildInputs = [pkgs.pkg-config]; - buildInputs = [pkgs.libevdev]; - }; -in - with lib; { - options.jade.desktop.evremap = { - enable = mkEnableOption "Enable evremap"; - }; - config = mkIf cfg.enable { - systemd.services.evremap = { - script = "${evremap}/bin/evremap remap ${../../other/remaps-${config.networking.hostName}.toml}"; - wantedBy = ["multi-user.target"]; - unitConfig = { - Restart = "on-failure"; - }; - }; - }; - } diff --git a/modules/desktop/input/default.nix b/modules/desktop/input/default.nix new file mode 100644 index 0000000..88302da --- /dev/null +++ b/modules/desktop/input/default.nix @@ -0,0 +1,15 @@ +{...}: { + imports = [ + ./evremap.nix + ]; + services.xserver = { + xkb = { + layout = "us"; + variant = "altgr-intl"; + }; + + libinput = { + enable = true; + }; + }; +} diff --git a/modules/desktop/input/evremap.nix b/modules/desktop/input/evremap.nix new file mode 100644 index 0000000..bc9a1da --- /dev/null +++ b/modules/desktop/input/evremap.nix @@ -0,0 +1,110 @@ +{ + pkgs, + config, + lib, + utils, + ... +}: let + cfg = config.jade.input.remapping; + evremap = pkgs.rustPlatform.buildRustPackage { + pname = "evremap"; + version = "0.1.0"; + src = pkgs.fetchFromGitHub { + owner = "wez"; + repo = "evremap"; + rev = "4480c4eda223b98899b0fbd926bc34f7bd0e1a18"; + sha256 = "sha256-BxSrphgW1n465FX6bKVkq6O0XE2JqanfSYlsGwWUWkQ="; + }; + cargoHash = ""; + cargoLock.lockFile = ../../../other/evremap.Cargo.lock; + postPatch = '' + cp ${../../../other/evremap.Cargo.lock} Cargo.lock + ''; + nativeBuildInputs = [pkgs.pkg-config]; + buildInputs = [pkgs.libevdev]; + }; + toml = pkgs.formats.toml {}; +in + with lib; { + options.jade.input.remapping = { + enable = mkEnableOption "Enable evremap"; + devices = mkOption { + type = types.attrsOf (types.submodule ({name, ...}: { + options = { + device_name = mkOption { + type = types.str; + description = "The device name"; + default = name; + }; + remap = mkOption { + type = types.listOf (types.submodule ({...}: { + options.input = mkOption {type = types.listOf types.str;}; + options.output = mkOption {type = types.listOf types.str;}; + })); + default = []; + }; + swapKeys = mkOption { + description = "Lists with two keys to be swapped on the keyboard layout."; + type = types.listOf ( + # verify that each key swapping list contains two elements + types.addCheck (types.listOf types.str) (v: builtins.length v == 2) + ); + default = []; + }; + dual_role = mkOption { + type = types.listOf (types.submodule ({...}: { + options.input = mkOption {type = types.str;}; + options.hold = mkOption {type = types.listOf types.str;}; + options.tap = mkOption {type = types.listOf types.str;}; + })); + default = []; + }; + }; + })); + }; + }; + config = mkIf cfg.enable ( + with builtins; let + devs = map ({ + device_name, + remap, + swapKeys, + dual_role, + }: { + inherit device_name dual_role; + + # expand swapKeys to normal remaps + remap = concatLists [ + remap + (lib.lists.flatten (map (keys: [ + { + input = [(head keys)]; + output = [(lib.lists.last keys)]; + } + { + input = [(lib.lists.last keys)]; + output = [(head keys)]; + } + ]) + swapKeys)) + ]; + }) (attrValues cfg.devices); + in { + # generate numbered systemd services for each device to be remapped + # https://github.com/wez/evremap/issues/17 + systemd.services = listToAttrs (genList (i: { + name = "evremap${toString i}"; + value = let + cfgFile = toml.generate "remaps-${toString i}.toml" (elemAt devs i); + in { + script = "${evremap}/bin/evremap remap ${cfgFile}"; + wantedBy = ["multi-user.target"]; + unitConfig = { + Restart = "on-failure"; + }; + }; + }) (length devs)); + environment.systemPackages = [evremap]; + } + ); + } diff --git a/modules/desktop/x.nix b/modules/desktop/x.nix new file mode 100644 index 0000000..dab34ec --- /dev/null +++ b/modules/desktop/x.nix @@ -0,0 +1,18 @@ +{...}: { + services.xserver = { + enable = true; + + desktopManager = { + xterm.enable = false; + }; + + displayManager = { + defaultSession = "none+xmonad"; + gdm.enable = true; + }; + + windowManager.xmonad = { + enable = true; + }; + }; +} diff --git a/other/remaps-catbook-j.toml b/other/remaps-catbook-j.toml deleted file mode 100644 index 6a6067f..0000000 --- a/other/remaps-catbook-j.toml +++ /dev/null @@ -1,22 +0,0 @@ -device_name = "AT Translated Set 2 keyboard" - -[[remap]] -input = [ "KEY_Y" ] -output = [ "KEY_Z" ] - -[[remap]] -input = [ "KEY_Z" ] -output = [ "KEY_Y" ] - -[[remap]] -input = [ "KEY_LEFTALT" ] -output = [ "KEY_LEFTMETA" ] - -[[remap]] -input = [ "KEY_LEFTMETA" ] -output = [ "KEY_LEFTALT" ] - -[[dual_role]] -input = "KEY_CAPSLOCK" -hold = ["KEY_LEFTCTRL"] -tap = ["KEY_ESC"]