Skip to content

Commit 186e4e9

Browse files
JohnAZoidbergclaude
andcommitted
flake.nix: Fix Python environment for nix build
The build was failing because Python packages in nativeBuildInputs weren't available to the Python interpreter that CMake uses. This happened due to nixpkgs changes in how Python packages work. Changes: - Create bundled Python environment using python.withPackages - Add Python dependencies to zmake package so its sys.executable has access to required modules (pyyaml, pykwalify, etc.) - Patch zmake to use bundled Python for CMake's Python3_EXECUTABLE - Use --static flag to skip git version detection (sources don't have .git directories in nix build) - Explicitly set ZEPHYR_SDK_INSTALL_DIR and force zephyr toolchain - Update devShell to use bundled Python Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 21180a2 commit 186e4e9

1 file changed

Lines changed: 29 additions & 12 deletions

File tree

flake.nix

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@
3434
python = pkgs.python312;
3535
pythonPkgs = pkgs.python312Packages;
3636

37+
# Bundled Python with all required packages for the build
38+
buildPython = python.withPackages (ps: [
39+
ps.pyyaml
40+
ps.pykwalify
41+
ps.packaging
42+
ps.pyelftools
43+
ps.colorama
44+
ps.setuptools
45+
]);
46+
3747
setProjectDynamicToLicense = ''
3848
${pkgs.toml-cli}/bin/toml set pyproject.toml project.dynamic license | ${pkgs.moreutils}/bin/sponge pyproject.toml
3949
sed -e 's/dynamic = "license"/dynamic = ["license"]/' -i pyproject.toml
@@ -84,18 +94,17 @@
8494
pkgs.cmake
8595
pkgs.git
8696
pkgs.ninja
87-
pythonPkgs.pyyaml
88-
pythonPkgs.pykwalify
89-
pythonPkgs.packaging
90-
pythonPkgs.pyelftools
91-
pythonPkgs.colorama
97+
buildPython
9298
packages.binman
9399
];
94100

95101
dontUseCmakeConfigure = true;
96102

103+
# Ensure the zephyr SDK is found by zmake toolchain probing
104+
ZEPHYR_SDK_INSTALL_DIR = "${zephyr-sdk}";
105+
97106
buildPhase = ''
98-
${packages.zmake}/bin/zmake -j8 build ${build}
107+
${packages.zmake}/bin/zmake -j8 build --static -t zephyr ${build}
99108
'';
100109

101110
installPhase = ''
@@ -121,12 +130,25 @@
121130
pyproject = true;
122131
build-system = [pythonPkgs.setuptools];
123132

133+
# These dependencies are needed by zmake's Python (sys.executable)
134+
# which gets passed to CMake as Python3_EXECUTABLE
135+
dependencies = [
136+
pythonPkgs.pyyaml
137+
pythonPkgs.pykwalify
138+
pythonPkgs.packaging
139+
pythonPkgs.pyelftools
140+
pythonPkgs.colorama
141+
];
142+
124143
postPatch = ''
125144
sed -e 's#"/bin:/usr/bin"#"/bin:/usr/bin:${pkgs.gcc}/bin:${pkgs.dtc}/bin:${pkgs.ninja}/bin"${
126145
if pkgs.stdenv.hostPlatform.isDarwin
127146
then '',"DYLD_LIBRARY_PATH":"${pkgs.dtc}/lib"''
128147
else ""
129148
}#' -i zmake/jobserver.py
149+
150+
# Replace sys.executable with the bundled Python that has all required packages
151+
sed -e 's#"Python3_EXECUTABLE": sys.executable#"Python3_EXECUTABLE": "${buildPython}/bin/python3"#' -i zmake/zmake.py
130152
'';
131153
};
132154

@@ -206,12 +228,7 @@
206228
pkgs.cmake
207229
pkgs.git
208230
pkgs.ninja
209-
pythonPkgs.pyyaml
210-
pythonPkgs.pykwalify
211-
pythonPkgs.packaging
212-
pythonPkgs.pyelftools
213-
pythonPkgs.colorama
214-
pythonPkgs.setuptools
231+
buildPython
215232
packages.zmake
216233
packages.binman
217234
];

0 commit comments

Comments
 (0)