{
  lib,
  config,
  pkgs,
  system,
  ...
}: let
  inherit (lib.snowfall.system) is-darwin;

  cfg = config.marleyos.services.kanata;
  isGenericLinux = config.targets.genericLinux.enable;
  isNotNixOS = isGenericLinux || (is-darwin system);
in {
  options.marleyos.services.kanata.enable = lib.mkEnableOption "kanata";

  # Kanata is available as a module for nixOS and that should be preferred when
  # possible.
  config = lib.mkIf (cfg.enable && isNotNixOS) {
    home.packages = with pkgs; [
      kanata
    ];

    systemd.user.services.kanata = {
      Unit = {
        Description = "Kanata keyboard remapper";
        Documentation = "https://github.com/jtroo/kanata";
      };

      Service = {
        Environment = [
          "PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/bin"
          "DISPLAY=:0"
        ];
        Type = "simple";
        ExecStart = "/bin/sh -c 'exec ${lib.getExe pkgs.kanata} --cfg ${./config.kbd}'";
        Restart = "no";
      };

      Install.WantedBy = ["default.target"];
    };
  };
}
