]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Parse profiles after mkosi.conf.d
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Sat, 21 Sep 2024 09:58:22 +0000 (11:58 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Sat, 21 Sep 2024 13:37:40 +0000 (15:37 +0200)
Currently profiles can't depend on any of the configuration set in
mkosi.conf.d as they are parsed before mkosi.conf.d is parsed. Let's
parse the profile related configuration last instead so it can match
on all the configuration set in mkosi.conf.d.

To set the distribution and release and such based on the profile,
a dropin in mkosi.conf.d can match on the configured profile instead.

NEWS.md
mkosi/config.py
mkosi/resources/man/mkosi.md
tests/test_config.py

diff --git a/NEWS.md b/NEWS.md
index 95f8da062ecfee1931ebe87604519b6281ab00c4..ad08c83b7bcb85a4e4a4de03bc828062f8077cc7 100644 (file)
--- a/NEWS.md
+++ b/NEWS.md
   when running a command that boots the image
 - More directories aside from `/etc` and `/usr` are now picked up from
   sandbox trees (formerly known as package manager trees).
+- Profile configuration from `mkosi.profiles` is now parsed after
+  `mkosi.conf.d` instead of before it. To set defaults for use in `mkosi.conf.d`
+  based on the configured profile, use an early dropin in `mkosi.conf.d` that
+  matches on the configured profile instead.
 
 ## v24
 
index 4051c6538d9d290cfe3b92b671751beac4089ed9..2c8ecb92831816c9c307db80898815c36573620a 100644 (file)
@@ -3866,29 +3866,31 @@ class ParseContext:
                 setattr(self.config, s.dest, s.parse(v, getattr(self.config, s.dest, None)))
                 self.parse_new_includes()
 
+        profilepath = None
         if profiles:
             profile = self.finalize_value(SETTINGS_LOOKUP_BY_DEST["profile"])
             self.immutable.add("Profile")
 
             if profile:
-                for p in (profile, f"{profile}.conf"):
-                    p = Path("mkosi.profiles") / p
-                    if p.exists():
+                for p in (Path(profile), Path(f"{profile}.conf")):
+                    profilepath = Path("mkosi.profiles") / p
+                    if profilepath.exists():
                         break
                 else:
                     die(f"Profile '{profile}' not found in mkosi.profiles/")
 
                 setattr(self.config, "profile", profile)
 
-                with chdir(p if p.is_dir() else Path.cwd()):
-                    self.parse_config_one(p if p.is_file() else Path("."))
-
         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("."))
 
+        if profilepath:
+            with chdir(profilepath if profilepath.is_dir() else Path.cwd()):
+                self.parse_config_one(profilepath if profilepath.is_file() else Path("."))
+
         return True
 
 
index 231d9b60f643039d3e6ec0ac702439de4ab0f443..a04783688835c12af986dacfa2f3920b53a8c24e 100644 (file)
@@ -306,12 +306,12 @@ Configuration is parsed in the following order:
 * `mkosi.conf` is parsed if it exists in the directory configured with
   `--directory=` or the current working directory if `--directory=` is
   not used.
-* If a profile is defined, its configuration is parsed from the
-  `mkosi.profiles/` directory.
 * `mkosi.conf.d/` is parsed in the same directory if it exists. Each
   directory and each file with the `.conf` extension in `mkosi.conf.d/`
   is parsed. Any directory in `mkosi.conf.d` is parsed as if it were
   a regular top level directory.
+* If any profiles are configured, their configuration is parsed from the
+  `mkosi.profiles/` directory.
 * Subimages are parsed from the `mkosi.images` directory if it exists.
 
 Note that settings configured via the command line always override
@@ -1867,8 +1867,7 @@ config file is read:
 :   Select the given profile. A profile is a configuration file or
     directory in the `mkosi.profiles/` directory. When selected, this
     configuration file or directory is included after parsing the
-    `mkosi.conf` file, but before any `mkosi.conf.d/*.conf` drop in
-    configuration.
+    `mkosi.conf.d/*.conf` drop in configuration files.
 
 `Dependencies=`, `--dependency=`
 :   The images that this image depends on specified as a comma-separated
index cbbbbd70618a1bc68440e09f8c41dbbc16da1935..ed5f1e62e3a9da0996a30e3ff57083dd95abd7d1 100644 (file)
@@ -377,8 +377,8 @@ def test_profiles(tmp_path: Path) -> None:
         _, [config] = parse_config()
 
     assert config.profile == "profile"
-    # mkosi.conf.d/ should override the profile
-    assert config.distribution == Distribution.debian
+    # The profile should override mkosi.conf.d/
+    assert config.distribution == Distribution.fedora
     assert config.qemu_kvm == ConfigFeature.enabled
 
     (d / "mkosi.conf").unlink()
@@ -387,8 +387,8 @@ def test_profiles(tmp_path: Path) -> None:
         _, [config] = parse_config(["--profile", "profile"])
 
     assert config.profile == "profile"
-    # mkosi.conf.d/ should override the profile
-    assert config.distribution == Distribution.debian
+    # The profile should override mkosi.conf.d/
+    assert config.distribution == Distribution.fedora
     assert config.qemu_kvm == ConfigFeature.enabled