]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Add BuildSourcesEphemeral= 2046/head
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 6 Nov 2023 20:35:09 +0000 (21:35 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 7 Nov 2023 08:18:28 +0000 (09:18 +0100)
Unfortunately there are use cases where it's useful to persist
changes to the source directory. A prime example is projects with
Makefile that do not provide a properly functioning `make install`.
The only way to use those with mkosi build scripts is to do an
in-tree build in the build script and then mounting the source
directory into the VM or container when booting it. For this to work
we need changes to the source directory to be persisted. To support
this use case, let's gate the ephemeral source directories behind an
option that's disabled by default (for backwards compatibitity reasons).

NEWS.md
mkosi/__init__.py
mkosi/config.py
mkosi/resources/mkosi.md
tests/test_json.py

diff --git a/NEWS.md b/NEWS.md
index 49a0e0f0b6a4f846ff3e50ae7a9c3c5d70ebc4ea..b30e43e9b617be118e7ef5bb59c16f69902d681c 100644 (file)
--- a/NEWS.md
+++ b/NEWS.md
   `-kernel` or `QemuKernel=`
 - We don't create subdirectories beneath the configured cache directory
   anymore.
-- Source directories are now made ephemeral when running scripts. This
-  means any changes made to source directories while running scripts
-  will be undone after the scripts have finished executing.
+- Added `BuildSourcesEphemeral=` to make source directories ephemeral
+  when running scripts. This means any changes made to source
+  directories while running scripts will be undone after the scripts
+  have finished executing.
 - Workspace directories are now created outside of any source
   directories. mkosi will either use `XDG_CACHE_HOME`, `$HOME/.cache` or
   `/var/tmp` depending on the situation.
index adcf854d58f1661f556be25223872250320a7360..b6329b9b83eb3739d850fcfecb89f12be6075338 100644 (file)
@@ -325,7 +325,7 @@ def mount_build_overlay(state: MkosiState, volatile: bool = False) -> Iterator[P
 def finalize_mounts(config: MkosiConfig) -> Iterator[list[PathString]]:
     with contextlib.ExitStack() as stack:
         sources = [
-            (stack.enter_context(mount_overlay([source])), target)
+            (stack.enter_context(mount_overlay([source])) if config.build_sources_ephemeral else source, target)
             for source, target
             in [(Path.cwd(), Path.cwd())] + [t.with_prefix(Path.cwd()) for t in config.build_sources]
         ]
index d2f620d09a9d99767e8b6c0f477e132eebe03908..fbbae311bda571f4ae7dbe64926ef35d6fc2a747 100644 (file)
@@ -776,6 +776,7 @@ class MkosiConfig:
     postinst_scripts: list[Path]
     finalize_scripts: list[Path]
     build_sources: list[ConfigTree]
+    build_sources_ephemeral: bool
     environment: dict[str, str]
     with_tests: bool
     with_network: bool
@@ -1467,6 +1468,13 @@ SETTINGS = (
         match=config_match_build_sources,
         help="Path for sources to build",
     ),
+    MkosiConfigSetting(
+        dest="build_sources_ephemeral",
+        metavar="BOOL",
+        section="Content",
+        parse=config_parse_boolean,
+        help="Make build sources ephemeral when running scripts",
+    ),
     MkosiConfigSetting(
         dest="environment",
         short="-E",
@@ -2811,6 +2819,7 @@ Clean Package Manager Metadata: {yes_no_auto(config.clean_package_metadata)}
            Postinstall Scripts: {line_join_list(config.postinst_scripts)}
               Finalize Scripts: {line_join_list(config.finalize_scripts)}
                  Build Sources: {line_join_tree_list(config.build_sources)}
+       Build Sources Ephemeral: {yes_no(config.build_sources_ephemeral)}
             Script Environment: {line_join_list(env)}
     Run Tests in Build Scripts: {yes_no(config.with_tests)}
           Scripts With Network: {yes_no(config.with_network)}
index 9c93c76fcc9d6d5aea184995aa809be5c20277d0..170e782365246b04d21bba2ade6837064ec7002f 100644 (file)
@@ -906,6 +906,13 @@ boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`,
   with all build sources mounted in it is mounted to `/work/src` inside
   the image's root directory.
 
+`BuildSourcesEphemeral=`, `--build-sources-ephemeral=`
+
+: Takes a boolean. Disabled by default. Configures whether changes to
+  source directories (The working directory and configured using
+  `BuildSources=`) are persisted. If enabled, all source directories
+  will be reset to their original state after scripts finish executing.
+
 `Environment=`, `--environment=`
 
 : Adds variables to the environment that package managers and the
index 7a80b539c3bcdb1b37578e7a0644785406d6462b..373aedee8e584d69b7f4b6218daf4dcd2797d609 100644 (file)
@@ -99,6 +99,7 @@ def test_config() -> None:
                     "target": "/frob"
                 }
             ],
+            "BuildSourcesEphemeral": true,
             "CacheDirectory": "/is/this/the/cachedir",
             "CacheOnly": true,
             "Checksum": false,
@@ -268,6 +269,7 @@ def test_config() -> None:
         build_packages =  ["pkg1", "pkg2"],
         build_scripts =  [Path("/path/to/buildscript")],
         build_sources = [ConfigTree(Path("/qux"), Path("/frob"))],
+        build_sources_ephemeral = True,
         cache_dir = Path("/is/this/the/cachedir"),
         cache_only =  True,
         checksum =  False,