diff --git a/configs/airootfs/root/.automated_script.sh b/configs/airootfs/root/.automated_script.sh index 87c5184ee..dfc4443d4 100644 --- a/configs/airootfs/root/.automated_script.sh +++ b/configs/airootfs/root/.automated_script.sh @@ -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 diff --git a/configs/airootfs/usr/share/omarchy-iso/orchestrator/keyboard.py b/configs/airootfs/usr/share/omarchy-iso/orchestrator/keyboard.py index 97daa3299..d64b67d85 100644 --- a/configs/airootfs/usr/share/omarchy-iso/orchestrator/keyboard.py +++ b/configs/airootfs/usr/share/omarchy-iso/orchestrator/keyboard.py @@ -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. @@ -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") diff --git a/configs/airootfs/usr/share/omarchy-iso/orchestrator/phases_impl.py b/configs/airootfs/usr/share/omarchy-iso/orchestrator/phases_impl.py index 31a84c527..846aab89c 100644 --- a/configs/airootfs/usr/share/omarchy-iso/orchestrator/phases_impl.py +++ b/configs/airootfs/usr/share/omarchy-iso/orchestrator/phases_impl.py @@ -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: @@ -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) diff --git a/configs/airootfs/usr/share/omarchy-iso/orchestrator/uki_vconsole.py b/configs/airootfs/usr/share/omarchy-iso/orchestrator/uki_vconsole.py index ce8a6e4dc..8b6a8faa2 100644 --- a/configs/airootfs/usr/share/omarchy-iso/orchestrator/uki_vconsole.py +++ b/configs/airootfs/usr/share/omarchy-iso/orchestrator/uki_vconsole.py @@ -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: @@ -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: diff --git a/test/unit/test_keyboard.py b/test/unit/test_keyboard.py index dbaffff55..30a515ac1 100644 --- a/test/unit/test_keyboard.py +++ b/test/unit/test_keyboard.py @@ -6,6 +6,7 @@ import tempfile import unittest from pathlib import Path +from unittest import mock ROOT = Path(__file__).resolve().parents[2] @@ -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(): @@ -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: @@ -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")) @@ -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: diff --git a/test/unit/test_prebuilt_uki_keymap.py b/test/unit/test_prebuilt_uki_keymap.py index f734889e1..4dfe3e54c 100644 --- a/test/unit/test_prebuilt_uki_keymap.py +++ b/test/unit/test_prebuilt_uki_keymap.py @@ -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()) @@ -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 @@ -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): diff --git a/test/unit/test_uki_vconsole.py b/test/unit/test_uki_vconsole.py index 8339489ea..a0055c592 100644 --- a/test/unit/test_uki_vconsole.py +++ b/test/unit/test_uki_vconsole.py @@ -13,6 +13,7 @@ """ import os +import re import shutil import subprocess import sys @@ -30,7 +31,7 @@ types.ModuleType("orchestrator.archinstall_adapter"), ) from orchestrator import phases_impl # noqa: E402 -from orchestrator.uki_vconsole import ROOM, embed_vconsole, placeholder, vconsole_fits # noqa: E402 +from orchestrator.uki_vconsole import NON_LATIN_LAYOUTS, ROOM, embed_vconsole, placeholder, vconsole_fits # noqa: E402 from test_uki_cmdline import with_room as uki_with_cmdline_room # noqa: E402 BUILD = ROOT / "builder/build-root-image.sh" @@ -99,6 +100,63 @@ def test_a_uki_without_the_room_or_with_two_is_left_alone(self): self.assertFalse(embed_vconsole(self.uki, self.vconsole)) self.assertEqual(self.uki.read_bytes(), image) + def test_nonlatin_layouts_are_refused_so_the_prompt_stays_us(self): + before = self.uki.read_bytes() + for text in ( + "KEYMAP=ua\nXKBLAYOUT=ua,us\nXKBMODEL=pc105\n", + "KEYMAP=bg-cp1251\nXKBLAYOUT=bg,us\nXKBVARIANT=phonetic,\n", + "KEYMAP=kyrgyz\nXKBLAYOUT=kg,us\n", + "KEYMAP=ru\nXKBLAYOUT=ru,us\n", + "KEYMAP=sr-cy\nXKBLAYOUT=rs\n", + ): + with self.subTest(text=text): + self.vconsole.write_text(text) + self.assertFalse(vconsole_fits(self.uki, self.vconsole)) + self.assertFalse(embed_vconsole(self.uki, self.vconsole)) + self.assertEqual(self.uki.read_bytes(), before) + + def test_serbian_latin_and_multilayout_latin_settings_are_embedded(self): + cases = ( + ( + "KEYMAP=sr-latin\nXKBLAYOUT=rs\nXKBMODEL=pc105\nXKBVARIANT=latin\n", + ["KEYMAP=sr-latin", "XKBLAYOUT=rs", "XKBMODEL=pc105", "XKBVARIANT=latin"], + ), + ( + "KEYMAP=cz\nXKBLAYOUT=cz,us\nXKBMODEL=pc105\nXKBVARIANT=qwerty,\n" + "XKBOPTIONS=terminate:ctrl_alt_bksp,grp:shifts_toggle,grp_led:scroll\n", + [ + "KEYMAP=cz", + "XKBLAYOUT=cz,us", + "XKBMODEL=pc105", + "XKBVARIANT=qwerty,", + "XKBOPTIONS=terminate:ctrl_alt_bksp,grp:shifts_toggle,grp_led:scroll", + ], + ), + ) + for text, expected in cases: + with self.subTest(expected=expected[0]): + self.uki.write_bytes(BEFORE + placeholder() + AFTER) + self.vconsole.write_text(text) + self.assertTrue(vconsole_fits(self.uki, self.vconsole)) + self.assertTrue(embed_vconsole(self.uki, self.vconsole)) + written = self.uki.read_bytes()[len(BEFORE):len(BEFORE) + ROOM].decode() + settings = [line for line in written.splitlines() if not line.startswith("#")] + self.assertEqual(settings, expected) + + def test_nonlatin_layout_list_matches_omarchy_hooks_conf(self): + candidates = ( + Path(os.environ.get("OMARCHY_PATH") or "/nonexistent") / "etc/mkinitcpio.conf.d/omarchy_hooks.conf", + Path("/omarchy-source/etc/mkinitcpio.conf.d/omarchy_hooks.conf"), + ROOT.parent / "omarchy/etc/mkinitcpio.conf.d/omarchy_hooks.conf", + ) + hooks_conf = next((p for p in candidates if p.is_file()), None) + if hooks_conf is None: + self.skipTest("no Omarchy runtime checkout to compare NON_LATIN_LAYOUTS against") + match = re.search(r'case \$\(\. /etc/vconsole\.conf && echo "\$\{XKBLAYOUT%%,\*\}"\) in\s*\n\s*([^)]+)\)', hooks_conf.read_text()) + self.assertIsNotNone(match, f"could not parse non-Latin layout list from {hooks_conf}") + expected = frozenset(part.strip() for part in match.group(1).split("|")) + self.assertEqual(NON_LATIN_LAYOUTS, expected) + def test_a_second_embed_finds_no_room(self): self.assertTrue(embed_vconsole(self.uki, self.vconsole)) written = self.uki.read_bytes() @@ -159,7 +217,7 @@ def test_mkinitcpio_keeps_the_room_where_the_installer_can_write_it(self): class InstallTest(unittest.TestCase): """finalize_limine_boot on a target that picked uk.""" - def install(self, uki: bytes): + def install(self, uki: bytes, vconsole: str = UK): target = Path(self.enterContext(tempfile.TemporaryDirectory())) for path, text in { "usr/bin/limine-update": "", @@ -168,7 +226,7 @@ def install(self, uki: bytes): "boot/limine.conf": "/+Omarchy\n", "var/lib/omarchy-iso/prebuilt-uki.kernel": "linux-omarchy\n", "var/lib/omarchy-iso/prebuilt-uki.keymap": "\n", - "etc/vconsole.conf": UK, + "etc/vconsole.conf": vconsole, }.items(): (target / path).parent.mkdir(parents=True, exist_ok=True) (target / path).write_text(text) @@ -202,6 +260,16 @@ def test_an_image_without_room_builds_its_uki_on_the_target(self): self.assertTrue([cmd for cmd in ran if cmd.endswith("limine-mkinitcpio")], "no UKI was built for the layout") self.assertEqual(hashed, []) + def test_a_nonlatin_layout_keeps_the_prebuilt_uki_on_us(self): + ran, hashed = self.install( + uki_with_cmdline_room() + placeholder(), + vconsole="KEYMAP=ua\nXKBLAYOUT=ua,us\nXKBMODEL=pc105\n", + ) + self.assertFalse([cmd for cmd in ran if "limine-mkinitcpio" in cmd], "a UKI was built for a non-Latin layout") + self.assertEqual(len(hashed), 1) + self.assertIn(placeholder(), hashed[0], "the placeholder was overwritten with a non-Latin layout") + self.assertNotIn(b"XKBLAYOUT=ua", hashed[0]) + if __name__ == "__main__": unittest.main()