]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Use a default tools tree by default if mkosi.tools.conf exists
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Sun, 20 Apr 2025 18:18:11 +0000 (20:18 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 22 Apr 2025 08:49:00 +0000 (10:49 +0200)
mkosi/__init__.py
mkosi/config.py
mkosi/resources/man/mkosi.1.md

index 27bb2333a82215b562295449483096449b7a3878..f8a0248070dc45716b8ab8620129d57159bf3047 100644 (file)
@@ -4504,7 +4504,9 @@ def generate_key_cert_pair(args: Args) -> None:
     keylength = 2048
     expiration_date = datetime.date.today() + datetime.timedelta(int(args.genkey_valid_days))
 
-    configdir = finalize_configdir(args)
+    configdir = finalize_configdir(args.directory)
+    if not configdir:
+        die("genkey cannot be used with empty --directory")
 
     for f in (configdir / "mkosi.key", configdir / "mkosi.crt"):
         if f.exists() and not args.force:
@@ -4541,7 +4543,10 @@ def generate_key_cert_pair(args: Args) -> None:
 
 
 def finalize_image_version(args: Args, config: Config) -> None:
-    p = finalize_configdir(args) / "mkosi.version"
+    configdir = finalize_configdir(args.directory)
+    if not configdir:
+        die("Image version cannot be finalized with empty --directory")
+    p = configdir / "mkosi.version"
     assert config.image_version
     p.write_text(config.image_version)
     logging.info(f"Wrote new version {config.image_version} to {p}")
index 433f10208f9b69ea289de0b320a268651a1a3f1a..b1f8d2b14cb0a1afd6981d5e2d637da59388f788 100644 (file)
@@ -1005,6 +1005,14 @@ def config_default_release(namespace: dict[str, Any]) -> str:
     return cast(str, namespace["distribution"].default_release())
 
 
+def config_default_tools_tree(namespace: dict[str, Any]) -> Optional[Path]:
+    if namespace.get("image") != "main":
+        return None
+
+    configdir = finalize_configdir(namespace["directory"])
+    return Path("default") if configdir and (configdir / "mkosi.tools.conf").exists() else None
+
+
 def config_default_tools_tree_distribution(namespace: dict[str, Any]) -> Distribution:
     if d := os.getenv("MKOSI_HOST_DISTRIBUTION"):
         return Distribution(d).default_tools_tree_distribution()
@@ -3533,6 +3541,7 @@ SETTINGS: list[ConfigSetting[Any]] = [
         section="Build",
         parse=config_make_path_parser(constants=("default",)),
         path_suffixes=("tools",),
+        default_factory=config_default_tools_tree,
         help="Look up programs to execute inside the given tree",
         scope=SettingScope.universal,
     ),
@@ -4960,7 +4969,7 @@ def finalize_default_tools(
     main: ParseContext,
     finalized: dict[str, Any],
     *,
-    configdir: Path,
+    configdir: Optional[Path],
     resources: Path,
 ) -> Config:
     context = ParseContext(resources)
@@ -4997,7 +5006,7 @@ def finalize_default_tools(
         for name in finalized.get("environment", {}).keys() & finalized.get("pass_environment", [])
     }
 
-    if (p := configdir / "mkosi.tools.conf").exists():
+    if configdir and (p := configdir / "mkosi.tools.conf").exists():
         with chdir(p if p.is_dir() else Path.cwd()):
             context.parse_config_one(p, parse_profiles=p.is_dir(), parse_local=p.is_dir())
 
@@ -5007,18 +5016,19 @@ def finalize_default_tools(
     return Config.from_dict(context.finalize())
 
 
-def finalize_configdir(args: Args) -> Path:
+def finalize_configdir(directory: Optional[Path]) -> Optional[Path]:
     """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())
+    if directory is None:
+        return None
+
+    if not ((directory / "mkosi.conf").exists() or (directory / "mkosi.tools.conf").exists()) and (
+        (directory / "mkosi/mkosi.conf").is_file() or (directory / "mkosi/mkosi.tools.conf").exists()
     ):
-        return Path.cwd() / "mkosi"
+        return directory / "mkosi"
 
-    return Path.cwd()
+    return directory
 
 
 def bump_image_version(configdir: Path) -> str:
@@ -5128,17 +5138,17 @@ def parse_config(
 
     context.config["files"] = []
 
-    configdir = finalize_configdir(args)
+    configdir = finalize_configdir(args.directory)
 
     if (
-        (args.auto_bump and args.verb.needs_build())
-        or args.verb == Verb.bump
+        ((args.auto_bump and args.verb.needs_build()) or args.verb == Verb.bump)
         and context.cli.get("image_version") is not None
+        and configdir is not None
     ):
         context.cli["image_version"] = bump_image_version(configdir)
 
     # Parse the global configuration unless the user explicitly asked us not to.
-    if args.directory is not None:
+    if configdir is not None:
         with chdir(configdir):
             context.parse_config_one(configdir, parse_profiles=True, parse_local=True)
 
@@ -5168,7 +5178,7 @@ def parse_config(
         None if "dependencies" in context.cli or "dependencies" in context.config else []
     )
 
-    if args.directory is not None and (imagedir := configdir / "mkosi.images").exists():
+    if configdir is not None and (imagedir := configdir / "mkosi.images").exists():
         # For the subimages in mkosi.images/, we want settings that are marked as
         # "universal" to override whatever settings are specified in the subimage
         # configuration files. We achieve this by making it appear like these settings
index 0ecc0b336d779f44a2506ab48b50aa0930f60b14..19ae5d36a0c86f30edb23d1ed162be2257a5f108 100644 (file)
@@ -1166,7 +1166,7 @@ boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`,
 :   Specifies whether to install an **sshd** socket unit and matching service
     in the final image. Takes one of `always`, `never`, `auto` or `runtime`.
     Defaults to `auto`.
-    
+
     If set to `auto` and the generator binary `systemd-ssh-generator`
     is not preset in the image, or set to `always`, mkosi will install **sshd** units
     in the final image that expose SSH over VSock. If set to `never`,
@@ -1342,6 +1342,9 @@ boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`,
     using the settings below or with `mkosi.tools.conf` which can either be a
     file or directory containing extra configuration for the default tools tree.
 
+    If `mkosi.tools.conf` exists in the directory mkosi is invoked in,
+    a default tools tree is used if no tools tree is specified.
+
     The following table shows for which distributions default tools tree
     packages are defined and which packages are included in those default
     tools trees: