]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Introduce PassEnvironment=
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 12 Jul 2024 12:25:44 +0000 (14:25 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 12 Jul 2024 12:59:29 +0000 (14:59 +0200)
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.

mkosi/config.py
mkosi/resources/mkosi.md
tests/test_config.py
tests/test_json.py

index fea162500d524ef21076b0c18e9cae6377a2a0a5..76b4e7f72296a132a30abbdcaca9125ae4656c98 100644 (file)
@@ -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)}
index 9ea2dc98a24494147b4da275b156b8a847458f10..6ba9e5a64a5c444df65107ce4025ebdcac96fb05 100644 (file)
@@ -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
index 6a5b6d591033175dfab70d534544a511b2370018..17926cf6f143488354edf118735e82fb2fddabe7 100644 (file)
@@ -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
index 45145d9fa4039ecf0bde48bc8d85019e7d390aa2..5f5f7d55f591b793389bad0e0d20eef79705f654 100644 (file)
@@ -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")],