build_sources: list[ConfigTree]
build_sources_ephemeral: bool
environment: dict[str, str]
+ environment_files: list[Path]
with_tests: bool
with_network: bool
parse=config_make_list_parser(delimiter=" ", unescape=True),
help="Set an environment variable when running scripts",
),
+ MkosiConfigSetting(
+ dest="environment_files",
+ long="--env-file",
+ metavar="PATH",
+ section="Content",
+ parse=config_make_list_parser(delimiter=",", parse=make_path_parser()),
+ paths=("mkosi.env",),
+ path_default=False,
+ help="Enviroment files to set when running scripts",
+ ),
MkosiConfigSetting(
dest="with_tests",
short="-T",
if dnf := os.getenv("MKOSI_DNF"):
env["MKOSI_DNF"] = dnf
- for s in args.environment:
+ entries = [line for envfile in args.environment_files for line in envfile.read_text().strip().splitlines()]
+ for s in entries + args.environment:
key, sep, value = s.partition("=")
+ key, value = key.strip(), value.strip()
value = value if sep else os.getenv(key, "")
env[key] = value
Build Sources: {line_join_tree_list(config.build_sources)}
Build Sources Ephemeral: {yes_no(config.build_sources_ephemeral)}
Script Environment: {line_join_list(env)}
+ Environment Files: {line_join_list(config.environment_files)}
Run Tests in Build Scripts: {yes_no(config.with_tests)}
Scripts With Network: {yes_no(config.with_network)}
listed variables will be set. If the same variable is set twice, the
later setting overrides the earlier one.
+`EnvironmentFiles=`, `--env-file=`
+
+: Takes a comma-separated list of paths to files that contain enviroment
+ variable definitions to be added to the scripting environment. Uses
+ `mkosi.env` if it is found in the local directory. The variables are
+ first read from `mkosi.env` if it exists, then from the given list of
+ files and then from the `Environment=` settings.
+
`WithTests=`, `--without-tests`, `-T`
: If set to false (or when the command-line option is used), the
def test_deterministic() -> None:
assert MkosiConfig.default() == MkosiConfig.default()
+
+
+def test_environment(tmp_path: Path) -> None:
+ d = tmp_path
+
+ (d / "mkosi.conf").write_text(
+ """\
+ [Content]
+ Environment=TestValue2=300
+ TestValue3=400
+ EnvironmentFiles=other.env
+ """
+ )
+
+ (d / "mkosi.env").write_text(
+ """\
+ TestValue1=90
+ TestValue4=99
+ """
+ )
+
+ (d / "other.env").write_text(
+ """\
+ TestValue1=100
+ TestValue2=200
+ """
+ )
+
+ with chdir(d):
+ _, [config] = parse_config()
+
+ expected = {
+ "TestValue1": "100", # from other.env
+ "TestValue2": "300", # from mkosi.conf
+ "TestValue3": "400", # from mkosi.conf
+ "TestValue4": "99", # from mkosi.env
+ }
+
+ # Only check values for keys from expected, as config.environment contains other items as well
+ assert {k: config.environment[k] for k in expected.keys()} == expected
+
+ assert config.environment_files == [Path.cwd() / "mkosi.env", Path.cwd() / "other.env"]
],
"Distribution": "fedora",
"Environment": {},
+ "EnvironmentFiles": [],
"Ephemeral": true,
"ExtraSearchPaths": [],
"ExtraTrees": [],
dependencies = ("dep1",),
distribution = Distribution.fedora,
environment = {},
+ environment_files = [],
ephemeral = True,
extra_search_paths = [],
extra_trees = [],