1From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2From: Moritz Sanft <58110325+msanft@users.noreply.github.com>
3Date: Tue, 3 Sep 2024 08:57:26 +0200
4Subject: [PATCH] Use wrapped binaries instead of Python interpreter
5
6Rather than calling ukify and mkosi with sys.executable, which doesn't use the Python wrappers for PATH and PYTHONPATH, we call the wrapped binaries directly.
7
8Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com>
9---
10 mkosi/__init__.py | 11 +++++------
11 mkosi/bootloader.py | 5 +----
12 mkosi/run.py | 8 ++++----
13 3 files changed, 10 insertions(+), 14 deletions(-)
14
15diff --git a/mkosi/__init__.py b/mkosi/__init__.py
16index 65cac772bf1fc9feabec5740ed89a958ba406125..12b29061c819d50559132aa6c3b6f24a12945bb9 100644
17--- a/mkosi/__init__.py
18+++ b/mkosi/__init__.py
19@@ -702,7 +702,7 @@ def script_maybe_chroot_sandbox(
20
21 helpers = {
22 "mkosi-chroot": [
23- finalize_interpreter(bool(context.config.tools_tree)), "-SI", "/sandbox.py",
24+ @MKOSI_SANDBOX@,
25 "--bind", "/buildroot", "/",
26 "--bind", "/var/tmp", "/var/tmp",
27 *apivfs_options(root=Path("/")),
28@@ -1593,7 +1593,7 @@ def run_ukify(
29 sign: bool = True,
30 json_out: bool = False,
31 ) -> dict[str, Any]:
32- ukify = context.config.find_binary("ukify", "/usr/lib/systemd/ukify")
33+ ukify = context.config.find_binary("ukify", "@UKIFY@")
34 if not ukify:
35 die("Could not find ukify")
36
37@@ -1605,7 +1605,6 @@ def run_ukify(
38 (context.workspace / "cmdline").write_text(f"{' '.join(cmdline)}\x00")
39
40 cmd = [
41- python_binary(context.config),
42 ukify,
43 "build",
44 *arguments,
45@@ -1700,7 +1699,7 @@ def build_uki(
46 profiles: Sequence[Path],
47 output: Path,
48 ) -> dict[str, Any]:
49- if not (ukify := context.config.find_binary("ukify", "/usr/lib/systemd/ukify")):
50+ if not (ukify := context.config.find_binary("ukify", "@UKIFY@")):
51 die("Could not find ukify")
52
53 json_out = False
54@@ -1887,7 +1886,7 @@ def want_uki(context: Context) -> bool:
55 or (
56 context.config.unified_kernel_images == ConfigFeature.auto
57 and systemd_stub_binary(context).exists()
58- and context.config.find_binary("ukify", "/usr/lib/systemd/ukify") is not None
59+ and context.config.find_binary("ukify", "@UKIFY@") is not None
60 )
61 )
62
63@@ -2769,7 +2768,7 @@ def check_ukify(
64 reason: str,
65 hint: Optional[str] = None,
66 ) -> None:
67- ukify = check_tool(config, "ukify", "/usr/lib/systemd/ukify", reason=reason, hint=hint)
68+ ukify = check_tool(config, "ukify", "@UKIFY@", reason=reason, hint=hint)
69
70 v = systemd_tool_version(python_binary(config), ukify, sandbox=config.sandbox)
71 if v < version:
72diff --git a/mkosi/bootloader.py b/mkosi/bootloader.py
73index 6f112b854f72a8863dc5e7348f0154851d3dda96..8fdf2c5df7950c032bfcd36d89f7824e86ec9173 100644
74--- a/mkosi/bootloader.py
75+++ b/mkosi/bootloader.py
76@@ -268,10 +268,7 @@ def find_signed_grub_image(context: Context) -> Optional[Path]:
77
78
79 def python_binary(config: Config) -> PathString:
80- # If there's no tools tree, prefer the interpreter from MKOSI_INTERPRETER. If there is a tools
81- # tree, just use the default python3 interpreter.
82- exe = Path(sys.executable)
83- return "python3" if config.tools_tree or not exe.is_relative_to("/usr") else exe
84+ return "@PYTHON_PEFILE@"
85
86
87 def extract_pe_section(context: Context, binary: Path, section: str, output: Path) -> Path:
88diff --git a/mkosi/run.py b/mkosi/run.py
89index 422006d889802182d7e2f1734b2c342318583e7b..b9a1490bcd7780fea75d834e3ea4fb9a7033cc51 100644
90--- a/mkosi/run.py
91+++ b/mkosi/run.py
92@@ -277,7 +277,7 @@ def finalize_path(
93 # Make sure that /usr/bin and /usr/sbin are always in $PATH.
94 path += [s for s in ("/usr/bin", "/usr/sbin") if s not in path]
95 else:
96- path += ["/usr/bin", "/usr/sbin"]
97+ path += ["/usr/bin", "/usr/sbin", "@NIX_PATH@"]
98
99 if prefix_usr:
100 path = [os.fspath(root / s.lstrip("/")) if s in ("/usr/bin", "/usr/sbin") else s for s in path]
101@@ -463,7 +463,7 @@ def sandbox_cmd(
102 cmdline: list[PathString] = [
103 *setup,
104 *(["strace", "--detach-on=execve"] if ARG_DEBUG_SANDBOX.get() else []),
105- sys.executable, "-SI", module / "sandbox.py",
106+ @MKOSI_SANDBOX@,
107 "--proc", "/proc",
108 # We mounted a subdirectory of TMPDIR to /var/tmp so we unset TMPDIR so that /tmp or /var/tmp are
109 # used instead.
110@@ -633,7 +633,7 @@ def chroot_options() -> list[PathString]:
111 "--unshare-ipc",
112 "--setenv", "container", "mkosi",
113 "--setenv", "HOME", "/",
114- "--setenv", "PATH", "/usr/bin:/usr/sbin",
115+ "--setenv", "PATH", "/usr/bin:/usr/sbin:@NIX_PATH@",
116 "--setenv", "BUILDROOT", "/",
117 ] # fmt: skip
118
119@@ -647,7 +647,7 @@ def chroot_cmd(
120 ) -> Iterator[list[PathString]]:
121 with vartmpdir() as dir, resource_path(sys.modules[__package__ or __name__]) as module:
122 cmdline: list[PathString] = [
123- sys.executable, "-SI", module / "sandbox.py",
124+ @MKOSI_SANDBOX@,
125 *root("/"),
126 # We mounted a subdirectory of TMPDIR to /var/tmp so we unset TMPDIR so that /tmp or /var/tmp are
127 # used instead.