aboutsummaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2022-07-06 17:16:40 +0530
committerAkshay Nair <phenax5@gmail.com>2022-07-06 17:16:40 +0530
commit47d67f1b085bad0e20567332fd2c707df3d42e20 (patch)
treebb7d5a46f8bc594a0f503e0247ffb48ba0906ca1 /modules
parentc17dad8344607a46df76b01d03873b1d02683c83 (diff)
downloadnixos-config-47d67f1b085bad0e20567332fd2c707df3d42e20.tar.gz
nixos-config-47d67f1b085bad0e20567332fd2c707df3d42e20.zip
feat: adds kmonad
Diffstat (limited to 'modules')
-rw-r--r--modules/keyboard/default.nix20
-rw-r--r--modules/keyboard/kmonad-nixos-module.nix187
-rw-r--r--modules/keyboard/kmonad.k2.kbd30
-rw-r--r--modules/keyboard/kmonad.nix32
-rw-r--r--modules/login.nix10
5 files changed, 277 insertions, 2 deletions
diff --git a/modules/keyboard/default.nix b/modules/keyboard/default.nix
new file mode 100644
index 0000000..92f29b0
--- /dev/null
+++ b/modules/keyboard/default.nix
@@ -0,0 +1,20 @@
+{ pkgs, ... }:
+{
+ imports = [ ./kmonad.nix ];
+
+ services.kmonad = {
+ enable = true;
+ keyboards = {
+ k2 = {
+ device = "/dev/input/by-id/usb-Keychron_Keychron_K2-event-kbd";
+ defcfg = {
+ enable = true;
+ fallthrough = true;
+ allowCommands = false;
+ compose = { key = null; };
+ };
+ config = builtins.readFile ./kmonad.k2.kbd;
+ };
+ };
+ };
+}
diff --git a/modules/keyboard/kmonad-nixos-module.nix b/modules/keyboard/kmonad-nixos-module.nix
new file mode 100644
index 0000000..5f6cd34
--- /dev/null
+++ b/modules/keyboard/kmonad-nixos-module.nix
@@ -0,0 +1,187 @@
+{ config, lib, pkgs, ... }:
+
+let
+ cfg = config.services.kmonad;
+
+ # Per-keyboard options:
+ keyboard = { name, ... }: {
+ options = {
+ name = lib.mkOption {
+ type = lib.types.str;
+ example = "laptop-internal";
+ description = "Keyboard name.";
+ };
+
+ device = lib.mkOption {
+ type = lib.types.path;
+ example = "/dev/input/by-id/some-dev";
+ description = "Path to the keyboard's device file.";
+ };
+
+ extraGroups = lib.mkOption {
+ type = lib.types.listOf lib.types.str;
+ default = [ ];
+ example = [ "openrazer" ];
+ description = ''
+ Extra permission groups to attach to the KMonad instance for
+ this keyboard.
+
+ Since KMonad runs as an unprivileged user, it may sometimes
+ need extra permissions in order to read the keyboard device
+ file. If your keyboard's device file isn't in the input
+ group you'll need to list its group in this option.
+ '';
+ };
+
+ defcfg = {
+ enable = lib.mkEnableOption ''
+ Automatically generate the defcfg block.
+
+ When this is option is set to true the config option for
+ this keyboard should not include a defcfg block.
+ '';
+
+ compose = {
+ key = lib.mkOption {
+ type = lib.types.nullOr lib.types.str;
+ default = "ralt";
+ description = "The (optional) compose key to use.";
+ };
+
+ delay = lib.mkOption {
+ type = lib.types.int;
+ default = 5;
+ description = "The delay (in milliseconds) between compose key sequences.";
+ };
+ };
+
+ fallthrough = lib.mkEnableOption "Reemit unhandled key events.";
+
+ allowCommands = lib.mkEnableOption "Allow keys to run shell commands.";
+ };
+
+ config = lib.mkOption {
+ type = lib.types.lines;
+ description = "Keyboard configuration.";
+ };
+ };
+
+ config = {
+ name = lib.mkDefault name;
+ };
+ };
+
+ # Create a complete KMonad configuration file:
+ mkCfg = keyboard:
+ let defcfg = ''
+ (defcfg
+ input (device-file "${keyboard.device}")
+ output (uinput-sink "kmonad-${keyboard.name}")
+ '' +
+ lib.optionalString (keyboard.defcfg.compose.key != null) ''
+ cmp-seq ${keyboard.defcfg.compose.key}
+ cmp-seq-delay ${toString keyboard.defcfg.compose.delay}
+ '' + ''
+ fallthrough ${lib.boolToString keyboard.defcfg.fallthrough}
+ allow-cmd ${lib.boolToString keyboard.defcfg.allowCommands}
+ )
+ '';
+ in
+ pkgs.writeTextFile {
+ name = "kmonad-${keyboard.name}.cfg";
+ text = lib.optionalString keyboard.defcfg.enable (defcfg + "\n") + keyboard.config;
+ checkPhase = "${cfg.package}/bin/kmonad -d $out";
+ };
+
+ # Build a systemd path config that starts the service below when a
+ # keyboard device appears:
+ mkPath = keyboard: rec {
+ name = "kmonad-${keyboard.name}";
+ value = {
+ description = "KMonad trigger for ${keyboard.device}";
+ wantedBy = [ "default.target" ];
+ pathConfig.Unit = "${name}.service";
+ pathConfig.PathExists = keyboard.device;
+ };
+ };
+
+ # Build a systemd service that starts KMonad:
+ mkService = keyboard:
+ let
+ cmd = [
+ "${cfg.package}/bin/kmonad"
+ # NOTE: Removed because of version mismatch with 0.4.1 also because this is redundant
+ # "--input"
+ # ''device-file "${keyboard.device}"''
+ ] ++ cfg.extraArgs ++ [
+ "${mkCfg keyboard}"
+ ];
+
+ groups = [
+ "input"
+ "uinput"
+ ] ++ keyboard.extraGroups;
+ in
+ {
+ name = "kmonad-${keyboard.name}";
+ value = {
+ description = "KMonad for ${keyboard.device}";
+ script = lib.escapeShellArgs cmd;
+ serviceConfig.Restart = "no";
+ serviceConfig.User = "kmonad";
+ serviceConfig.SupplementaryGroups = groups;
+ serviceConfig.Nice = -20;
+ };
+ };
+in
+{
+ options.services.kmonad = {
+ enable = lib.mkEnableOption "KMonad: An advanced keyboard manager.";
+
+ package = lib.mkOption {
+ type = lib.types.package;
+ default = pkgs.kmonad;
+ example = "pkgs.haskellPackages.kmonad";
+ description = "The KMonad package to use.";
+ };
+
+ keyboards = lib.mkOption {
+ type = lib.types.attrsOf (lib.types.submodule keyboard);
+ default = { };
+ description = "Keyboard configuration.";
+ };
+
+ extraArgs = lib.mkOption {
+ type = lib.types.listOf lib.types.str;
+ default = [ ];
+ example = [ "--log-level" "debug" ];
+ description = "Extra arguments to pass to KMonad.";
+ };
+ };
+
+ config = lib.mkIf cfg.enable {
+ environment.systemPackages = [ cfg.package ];
+
+ users.groups.uinput = { };
+ users.groups.kmonad = { };
+
+ users.users.kmonad = {
+ description = "KMonad system user.";
+ group = "kmonad";
+ isSystemUser = true;
+ };
+
+ services.udev.extraRules = ''
+ # KMonad user access to /dev/uinput
+ KERNEL=="uinput", MODE="0660", GROUP="uinput", OPTIONS+="static_node=uinput"
+ '';
+
+ systemd.paths =
+ builtins.listToAttrs
+ (map mkPath (builtins.attrValues cfg.keyboards));
+
+ systemd.services =
+ builtins.listToAttrs
+ (map mkService (builtins.attrValues cfg.keyboards));
+ };
+}
diff --git a/modules/keyboard/kmonad.k2.kbd b/modules/keyboard/kmonad.k2.kbd
new file mode 100644
index 0000000..e77c26f
--- /dev/null
+++ b/modules/keyboard/kmonad.k2.kbd
@@ -0,0 +1,30 @@
+(defsrc
+ esc
+ grv 1 2 3 4 5 6 7 8 9 0 - = bspc
+ tab q w e r t y u i o p [ ] \
+ caps a s d f g h j k l ; ' ret
+ lsft z x c v b n m , . / rsft
+ lctl lmet lalt spc ralt rmet rctl left down up right
+)
+
+(defalias
+ nv (tap-next f (layer-toggle navigation))
+)
+
+(deflayer default
+ esc
+ grv 1 2 3 4 5 6 7 8 9 0 - = bspc
+ tab q w e r t y u i o p [ ] \
+ esc a s d @nv g h j k l ; ' ret
+ lsft z x c v b n m , . / rsft
+ lctl lmet lalt spc ralt rmet rctl left down up right
+)
+
+(deflayer navigation
+ _
+ grv 1 2 3 4 5 6 7 8 9 0 - = bspc
+ _ _ _ _ _ _ _ _ _ _ _ _ _ _
+ _ _ _ _ _ _ left down up right _ _ _
+ _ _ _ _ _ _ _ _ _ _ _ _
+ _ _ _ _ _ _ _ _ _ _ _
+)
diff --git a/modules/keyboard/kmonad.nix b/modules/keyboard/kmonad.nix
new file mode 100644
index 0000000..61312b1
--- /dev/null
+++ b/modules/keyboard/kmonad.nix
@@ -0,0 +1,32 @@
+{ pkgs, ... }:
+let
+ kmonadVersion = "0.4.1";
+ # kmonadNixRev = "ac3c0db4f7fca0f66980d1b76a64630a66a36c21";
+
+ kmonadBin = pkgs.fetchurl {
+ url = "https://github.com/kmonad/kmonad/releases/download/${kmonadVersion}/kmonad-${kmonadVersion}-linux";
+ sha256 = "sha256-g55Y58wj1t0GhG80PAyb4PknaYGJ5JfaNe9RlnA/eo8=";
+ };
+
+ kmonadPkg = pkgs.runCommand "kmonad" { }
+ ''#!${pkgs.stdenv.shell}
+ mkdir -p $out/bin
+ cp ${kmonadBin} $out/bin/kmonad
+ chmod +x $out/bin/*
+ '';
+
+ # kmonadGit = builtins.fetchTarball {
+ # url = "https://github.com/kmonad/kmonad/archive/${kmonadNixRev}.tar.gz";
+ # sha256 = "sha256:0pjb8971f7z27w7py4n0zm8acjpylj7wz7vx8iz3i7qmj6k45c2y";
+ # };
+in
+{
+ imports = [
+ ./kmonad-nixos-module.nix
+ # "${kmonadGit}/nix/nixos-module.nix"
+ ];
+
+ services.kmonad = {
+ package = kmonadPkg;
+ };
+}
diff --git a/modules/login.nix b/modules/login.nix
index 35997ea..1462871 100644
--- a/modules/login.nix
+++ b/modules/login.nix
@@ -2,12 +2,12 @@
let
sessions = [
[ "tty1" windowManagers.xmonad ]
- #[ "tty2" windowManagers.bspwm ]
+ [ "tty2" windowManagers.bspwm ]
];
windowManagers = {
dwm = looped "dwm";
xmonad = exec "xmonad";
- bspwm = "st; ${exec "bspwm"};";
+ bspwm = exec "bspwm";
};
exec = s: "exec ${s}";
looped = s: ''
@@ -18,6 +18,11 @@ let
in
{
+ users.groups = {
+ uinput = { };
+ storage = { };
+ };
+
# User
users.users.imsohexy = {
isNormalUser = true;
@@ -36,6 +41,7 @@ in
"plugdev"
"adbusers"
"vboxusers"
+ "uinput"
];
shell = pkgs.zsh;
};