Skip to content

Commit 4e2566e

Browse files
authored
mkosi: 22 -> 24.3-unstable-2024-08-28 (#338962)
2 parents 6cc3e27 + 67e42aa commit 4e2566e

4 files changed

Lines changed: 253 additions & 62 deletions

File tree

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
From eb36791f873dd645b1cbfa693b9c246943647190 Mon Sep 17 00:00:00 2001
2+
From: Moritz Sanft <58110325+msanft@users.noreply.github.com>
3+
Date: Tue, 3 Sep 2024 08:57:26 +0200
4+
Subject: [PATCH 1/3] Use wrapped binaries instead of Python interpreter
5+
6+
Rather 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+
8+
Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com>
9+
---
10+
mkosi/__init__.py | 19 ++++---------------
11+
mkosi/run.py | 8 ++++----
12+
2 files changed, 8 insertions(+), 19 deletions(-)
13+
14+
diff --git a/mkosi/__init__.py b/mkosi/__init__.py
15+
index cc8482c4..ba44ad31 100644
16+
--- a/mkosi/__init__.py
17+
+++ b/mkosi/__init__.py
18+
@@ -2059,16 +2059,7 @@ def join_initrds(initrds: Sequence[Path], output: Path) -> Path:
19+
20+
21+
def python_binary(config: Config, *, binary: Optional[PathString]) -> PathString:
22+
- tools = (
23+
- not binary or
24+
- not (path := config.find_binary(binary)) or
25+
- not any(path.is_relative_to(d) for d in config.extra_search_paths)
26+
- )
27+
-
28+
- # If there's no tools tree, prefer the interpreter from MKOSI_INTERPRETER. If there is a tools
29+
- # tree, just use the default python3 interpreter.
30+
- exe = Path(sys.executable)
31+
- return "python3" if (tools and config.tools_tree) or not exe.is_relative_to("/usr") else exe
32+
+ return "@PYTHON_PEFILE@"
33+
34+
35+
def extract_pe_section(context: Context, binary: Path, section: str, output: Path) -> Path:
36+
@@ -2135,11 +2126,10 @@ def build_uki(
37+
if not (arch := context.config.architecture.to_efi()):
38+
die(f"Architecture {context.config.architecture} does not support UEFI")
39+
40+
- if not (ukify := context.config.find_binary("ukify", "/usr/lib/systemd/ukify")):
41+
+ if not (ukify := context.config.find_binary("ukify", "@UKIFY@")):
42+
die("Could not find ukify")
43+
44+
cmd: list[PathString] = [
45+
- python_binary(context.config, binary=ukify),
46+
ukify,
47+
*(["--cmdline", f"@{context.workspace / 'cmdline'}"] if cmdline else []),
48+
"--os-release", f"@{context.root / 'usr/lib/os-release'}",
49+
@@ -2213,7 +2203,6 @@ def build_uki(
50+
# new .ucode section support?
51+
if (
52+
systemd_tool_version(
53+
- python_binary(context.config, binary=ukify),
54+
ukify,
55+
sandbox=context.sandbox,
56+
) >= "256" and
57+
@@ -2303,7 +2292,7 @@ def want_uki(context: Context) -> bool:
58+
context.config.unified_kernel_images == ConfigFeature.enabled or (
59+
context.config.unified_kernel_images == ConfigFeature.auto and
60+
systemd_stub_binary(context).exists() and
61+
- context.config.find_binary("ukify", "/usr/lib/systemd/ukify") is not None
62+
+ context.config.find_binary("ukify", "@UKIFY@") is not None
63+
)
64+
)
65+
66+
@@ -2914,7 +2903,7 @@ def check_ukify(
67+
reason: str,
68+
hint: Optional[str] = None,
69+
) -> None:
70+
- ukify = check_tool(config, "ukify", "/usr/lib/systemd/ukify", reason=reason, hint=hint)
71+
+ ukify = check_tool(config, "ukify", "@UKIFY@", reason=reason, hint=hint)
72+
73+
v = systemd_tool_version(python_binary(config, binary=ukify), ukify, sandbox=config.sandbox)
74+
if v < version:
75+
diff --git a/mkosi/run.py b/mkosi/run.py
76+
index fd3bc98e..de47349a 100644
77+
--- a/mkosi/run.py
78+
+++ b/mkosi/run.py
79+
@@ -450,7 +450,7 @@ def sandbox_cmd(
80+
) -> Iterator[list[PathString]]:
81+
cmdline: list[PathString] = [
82+
*setup,
83+
- sys.executable, "-SI", mkosi.sandbox.__file__,
84+
+ @MKOSI_SANDBOX@,
85+
"--proc", "/proc",
86+
# We mounted a subdirectory of TMPDIR to /var/tmp so we unset TMPDIR so that /tmp or /var/tmp are used instead.
87+
"--unsetenv", "TMPDIR",
88+
@@ -563,7 +563,7 @@ def apivfs_options(*, root: Path = Path("/buildroot")) -> list[PathString]:
89+
def apivfs_script_cmd(*, tools: bool, options: Sequence[PathString] = ()) -> list[PathString]:
90+
exe = Path(sys.executable)
91+
return [
92+
- "python3" if tools or not exe.is_relative_to("/usr") else exe, "-SI", "/sandbox.py",
93+
+ @MKOSI_SANDBOX@,
94+
"--bind", "/", "/",
95+
"--same-dir",
96+
"--bind", "/var/tmp", "/buildroot/var/tmp",
97+
@@ -597,7 +597,7 @@ def chroot_cmd(
98+
options: Sequence[PathString] = (),
99+
) -> Iterator[list[PathString]]:
100+
cmdline: list[PathString] = [
101+
- sys.executable, "-SI", mkosi.sandbox.__file__,
102+
+ @MKOSI_SANDBOX@,
103+
"--bind", root, "/",
104+
# We mounted a subdirectory of TMPDIR to /var/tmp so we unset TMPDIR so that /tmp or /var/tmp are used instead.
105+
"--unsetenv", "TMPDIR",
106+
@@ -619,7 +619,7 @@ def chroot_cmd(
107+
def chroot_script_cmd(*, tools: bool, network: bool = False, work: bool = False) -> list[PathString]:
108+
exe = Path(sys.executable)
109+
return [
110+
- "python3" if tools or not exe.is_relative_to("/usr") else exe, "-SI", "/sandbox.py",
111+
+ @MKOSI_SANDBOX@,
112+
"--bind", "/buildroot", "/",
113+
"--bind", "/var/tmp", "/var/tmp",
114+
*apivfs_options(root=Path("/")),
115+
--
116+
2.45.2
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
From a1e6ccfeaf8ef10361280b9ecad958e9d556005b Mon Sep 17 00:00:00 2001
2+
From: Moritz Sanft <58110325+msanft@users.noreply.github.com>
3+
Date: Tue, 3 Sep 2024 09:00:34 +0200
4+
Subject: [PATCH 2/3] Fix library resolving
5+
6+
As ctypes doesn't do lookups in the Nix store for libraries, we supply the exact paths.
7+
8+
Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com>
9+
---
10+
mkosi/sandbox/__init__.py | 4 ++--
11+
1 file changed, 2 insertions(+), 2 deletions(-)
12+
13+
diff --git a/mkosi/sandbox/__init__.py b/mkosi/sandbox/__init__.py
14+
index 7db340c5..3d0a0e56 100644
15+
--- a/mkosi/sandbox/__init__.py
16+
+++ b/mkosi/sandbox/__init__.py
17+
@@ -78,7 +78,7 @@ class cap_user_data_t(ctypes.Structure):
18+
]
19+
20+
21+
-libc = ctypes.CDLL(None, use_errno=True)
22+
+libc = ctypes.CDLL("@LIBC@", use_errno=True)
23+
24+
libc.syscall.restype = ctypes.c_long
25+
libc.unshare.argtypes = (ctypes.c_int,)
26+
@@ -175,7 +175,7 @@ def seccomp_suppress_chown() -> None:
27+
Unfortunately, non-root users can only create files owned by their own uid. To still allow non-root users to build
28+
images, if requested we install a seccomp filter that makes calls to chown() and friends a noop.
29+
"""
30+
- libseccomp = ctypes.CDLL("libseccomp.so.2")
31+
+ libseccomp = ctypes.CDLL("@LIBSECCOMP@")
32+
if libseccomp is None:
33+
raise FileNotFoundError("libseccomp.so.2")
34+
35+
--
36+
2.45.2
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
From e834d51aa2542b141ceafdd42285ded6a9997c90 Mon Sep 17 00:00:00 2001
2+
From: Moritz Sanft <58110325+msanft@users.noreply.github.com>
3+
Date: Tue, 3 Sep 2024 09:09:19 +0200
4+
Subject: [PATCH 3/3] Fix QEMU firmware path
5+
6+
Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com>
7+
---
8+
mkosi/qemu.py | 2 +-
9+
1 file changed, 1 insertion(+), 1 deletion(-)
10+
11+
diff --git a/mkosi/qemu.py b/mkosi/qemu.py
12+
index b98bec65..886598aa 100644
13+
--- a/mkosi/qemu.py
14+
+++ b/mkosi/qemu.py
15+
@@ -182,7 +182,7 @@ def find_ovmf_firmware(config: Config, qemu: Path, firmware: QemuFirmware) -> Op
16+
17+
tools = Path("/") if any(qemu.is_relative_to(d) for d in config.extra_search_paths) else config.tools()
18+
19+
- desc = list((tools / "usr/share/qemu/firmware").glob("*"))
20+
+ desc = list((tools / "@QEMU_FIRMWARE@").glob("*"))
21+
if tools == Path("/"):
22+
desc += list((tools / "etc/qemu/firmware").glob("*"))
23+
24+
--
25+
2.45.2

pkgs/tools/virtualization/mkosi/default.nix

Lines changed: 76 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1-
{ lib
2-
, fetchFromGitHub
3-
, stdenv
4-
, python3
5-
, bubblewrap
6-
, systemd
7-
, pandoc
8-
, kmod
9-
, gnutar
10-
, util-linux
11-
, cpio
12-
, bash
13-
, coreutils
14-
, btrfs-progs
1+
{
2+
lib,
3+
fetchFromGitHub,
4+
stdenv,
5+
python3,
6+
systemd,
7+
pandoc,
8+
kmod,
9+
gnutar,
10+
util-linux,
11+
cpio,
12+
bash,
13+
coreutils,
14+
btrfs-progs,
15+
libseccomp,
16+
replaceVars,
1517

1618
# Python packages
17-
, setuptools
18-
, setuptools-scm
19-
, wheel
20-
, buildPythonApplication
21-
, pytestCheckHook
22-
, pefile
19+
setuptools,
20+
setuptools-scm,
21+
wheel,
22+
buildPythonApplication,
23+
pytestCheckHook,
24+
pefile,
2325

2426
# Optional dependencies
25-
, withQemu ? false
26-
, qemu
27+
withQemu ? false,
28+
qemu,
2729
}:
2830
let
2931
# For systemd features used by mkosi, see
@@ -38,34 +40,51 @@ let
3840
withKernelInstall = true;
3941
};
4042

41-
python3pefile = python3.withPackages (ps: with ps; [
42-
pefile
43-
]);
43+
python3pefile = python3.withPackages (
44+
ps: with ps; [
45+
pefile
46+
]
47+
);
4448
in
4549
buildPythonApplication rec {
4650
pname = "mkosi";
47-
version = "22";
51+
version = "24.3-unstable-2024-08-28";
4852
format = "pyproject";
4953

50-
outputs = [ "out" "man" ];
54+
outputs = [
55+
"out"
56+
"man"
57+
];
5158

5259
src = fetchFromGitHub {
5360
owner = "systemd";
5461
repo = "mkosi";
55-
rev = "v${version}";
56-
hash = "sha256-Zom1GlyhqgpTKfjcBOUEJMlubSn+TQsk97js1/UfDHY=";
62+
rev = "8c2f828701a1bdb3dc9b80d6f2ab979f0430a6b8";
63+
hash = "sha256-rO/4ki2nAJQN2slmYuHKESGBBDMXC/ikGf6dMDcKFr4=";
5764
};
5865

59-
# Fix ctypes finding library
60-
# https://github.com/NixOS/nixpkgs/issues/7307
61-
postPatch = lib.optionalString stdenv.isLinux ''
62-
substituteInPlace mkosi/user.py \
63-
--replace-fail 'ctypes.util.find_library("c")' "'${stdenv.cc.libc}/lib/libc.so.6'"
64-
substituteInPlace mkosi/__init__.py \
65-
--replace-fail '/usr/lib/systemd/ukify' "${systemdForMkosi}/lib/systemd/ukify"
66-
'' + lib.optionalString withQemu ''
67-
substituteInPlace mkosi/qemu.py \
68-
--replace-fail "usr/share/qemu/firmware" "${qemu}/share/qemu/firmware"
66+
patches =
67+
[
68+
(replaceVars ./0001-Use-wrapped-binaries-instead-of-Python-interpreter.patch {
69+
UKIFY = "${systemdForMkosi}/lib/systemd/ukify";
70+
PYTHON_PEFILE = "${python3pefile}/bin/python3.12";
71+
MKOSI_SANDBOX = "~MKOSI_SANDBOX~"; # to satisfy replaceVars, will be replaced in postPatch
72+
})
73+
(replaceVars ./0002-Fix-library-resolving.patch {
74+
LIBC = "${stdenv.cc.libc}/lib/libc.so.6";
75+
LIBSECCOMP = "${libseccomp.lib}/lib/libseccomp.so.2";
76+
})
77+
]
78+
++ lib.optional withQemu (
79+
replaceVars ./0003-Fix-QEMU-firmware-path.patch {
80+
QEMU_FIRMWARE = "${qemu}/share/qemu/firmware";
81+
}
82+
);
83+
84+
postPatch = ''
85+
# As we need the $out reference, we can't use `replaceVars` here.
86+
substituteInPlace mkosi/run.py \
87+
--replace-fail '~MKOSI_SANDBOX~' "\"$out/bin/mkosi-sandbox\""
6988
'';
7089

7190
nativeBuildInputs = [
@@ -75,19 +94,20 @@ buildPythonApplication rec {
7594
wheel
7695
];
7796

78-
propagatedBuildInputs = [
79-
bash
80-
btrfs-progs
81-
bubblewrap
82-
coreutils
83-
cpio
84-
gnutar
85-
kmod
86-
systemdForMkosi
87-
util-linux
88-
] ++ lib.optional withQemu [
89-
qemu
90-
];
97+
propagatedBuildInputs =
98+
[
99+
bash
100+
btrfs-progs
101+
coreutils
102+
cpio
103+
gnutar
104+
kmod
105+
systemdForMkosi
106+
util-linux
107+
]
108+
++ lib.optional withQemu [
109+
qemu
110+
];
91111

92112
postBuild = ''
93113
./tools/make-man-page.sh
@@ -97,27 +117,21 @@ buildPythonApplication rec {
97117
pytestCheckHook
98118
];
99119

100-
pythonImportsCheck = [
101-
"mkosi"
102-
];
103-
104120
postInstall = ''
105121
mkdir -p $out/share/man/man1
106122
mv mkosi/resources/mkosi.1 $out/share/man/man1/
107123
'';
108124

109-
makeWrapperArgs = [
110-
"--set MKOSI_INTERPRETER ${python3pefile}/bin/python3"
111-
"--prefix PYTHONPATH : \"$PYTHONPATH\""
112-
];
113-
114125
meta = with lib; {
115126
description = "Build legacy-free OS images";
116127
homepage = "https://github.com/systemd/mkosi";
117128
changelog = "https://github.com/systemd/mkosi/releases/tag/v${version}";
118129
license = licenses.lgpl21Only;
119130
mainProgram = "mkosi";
120-
maintainers = with maintainers; [ malt3 ];
131+
maintainers = with maintainers; [
132+
malt3
133+
msanft
134+
];
121135
platforms = platforms.linux;
122136
# `mkosi qemu` boot fails in the uefi shell, image isn't found.
123137
broken = withQemu;

0 commit comments

Comments
 (0)