From a7ea828f09d5b227cab62224422416fda108b57e Mon Sep 17 00:00:00 2001 From: Frederik Rietdijk Date: Sat, 13 Jun 2020 09:12:26 +0200 Subject: [PATCH 1/2] pythonRemoveBinBytecodeHook: fix explanation --- .../python/hooks/python-remove-bin-bytecode-hook.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkgs/development/interpreters/python/hooks/python-remove-bin-bytecode-hook.sh b/pkgs/development/interpreters/python/hooks/python-remove-bin-bytecode-hook.sh index 2add23f2316..1180694294d 100644 --- a/pkgs/development/interpreters/python/hooks/python-remove-bin-bytecode-hook.sh +++ b/pkgs/development/interpreters/python/hooks/python-remove-bin-bytecode-hook.sh @@ -1,9 +1,9 @@ -# Setup hook for detecting conflicts in Python packages +# Setup hook for removing bytecode from the bin folder echo "Sourcing python-remove-bin-bytecode-hook.sh" -# Check if we have two packages with the same name in the closure and fail. -# If this happens, something went wrong with the dependencies specs. -# Intentionally kept in a subdirectory, see catch_conflicts/README.md. +# The bin folder is added to $PATH and should only contain executables. +# It may happen there are executables with a .py extension for which +# bytecode is generated. This hook removes that bytecode. pythonRemoveBinBytecodePhase () { if [ -d "$out/bin" ]; then From 818cf7827bd5950a41cdc17db81f19ee2cbcc851 Mon Sep 17 00:00:00 2001 From: Frederik Rietdijk Date: Fri, 12 Jun 2020 21:28:27 +0200 Subject: [PATCH 2/2] buildPythonPackage: recompile bytecode for reproducibility Due to a change in pip the unpacked wheels are no longer reproducible. We recompile the bytecode to cleanup this error. https://github.com/NixOS/nixpkgs/issues/81441 --- .../interpreters/python/hooks/default.nix | 11 +++++++++ .../hooks/python-recompile-bytecode-hook.sh | 24 +++++++++++++++++++ .../python/mk-python-derivation.nix | 2 ++ pkgs/top-level/python-packages.nix | 1 + 4 files changed, 38 insertions(+) create mode 100644 pkgs/development/interpreters/python/hooks/python-recompile-bytecode-hook.sh diff --git a/pkgs/development/interpreters/python/hooks/default.nix b/pkgs/development/interpreters/python/hooks/default.nix index 4d736426f3b..d14eb9cbb09 100644 --- a/pkgs/development/interpreters/python/hooks/default.nix +++ b/pkgs/development/interpreters/python/hooks/default.nix @@ -1,5 +1,6 @@ # Hooks for building Python packages. { python +, lib , callPackage , makeSetupHook , disabledIf @@ -97,6 +98,16 @@ in rec { }; } ./python-namespaces-hook.sh) {}; + pythonRecompileBytecodeHook = callPackage ({ }: + makeSetupHook { + name = "python-recompile-bytecode-hook"; + substitutions = { + inherit pythonInterpreter pythonSitePackages; + compileArgs = lib.concatStringsSep " " (["-q" "-f" "-i -"] ++ lib.optionals isPy3k ["-j $NIX_BUILD_CORES"]); + bytecodeName = if isPy3k then "__pycache__" else "*.pyc"; + }; + } ./python-recompile-bytecode-hook.sh ) {}; + pythonRemoveBinBytecodeHook = callPackage ({ }: makeSetupHook { name = "python-remove-bin-bytecode-hook"; diff --git a/pkgs/development/interpreters/python/hooks/python-recompile-bytecode-hook.sh b/pkgs/development/interpreters/python/hooks/python-recompile-bytecode-hook.sh new file mode 100644 index 00000000000..649d0c17ea0 --- /dev/null +++ b/pkgs/development/interpreters/python/hooks/python-recompile-bytecode-hook.sh @@ -0,0 +1,24 @@ +# Setup hook for recompiling bytecode. +# https://github.com/NixOS/nixpkgs/issues/81441 +echo "Sourcing python-recompile-bytecode-hook.sh" + +# Remove all bytecode from the $out output. Then, recompile only site packages folder +# Note this effectively duplicates `python-remove-bin-bytecode`, but long-term +# this hook should be removed again. + +pythonRecompileBytecodePhase () { + # TODO: consider other outputs than $out + + items="$(find "$out" -name "@bytecodeName@")" + if [[ -n $items ]]; then + for pycache in $items; do + rm -rf "$pycache" + done + fi + + find "$out"/@pythonSitePackages@ -name "*.py" -exec @pythonInterpreter@ -OO -m compileall @compileArgs@ {} + +} + +if [ -z "${dontUsePythonRecompileBytecode-}" ]; then + postPhases+=" pythonRecompileBytecodePhase" +fi diff --git a/pkgs/development/interpreters/python/mk-python-derivation.nix b/pkgs/development/interpreters/python/mk-python-derivation.nix index 22938a45585..41efddbb3e0 100644 --- a/pkgs/development/interpreters/python/mk-python-derivation.nix +++ b/pkgs/development/interpreters/python/mk-python-derivation.nix @@ -17,6 +17,7 @@ , pythonCatchConflictsHook , pythonImportsCheckHook , pythonNamespacesHook +, pythonRecompileBytecodeHook , pythonRemoveBinBytecodeHook , pythonRemoveTestsDirHook , setuptoolsBuildHook @@ -110,6 +111,7 @@ let python wrapPython ensureNewerSourcesForZipFilesHook # move to wheel installer (pip) or builder (setuptools, flit, ...)? + pythonRecompileBytecodeHook # Remove when solved https://github.com/NixOS/nixpkgs/issues/81441 pythonRemoveTestsDirHook ] ++ lib.optionals catchConflicts [ setuptools pythonCatchConflictsHook diff --git a/pkgs/top-level/python-packages.nix b/pkgs/top-level/python-packages.nix index 0aa27ff933e..f68b9482aa3 100644 --- a/pkgs/top-level/python-packages.nix +++ b/pkgs/top-level/python-packages.nix @@ -118,6 +118,7 @@ in { pythonCatchConflictsHook pythonImportsCheckHook pythonNamespacesHook + pythonRecompileBytecodeHook pythonRemoveBinBytecodeHook pythonRemoveTestsDirHook setuptoolsBuildHook