Repository navigation
Description
Summary
A pythonXX.withPackages environment passed to mkProjectShell via extraPackages is
silently shadowed by the builder's own bare python. The consumer gets the pinned 3.14
interpreter on PATH with none of their libraries, and there is no error or warning —
the shell builds and loads normally, and the breakage only surfaces later as
ModuleNotFoundError in an unrelated script.
This is the natural way to add Python libraries to a project shell (extraPackages is
documented as "your project tools go here" in the scaffolded flake.nix), so it is easy
to land in and hard to diagnose.
Reproduction
Minimal flake, no other project content, pinned to 3e0b8c3:
{
inputs.vigos.url = "github:vig-os/devkit/3e0b8c3620fdc8f2fe25cdabc1877d2d39cd34ec";
outputs = { self, vigos, ... }:
let
system = "x86_64-linux";
pkgs = import vigos.inputs.nixpkgs {
inherit system;
overlays = [ vigos.overlays.default ];
config.allowUnfree = true;
};
pyEnv = pkgs.python313.withPackages (ps: [ ps.numpy ]);
in {
devShells.${system} = {
a = vigos.lib.mkProjectShell { inherit pkgs; extraPackages = [ pyEnv ]; };
b = vigos.lib.mkProjectShell { inherit pkgs; python = pkgs.python313; extraPackages = [ pyEnv ]; };
c = vigos.lib.mkProjectShell { inherit pkgs; python = pyEnv; };
};
};
}$ nix develop .#a -c python3 -c 'import numpy'
ModuleNotFoundError: No module named 'numpy'| variant | config | python3 --version |
import numpy |
|---|---|---|---|
| a | extraPackages = [ pyEnv ] |
3.14.4 | ❌ ModuleNotFoundError |
| b | python = python313 + extraPackages |
3.13.13 | ✅ (but see caveat) |
| c | python = pyEnv |
3.13.13 | ✅ |
Root cause
mkProjectShell unconditionally appends the bare interpreter to packages
(flake.nix:549-555):
packages = (devTools pkgs) ++ [ python ] ++ extraPackages ++ modulePackages;python defaults to pkgs.python314 and precedes extraPackages, so its bin/python3
wins the buildEnv symlink race against the consumer's env.
pythonOverrideHook (flake.nix:524-526) exists to fix exactly this class of shadowing,
but it is gated on the interpreter having been overridden:
pythonOverrideHook = pkgs.lib.optionalString (python != pkgs.python314) ''
export PATH="${python}/bin:$PATH"
'';With the default interpreter the hook is empty, so a consumer who only wants to add
libraries to the pinned Python — without changing the interpreter version — has no
supported path at all.
Caveat on variant b
b appears to work but does so by accident. type -a python3 resolves to the bare
python3-3.13.13, not the -env:
python3 is /nix/store/…-python3-3.13.13/bin/python3 ← prepended by pythonOverrideHook
python3 is /nix/store/…-python3-3.14.4/bin/python3
python3 is /nix/store/…-python3-3.13.13/bin/python3
numpy imports only because nixpkgs' Python setup hook puts the env's site-packages on
PYTHONPATH, and the two happen to share an ABI. Change either side of that coincidence
(different CPython minor for the env vs. the override) and it breaks — silently again, in
the other direction. So the documented #1038 override is not a reliable fix for this case.
Suggested fixes
Any one of these would close it; roughly in order of preference:
- Let
extraPackageswin: orderextraPackagesbefore the barepythonin
packages, or applylib.lowPrioto the builder'spythonthe wayvig-utils
already is innix/devtools.nix:36. That makes the natural spelling correct. - Detect and fail loudly: if any entry of
extraPackagesprovidesbin/python3,
abort evaluation with a message pointing at thepythonargument. Silence is the
worst part of this bug. - Document
python = pyEnv(variant c) as the way to add Python libraries.
It works today, resolves the env directly rather than relying onPYTHONPATH, and
keepsUV_PYTHONconsistent — but nothing currently suggests passing awithPackages
env as thepythonargument, and the#1038comment reads as version-override only.
Happy to send a PR for whichever you prefer.
Environment
- devkit
3e0b8c3620fdc8f2fe25cdabc1877d2d39cd34ec - nixpkgs
3426825(viavigos/nixpkgs) - Determinate Nix 3.21.7 (Nix 2.34.8), x86_64-linux
Activity
test: assert extraPackages python env wins python3 on PATH
fix: let extraPackages python env win python3 on PATH
docs: changelog entry for extraPackages python shadowing fix
fix: let extraPackages python env win python3 on PATH (#1246)
c-vigo commentedon Jul 21, 2026
Fixed on dev via PR #1246 (merge commit ba0c770). Root cause differed from the proposal: the shadowing came from vig-utils' propagated pinned 3.14 interpreter via its Python setup hook, not from packages-list order — reordering/dropping the bare python did not fix it. The builder now PATH-prepends any extraPackages-provided withPackages env in the shellHook, before pythonOverrideHook so an explicit python override still wins. Proven by a real-build test (RED reproduced the ModuleNotFoundError). Ships with 1.4.1.
chore: release 1.4.1 (#297)
chore: release 1.4.1 (#1247)