How to get the service running for keyd?

Hello folks,

I’m looking to use keyd[1] to manage my keymappings as I do under another distribution. It is a daemon I guess that intercepts keypresses and remaps them. I like it because it works in framebuffer consoles, etc., not just in a graphical session.

I have managed to get the desired text contents into exactly this path: /etc/keyd/default.conf :cowboy_hat_face:

However, when I reboot, I notice my config doesn’t seem to work. Capslock is not overridden, grr :japanese_ogre:

It looks like there is no service running for keyd (it doesn’t show in the output of ps -aux, nor in that of systemctl list-units --all).

I have tried throwing services.keyd.enable = true; right under the line for sshd, but no dice. I do get an error, however:

[hb@nixos:~]$ sudo nixos-rebuild switch
[sudo] password for hb: 
error: The option `services.keyd' does not exist. Definition values:
       - In `/etc/nixos/configuration.nix':
           {
             enable = true;
           }
(use '--show-trace' to show detailed location information)
building Nix...
error: The option `services.keyd' does not exist. Definition values:
       - In `/etc/nixos/configuration.nix':
           {
             enable = true;
           }
(use '--show-trace' to show detailed location information)
building the system configuration...
error: The option `services.keyd' does not exist. Definition values:
       - In `/etc/nixos/configuration.nix':
           {
             enable = true;
           }
(use '--show-trace' to show detailed location information)

Any ideas?

(1) nixpkgs/default.nix at def9e420d27c951026d57dc96ce0218c3131f412 · NixOS/nixpkgs · GitHub

You can’t just services.keyd.enable = true, that setting has to be defined by a module. And there’s no keyd module in nixos (22.11 at least), though there is a package. It looks like someone made a PR for a module but it got marked as merged incorrectly: https://github.com/NixOS/nixpkgs/pull/158793

And it looks like someone pulled out just the module here: nixos/keyd.nix at 990149fbddbd53c1bdb0f003c7ffdfcbe40fd070 · elcuervo/nixos · GitHub

I didn’t see those when I wanted to use keyd so I just wrote my own basic module, which you can also use if you like:

{ config, pkgs, lib, ... }:
with lib;
let
  cfg = config.services.keyd;
in
{
  options = {
    services.keyd = {
      enable = mkEnableOption "enable keyd - key remapper daemon";
      config = mkOption {
        type = types.lines;
        default = "";
        description = ''
          Content of config file
        '';
      };
    };
  };

  config = mkIf cfg.enable {
    systemd.services.keyd = {
      description = "key remapping daemon";
      wantedBy = [ "sysinit.target" ];
      requires = [ "local-fs.target" ];
      after = [ "local-fs.target" ];
      environment.KEYD_CONFIG_DIR = pkgs.writeTextDir "keymap.conf" cfg.config;
      serviceConfig.ExecStart = "${pkgs.keyd}/bin/keyd";
    };
  };
}

I use it like this in my machine config:

  services.keyd.enable = true;
  services.keyd.config = ''
    [ids]
    *
    [main]
    # ... rest of config here
  '';

Of course, if you don’t care much about modularity you can just stick the systemd.services.keyd = ... part in your config directly.

1 Like

I use a simple service definition in my configuration.nix:

  systemd.services.keyd = {
    description = "key remapping daemon";
    enable = true;
    serviceConfig = {
      Type = "simple";
      ExecStart = "${pkgs.keyd}/bin/keyd";
    };
    wantedBy = [ "sysinit.target" ];
    requires = [ "local-fs.target" ];
    after = [ "local-fs.target" ];
  };

The configuration files are expected to be in /etc/keyd. This works really well for me.

1 Like

What about kanata? It has similar functionalities with keyd and has a nixos module (I am the author of the module), which means you can use services.kanata to enable it.

Hmm kanata looks good!
I don’t know if it is is fully a “sexp” syntax but it looks very readable and I could see myself enjoying it.

Thanks everyone for the replies. I will try to get my keymap working.