How to bring Karabiner Elements to NixOS?

Hi,

Recently, I bought a macbook air M1 for professional reasons. This is a new world for me.

I found out about a software called Karabiner Elements. This is a great software! it has been specially helpful to bring a Emacs UX outside of Emacs, like having Emacs keybindings everywhere.

I have tried similar stuff on Linux before (on NixOS and Ubuntu). But, it was painful.

Apparently, on Linux you can get it to do the same thing with a
combination of xmodmap, setxkbmap and xcape… A pretty different UX overall, since Karabiner is simple, effective, and extensible.

Since Karabiner is an open source project and since NixOS has a great interface to the OS internals, how hard would it be to build something such as Karabiner for NixOS?

Thanks!

I think you wanted to link to GitHub - pqrs-org/Karabiner-Elements: Karabiner-Elements is a powerful utility for keyboard customization on macOS Sierra (10.12) or later. and not GitHub - rahiel/emacs-keys-everywhere: Enable Emacs key bindings on the desktop.?

2 Likes

This is correct. Thanks, I have fixed it.

Probably very hard, if not outright impossible. From what I can tell, Karabiner uses some very nice accessibility features of Mac OS to implement the keybinding settings.

This is ultimately possible because Apple have clearly put a lot of money into paying talented developers to design this with a good API for extensibility. Awesome to see!

The closest you’ll get in our world is GNOME/KDE, but (perhaps surprisingly) those projects seem to support less and less customization.

I think it’s a side effect of them maturing (further), and therefore cleaning up and simplifying the codebases, but that isn’t matched by an equally paced increase in developer hours to also support quite as many niche use cases through clean APIs. So instead we see a lot being standardized and phased out.

Another problem is that, because the ecosystem isn’t either GNOME or KDE, most graphical applications will use the library support of one or the other. That means even if one of them offered perfect emacs keybinding support, you would still not be able to get it for all applications if the other does not. Then there are also a lot of applications that are tied to neither GNOME nor KDE and just do their own thing, and getting all of them to support your keybinding management would be quite a challenge. Mac OS does not have this problem - they can (and do) control what desktops run in their ecosystem, and mandate all applications follow their standard.

Going via X directly isn’t particularly advisable either, I think. You’ll have to be quite creative with the bindings, since X doesn’t have a standard way of telling what a “keybinding” means to an application, so you’ll need to map key sequences to other key sequences. This means that applications’ shortcuts changing will break your config… It’s probably also not very futureproof, wayland is slowly starting to phase out X.

Perhaps in a couple of years you’ll be able to do things like this on at least GNOME or KDE applications. Federico himself has recently started taking a closer look at accessibility in GNOME - given what he’s managed to do with librsvg I would call that exciting: Mena Quintero: Paying technical debt in our accessibility infrastructure [LWN.net]

To get from there to a perfect solution, you’d probably need to get a freedesktop standard, and wait for everybody to adopt it. This can take decades, and may never happen for something this difficult. But you can probably get to 80% of perfect with a lot of work in the next decade or so.

Karabiner itself can’t be directly ported, of course. It uses Mac OS features to do what it does, so it simply cannot be used with either X, GNOME or KDE.

1 Like

kmonad is very nice, and is in nixpkgs as haskellPackages.kmonad

It doesn’t require X (or Wayland). You can optionally use it to control X windows using sxhkd / xdotool / lots of other things, or Wayland windows using swhkd / ydotool etc.

2 Likes

Also, there is this cool library called NKeymaps. It started as something for the keybindings on the Nyxt browser. But I believe it can be extended to provide similar features provided by Kmonad.

1 Like

keyd is close to have a NixOS Module merged

https://github.com/NixOS/nixpkgs/pull/158793

3 Likes

kanata, which already has a nixos module, is a keyboard customization tool similar to kmonad and keyd.

2 Likes