]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
nixos: Use repository key fetching by default on nixos
authorDaanDeMeyer <daan.j.demeyer@gmail.com>
Mon, 29 Dec 2025 18:54:01 +0000 (19:54 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 30 Dec 2025 14:59:18 +0000 (15:59 +0100)
nixos generally won't have any keys in the expected locations so let's
use repository key fetching by default if we're building from a nixos
host (or tools tree).

mkosi/__init__.py
mkosi/config.py
mkosi/distribution/__init__.py
mkosi/installer/pacman.py
mkosi/util.py
tests/test_config.py

index dc612b0ae5155b627545bda54970bf3819d7ca8e..310d0954544624ff94e386dcc9d86c0735e870cc 100644 (file)
@@ -3446,8 +3446,8 @@ def make_image(
         cmdline += ["--definitions", workdir(d)]
         opts += ["--ro-bind", d, workdir(d)]
 
-    def can_orphan_file(distribution: Optional[Distribution], release: Optional[str]) -> bool:
-        if distribution is None:
+    def can_orphan_file(distribution: Union[Distribution, str, None], release: Optional[str]) -> bool:
+        if not isinstance(distribution, Distribution):
             return True
 
         return not (
index 63ab6a86853861c86a2a9bbe203eef33d1df0fe9..09e0e25c1e8a4b093208392f78c6be8adfab18d1 100644 (file)
@@ -1006,12 +1006,12 @@ def config_default_output(namespace: dict[str, Any]) -> str:
 
 
 def config_default_distribution(namespace: dict[str, Any]) -> Distribution:
-    if d := os.getenv("MKOSI_HOST_DISTRIBUTION"):
+    if (d := os.getenv("MKOSI_HOST_DISTRIBUTION")) and d in Distribution.values():
         return Distribution(d)
 
     detected = detect_distribution()[0]
 
-    if not detected:
+    if not isinstance(detected, Distribution):
         logging.info(
             "Distribution of your host can't be detected or isn't a supported target. "
             "Defaulting to Distribution=custom."
@@ -1022,10 +1022,14 @@ def config_default_distribution(namespace: dict[str, Any]) -> Distribution:
 
 
 def config_default_release(namespace: dict[str, Any]) -> str:
-    hd: Optional[Distribution]
+    hd: Union[Distribution, str, None]
     hr: Optional[str]
 
-    if (d := os.getenv("MKOSI_HOST_DISTRIBUTION")) and (r := os.getenv("MKOSI_HOST_RELEASE")):
+    if (
+        (d := os.getenv("MKOSI_HOST_DISTRIBUTION"))
+        and d in Distribution.values()
+        and (r := os.getenv("MKOSI_HOST_RELEASE"))
+    ):
         hd, hr = Distribution(d), r
     else:
         hd, hr = detect_distribution()
@@ -1038,12 +1042,12 @@ def config_default_release(namespace: dict[str, Any]) -> str:
 
 
 def config_default_tools_tree_distribution(namespace: dict[str, Any]) -> Distribution:
-    if d := os.getenv("MKOSI_HOST_DISTRIBUTION"):
+    if (d := os.getenv("MKOSI_HOST_DISTRIBUTION")) and d in Distribution.values():
         return Distribution(d).installer.default_tools_tree_distribution() or Distribution(d)
 
     detected = detect_distribution()[0]
 
-    if not detected:
+    if not isinstance(detected, Distribution):
         return Distribution.custom
 
     return detected.installer.default_tools_tree_distribution() or detected
@@ -1054,10 +1058,11 @@ def config_default_repository_key_fetch(namespace: dict[str, Any]) -> bool:
         return distribution == Distribution.arch or distribution.is_rpm_distribution()
 
     if namespace["tools_tree"] not in (Path("default"), Path("yes")):
-        return (
-            detect_distribution(namespace["tools_tree"] or Path("/"))[0] == Distribution.ubuntu
-            and needs_repository_key_fetch(namespace["distribution"])
-        )  # fmt: skip
+        d = detect_distribution(namespace["tools_tree"] or Path("/"))[0]
+        if d == "nixos":
+            return True
+
+        return d == Distribution.ubuntu and needs_repository_key_fetch(namespace["distribution"])
 
     return namespace["tools_tree_distribution"] == Distribution.ubuntu and needs_repository_key_fetch(
         namespace["distribution"]
index e59025acafaabdf143c89cd7ee188b937c99d753..5d42b8f5b3e07f6b83c8961d18106d12b1ae3d0b 100644 (file)
@@ -5,7 +5,7 @@ import importlib
 import urllib.parse
 from collections.abc import Sequence
 from pathlib import Path
-from typing import TYPE_CHECKING, Optional
+from typing import TYPE_CHECKING, Optional, Union
 
 from mkosi.log import die
 from mkosi.util import StrEnum, read_env_file
@@ -151,7 +151,7 @@ class DistributionInstaller:
         return False
 
 
-def detect_distribution(root: Path = Path("/")) -> tuple[Optional[Distribution], Optional[str]]:
+def detect_distribution(root: Path = Path("/")) -> tuple[Union[Distribution, str, None], Optional[str]]:
     try:
         os_release = read_env_file(root / "etc/os-release")
     except FileNotFoundError:
@@ -160,25 +160,28 @@ def detect_distribution(root: Path = Path("/")) -> tuple[Optional[Distribution],
         except FileNotFoundError:
             return None, None
 
-    dist_id = os_release.get("ID", "linux")
+    dist_id = os_release.get("ID")
     dist_id_like = os_release.get("ID_LIKE", "").split()
     version_id = os_release.get("VERSION_ID", None) if dist_id != "opensuse-tumbleweed" else "tumbleweed"
     version_codename = os_release.get("VERSION_CODENAME", None)
 
     quirks = {
-        "azurelinux": Distribution.azure,
+        "azurelinux": "azure",
     }
 
     d: Optional[Distribution] = None
     for the_id in [dist_id, *dist_id_like]:
-        d = Distribution.__members__.get(the_id, quirks.get(the_id))
-        if d is not None:
+        if not the_id:
+            continue
+
+        if the_id in Distribution.values() or the_id in quirks:
+            d = Distribution(quirks.get(the_id, the_id))
             break
 
     if d and d.is_apt_distribution() and version_codename:
         version_id = version_codename
 
-    return d, version_id
+    return d or dist_id, version_id
 
 
 def join_mirror(mirror: str, link: str) -> str:
index 433951d36f89eeec95853f4dd1fde19a3c78fc29..3898c6f5ea90ad20c3be09ba745288359907d1cc 100644 (file)
@@ -9,7 +9,7 @@ from pathlib import Path
 
 from mkosi.config import Config
 from mkosi.context import Context
-from mkosi.distribution import detect_distribution
+from mkosi.distribution import Distribution, detect_distribution
 from mkosi.installer import PackageManager
 from mkosi.log import complete_step
 from mkosi.run import CompletedProcess, run, workdir
@@ -234,6 +234,7 @@ class Pacman(PackageManager):
 
         if (
             (d := detect_distribution(context.config.tools())[0])
+            and isinstance(d, Distribution)
             and d.is_apt_distribution()
             and (context.sandbox_tree / "usr/share/pacman/keyrings").exists()
         ):
index 4b55a6d0b15f57fc69102780ca47349d3c867b95..19f0b4c7900889a677384deb70953307a1b46e7d 100644 (file)
@@ -185,7 +185,7 @@ class StrEnum(enum.Enum):
 
     @classmethod
     def values(cls) -> list[str]:
-        return list(s.replace("_", "-") for s in map(str, cls.__members__))
+        return [d.value for d in cls.__members__.values()]
 
     @classmethod
     def choices(cls) -> list[str]:
index 12e04618db1f7a95b699fa97783d4c505711a24a..20b5ecd40cd8d6ddd5e01a04c65ecf8a7cda1241 100644 (file)
@@ -1514,7 +1514,7 @@ def test_tools(tmp_path: Path) -> None:
         _, tools, _ = parse_config(argv, resources=resources)
         assert tools
         host = detect_distribution()[0]
-        if host:
+        if isinstance(host, Distribution):
             assert tools.distribution == (
                 host.installer.default_tools_tree_distribution() or tools.distribution
             )