]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
mkosi: extend --environment to cover all scripts 765/head
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 7 Aug 2021 12:17:38 +0000 (14:17 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 9 Aug 2021 07:00:41 +0000 (09:00 +0200)
I would like to set environment variables for other scripts. But I
don't think adding separate options make sense: it should be fine to
just set the same environment variable for all scripts that are
invoked… After all, variables are best for "global" settings. Scripts
already get positional arguments that allow them to distinguish build
phases, so they don't need to use variables for this.

NEWS.md
mkosi.md
mkosi/__init__.py
mkosi/backend.py

diff --git a/NEWS.md b/NEWS.md
index 3e8a3459bcb664ce6b2c843325f8c25362a54f9c..78b59dd2c060fd2dcdb6734a05e390bf8dd132d0 100644 (file)
--- a/NEWS.md
+++ b/NEWS.md
@@ -1,5 +1,11 @@
 # mkosi Changelog
 
+## v11 (unreleased)
+
+- The `--build-environemnt` option was renamed to `--environment` and
+  extended to cover *all* invoked scripts, not just the `mkosi.build`.
+  The old name is still understood.
+
 ## v10
 
 - Minimum supported Python version is now 3.7.
index f31d0b2dce96cdbee781c455c1631acae93a22c9..1eacb632cedad1e379af20eefc572d171ac2825d 100644 (file)
--- a/mkosi.md
+++ b/mkosi.md
@@ -740,13 +740,14 @@ details see the table below.
 
 `--environment=`
 
-: Adds environment variables to the environment that the build script
-  is executed with. Takes a space-separated list of variable
-  assignments or just variable names. In the latter case, the values
-  of those variables will be passed through from the environment in
-  which `mkosi` was invoked. This option may be specified more than
-  once, in which case all listed variables will be set. If the same
-  variable is set twice, the later setting overrides the earlier one.
+: Adds variables to the environment that the
+  build/prepare/postinstall/finalize scripts are executed with. Takes
+  a space-separated list of variable assignments or just variable
+  names. In the latter case, the values of those variables will be
+  passed through from the environment in which `mkosi` was
+  invoked. This option may be specified more than once, in which case
+  all listed variables will be set. If the same variable is set twice,
+  the later setting overrides the earlier one.
 
 `--build-sources=`
 
index b17f15eafd813cea2e20eb8bc57363e239f7597a..a962c844d3a79bc03ad6c63f2989c1f60a611b44 100644 (file)
@@ -3098,13 +3098,15 @@ def nspawn_params_for_build_sources(args: CommandLineArguments, sft: SourceFileT
         params.append("--setenv=SRCDIR=/root/src")
         params.append("--chdir=/root/src")
         if sft == SourceFileTransfer.mount:
-            params.append("--bind=" + args.build_sources + ":/root/src")
+            params.append(f"--bind={args.build_sources}:/root/src")
 
         if args.read_only:
             params.append("--overlay=+/root/src::/root/src")
     else:
         params.append("--chdir=/root")
 
+    params.extend(f"--setenv={env}" for env in args.environment)
+
     return params
 
 
@@ -3180,7 +3182,8 @@ def run_finalize_script(args: CommandLineArguments, root: str, do_run_build_scri
     verb = "build" if do_run_build_script else "final"
 
     with complete_step("Running finalize script…"):
-        env = collections.ChainMap({"BUILDROOT": root, "OUTPUTDIR": output_dir(args)}, os.environ)
+        env = dict(cast(Tuple[str, str], v.split("=", maxsplit=1)) for v in args.environment)
+        env = collections.ChainMap(dict(BUILDROOT=root, OUTPUTDIR=output_dir(args)), env, os.environ)
         run([args.finalize_script, verb], env=env)
 
 
@@ -4814,7 +4817,7 @@ def create_parser() -> ArgumentParserMkosi:
         "-E",
         action=SpaceDelimitedListAction,
         default=[],
-        help="Set an environment variable when running the build script",
+        help="Set an environment variable when running scripts",
         metavar="NAME[=VALUE]",
     )
     group.add_argument(
@@ -6064,7 +6067,7 @@ def print_summary(args: CommandLineArguments) -> None:
     if args.remove_files:
         MkosiPrinter.info("              Remove Files: " + line_join_list(args.remove_files))
     MkosiPrinter.info("              Build Script: " + none_to_none(args.build_script))
-    MkosiPrinter.info("         Build Environment: " + line_join_list(args.environment))
+    MkosiPrinter.info("        Script Environment: " + line_join_list(args.environment))
 
     if args.build_script:
         MkosiPrinter.info("                 Run tests: " + yes_no(args.with_tests))
index cc8e4a655895655584bd126b3e99ac9f1ffc95c1..face357bf8d614edc2119f7b9b9d36dfb9ed9f81 100644 (file)
@@ -359,11 +359,15 @@ def nspawn_params_for_blockdev_access(args: CommandLineArguments, loopdev: str)
         "--bind-ro=/dev/block",
         "--bind-ro=/dev/disk",
     ]
+
     for partno in (args.esp_partno, args.bios_partno, args.root_partno, args.xbootldr_partno):
         if partno is not None:
             p = partition(loopdev, partno)
             if os.path.exists(p):
                 params += [f"--bind-ro={p}", f"--property=DeviceAllow={p}"]
+
+    params.extend(f"--setenv={env}" for env in args.environment)
+
     return params