From 19a989fdafe3b50b7c6629efa64e6e4b1fa0c31a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sat, 7 Aug 2021 14:17:38 +0200 Subject: [PATCH] mkosi: extend --environment to cover all scripts MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 | 6 ++++++ mkosi.md | 15 ++++++++------- mkosi/__init__.py | 11 +++++++---- mkosi/backend.py | 4 ++++ 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/NEWS.md b/NEWS.md index 3e8a3459b..78b59dd2c 100644 --- 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. diff --git a/mkosi.md b/mkosi.md index f31d0b2dc..1eacb632c 100644 --- 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=` diff --git a/mkosi/__init__.py b/mkosi/__init__.py index b17f15eaf..a962c844d 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -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)) diff --git a/mkosi/backend.py b/mkosi/backend.py index cc8e4a655..face357bf 100644 --- a/mkosi/backend.py +++ b/mkosi/backend.py @@ -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 -- 2.47.2