]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Add %F to access the default filesystem of a distribution
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 5 Jul 2024 07:19:51 +0000 (09:19 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 5 Jul 2024 07:50:48 +0000 (09:50 +0200)
One annoyance about using mkosi.repart has always been that to keep
using the default filesystem per distribution you have to write a
lot of matches. Now that systemd-repart supports
$SYSTEMD_REPART_OVERRIDE_FSTYPE_ROOT, let's add a specifier to access
the default filesystem so that it can be combined with the environment
variable to get the same result.

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

index 8395116b32afee9786fbfce11647349426b5febf..3775757a1473120999dd00437b5d7ee1f3718461 100644 (file)
@@ -1174,6 +1174,7 @@ class Match:
 class Specifier:
     char: str
     callback: Callable[[argparse.Namespace, Path], str]
+    depends: tuple[str, ...] = tuple()
 
 
 class CustomHelpFormatter(argparse.HelpFormatter):
@@ -3072,6 +3073,11 @@ SPECIFIERS = (
         char="D",
         callback=lambda ns, config: os.fspath(ns.directory.resolve()),
     ),
+    Specifier(
+        char="F",
+        callback=lambda ns, config: ns.distribution.filesystem(),
+        depends=("distribution",),
+    ),
 )
 
 SPECIFIERS_LOOKUP_BY_CHAR = {s.char: s for s in SPECIFIERS}
@@ -3313,7 +3319,17 @@ def parse_config(argv: Sequence[str] = (), *, resources: Path = Path("/")) -> tu
 
                     result += str(v)
                 elif specifier := SPECIFIERS_LOOKUP_BY_CHAR.get(c):
-                    result += specifier.callback(namespace, path)
+                    for d in specifier.depends:
+                        setting = SETTINGS_LOOKUP_BY_DEST[d]
+
+                        if finalize_default(setting) is None:
+                            logging.warning(
+                                f"Setting {setting.name} which specifier '%{c}' in {text} depends on is not yet set, "
+                                "ignoring"
+                            )
+                            break
+                    else:
+                        result += specifier.callback(namespace, path)
                 else:
                     logging.warning(f"Unknown specifier '%{c}' found in {text}, ignoring")
             elif c == "%":
index 8ccbfa284918396ffab932cc4400a54721b04a98..844a6eb5c56a961c5163ca3b2163cad9bf01ca16 100644 (file)
@@ -1845,6 +1845,12 @@ There are also specifiers that are independent of settings:
 | `%P`      | Current working directory               |
 | `%D`      | Directory that mkosi was invoked in     |
 
+Finally, there are specifiers that are derived from a setting:
+
+| Specifier | Value                                                 |
+|-----------|-------------------------------------------------------|
+| `%F`      | The default filesystem of the configured distribution |
+
 Note that the current working directory changes as mkosi parses its
 configuration. Specifically, each time mkosi parses a directory
 containing a `mkosi.conf` file, mkosi changes its working directory to
index dc9332179ae4c418370184ac2dd65d66e41edc91..712764622fa10f32a9d2466e9e95fb1042d7b343 100644 (file)
@@ -966,6 +966,7 @@ def test_specifiers(tmp_path: Path) -> None:
                     ConfigRootDirectory=%D
                     ConfigRootConfdir=%C
                     ConfigRootPwd=%P
+                    Filesystem=%F
         """
     )
 
@@ -1008,6 +1009,7 @@ def test_specifiers(tmp_path: Path) -> None:
             "ConfigQedDirectory": os.fspath(d),
             "ConfigQedConfdir": os.fspath(d / "mkosi.conf.d/qed"),
             "ConfigQedPwd": os.fspath(d / "mkosi.conf.d/qed"),
+            "Filesystem": "ext4",
         }
 
         assert {k: v for k, v in config.environment.items() if k in expected} == expected