From ccd910a744b1640b25a36fa71d5cfedcc1832001 Mon Sep 17 00:00:00 2001 From: TechDufus Date: Mon, 30 Nov 2026 11:36:40 -0600 Subject: [PATCH] fix(system): use the US keymap in VMs A VM is typed on through its hypervisor console. QEMU with -k en-us sends each character as its US QWERTY key, so a Dvorak guest remaps every key a second time and the disk-unlock prompt rejects the password. gliff and SSH carry the client's own layout. bootstrap now sets us/us with caps:none when systemd-detect-virt reports a VM and keeps Dvorak elsewhere. The unlock prompt follows at the next boot-image rebuild. --- .github/workflows/mise.yml | 3 ++ README.md | 2 + docs/fork.md | 2 +- modules/system/bootstrap.sh | 17 ++++++-- modules/system/mise.linux.toml | 6 +++ modules/system/tests/test_keymap.sh | 66 +++++++++++++++++++++++++++++ 6 files changed, 91 insertions(+), 5 deletions(-) create mode 100755 modules/system/tests/test_keymap.sh diff --git a/.github/workflows/mise.yml b/.github/workflows/mise.yml index 279288c0..40445eda 100644 --- a/.github/workflows/mise.yml +++ b/.github/workflows/mise.yml @@ -144,6 +144,9 @@ jobs: - name: Test isolated sudoers policy as the runner account run: bash modules/system/tests/test_sudoers.sh + - name: Test keymap choice as the runner account + run: bash modules/system/tests/test_keymap.sh + macos: runs-on: macos-latest steps: diff --git a/README.md b/README.md index db33ecfc..8d5c78de 100644 --- a/README.md +++ b/README.md @@ -250,6 +250,8 @@ It requires a non-root Linux account, installed `visudo`, bubblewrap, permitted The workflow defines a dedicated `system-sudoers` Ubuntu job that installs packaged sudo/bubblewrap, loads an AppArmor profile that grants user namespaces to `/usr/bin/bwrap` only (Ubuntu 24.04 otherwise blocks them for unprivileged programs; the host-wide restriction stays on), and runs the same test script directly as the non-root runner account, outside the wildcard suites. This route covers isolated sudoers safety, not full Omarchy provisioning or desktop runtime; consult the workflow results for execution evidence. +The same job runs `check:system-keymap`, which checks the Dvorak and virtual-machine US keymap choice against a stubbed `localectl`. It needs only a non-root Linux account. + ## Documentation diff --git a/docs/fork.md b/docs/fork.md index 392b6760..4bc38f49 100644 --- a/docs/fork.md +++ b/docs/fork.md @@ -47,7 +47,7 @@ The other 1Password references: | Setting | Where | What it does | | --- | --- | --- | -| Keyboard and locale (Linux) | `modules/system/bootstrap.sh` | Sets the console keymap to `dvorak`, the X11 layout to `us` / `dvorak` with `caps:none`, and the locale to `en_US.UTF-8` | +| Keyboard and locale (Linux) | `modules/system/bootstrap.sh` | Sets the console keymap to `dvorak`, the X11 layout to `us` / `dvorak` with `caps:none`, and the locale to `en_US.UTF-8`. In a virtual machine both are plain `us`, because a hypervisor console sends US key positions | | Passwordless sudo (Linux) | `modules/system/bootstrap.sh` | Installs `/etc/sudoers.d/techdufus`, granting the account that runs bootstrap `NOPASSWD: ALL` | | Login shell | `[bootstrap.user]` in `modules/zsh/mise.linux.toml` and `mise.macos.toml` | Changes it to zsh | | CapsLock | `modules/omarchy/hypr/keyboard.lua` and `summon.lua`; `modules/hammerspoon/karabiner/karabiner.json` | CapsLock stops being CapsLock and becomes the app leader | diff --git a/modules/system/bootstrap.sh b/modules/system/bootstrap.sh index 06af8450..e83cb5dd 100755 --- a/modules/system/bootstrap.sh +++ b/modules/system/bootstrap.sh @@ -45,13 +45,22 @@ fi field() { printf '%s\n' "$status" | sed -n "s/^[[:space:]]*$1:[[:space:]]*//p" | head -n 1; } -if [ "$(field 'VC Keymap')" != "dvorak" ]; then - sudo localectl set-keymap dvorak +# A virtual machine is typed on through its hypervisor console, which sends each +# character as its US QWERTY key (QEMU -k en-us), so the guest must read US QWERTY +# or every key is remapped twice. gliff and SSH carry the client's own layout. +keymap=dvorak variant=dvorak +if systemd-detect-virt --quiet --vm; then + keymap=us variant= fi +if [ "$(field 'VC Keymap')" != "$keymap" ]; then + sudo localectl set-keymap "$keymap" +fi + +# localectl prints no X11 Variant row when the variant is unset. if [ "$(field 'X11 Layout')" != "us" ] || [ "$(field 'X11 Model')" != "pc105" ] || - [ "$(field 'X11 Variant')" != "dvorak" ] || [ "$(field 'X11 Options')" != "caps:none" ]; then - sudo localectl set-x11-keymap us pc105 dvorak caps:none + [ "$(field 'X11 Variant')" != "$variant" ] || [ "$(field 'X11 Options')" != "caps:none" ]; then + sudo localectl set-x11-keymap us pc105 "$variant" caps:none fi if [ "$(field 'System Locale')" != "LANG=en_US.UTF-8" ]; then diff --git a/modules/system/mise.linux.toml b/modules/system/mise.linux.toml index 242daf0e..ce54fcaa 100644 --- a/modules/system/mise.linux.toml +++ b/modules/system/mise.linux.toml @@ -19,3 +19,9 @@ run = "./bootstrap.sh" [tasks."check:system-sudoers"] description = "Test sudoers with real visudo in an isolated Linux namespace" run = "bash ./tests/test_sudoers.sh" + +# Requires a non-root Linux account: bootstrap refuses root, which the Arch CI +# container runs as, so this stays out of the `test:*` wildcard too. +[tasks."check:system-keymap"] +description = "Test the Dvorak and virtual-machine US keymap choice against a stubbed localectl" +run = "bash ./tests/test_keymap.sh" diff --git a/modules/system/tests/test_keymap.sh b/modules/system/tests/test_keymap.sh new file mode 100755 index 00000000..b476733d --- /dev/null +++ b/modules/system/tests/test_keymap.sh @@ -0,0 +1,66 @@ +#!/usr/bin/env bash +# Run the shipped bootstrap with stubbed sudo, localectl and systemd-detect-virt and check +# the layout it converges to on bare metal and in a virtual machine. Requires a non-root +# account, because bootstrap refuses root; nothing privileged or live is called. +set -euo pipefail + +MODULE=$(cd "$(dirname "$0")/.." && pwd) +T=$(mktemp -d) +trap 'rm -rf "$T"' EXIT +fail() { echo "FAIL: $*" >&2; exit 1; } +[ "$(/usr/bin/id -u)" -ne 0 ] || fail "run as a non-root account" +mkdir "$T/bin" + +# sudo runs nothing: the sudoers step has its own test and udev must not be touched. It +# logs each localectl call with every argument bracketed, so an empty variant stays visible. +cat >"$T/bin/sudo" <<'EOF' +#!/usr/bin/env bash +[ "$1" = localectl ] || exit 0 +shift +printf '[%s]' "$@" >>"$STUB_LOG" +echo >>"$STUB_LOG" +EOF +cat >"$T/bin/localectl" <<'EOF' +#!/usr/bin/env bash +cat "$STUB_STATUS" +EOF +cat >"$T/bin/systemd-detect-virt" <<'EOF' +#!/usr/bin/env bash +[ "$STUB_VM" = 1 ] +EOF +chmod +x "$T/bin"/* + +# `localectl status` as systemd prints it: an unset X11 variant has no row. +cat >"$T/dvorak" <<'EOF' +System Locale: LANG=en_US.UTF-8 + VC Keymap: dvorak + X11 Layout: us + X11 Model: pc105 + X11 Variant: dvorak + X11 Options: caps:none +EOF +cat >"$T/us" <<'EOF' +System Locale: LANG=en_US.UTF-8 + VC Keymap: us + X11 Layout: us + X11 Model: pc105 + X11 Options: caps:none +EOF + +# $1 = 1 in a virtual machine, 0 on bare metal; $2 = the layout the machine has now; +# $3 = the localectl calls bootstrap must make, one per line. +check() { + : >"$T/log" + PATH="$T/bin:$PATH" STUB_VM="$1" STUB_STATUS="$T/$2" STUB_LOG="$T/log" \ + bash "$MODULE/bootstrap.sh" >"$T/out" 2>&1 || { cat "$T/out" >&2; fail "vm=$1 from $2: bootstrap failed"; } + [ "$(cat "$T/log")" = "$3" ] || fail "vm=$1 from $2: localectl calls were: $(cat "$T/log")" +} + +check 1 dvorak '[set-keymap][us] +[set-x11-keymap][us][pc105][][caps:none]' +check 1 us '' +check 0 us '[set-keymap][dvorak] +[set-x11-keymap][us][pc105][dvorak][caps:none]' +check 0 dvorak '' + +echo "system keymap tests passed"