]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Add support for loading environment files
authorKuntal Majumder <12135951+hellozee@users.noreply.github.com>
Sat, 16 Dec 2023 17:02:56 +0000 (18:02 +0100)
committerGitHub <noreply@github.com>
Sat, 16 Dec 2023 17:02:56 +0000 (18:02 +0100)
Fixes #738

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

index c733ef879ad4c1cbc352e19e86e1987cda4b3146..abf560dd92d5667b5875a8f6c190eef79dffa64a 100644 (file)
@@ -954,6 +954,7 @@ class MkosiConfig:
     build_sources: list[ConfigTree]
     build_sources_ephemeral: bool
     environment: dict[str, str]
+    environment_files: list[Path]
     with_tests: bool
     with_network: bool
 
@@ -1677,6 +1678,16 @@ SETTINGS = (
         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",
@@ -2879,8 +2890,10 @@ def load_environment(args: argparse.Namespace) -> dict[str, str]:
     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
 
@@ -3077,6 +3090,7 @@ def summary(config: MkosiConfig) -> str:
                       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)}
 
index 2c8dd3e036bb62105bae27212874b7d28790ade2..4a484df7cdae89a61ecdd78f3980ba66219acf00 100644 (file)
@@ -976,6 +976,14 @@ boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`,
   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
index 7539347d1c229868ff39d27729b3190d91aed3c0..b02bbf0b19c5747bf3601fd8dfb509a3b0db23f3 100644 (file)
@@ -753,3 +753,45 @@ def test_specifiers(tmp_path: Path) -> None:
 
 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"]
index 660d0a97dacfd54aa205e1cec9b0fdd6ac39e78d..41e159f9819fe743721523c8a90546be4debc71b 100644 (file)
@@ -117,6 +117,7 @@ def test_config() -> None:
             ],
             "Distribution": "fedora",
             "Environment": {},
+            "EnvironmentFiles": [],
             "Ephemeral": true,
             "ExtraSearchPaths": [],
             "ExtraTrees": [],
@@ -308,6 +309,7 @@ def test_config() -> None:
         dependencies = ("dep1",),
         distribution = Distribution.fedora,
         environment = {},
+        environment_files = [],
         ephemeral = True,
         extra_search_paths = [],
         extra_trees = [],