Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions mkosi/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,7 @@ class Args:
cmdline: list[str]
force: int
directory: Optional[Path]
default_config: Optional[Path]
debug: bool
debug_shell: bool
debug_workspace: bool
Expand Down Expand Up @@ -4328,6 +4329,14 @@ def create_argument_parser(chdir: bool = True) -> argparse.ArgumentParser:
help="Change to specified directory before doing anything",
metavar="PATH",
)
parser.add_argument(
"--default",
dest="default_config",
help="Use a specific configuration file as the entry point, allowing to set various defaults",
type=Path,
default=None,
metavar="PATH",
)
parser.add_argument(
"--debug",
help="Turn on debugging output",
Expand Down Expand Up @@ -4412,10 +4421,6 @@ def create_argument_parser(chdir: bool = True) -> argparse.ArgumentParser:
nargs=0,
action=IgnoreAction,
)
parser.add_argument(
"--default",
action=IgnoreAction,
)
parser.add_argument(
"--cache",
action=IgnoreAction,
Expand Down Expand Up @@ -4531,6 +4536,7 @@ def __call__(

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

if extras:
if parse_local:
for localpath in (
*([p] if (p := path.parent / "mkosi.local").is_dir() else []),
*([p] if (p := path.parent / "mkosi.local.conf").is_file() else []),
):
localpaths = []
if (p := path.parent / "mkosi.local").is_dir():
localpaths.append(p)
if (p := path.parent / "mkosi.local.conf").is_file():
localpaths.append(p)
if self.default_config is not None and self.default_config.is_file():
localpaths.append(self.default_config)
for localpath in localpaths:
with chdir(localpath if localpath.is_dir() else Path.cwd()):
self.parse_config_one(localpath if localpath.is_file() else Path.cwd())

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

# Config paths need to be absolute - as a special case, if it is not, look in the parent
# of --directory. This works nicely on OBS, where the --directory is the git repository
# and the --default is the mkosi.conf selected at source service time, placed in the parent.
if args.default_config is not None:
if args.directory is not None and not args.default_config.is_absolute():
context.default_config = args.directory.parent / args.default_config
else:
context.default_config = args.default_config
Comment on lines +5162 to +5166
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the args.default_config is None anyways, this could be shortened into

Suggested change
if args.default_config is not None:
if args.directory is not None and not args.default_config.is_absolute():
context.default_config = args.directory.parent / args.default_config
else:
context.default_config = args.default_config
if (
args.default_config is not None:
and args.directory is not None
and not args.default_config.is_absolute()
):
context.default_config = args.directory.parent / args.default_config

(well, it's longer, but slightly less complex)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With that change, unless args.directory is set, args.default_config is ignored, even if it's an absolute path which would otherwise work


context.parse_new_includes()

context.config["files"] = []
Expand Down
6 changes: 6 additions & 0 deletions mkosi/resources/man/mkosi.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ Those settings cannot be configured in the configuration files.
working directory. If the empty string is specified, all configuration in
the current working directory will be ignored.

`--default=`
: Takes an absolute path to a configuration file. **mkosi** parses this file
before doing anything. This allows a user to select an entry point to
programmatically decide among various available options (e.g.: profiles)
without having to select them on the command line.

`--debug`
: Enable additional debugging output.

Expand Down
2 changes: 2 additions & 0 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def test_args(path: Optional[Path]) -> None:
"DebugSandbox": false,
"DebugShell": false,
"DebugWorkspace": false,
"DefaultConfig": null,
"Directory": {f'"{os.fspath(path)}"' if path is not None else "null"},
"DocFormat": "auto",
"Force": 9001,
Expand All @@ -82,6 +83,7 @@ def test_args(path: Optional[Path]) -> None:
debug_sandbox=False,
debug_shell=False,
debug_workspace=False,
default_config=None,
directory=Path(path) if path is not None else None,
doc_format=DocFormat.auto,
force=9001,
Expand Down