]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Parse mkosi.local.conf before parsing any other files
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 27 Oct 2023 19:17:27 +0000 (21:17 +0200)
committerJörg Behrmann <behrmann@physik.fu-berlin.de>
Sat, 28 Oct 2023 08:23:57 +0000 (10:23 +0200)
Currently extra local configuration is a bit implicit, users have
to add a drop-in in mkosi.conf.d/ named 00-local.conf or so and add
that to the gitignore. This file will then unconditionally override
settings from mkosi.conf even if that's not intended.

Let's make local configuration a bit more explicit by parsing
mkosi.local.conf in a directory first before parsing anything else.
This makes local configuration easy to detect and grep for, and
makes sure that local configuration is by default overridden by
non-local configuration.

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

index b03719441596a02046930e4354d595bd9b972bff..c45f2d2b754588f2297e1fcb2496999059e3f8c2 100644 (file)
@@ -2292,6 +2292,9 @@ def parse_config(argv: Sequence[str] = ()) -> tuple[MkosiArgs, tuple[MkosiConfig
             return False
 
         if extras:
+            if (path.parent / "mkosi.local.conf").exists():
+                parse_config(path.parent / "mkosi.local.conf", namespace, defaults)
+
             for s in SETTINGS:
                 ns = defaults if s.path_default else namespace
                 for f in s.paths:
index c50d138002c3ef1e71e71912359472af3fe836c3..3ed010dad082c3d02e6dafbba50e3d0c8dd58937 100644 (file)
@@ -252,6 +252,10 @@ grouped by section below.
 Configuration is parsed in the following order:
 
 * The command line arguments are parsed
+* `mkosi.local.conf` is parsed if it exists. This file should be in the
+  gitignore (or equivalent) and is intended for local configuration.
+* Any default paths (depending on the option) are configured if the
+  corresponding path exists.
 * `mkosi.conf` is parsed if it exists in the directory configured with
   `--directory=` or the current working directory if `--directory=` is
   not used.
@@ -259,8 +263,6 @@ Configuration is parsed in the following order:
   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.
-* Any default paths (depending on the option) are configured if the
-  corresponding path exists.
 
 Note that if the same setting is configured twice, the later assignment
 overrides the earlier assignment unless the setting is a list based
@@ -292,6 +294,11 @@ setting in configuration files that have not been parsed yet. Also note
 that matching against a setting and then changing its value afterwards
 in a different config file may lead to unexpected results.
 
+The `[Match]` section of a `mkosi.conf` file in a directory applies to
+the entire directory. If the conditions are not satisfied, the entire
+directory is skipped. The `[Match]` sections of files in `mkosi.conf.d/`
+and `mkosi.local.conf` only apply to the file itself.
+
 Command line options that take no argument are shown without `=` in
 their long version. In the config files, they should be specified with a
 boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`,
index f721e9688ac845c41c580c0937301e877f26d87e..5c1fe30048adcd1ad9f2b2b1b26c87091762b860 100644 (file)
@@ -270,6 +270,34 @@ def test_override_default(tmp_path: Path) -> None:
     assert config.tools_tree is None
 
 
+def test_local_config(tmp_path: Path) -> None:
+    d = tmp_path
+
+    (d / "mkosi.local.conf").write_text(
+        """\
+        [Distribution]
+        Distribution=debian
+        """
+    )
+
+    with chdir(d):
+        _, [config] = parse_config()
+
+    assert config.distribution == Distribution.debian
+
+    (d / "mkosi.conf").write_text(
+        """\
+        [Distribution]
+        Distribution=fedora
+        """
+    )
+
+    with chdir(d):
+        _, [config] = parse_config()
+
+    assert config.distribution == Distribution.fedora
+
+
 def test_parse_load_verb(tmp_path: Path) -> None:
     with chdir(tmp_path):
         assert parse_config(["build"])[0].verb == Verb.build