]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Use mkosi/ subdirectory if no configuration exists in cwd 3643/head
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 2 Apr 2025 08:39:27 +0000 (10:39 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 2 Apr 2025 09:15:35 +0000 (11:15 +0200)
In systemd we've ended up with a large amount of mkosi specific
files and directories in the top level of the repository. Let's allow
moving these to a mkosi/ directory by transparently picking this up.

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

index 3106dd5d4afddde68d9f17a0b002e984d82df5b9..d7692f88c300f2192855d91455093c0086201306 100644 (file)
@@ -4999,6 +4999,16 @@ def parse_config(
     if not args.verb.needs_config():
         return args, None, ()
 
+    # Allow locating all mkosi configuration in a mkosi/ subdirectory instead of in the top-level directory
+    # of a git repository.
+    if (
+        args.directory is not None
+        and not (Path("mkosi.conf").exists() or Path("mkosi.tools.conf").exists())
+        and (Path("mkosi/mkosi.conf").is_file() or Path("mkosi/mkosi.tools.conf").exists())
+    ):
+        os.chdir(args.directory / "mkosi")
+        args = dataclasses.replace(args, directory=args.directory / "mkosi")
+
     if have_history(args):
         try:
             j = json.loads(Path(".mkosi-private/history/latest.json").read_text())
index 66084b164c11468586d25f7cb897aac4d9a32158..a0b673a2b7b32d5bba45f9522e0a704d7a24e5dc 100644 (file)
@@ -248,7 +248,14 @@ Those settings cannot be configured in the configuration files.
 :   Takes a path to a directory. **mkosi** switches to this directory before
     doing anything. Note that the various configuration files are searched
     for in this directory, hence using this option is an effective way to
-    build a project located in a specific directory.
+    build a project located in a specific directory. Defaults to the current
+    working directory. If the empty string is specified, all configuration in
+    the current working directory will be ignored.
+
+:   If the specified directory does not contain a `mkosi.conf` or
+    `mkosi.tools.conf` and a `mkosi/mkosi.conf` or `mkosi/mkosi.tools.conf`
+    exists, the `mkosi/` subdirectory of the specified directory will be
+    used instead.
 
 `--debug`
 :   Enable additional debugging output.
index e071d0ff0b23344fa629f4f8ad243f52702479e3..ce4a313922ba145b8ed6019bfdb96e1ff436948f 100644 (file)
@@ -1460,3 +1460,31 @@ def test_tools(tmp_path: Path) -> None:
         _, tools, _ = parse_config(argv, resources=resources)
         assert tools
         assert tools.distribution == Distribution.arch
+
+
+def test_subdir(tmp_path: Path) -> None:
+    d = tmp_path
+
+    with chdir(d):
+        (d / "mkosi").mkdir()
+        (d / "mkosi/mkosi.conf").write_text(
+            """
+            [Output]
+            Output=qed
+            """
+        )
+
+        _, _, [config] = parse_config()
+        assert config.output == "qed"
+
+        os.chdir(d)
+
+        (d / "mkosi.conf").write_text(
+            """
+            [Output]
+            Output=abc
+            """
+        )
+
+        _, _, [config] = parse_config()
+        assert config.output == "abc"