From: Daan De Meyer Date: Fri, 12 Jul 2024 12:25:44 +0000 (+0200) Subject: Introduce PassEnvironment= X-Git-Tag: v24~38^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f34b40b77f5a99b4049a51dee954fee4a11d557c;p=thirdparty%2Fmkosi.git Introduce PassEnvironment= We can't mark Environment= as a universal setting as only some environment variables should be universal and others shouldn't be. So let's introduce PassEnvironment= to mark specific environment variables as universal so that they are passed to subimage builds. --- diff --git a/mkosi/config.py b/mkosi/config.py index fea162500..76b4e7f72 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -1380,6 +1380,7 @@ class Config: initrd_include: list[Path] dependencies: list[str] minimum_version: Optional[GenericVersion] + pass_environment: list[str] distribution: Distribution release: str @@ -1883,6 +1884,13 @@ SETTINGS = ( paths=("mkosi.configure",), help="Configure script to run before doing anything", ), + ConfigSetting( + dest="pass_environment", + metavar="NAME", + section="Config", + parse=config_make_list_parser(delimiter=" "), + help="Environment variables to pass to subimages", + ), ConfigSetting( dest="distribution", short="-d", @@ -3763,6 +3771,16 @@ def parse_config(argv: Sequence[str] = (), *, resources: Path = Path("/")) -> tu elif hasattr(ParseContext.cli, s.dest): delattr(ParseContext.cli, s.dest) + setattr( + ParseContext.cli, + "environment", + { + name: getattr(ParseContext.config, "environment")[name] + for name in getattr(ParseContext.config, "pass_environment", {}) + if name in getattr(ParseContext.config, "environment", {}) + } + ) + for p in sorted(Path("mkosi.images").iterdir()): if not p.is_dir() and not p.suffix == ".conf": continue @@ -4025,6 +4043,7 @@ def summary(config: Config) -> str: Dependencies: {line_join_list(config.dependencies)} Minimum Version: {none_to_none(config.minimum_version)} Configure Scripts: {line_join_list(config.configure_scripts)} + Pass Environment: {line_join_list(config.pass_environment)} {bold("DISTRIBUTION")}: Distribution: {bold(config.distribution)} diff --git a/mkosi/resources/mkosi.md b/mkosi/resources/mkosi.md index 9ea2dc98a..6ba9e5a64 100644 --- a/mkosi/resources/mkosi.md +++ b/mkosi/resources/mkosi.md @@ -1822,6 +1822,12 @@ config file is read: the configure scripts for this image. See the **Scripts** section for more information. +`PassEnvironment=`, `--pass-environment=` +: Takes a list of environment variable names separated by spaces. When + building multiple images, pass the listed environment variables to + each individual subimage as if they were "universal" settings. See + the **Building multiple images** section for more information. + ## Specifiers The current value of various settings can be accessed when parsing diff --git a/tests/test_config.py b/tests/test_config.py index 6a5b6d591..17926cf6f 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1196,9 +1196,13 @@ def test_environment(tmp_path: Path) -> None: (d / "mkosi.conf").write_text( """\ + [Config] + PassEnvironment=PassThisEnv + [Content] Environment=TestValue2=300 TestValue3=400 + PassThisEnv=abc EnvironmentFiles=other.env """ ) @@ -1217,8 +1221,11 @@ def test_environment(tmp_path: Path) -> None: """ ) + (d / "mkosi.images").mkdir() + (d / "mkosi.images/sub.conf").touch() + with chdir(d): - _, [config] = parse_config() + _, [sub, config] = parse_config() expected = { "TestValue1": "100", # from other.env @@ -1231,3 +1238,6 @@ def test_environment(tmp_path: Path) -> None: assert {k: config.environment[k] for k in expected.keys()} == expected assert config.environment_files == [Path.cwd() / "mkosi.env", Path.cwd() / "other.env"] + + assert sub.environment["PassThisEnv"] == "abc" + assert "TestValue2" not in sub.environment diff --git a/tests/test_json.py b/tests/test_json.py index 45145d9fa..5f5f7d55f 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -206,6 +206,9 @@ def test_config() -> None: } ], "Packages": [], + "PassEnvironment": [ + "abc" + ], "Passphrase": null, "PostInstallationScripts": [ "/bar/qux" @@ -428,6 +431,7 @@ def test_config() -> None: package_directories=[], package_manager_trees=[ConfigTree(Path("/foo/bar"), None)], packages=[], + pass_environment=["abc"], passphrase=None, postinst_scripts=[Path("/bar/qux")], postoutput_scripts=[Path("/foo/src")],