From e4a98cf325990113185ae615fb5cf75235b749a7 Mon Sep 17 00:00:00 2001 From: Sem Mulder Date: Mon, 9 May 2022 17:27:24 +0200 Subject: [PATCH] Reset PYTHONPATH before running Python script in hooks --- hooks/default.nix | 64 +++++++++++++--------------- hooks/remove-special-dependencies.sh | 7 ++- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/hooks/default.nix b/hooks/default.nix index 20f1756..f6d1ea5 100644 --- a/hooks/default.nix +++ b/hooks/default.nix @@ -9,44 +9,40 @@ let callPackage = python.pythonForBuild.pkgs.callPackage; pythonInterpreter = python.pythonForBuild.interpreter; pythonSitePackages = python.sitePackages; + nonOverlayedPython = pkgs.python3.pythonForBuild.withPackages (ps: [ ps.tomlkit ]); + makeRemoveSpecialDependenciesHook = { fields, kind }: + nonOverlayedPython.pkgs.callPackage + ( + {}: + makeSetupHook + { + name = "remove-path-dependencies.sh"; + deps = [ ]; + substitutions = { + # NOTE: We have to use a non-overlayed Python here because otherwise we run into an infinite recursion + # because building of tomlkit and its dependencies also use these hooks. + pythonPath = nonOverlayedPython.pkgs.makePythonPath [ nonOverlayedPython ]; + pythonInterpreter = nonOverlayedPython.interpreter; + pyprojectPatchScript = "${./pyproject-without-special-deps.py}"; + fields = fields; + kind = kind; + }; + } ./remove-special-dependencies.sh + ) + { }; in { - # NOTE: We have to use a non-overlayed Python here because otherwise we run into an infinite recursion - # because building of tomlkit and its dependencies also use these hooks. - removePathDependenciesHook = nonOverlayedPython.pkgs.callPackage - ( - {}: - makeSetupHook - { - name = "remove-path-dependencies.sh"; - deps = [ ]; - substitutions = { - pythonInterpreter = nonOverlayedPython.interpreter; - pyprojectPatchScript = "${./pyproject-without-special-deps.py}"; - fields = [ "path" ]; - kind = "path"; - }; - } ./remove-special-dependencies.sh - ) - { }; + removePathDependenciesHook = makeRemoveSpecialDependenciesHook { + fields = [ "path" ]; + kind = "path"; + }; + + removeGitDependenciesHook = makeRemoveSpecialDependenciesHook { + fields = [ "git" "branch" "rev" "tag" ]; + kind = "git"; + }; - removeGitDependenciesHook = nonOverlayedPython.pkgs.callPackage - ( - {}: - makeSetupHook - { - name = "remove-git-dependencies.sh"; - deps = [ ]; - substitutions = { - pythonInterpreter = nonOverlayedPython.interpreter; - pyprojectPatchScript = "${./pyproject-without-special-deps.py}"; - fields = [ "git" "branch" "rev" "tag" ]; - kind = "git"; - }; - } ./remove-special-dependencies.sh - ) - { }; pipBuildHook = callPackage ( diff --git a/hooks/remove-special-dependencies.sh b/hooks/remove-special-dependencies.sh index a20b204..904efa6 100644 --- a/hooks/remove-special-dependencies.sh +++ b/hooks/remove-special-dependencies.sh @@ -1,11 +1,16 @@ remove-@kind@-dependencies-hook() { + # Tell poetry not to resolve special dependencies. Any version is fine! + if ! test -f pyproject.toml; then return fi echo "Removing @kind@ dependencies" - # Tell poetry not to resolve special dependencies. Any version is fine! + # NOTE: We have to reset PYTHONPATH to avoid having propagatedBuildInputs + # from the currently building derivation leaking into our unrelated Python + # environment. + PYTHONPATH=@pythonPath@ \ @pythonInterpreter@ \ @pyprojectPatchScript@ \ --fields-to-remove @fields@ < pyproject.toml > pyproject.formatted.toml