-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
nixos/keymapper: init module #328921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
spitulax
wants to merge
1
commit into
NixOS:master
Choose a base branch
from
spitulax:keymapper-module
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+352
−0
Open
nixos/keymapper: init module #328921
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,267 @@ | ||
| { | ||
| config, | ||
| pkgs, | ||
| lib, | ||
| ... | ||
| }: | ||
| let | ||
| inherit (lib) | ||
| concatStringsSep | ||
| mapAttrsToList | ||
| removeAttrs | ||
| pipe | ||
| hasPrefix | ||
| removePrefix | ||
| filter | ||
| optionalString | ||
| zipListsWith | ||
| mkOption | ||
| types | ||
| foldAttrs | ||
| mergeAttrs | ||
| mkEnableOption | ||
| literalExpression | ||
| foldl | ||
| optional | ||
| ; | ||
|
|
||
| cfg = config.services.keymapper; | ||
|
|
||
| generateDirectives = "@forward-modifiers " + (concatStringsSep " " cfg.forwardModifiers); | ||
|
|
||
| generateAliases = aliases: concatStringsSep "\n" (mapAttrsToList (n: v: n + " = " + v) aliases); | ||
|
|
||
| generateConfig = | ||
| settings: | ||
| let | ||
| genContextDefinition = | ||
| context: | ||
| let | ||
| contextBody = pipe (removeAttrs context [ "stage" ]) [ | ||
| (mapAttrsToList ( | ||
| n: v: | ||
| if hasPrefix "no" n && v != null then | ||
| ''${removePrefix "no" n}!="${v}"'' | ||
| else if v != null then | ||
| ''${n}="${v}"'' | ||
| else | ||
| null | ||
| )) | ||
| (filter (x: x != null)) | ||
| (concatStringsSep " ") | ||
| ]; | ||
| in | ||
| (optionalString context.stage "[stage]") | ||
| + (optionalString (context.stage && contextBody != "") "\n\n") | ||
| + (optionalString (contextBody != "") "[${contextBody}]"); | ||
|
|
||
| genMappingDefinition = | ||
| mappings: | ||
| optionalString (mappings != [ ]) ( | ||
| concatStringsSep "\n" (map (v: "${v.input} >> ${v.output}") mappings) | ||
| ); | ||
|
|
||
| contextDefinitions = map (x: genContextDefinition (removeAttrs x [ "mappings" ])) settings; | ||
| mappingDefinitions = map (x: genMappingDefinition x.mappings) settings; | ||
| in | ||
| optionalString (settings != [ ]) ( | ||
| concatStringsSep "\n\n" ( | ||
| zipListsWith ( | ||
| a: b: | ||
| if a == "" then | ||
| b | ||
| else if b == "" then | ||
| a | ||
| else | ||
| a + "\n" + b | ||
| ) contextDefinitions mappingDefinitions | ||
| ) | ||
| ); | ||
|
|
||
| mkContextOption = name: example: desc: { | ||
| ${name} = mkOption { | ||
| type = types.nullOr types.str; | ||
| default = null; | ||
| inherit example; | ||
| description = desc + " where the mappings are enabled."; | ||
| }; | ||
| "no${name}" = mkOption { | ||
| type = types.nullOr types.str; | ||
| default = null; | ||
| visible = false; | ||
| }; | ||
| }; | ||
|
|
||
| mappingModule = types.submodule { | ||
| options = { | ||
| input = mkOption { | ||
| type = types.str; | ||
| description = "Input expression of a mapping."; | ||
| }; | ||
| output = mkOption { | ||
| type = types.str; | ||
| description = "Output expression of a mapping."; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| contextModule = types.submodule { | ||
| options = | ||
| (foldAttrs mergeAttrs { } [ | ||
| (mkContextOption "system" "Linux" "The system") | ||
| (mkContextOption "title" "Chromium" "The focused window by title") | ||
| (mkContextOption "class" "qtcreator" "The focused window by class name") | ||
| (mkContextOption "path" "notepad.exe" "Process path") | ||
| (mkContextOption "device" null "Input device an event originates from") | ||
| (mkContextOption "device_id" null "Input device an event originates from") | ||
| (mkContextOption "modifier" "Virtual1 !Virtual2" "State of one or more keys") | ||
| ]) | ||
| // { | ||
| stage = mkEnableOption null // { | ||
| description = '' | ||
| Whether to split the configuration into stages. | ||
| This option is equivalent to adding `[stage]` to the {file}`keymapper.conf`."; | ||
| ''; | ||
| }; | ||
| mappings = mkOption { | ||
| type = types.listOf mappingModule; | ||
| default = [ ]; | ||
| example = literalExpression '' | ||
| [ | ||
| { input = "CapsLock"; output = "Backspace"; } | ||
| { input = "Z"; output = "Y"; } | ||
| { input = "Y"; output = "Z"; } | ||
| { input = "Control{Q}"; output = "Alt{F4}"; } | ||
| ] | ||
| ''; | ||
| description = '' | ||
| Declaration of mappings. The order of the list is reflected in the config file. | ||
| ''; | ||
| }; | ||
| }; | ||
| }; | ||
| in | ||
| { | ||
| options.services.keymapper = { | ||
| enable = lib.mkEnableOption '' | ||
| keymapper, A cross-platform context-aware key remapper. | ||
|
|
||
| The program is split into two parts: | ||
| - {command}`keymapperd` is the service which needs to be given the permissions to grab the keyboard devices and inject keys. | ||
| - {command}`keymapper` should be run as normal user in a graphical environment. It loads the configuration, informs the service about it and the active context and also executes mapped terminal commands. | ||
| This module only enables {command}`keymapperd`. You have to add {command}`keymapper` to the desktop environment's auto-started application. | ||
| ''; | ||
|
|
||
| package = lib.mkPackageOption pkgs "keymapper" { }; | ||
|
|
||
| aliases = mkOption { | ||
| type = types.attrsOf types.str; | ||
| default = { }; | ||
| example = literalExpression '' | ||
| { | ||
| Win = "Meta"; | ||
| Alt = "AltLeft | AltRight"; | ||
| } | ||
| ''; | ||
| description = "Aliases of keys and/or sequences."; | ||
| }; | ||
|
|
||
| contexts = mkOption { | ||
| type = types.listOf contextModule; | ||
| default = [ ]; | ||
| example = literalExpression '' | ||
| [ | ||
| { | ||
| mappings = [ | ||
| { input = "CapsLock"; output = "Backspace"; } | ||
| { input = "Z"; output = "Y"; } | ||
| { input = "Y"; output = "Z"; } | ||
| { input = "Control{Q}"; output = "Alt{F4}"; } | ||
| ]; | ||
| } | ||
| { stage = true; } | ||
| { | ||
| system = "Linux"; | ||
| noclass = "Thunar"; | ||
| mappings = [ | ||
| { input = "Control{H}"; output = "Backspace"; } | ||
| { input = "Control{M}"; output = "Enter"; } | ||
| ]; | ||
| } | ||
| ] | ||
| ''; | ||
| description = '' | ||
| Enable mappings only in specific contexts or if only {option}`mappings` | ||
| is specified then it is enabled unconditionally. | ||
|
|
||
| Prepend "no" to the option's name (not applicable for {option}`mappings` and {option}`stage`) | ||
| to indicate reverse option. | ||
| For example, {option}`system` will activate the mappings in specified | ||
| system but {option}`nosystem` will activate it in all systems except | ||
| the one specified. | ||
|
|
||
| If at least one of the context is specified but {option}`mappings` is | ||
| left unspecified, then the context shares {option}`mappings` of the next context. | ||
| ''; | ||
| }; | ||
|
|
||
| forwardModifiers = mkOption { | ||
| type = types.listOf types.str; | ||
| default = [ | ||
| "Shift" | ||
| "Control" | ||
| "Alt" | ||
| ]; | ||
| description = '' | ||
| Allows to set a list of keys which should never be [held back](https://github.com/houmain/keymapper?tab=readme-ov-file#order-of-mappings). | ||
| ''; | ||
| }; | ||
|
|
||
| extraConfig = mkOption { | ||
| type = types.lines; | ||
| default = ""; | ||
| description = "Extra configuration lines to add."; | ||
| }; | ||
|
|
||
| extraConfigFirst = mkEnableOption null // { | ||
| description = "Whether to put lines in {option}`extraConfig` before {option}`contexts`"; | ||
| }; | ||
| }; | ||
|
|
||
| config = lib.mkIf cfg.enable { | ||
| environment.systemPackages = [ cfg.package ]; | ||
|
|
||
| environment.etc."keymapper.conf".text = | ||
| concatStringsSep "\n\n" ( | ||
| foldl (acc: x: acc ++ (optional (x != "") x)) [ ] ( | ||
| if !cfg.extraConfigFirst then | ||
| [ | ||
| generateDirectives | ||
| (generateAliases cfg.aliases) | ||
| (generateConfig cfg.contexts) | ||
| cfg.extraConfig | ||
| ] | ||
| else | ||
| [ | ||
| generateDirectives | ||
| (generateAliases cfg.aliases) | ||
| cfg.extraConfig | ||
| (generateConfig cfg.contexts) | ||
| ] | ||
| ) | ||
| ) | ||
| + "\n"; | ||
|
|
||
| systemd.services.keymapperd = { | ||
| description = "Keymapper Daemon"; | ||
| wantedBy = [ "multi-user.target" ]; | ||
| serviceConfig = { | ||
| Type = "exec"; | ||
| ExecStart = "${cfg.package}/bin/keymapperd -v"; | ||
| Restart = "always"; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| meta.maintainers = with lib.maintainers; [ spitulax ]; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import ./make-test-python.nix ( | ||
| { lib, pkgs, ... }: | ||
| let | ||
| expectedConfig = pkgs.writeText "keymapper.conf" '' | ||
| @forward-modifiers Shift Control Alt | ||
|
|
||
| Alt = AltLeft | ||
| AltGr = AltRight | ||
| Super = Meta | ||
|
|
||
| ScrollLock >> CapsLock | ||
| CapsLock >> Escape | ||
| Alt{C} >> edit_copy | ||
| Alt{V} >> edit_paste | ||
|
|
||
| [stage] | ||
|
|
||
| [class="kitty" system!="Darwin"] | ||
| edit_copy >> Control{Shift{C}} | ||
| edit_paste >> Control{Shift{V}} | ||
| ''; | ||
| in | ||
| { | ||
| name = "keymapper"; | ||
| meta.maintainers = with lib.maintainers; [ spitulax ]; | ||
|
|
||
| nodes.machine.services.keymapper = { | ||
| enable = true; | ||
| aliases = { | ||
| "Alt" = "AltLeft"; | ||
| "AltGr" = "AltRight"; | ||
| "Super" = "Meta"; | ||
| }; | ||
| contexts = [ | ||
| { | ||
| mappings = [ | ||
| { | ||
| input = "ScrollLock"; | ||
| output = "CapsLock"; | ||
| } | ||
| { | ||
| input = "CapsLock"; | ||
| output = "Escape"; | ||
| } | ||
| { | ||
| input = "Alt{C}"; | ||
| output = "edit_copy"; | ||
| } | ||
| { | ||
| input = "Alt{V}"; | ||
| output = "edit_paste"; | ||
| } | ||
| ]; | ||
| } | ||
| { stage = true; } | ||
| { | ||
| class = "kitty"; | ||
| nosystem = "Darwin"; | ||
| mappings = [ | ||
| { | ||
| input = "edit_copy"; | ||
| output = "Control{Shift{C}}"; | ||
| } | ||
| { | ||
| input = "edit_paste"; | ||
| output = "Control{Shift{V}}"; | ||
| } | ||
| ]; | ||
| } | ||
| ]; | ||
| }; | ||
|
|
||
| testScript = '' | ||
| machine.wait_for_unit("keymapperd.service") | ||
| machine.wait_for_file("/etc/keymapper.conf") | ||
| machine.succeed("keymapper --check") | ||
| machine.copy_from_host("${expectedConfig}", "/tmp/keymapper.conf") | ||
| print(machine.succeed("diff /etc/keymapper.conf /tmp/keymapper.conf")) | ||
| ''; | ||
| } | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@spitulax Auto-quoting here prevents usage of regexes in config, as in: