From: Daan De Meyer Date: Sat, 21 Sep 2024 09:58:22 +0000 (+0200) Subject: Parse profiles after mkosi.conf.d X-Git-Tag: v25~273^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f2a9d0f5b668a96c39dbbd0ddb1cce33a955ca73;p=thirdparty%2Fmkosi.git Parse profiles after mkosi.conf.d 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. --- diff --git a/NEWS.md b/NEWS.md index 95f8da062..ad08c83b7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -55,6 +55,10 @@ 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 diff --git a/mkosi/config.py b/mkosi/config.py index 4051c6538..2c8ecb928 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -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 diff --git a/mkosi/resources/man/mkosi.md b/mkosi/resources/man/mkosi.md index 231d9b60f..a04783688 100644 --- a/mkosi/resources/man/mkosi.md +++ b/mkosi/resources/man/mkosi.md @@ -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 diff --git a/tests/test_config.py b/tests/test_config.py index cbbbbd706..ed5f1e62e 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -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