From: Cornelius Hoffmann Date: Sat, 9 Mar 2024 12:27:57 +0000 (+0100) Subject: Make config available as a json file X-Git-Tag: v22~22 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=360b30278edefea1b648c77992dcdd42f307aabb;p=thirdparty%2Fmkosi.git Make config available as a json file Allows to access cli arguments and profile settings without reparsing the config in scripts --- diff --git a/mkosi/__init__.py b/mkosi/__init__.py index efdfda4e9..b0476ca5a 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -424,6 +424,7 @@ def run_sync_scripts(context: Context) -> None: SRCDIR="/work/src", MKOSI_UID=str(INVOKING_USER.uid), MKOSI_GID=str(INVOKING_USER.gid), + MKOSI_CONFIG="/work/config.json", CACHED=one_zero(have_cache(context.config)), ) @@ -440,6 +441,7 @@ def run_sync_scripts(context: Context) -> None: *sources, *finalize_crypto_mounts(context.config.tools()), "--ro-bind", script, "/work/sync", + "--ro-bind", context.workspace / "config.json", "/work/config.json", "--chdir", "/work/src", ] @@ -475,6 +477,7 @@ def run_prepare_scripts(context: Context, build: bool) -> None: CHROOT_SCRIPT="/work/prepare", MKOSI_UID=str(INVOKING_USER.uid), MKOSI_GID=str(INVOKING_USER.gid), + MKOSI_CONFIG="/work/config.json", WITH_DOCS=one_zero(context.config.with_docs), WITH_NETWORK=one_zero(context.config.with_network), WITH_TESTS=one_zero(context.config.with_tests), @@ -521,6 +524,7 @@ def run_prepare_scripts(context: Context, build: bool) -> None: network=True, options=sources + [ "--ro-bind", script, "/work/prepare", + "--ro-bind", context.workspace / "config.json", "/work/config.json", "--ro-bind", cd, "/work/scripts", "--bind", context.root, context.root, *context.config.distribution.package_manager(context.config).mounts(context), @@ -551,6 +555,7 @@ def run_build_scripts(context: Context) -> None: CHROOT_SCRIPT="/work/build-script", MKOSI_UID=str(INVOKING_USER.uid), MKOSI_GID=str(INVOKING_USER.gid), + MKOSI_CONFIG="/work/config.json", WITH_DOCS=one_zero(context.config.with_docs), WITH_NETWORK=one_zero(context.config.with_network), WITH_TESTS=one_zero(context.config.with_tests), @@ -599,6 +604,7 @@ def run_build_scripts(context: Context) -> None: network=context.config.with_network, options=sources + [ "--ro-bind", script, "/work/build-script", + "--ro-bind", context.workspace / "config.json", "/work/config.json", "--ro-bind", cd, "/work/scripts", "--bind", context.root, context.root, "--bind", context.install_dir, "/work/dest", @@ -638,6 +644,7 @@ def run_postinst_scripts(context: Context) -> None: PACKAGEDIR="/work/packages", MKOSI_UID=str(INVOKING_USER.uid), MKOSI_GID=str(INVOKING_USER.gid), + MKOSI_CONFIG="/work/config.json", ) with ( @@ -673,6 +680,7 @@ def run_postinst_scripts(context: Context) -> None: network=context.config.with_network, options=sources + [ "--ro-bind", script, "/work/postinst", + "--ro-bind", context.workspace / "config.json", "/work/config.json", "--ro-bind", cd, "/work/scripts", "--bind", context.root, context.root, "--bind", context.staging, "/work/out", @@ -702,6 +710,7 @@ def run_finalize_scripts(context: Context) -> None: CHROOT_SCRIPT="/work/finalize", MKOSI_UID=str(INVOKING_USER.uid), MKOSI_GID=str(INVOKING_USER.gid), + MKOSI_CONFIG="/work/config.json", ) with ( @@ -737,6 +746,7 @@ def run_finalize_scripts(context: Context) -> None: network=context.config.with_network, options=sources + [ "--ro-bind", script, "/work/finalize", + "--ro-bind", context.workspace / "config.json", "/work/config.json", "--ro-bind", cd, "/work/scripts", "--bind", context.root, context.root, "--bind", context.staging, "/work/out", @@ -3130,6 +3140,7 @@ def setup_workspace(args: Args, config: Config) -> Iterator[Path]: workspace = Path(tempfile.mkdtemp(dir=config.workspace_dir_or_default(), prefix="mkosi-workspace")) stack.callback(lambda: rmtree(workspace, sandbox=config.sandbox)) (workspace / "tmp").mkdir(mode=0o1777) + (workspace / "config.json").write_text(config.to_json()) with scopedenv({"TMPDIR" : os.fspath(workspace / "tmp")}): try: diff --git a/mkosi/resources/mkosi.md b/mkosi/resources/mkosi.md index 662bdd48c..2976271f7 100644 --- a/mkosi/resources/mkosi.md +++ b/mkosi/resources/mkosi.md @@ -2010,6 +2010,10 @@ Scripts executed by mkosi receive the following environment variables: with `setpriv` to run commands as the user that invoked mkosi (e.g. `setpriv --reuid=$MKOSI_UID --regid=$MKOSI_GID --clear-groups `) +* `$MKOSI_CONFIG` is a file containing a json summary of the settings of the + current image. This file can be parsed inside scripts to gain access to all + settings for the current image. + Consult this table for which script receives which environment variables: | Variable | `mkosi.sync` | `mkosi.prepare` | `mkosi.build` | `mkosi.postinst` | `mkosi.finalize` | @@ -2034,6 +2038,7 @@ Consult this table for which script receives which environment variables: | `SOURCE_DATE_EPOCH` | | X | X | X | X | | `MKOSI_UID` | X | X | X | X | X | | `MKOSI_GID` | X | X | X | X | X | +| `MKOSI_CONFIG` | X | X | X | X | X | Additionally, when a script is executed, a few scripts are made available via `$PATH` to simplify common usecases.