]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Move default environment into MkosiConfig
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 20 Jul 2023 08:45:41 +0000 (10:45 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 20 Jul 2023 09:19:35 +0000 (11:19 +0200)
We already do this for --kernel-command-line-extra and --credential,
let's be consistent and do --environment the same way.

mkosi/__init__.py
mkosi/config.py
mkosi/distributions/arch.py
mkosi/distributions/debian.py
mkosi/distributions/fedora.py
mkosi/distributions/gentoo.py
mkosi/distributions/opensuse.py
mkosi/state.py

index df11535ff714b5cccbfe6c4381fbf1ae070453ca..7b372c538b8cbdb7580b06e9613e79cc7b040123 100644 (file)
@@ -316,7 +316,7 @@ def run_prepare_script(state: MkosiState, build: bool) -> None:
                 ["chroot", "/work/prepare", "build"],
                 apivfs=state.root,
                 scripts=dict(chroot=chroot_cmd(state.root, options=options, network=True)),
-                env=dict(SRCDIR="/work/src") | state.environment,
+                env=dict(SRCDIR="/work/src") | state.config.environment,
             )
             shutil.rmtree(state.root / "work")
     else:
@@ -325,7 +325,7 @@ def run_prepare_script(state: MkosiState, build: bool) -> None:
                 ["chroot", "/work/prepare", "final"],
                 apivfs=state.root,
                 scripts=dict(chroot=chroot_cmd(state.root, options=options, network=True)),
-                env=dict(SRCDIR="/work/src") | state.environment,
+                env=dict(SRCDIR="/work/src") | state.config.environment,
             )
             shutil.rmtree(state.root / "work")
 
@@ -345,7 +345,7 @@ def run_postinst_script(state: MkosiState) -> None:
                     network=state.config.with_network,
                 ),
             ),
-            env=state.environment,
+            env=state.config.environment,
         )
 
         shutil.rmtree(state.root / "work")
@@ -357,7 +357,7 @@ def run_finalize_script(state: MkosiState) -> None:
 
     with complete_step("Running finalize script…"):
         run([state.config.finalize_script],
-            env={**state.environment, "BUILDROOT": str(state.root), "OUTPUTDIR": str(state.staging)})
+            env={**state.config.environment, "BUILDROOT": str(state.root), "OUTPUTDIR": str(state.staging)})
 
 
 def certificate_common_name(state: MkosiState, certificate: Path) -> str:
@@ -1578,7 +1578,7 @@ def run_selinux_relabel(state: MkosiState) -> None:
             cmd=["chroot", "sh", "-c", cmd],
             apivfs=state.root,
             scripts=dict(chroot=chroot_cmd(state.root)),
-            env=state.environment,
+            env=state.config.environment,
         )
 
 
@@ -1711,7 +1711,7 @@ def make_image(state: MkosiState, skip: Sequence[str] = [], split: bool = False)
     for fs, options in state.installer.filesystem_options(state).items():
         env[f"SYSTEMD_REPART_MKFS_OPTIONS_{fs.upper()}"] = " ".join(options)
 
-    for option, value in state.environment.items():
+    for option, value in state.config.environment.items():
         if option.startswith("SYSTEMD_REPART_MKFS_OPTIONS_"):
             env[option] = value
 
@@ -1873,7 +1873,7 @@ def run_build_script(state: MkosiState) -> None:
             ["chroot", "/work/build-script"],
             apivfs=state.root,
             scripts=dict(chroot=chroot_cmd(state.root, options=options, network=state.config.with_network)),
-            env=env | state.environment,
+            env=env | state.config.environment,
         )
 
 
index 04897088085c3956f2d45f3e2f477ce172b10532..8b1d5baefd75193507c786d3abd0b133186c7933 100644 (file)
@@ -2042,6 +2042,22 @@ def load_kernel_command_line_extra(args: argparse.Namespace) -> list[str]:
     return cmdline
 
 
+def load_environment(args: argparse.Namespace) -> dict[str, str]:
+    env = {}
+
+    if args.image_id is not None:
+        env["IMAGE_ID"] = args.image_id
+    if args.image_version is not None:
+        env["IMAGE_VERSION"] = args.image_version
+    if (proxy := os.environ.get("http_proxy")):
+        env["http_proxy"] = proxy
+    if (proxy := os.environ.get("https_proxy")):
+        env["https_proxy"] = proxy
+
+    # Mypy doesn't like | here.
+    return {**env, **args.environment}
+
+
 def load_args(args: argparse.Namespace) -> MkosiArgs:
     if args.debug:
         ARG_DEBUG.set(args.debug)
@@ -2089,6 +2105,7 @@ def load_config(args: argparse.Namespace) -> MkosiConfig:
 
     args.credentials = load_credentials(args)
     args.kernel_command_line_extra = load_kernel_command_line_extra(args)
+    args.environment = load_environment(args)
 
     if args.secure_boot and args.verb != Verb.genkey:
         if args.secure_boot_key is None:
index 6bad590cd3d77168fcf4dc5a52e327b80e2c5f83..cd3c76ea250b8e3c9bca263d154c175a7bbe3ce0 100644 (file)
@@ -135,4 +135,4 @@ def invoke_pacman(state: MkosiState, packages: Sequence[str], apivfs: bool = Tru
 
     bwrap(cmdline,
           apivfs=state.root if apivfs else None,
-          env=dict(KERNEL_INSTALL_BYPASS="1") | state.environment)
+          env=dict(KERNEL_INSTALL_BYPASS="1") | state.config.environment)
index febaf57812439b45f93c3033ab7a8a23d63b7379..f95a1da1bb956f4ba87e6684410a76a15b1425c6 100644 (file)
@@ -245,7 +245,7 @@ def invoke_apt(
 
     bwrap(["apt-get", *options, operation, *packages],
           apivfs=state.root if apivfs else None,
-          env=env | state.environment)
+          env=env | state.config.environment)
 
 
 def install_apt_sources(state: MkosiState, repos: Sequence[str]) -> None:
index 1d466dd3f2bf220a6c9bd538bd65f332e48a4bb5..9dab7e6905a30f2504d3c4f898a15b86b8555ba8 100644 (file)
@@ -220,7 +220,7 @@ def invoke_dnf(
 
     bwrap(cmdline,
           apivfs=state.root if apivfs else None,
-          env=dict(KERNEL_INSTALL_BYPASS="1") | env | state.environment)
+          env=dict(KERNEL_INSTALL_BYPASS="1") | env | state.config.environment)
 
     fixup_rpmdb_location(state.root)
 
index 7547deb448edfedf894a6e909514e3a48214c1a6..aa742af7e6209dcba821c37fb7180862cffb0980 100644 (file)
@@ -67,7 +67,7 @@ def invoke_emerge(
                 "parallel-install",
                 *(["noman", "nodoc", "noinfo"] if state.config.with_docs else []),
             ]),
-        ) | env | state.environment,
+        ) | env | state.config.environment,
     )
 
 
index ebfa1faa55a7711c11a8f92efd5bb847b06f45ce..afca52dc849113230c570f52a5a22715313d9c95 100644 (file)
@@ -141,7 +141,7 @@ def invoke_zypper(
 
     bwrap(cmdline,
           apivfs=state.root if apivfs else None,
-          env=dict(ZYPP_CONF=str(state.pkgmngr / "etc/zypp/zypp.conf"), KERNEL_INSTALL_BYPASS="1") | state.environment)
+          env=dict(ZYPP_CONF=str(state.pkgmngr / "etc/zypp/zypp.conf"), KERNEL_INSTALL_BYPASS="1") | state.config.environment)
 
     fixup_rpmdb_location(state.root)
 
index 79323ea5d8b2043ee23eb60afc20bbae70fa5d9c..f83b74ba23ad9692a07c85e263b1890d2bf3b568 100644 (file)
@@ -1,7 +1,6 @@
 # SPDX-License-Identifier: LGPL-2.1+
 
 import importlib
-import os
 import tempfile
 from pathlib import Path
 
@@ -20,16 +19,6 @@ class MkosiState:
 
         self._workspace = tempfile.TemporaryDirectory(dir=config.workspace_dir or Path.cwd(), prefix=".mkosi.tmp")
 
-        self.environment = self.config.environment.copy()
-        if self.config.image_id is not None:
-            self.environment["IMAGE_ID"] = self.config.image_id
-        if self.config.image_version is not None:
-            self.environment["IMAGE_VERSION"] = self.config.image_version
-        if (proxy := os.environ.get("http_proxy")):
-            self.environment["http_proxy"] = proxy
-        if (proxy := os.environ.get("https_proxy")):
-            self.environment["https_proxy"] = proxy
-
         try:
             distro = str(self.config.distribution)
             mod = importlib.import_module(f"mkosi.distributions.{distro}")