Skip to content

Commit 3e38c02

Browse files
committed
Bring back --default to allow OBS builds to select an entry point among many
When building a mkosi repository called 'particleos', OBS calls mkosi as such: mkosi --directory /usr/src/packages/SOURCES/particleos --default mkosi.conf with mkosi.conf being the source file extracted at source time, ie: defined by the user, among the many files available in the repository. So this can be used to select the entry point, that defines what to build, when a repository provides many build types, profiles, images, etc. Right now --default is a no-op, so make it add the passed conf file as a high priority config file, so that the entry point can have precedence over anything else. The file is placed at: /usr/src/packages/SOURCES/mkosi.conf This cannot be controlled by the user, and it's fixed. Config file parsing requires absolute paths, and aborts otherwise. Because it is called with a filename only, but mkosi executes from the --directory location, special case it and if a non-absolute path is passed, look in the parent of --directory so that the use case of providing an entry point can be enabled.
1 parent 44b96b9 commit 3e38c02

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

mkosi/config.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,7 @@ class Args:
17681768
cmdline: list[str]
17691769
force: int
17701770
directory: Optional[Path]
1771+
default_config: Optional[Path]
17711772
debug: bool
17721773
debug_shell: bool
17731774
debug_workspace: bool
@@ -4328,6 +4329,14 @@ def create_argument_parser(chdir: bool = True) -> argparse.ArgumentParser:
43284329
help="Change to specified directory before doing anything",
43294330
metavar="PATH",
43304331
)
4332+
parser.add_argument(
4333+
"--default",
4334+
dest="default_config",
4335+
help="Use a specific configuration file as the entry point, allowing to set various defaults",
4336+
type=Path,
4337+
default=None,
4338+
metavar="PATH",
4339+
)
43314340
parser.add_argument(
43324341
"--debug",
43334342
help="Turn on debugging output",
@@ -4412,10 +4421,6 @@ def create_argument_parser(chdir: bool = True) -> argparse.ArgumentParser:
44124421
nargs=0,
44134422
action=IgnoreAction,
44144423
)
4415-
parser.add_argument(
4416-
"--default",
4417-
action=IgnoreAction,
4418-
)
44194424
parser.add_argument(
44204425
"--cache",
44214426
action=IgnoreAction,
@@ -4531,6 +4536,7 @@ def __call__(
45314536

45324537
class ParseContext:
45334538
def __init__(self, resources: Path = Path("/")) -> None:
4539+
self.default_config: Optional[Path] = None
45344540
self.resources = resources
45354541
# We keep two namespaces around, one for the settings specified on the CLI and one for
45364542
# the settings specified in configuration files. This is required to implement both [Match]
@@ -4810,10 +4816,14 @@ def parse_config_one(self, path: Path, parse_profiles: bool = False, parse_local
48104816

48114817
if extras:
48124818
if parse_local:
4813-
for localpath in (
4814-
*([p] if (p := path.parent / "mkosi.local").is_dir() else []),
4815-
*([p] if (p := path.parent / "mkosi.local.conf").is_file() else []),
4816-
):
4819+
localpaths = []
4820+
if (p := path.parent / "mkosi.local").is_dir():
4821+
localpaths.append(p)
4822+
if (p := path.parent / "mkosi.local.conf").is_file():
4823+
localpaths.append(p)
4824+
if self.default_config is not None and self.default_config.is_file():
4825+
localpaths.append(self.default_config)
4826+
for localpath in localpaths:
48174827
with chdir(localpath if localpath.is_dir() else Path.cwd()):
48184828
self.parse_config_one(localpath if localpath.is_file() else Path.cwd())
48194829

@@ -5146,6 +5156,15 @@ def parse_config(
51465156
# One of the specifiers needs access to the directory, so make sure it is available.
51475157
context.config["directory"] = args.directory
51485158

5159+
# Config paths need to be absolute - as a special case, if it is not, look in the parent
5160+
# of --directory. This works nicely on OBS, where the --directory is the git repository
5161+
# and the --default is the mkosi.conf selected at source service time, placed in the parent.
5162+
if args.default_config is not None:
5163+
if args.directory is not None and not args.default_config.is_absolute():
5164+
context.default_config = args.directory.parent / args.default_config
5165+
else:
5166+
context.default_config = args.default_config
5167+
51495168
context.parse_new_includes()
51505169

51515170
context.config["files"] = []

mkosi/resources/man/mkosi.1.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,12 @@ Those settings cannot be configured in the configuration files.
256256
working directory. If the empty string is specified, all configuration in
257257
the current working directory will be ignored.
258258

259+
`--default=`
260+
: Takes an absolute path to a configuration file. **mkosi** parses this file
261+
before doing anything. This allows a user to select an entry point to
262+
programmatically decide among various available options (e.g.: profiles)
263+
without having to select them on the command line.
264+
259265
`--debug`
260266
: Enable additional debugging output.
261267

tests/test_json.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def test_args(path: Optional[Path]) -> None:
6161
"DebugSandbox": false,
6262
"DebugShell": false,
6363
"DebugWorkspace": false,
64+
"DefaultConfig": null,
6465
"Directory": {f'"{os.fspath(path)}"' if path is not None else "null"},
6566
"DocFormat": "auto",
6667
"Force": 9001,
@@ -82,6 +83,7 @@ def test_args(path: Optional[Path]) -> None:
8283
debug_sandbox=False,
8384
debug_shell=False,
8485
debug_workspace=False,
86+
default_config=None,
8587
directory=Path(path) if path is not None else None,
8688
doc_format=DocFormat.auto,
8789
force=9001,

0 commit comments

Comments
 (0)