Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions configs/airootfs/root/.automated_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ else
# Japanese or Korean at all. The launcher is the runtime's own, the one
# first-boot owner setup is drawn with.
# shellcheck disable=SC1091
source /usr/share/omarchy-iso/setup-form.sh
# shellcheck disable=SC1091
source /usr/share/omarchy-iso/kmscon.sh
# shellcheck disable=SC1091
source /usr/share/omarchy-iso/kmscon-session.sh
Expand Down
43 changes: 39 additions & 4 deletions configs/airootfs/usr/share/omarchy-iso/orchestrator/keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,48 @@

archinstall's set_keyboard_language boots the installed system in a
systemd-nspawn container just to run localectl. systemd-firstboot --root
produces the part of that output Omarchy actually consumes — KEYMAP for the
console plus the XKB* settings in vconsole.conf that omarchy's
detect-keyboard-layout.sh copies into Hyprland's kb_layout — without booting
anything. The Xorg 00-keyboard.conf that localectl also writes is not
produces the part of that output Omarchy actually consumes without booting
anything. That part is KEYMAP for the console and the XKB* settings in
vconsole.conf, which Omarchy's default/hypr/keyboard.lua reads for Hyprland's
kb_layout. The Xorg 00-keyboard.conf that localectl also writes is not
generated: nothing on an Omarchy system reads it.
"""

from __future__ import annotations

import os
from pathlib import Path

from .command import capture


def setup_form_path() -> Path | None:
"""Find the shared setup form, as the configurator does."""
omarchy_path = os.environ.get("OMARCHY_PATH") or "/nonexistent"
candidates = (
os.environ.get("SETUP_FORM", ""),
"/usr/share/omarchy-iso/setup-form.sh",
str(Path(omarchy_path) / "install/provisioning/setup-form.sh"),
)
return next((Path(path) for path in candidates if path and Path(path).is_file()), None)


def fallback_xkb_settings(keymap: str) -> str:
"""Return the XKB lines that the Omarchy setup form gives a keymap.

systemd-firstboot writes XKBLAYOUT only for keymaps in kbd-model-map.
The setup form holds the settings for the keymaps that it lacks, and
first-boot setup adds the same lines. Returns an empty string for a keymap
that the form has no settings for, and when the form predates
omarchy_keymap_xkb_settings.
"""
form = setup_form_path()
if form is None:
return ""
result = capture(["bash", "-c", 'source "$1" && omarchy_keymap_xkb_settings "$2"', "bash", str(form), keymap])
return result.stdout if result.returncode == 0 else ""


def configure_keyboard(target: Path, language: str, input_method: str | None = None, xkb_layout: str = "") -> bool:
"""Write the console keymap into a mounted target without booting it.

Expand Down Expand Up @@ -61,6 +89,13 @@ def configure_keyboard(target: Path, language: str, input_method: str | None = N
if font:
vconsole_path.write_text(vconsole_path.read_text() + font + "\n")

# Hyprland reads XKBLAYOUT from this file. Without it the desktop types US
# while the console and the disk password prompt use the chosen layout.
if not any(line.startswith("XKBLAYOUT=") and len(line) > len("XKBLAYOUT=") for line in vconsole_path.read_text().splitlines()):
extra = fallback_xkb_settings(language)
if extra:
vconsole_path.write_text(vconsole_path.read_text() + extra)

preference = target / "etc/omarchy/input-method"
preference.parent.mkdir(parents=True, exist_ok=True)
preference.write_text(f"INPUT_METHOD={input_method}\nXKB_LAYOUT={xkb_layout}\n")
Expand Down
35 changes: 27 additions & 8 deletions configs/airootfs/usr/share/omarchy-iso/orchestrator/phases_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3151,8 +3151,19 @@ def _prebuilt_uki_keymap_stale(target: Path) -> str:
"""
recorded = target / "var" / "lib" / "omarchy-iso" / "prebuilt-uki.keymap"
baked = (recorded.read_text().strip() if recorded.is_file() else "") or "us"
chosen = _keymap_of(target / "etc" / "vconsole.conf")
return chosen if chosen and chosen != baked else ""
vconsole = target / "etc" / "vconsole.conf"
chosen = _keymap_of(vconsole)
if not chosen:
return ""
# omarchy_hooks.conf and omarchy_vconsole.conf keep vconsole.conf out of the
# initramfs when the leading XKB layout is non-Latin, so the LUKS prompt
# stays on US. A pre-built UKI baked with us already matches that; embedding
# vconsole.conf into it would put Plymouth into Cyrillic/Greek/Hebrew until
# the first kernel update strips it.
from .uki_vconsole import vconsole_is_nonlatin
if vconsole_is_nonlatin(vconsole):
return "" if baked == "us" else chosen
return chosen if chosen != baked else ""


def _is_apple_t2_hardware() -> bool:
Expand Down Expand Up @@ -3327,12 +3338,20 @@ def finalize_limine_boot(ctx: InstallContext) -> None:
stale_keymap = _prebuilt_uki_keymap_stale(ctx.target)
vconsole = ctx.target / "etc" / "vconsole.conf"
keymap_fits = False
if prebuilt_uki.is_file() and stale_keymap:
from .uki_vconsole import vconsole_fits
keymap_fits = vconsole_fits(prebuilt_uki, vconsole)
if not keymap_fits:
info(f"› keyboard layout {stale_keymap} differs from the pre-built UKI's, which has no room for it; "
"building the UKI on this machine so the LUKS prompt uses it")
if prebuilt_uki.is_file():
from .uki_vconsole import vconsole_fits, vconsole_is_nonlatin
if stale_keymap:
keymap_fits = vconsole_fits(prebuilt_uki, vconsole)
if not keymap_fits:
reason = (
"uses a non-Latin layout that cannot be embedded in place"
if vconsole_is_nonlatin(vconsole)
else "differs from the pre-built UKI's, which has no room for it"
)
info(f"› keyboard layout {stale_keymap} {reason}; "
"building the UKI on this machine so the LUKS prompt uses it")
elif vconsole_is_nonlatin(vconsole):
info("› non-Latin keyboard layout; keeping pre-built UKI on US for the LUKS prompt")
# And for an encrypted deferred install: its first boot unlocks with the
# provisioning key, which only an initramfs built here carries.
provisioning_key = _initramfs_carries_provisioning_key(ctx.target)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,38 @@
# What the initramfs uses: the console keymap and Plymouth's XKB layout. A
# FONT= names a file only an initramfs built for it carries.
SETTINGS = ("KEYMAP", "XKB")
# Layouts that cannot type Latin letters. Keep in sync with
# etc/mkinitcpio.conf.d/omarchy_hooks.conf and omarchy_vconsole.conf in omarchy.
NON_LATIN_LAYOUTS = frozenset(
"af am ara bd bg by et ge gr il in iq ir kg kh kz la lk mk mm mn mv np rs ru sy th tj ua".split()
)


def vconsole_is_nonlatin(vconsole: Path) -> bool:
"""Whether vconsole.conf's leading XKBLAYOUT cannot type Latin letters.

Matches omarchy_hooks.conf and omarchy_vconsole.conf, which leave
/etc/vconsole.conf out of the initramfs for these layouts so the LUKS
prompt uses US, while preserving Latin variants such as rs(latin)
(Serbian Latin, sr-latin).
"""
try:
lines = vconsole.read_text().splitlines()
except OSError:
return False
layout = ""
variant = ""
for line in lines:
line = line.strip()
if line.startswith("XKBLAYOUT="):
value = line.partition("=")[2].split("#", 1)[0].strip().strip("\"'")
layout = value.split(",", 1)[0]
elif line.startswith("XKBVARIANT="):
value = line.partition("=")[2].split("#", 1)[0].strip().strip("\"'")
variant = value.split(",", 1)[0]
if layout == "rs" and variant.startswith("latin"):
return False
return layout in NON_LATIN_LAYOUTS


def placeholder() -> bytes:
Expand All @@ -41,7 +73,10 @@ def placeholder() -> bytes:

def _settings(vconsole: Path) -> bytes | None:
"""The keyboard lines of a vconsole.conf, padded with a comment to ROOM
bytes. None when there are none, or they do not fit."""
bytes. None when there are none, the leading XKB layout is non-Latin, or
they do not fit."""
if vconsole_is_nonlatin(vconsole):
return None
try:
lines = [line.strip() for line in vconsole.read_text().splitlines()]
except OSError:
Expand Down
68 changes: 66 additions & 2 deletions test/unit/test_keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import tempfile
import unittest
from pathlib import Path
from unittest import mock


ROOT = Path(__file__).resolve().parents[2]
Expand All @@ -25,6 +26,10 @@
)


def setup_form() -> Path | None:
return next((candidate for candidate in SETUP_FORM_CANDIDATES if candidate.is_file()), None)


def supported_keymaps():
for candidate in SETUP_FORM_CANDIDATES:
if not candidate.is_file():
Expand Down Expand Up @@ -58,6 +63,45 @@ def localectl_lists_keymaps() -> bool:
LOCALECTL_AVAILABLE = localectl_lists_keymaps()


class FallbackXkbSettingsTest(unittest.TestCase):
def test_reads_extra_xkb_lines_from_setup_form(self):
with tempfile.TemporaryDirectory() as directory:
form = Path(directory) / "setup-form.sh"
form.write_text(
"omarchy_keymap_xkb_settings() {\n"
" case $1 in\n"
" no-latin1) printf 'XKBLAYOUT=no\\nXKBMODEL=pc105\\n' ;;\n"
" *) return 1 ;;\n"
" esac\n"
"}\n"
)
with mock.patch.dict(os.environ, {"SETUP_FORM": str(form)}):
self.assertEqual(KEYBOARD.fallback_xkb_settings("no-latin1"), "XKBLAYOUT=no\nXKBMODEL=pc105\n")
self.assertEqual(KEYBOARD.fallback_xkb_settings("us"), "")

def test_older_setup_form_without_helper_returns_empty(self):
with tempfile.TemporaryDirectory() as directory:
form = Path(directory) / "setup-form.sh"
form.write_text("OMARCHY_KEYBOARD_LAYOUTS=$'English (US)|us'\n")
with mock.patch.dict(os.environ, {"SETUP_FORM": str(form)}):
self.assertEqual(KEYBOARD.fallback_xkb_settings("no-latin1"), "")

def test_empty_omarchy_path_does_not_resolve_relative_path(self):
with tempfile.TemporaryDirectory() as directory:
rel = Path(directory) / "install/provisioning/setup-form.sh"
rel.parent.mkdir(parents=True)
rel.write_text("omarchy_keymap_xkb_settings() { echo XKBLAYOUT=no; }\n")
cwd = os.getcwd()
try:
os.chdir(directory)
real_is_file = Path.is_file
with mock.patch.dict(os.environ, {"SETUP_FORM": "", "OMARCHY_PATH": ""}), \
mock.patch.object(Path, "is_file", new=lambda p: False if str(p) == "/usr/share/omarchy-iso/setup-form.sh" else real_is_file(p)):
self.assertIsNone(KEYBOARD.setup_form_path())
finally:
os.chdir(cwd)


@unittest.skipUnless(LOCALECTL_AVAILABLE, "localectl cannot list keymaps on this host")
class KeyboardConfigurationTest(unittest.TestCase):
def target(self, directory: str) -> Path:
Expand All @@ -69,8 +113,8 @@ def target(self, directory: str) -> Path:
return target

def test_writes_keymap_and_xkb_settings_and_preserves_font(self):
# XKBLAYOUT is load-bearing: omarchy's detect-keyboard-layout.sh copies
# it into Hyprland's kb_layout on the installed system.
# XKBLAYOUT is load-bearing: Omarchy's default/hypr/keyboard.lua reads
# it for Hyprland's kb_layout on the installed system.
with tempfile.TemporaryDirectory() as directory:
target = self.target(directory)
self.assertTrue(KEYBOARD.configure_keyboard(target, "us"))
Expand All @@ -90,6 +134,26 @@ def test_all_configurator_keymaps_are_known_to_localectl(self):
self.assertIn(f"KEYMAP={keymap}", lines)
self.assertEqual(lines.count("FONT=default8x16"), 1)

def test_every_configurator_keymap_ends_with_an_xkb_layout(self):
# systemd-firstboot writes XKBLAYOUT only for keymaps in kbd-model-map.
# Without it Hyprland falls back to a US layout on the installed system.
# The XKB lines come from the same form as the layout list.
form = setup_form()
if SUPPORTED_KEYMAPS is None or form is None:
self.skipTest("no Omarchy runtime checkout to read the layout list from")
if "omarchy_keymap_xkb_settings" not in form.read_text():
self.skipTest("the Omarchy runtime predates omarchy_keymap_xkb_settings")
for keymap in SUPPORTED_KEYMAPS:
with self.subTest(keymap=keymap), tempfile.TemporaryDirectory() as directory, \
mock.patch.dict(os.environ, {"SETUP_FORM": str(form)}):
target = self.target(directory)
self.assertTrue(KEYBOARD.configure_keyboard(target, keymap))
lines = (target / "etc/vconsole.conf").read_text().splitlines()
self.assertTrue(
any(line.startswith("XKBLAYOUT=") and len(line) > 10 for line in lines),
f"no XKBLAYOUT for {keymap}: {lines}",
)

def test_unknown_and_empty_keymaps_match_archinstall_behavior(self):
for keymap, expected in (("definitely-not-a-keymap", False), ("", True)):
with self.subTest(keymap=keymap), tempfile.TemporaryDirectory() as directory:
Expand Down
16 changes: 14 additions & 2 deletions test/unit/test_prebuilt_uki_keymap.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@


class PrebuiltUkiKeymapTest(unittest.TestCase):
def target(self, *, recorded=None, chosen=None) -> Path:
def target(self, *, recorded=None, chosen=None, xkblayout="gb", xkbvariant="") -> Path:
"""A stand-in install target. recorded=None omits the file entirely
(an image built before it existed); chosen=None omits vconsole.conf."""
root = Path(tempfile.mkdtemp())
Expand All @@ -51,8 +51,9 @@ def target(self, *, recorded=None, chosen=None) -> Path:
if recorded is not None:
(root / "var/lib/omarchy-iso/prebuilt-uki.keymap").write_text(recorded)
if chosen is not None:
variant = f"XKBVARIANT={xkbvariant}\n" if xkbvariant else ""
(root / "etc/vconsole.conf").write_text(
f"KEYMAP={chosen}\nXKBLAYOUT=gb\nFONT=default8x16\n"
f"KEYMAP={chosen}\nXKBLAYOUT={xkblayout}\n{variant}FONT=default8x16\n"
)
return root

Expand All @@ -73,6 +74,17 @@ def test_matching_keymap_uses_the_prebuilt_uki(self):
def test_no_keyboard_choice_uses_the_prebuilt_uki(self):
self.assertEqual(self.stale(recorded="", chosen=None), "")

def test_nonlatin_layout_matches_a_us_prebuilt_uki(self):
"""omarchy_hooks.conf keeps vconsole.conf out of the initramfs for
non-Latin layouts, so a us-baked UKI already matches."""
for keymap, xkb in (("ua", "ua,us"), ("bg-cp1251", "bg,us"), ("kyrgyz", "kg,us"), ("ru", "ru,us")):
with self.subTest(keymap=keymap):
self.assertEqual(self.stale(recorded="\n", chosen=keymap, xkblayout=xkb), "")
self.assertEqual(self.stale(recorded="de-latin1\n", chosen=keymap, xkblayout=xkb), keymap)

def test_serbian_latin_is_still_embedded_against_a_us_prebuilt_uki(self):
self.assertEqual(self.stale(recorded="\n", chosen="sr-latin", xkblayout="rs", xkbvariant="latin"), "sr-latin")

# --- the lockout must be caught -----------------------------------------

def test_non_us_layout_against_an_empty_record_rebuilds(self):
Expand Down
Loading