]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Parse profiles in subimages as well
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 30 Dec 2024 16:10:53 +0000 (17:10 +0100)
committerJörg Behrmann <behrmann@physik.fu-berlin.de>
Mon, 30 Dec 2024 20:09:22 +0000 (21:09 +0100)
Let's parse from mkosi.profiles in subimages as well. We'll still insist
that the profiles used are set in the main image though. To make this work
we have to remove the check to see if a profile exists or not, since it might
not exist in every mkosi.profiles/ directory that we parse. At the same time
we remove the restriction that profiles are set before we parse mkosi.conf.d/
since this isn't really useful anymore now that we parse profiles last and not
first anymore. This allows us to get rid of the concept of immutable settings.

Fixes #3307

mkosi/config.py
tests/test_config.py

index 99a09a6192892648734e9eb3cf7227b4d70e8254..6f26f5308c340edc4caa638a9227b3a0bf0054fb 100644 (file)
@@ -4004,7 +4004,6 @@ class ParseContext:
         self.defaults = argparse.Namespace()
         # Compare inodes instead of paths so we can't get tricked by bind mounts and such.
         self.includes: set[tuple[int, int]] = set()
-        self.immutable: set[str] = set()
         self.only_sections: tuple[str, ...] = tuple()
 
     def expand_specifiers(self, text: str, path: Path) -> str:
@@ -4335,8 +4334,6 @@ class ParseContext:
                     and (image := getattr(self.config, "image", None)) is not None
                 ):
                     die(f"{path.absolute()}: Setting {name} cannot be configured in subimage {image}")
-                if name in self.immutable:
-                    die(f"{path.absolute()}: Setting {name} cannot be modified anymore at this point")
 
                 if section != s.section:
                     logging.warning(
@@ -4354,30 +4351,19 @@ class ParseContext:
                 setattr(self.config, s.dest, s.parse(v, getattr(self.config, s.dest, None)))
                 self.parse_new_includes()
 
-        profilepaths = []
-        if parse_profiles:
-            profiles = self.finalize_value(SETTINGS_LOOKUP_BY_DEST["profiles"])
-            self.immutable.add("Profiles")
-
-            for profile in profiles or []:
-                for p in (Path(profile), Path(f"{profile}.conf")):
-                    p = Path("mkosi.profiles") / p
-                    if p.exists():
-                        break
-                else:
-                    die(f"Profile '{profile}' not found in mkosi.profiles/")
-
-                profilepaths += [p]
-
         if extras and (path.parent / "mkosi.conf.d").exists():
             for p in sorted((path.parent / "mkosi.conf.d").iterdir()):
                 if p.is_dir() or p.suffix == ".conf":
                     with chdir(p if p.is_dir() else Path.cwd()):
                         self.parse_config_one(p if p.is_file() else Path("."))
 
-        for p in profilepaths:
-            with chdir(p if p.is_dir() else Path.cwd()):
-                self.parse_config_one(p if p.is_file() else Path("."))
+        if parse_profiles:
+            for profile in self.finalize_value(SETTINGS_LOOKUP_BY_DEST["profiles"]) or []:
+                for p in (Path(profile), Path(f"{profile}.conf")):
+                    p = Path("mkosi.profiles") / p
+                    if p.exists():
+                        with chdir(p if p.is_dir() else Path.cwd()):
+                            self.parse_config_one(p if p.is_file() else Path("."))
 
         return True
 
@@ -4551,7 +4537,11 @@ def parse_config(
             context.defaults = argparse.Namespace()
 
             with chdir(p if p.is_dir() else Path.cwd()):
-                if not context.parse_config_one(p if p.is_file() else Path("."), parse_local=True):
+                if not context.parse_config_one(
+                    p if p.is_file() else Path("."),
+                    parse_profiles=True,
+                    parse_local=True,
+                ):
                     continue
 
             # Consolidate all settings into one namespace again.
index aca3e189f6cce4eb82523daa7ea93011448afb60..075d7da964a5bd7276e0a06765f8c7a883a9d06b 100644 (file)
@@ -417,6 +417,20 @@ def test_profiles(tmp_path: Path) -> None:
     assert config.profiles == ["profile", "abc"]
     assert config.distribution == Distribution.opensuse
 
+    # Check that mkosi.profiles/ is parsed in subimages as well.
+    (d / "mkosi.images/subimage/mkosi.profiles").mkdir(parents=True)
+    (d / "mkosi.images/subimage/mkosi.profiles/abc.conf").write_text(
+        """
+        [Build]
+        Environment=Image=%I
+        """
+    )
+
+    with chdir(d):
+        _, [subimage, config] = parse_config()
+
+    assert subimage.environment["Image"] == "subimage"
+
 
 def test_override_default(tmp_path: Path) -> None:
     d = tmp_path