OVERHEAD

How to find the required files for a given XKB keymap

Linux Scripting

A POSIX shell script which creates a file at XKB_OUT listing all the paths under /usr/share/X11/xkb/ that were read by the xkbcli compile-keymap command.

#!/bin/sh

XKBLAYOUT="us"
XKB_OUT="/tmp/output.xkb"
INOTIFY_READY="/tmp/inotify.ready"
print_exit() { printf "ERROR! %b\n" "$1"; exit 1; }

# Can't use `if ! inotifywait` because `--timeout` exits with a non-zero
# error code.
# `--timeout` ensures `inotifywait` exits after inactivity; `1` can be too short.
# `--output` leads to `$XKB_OUT` being absent.
inotifywait \
	--monitor --recursive --event OPEN /usr/share/X11/xkb/ \
	--format "%w%f" --timeout 2 > "$XKB_OUT" \
	2> "$INOTIFY_READY" &

# Wait for STDERR output from the first `inotifywait` command, which
# signals that it's ready to see events generated by `xkbcli`.
if ! inotifywait -qqe MODIFY "$INOTIFY_READY"; then
	print_exit "Failed to watch '$INOTIFY_READY'!"; fi

# ...Except not even THAT is enough, so `sleep` after that too.
sleep 0.05

# Additionally, you can add other arguments like `--variant`, `--model`, etc.
if ! xkbcli compile-keymap --layout "$XKBLAYOUT" > /dev/null; then
	print_exit "Failed to compile null XKB keymap!"; fi

wait

Feel free to implement the cleanup logic for removing XKB_OUT & INOTIFY_READY afterwards as you see fit.

Once you have the list at XKB_OUT, you can parse it line-by-line to do whatever you want with the given paths. In my case, it’s for shrinking a USI as much as possible (and yes, that mkosi blog post is still coming; I haven’t forgotten).