Using KMonad with an unidentifiable Bluetooth keyboard

#tech

Normally, you specify which physical keyboard KMonad should apply its rules to, through an address on Linux /dev/input/by-id/.... Some Bluetooth keyboards are made wrong and don't get a persistent ID. What can you do then?

Here's the Bash script I run after every connect to find it by heuristic. I'm quite proud of it :-)

(In here, the file-path /home/kept/sysconf/kmonad/trust.kbd just points to my kmonad config file, replace as needed)

#!/usr/bin/env bash
# Find the current /dev/input/event for my bluetooth keeb, assuming I only have
# one connected.
MY_KBD="$(udevadm info /dev/input/event* | grep blue | head -1 | sed 's/.*\(event[^\/$]*\).*/\1/g')"
export MY_KBD
NEW_KMONAD_CONFIG="$(envsubst < /home/kept/sysconf/kmonad/trust.kbd)"

echo "$NEW_KMONAD_CONFIG" | sudo kmonad /dev/stdin

That's not the whole story yet. The kmonad config (again /home/kept/sysconf/kmonad/trust.kbd, named after the cheap & bad Trust Bluetooth Keyboard—don't buy it) also needs an edit, so that it contains the magic string $MY_KBD in the device-file declaration:

(device-file "/dev/input/$MY_KBD")
Created (6 months ago)