lol

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

+38
+11
pkgs/development/interpreters/python/hooks/default.nix
··· 1 1 # Hooks for building Python packages. 2 2 { python 3 + , lib 3 4 , callPackage 4 5 , makeSetupHook 5 6 , disabledIf ··· 96 97 inherit pythonSitePackages; 97 98 }; 98 99 } ./python-namespaces-hook.sh) {}; 100 + 101 + pythonRecompileBytecodeHook = callPackage ({ }: 102 + makeSetupHook { 103 + name = "python-recompile-bytecode-hook"; 104 + substitutions = { 105 + inherit pythonInterpreter pythonSitePackages; 106 + compileArgs = lib.concatStringsSep " " (["-q" "-f" "-i -"] ++ lib.optionals isPy3k ["-j $NIX_BUILD_CORES"]); 107 + bytecodeName = if isPy3k then "__pycache__" else "*.pyc"; 108 + }; 109 + } ./python-recompile-bytecode-hook.sh ) {}; 99 110 100 111 pythonRemoveBinBytecodeHook = callPackage ({ }: 101 112 makeSetupHook {
+24
pkgs/development/interpreters/python/hooks/python-recompile-bytecode-hook.sh
··· 1 + # Setup hook for recompiling bytecode. 2 + # https://github.com/NixOS/nixpkgs/issues/81441 3 + echo "Sourcing python-recompile-bytecode-hook.sh" 4 + 5 + # Remove all bytecode from the $out output. Then, recompile only site packages folder 6 + # Note this effectively duplicates `python-remove-bin-bytecode`, but long-term 7 + # this hook should be removed again. 8 + 9 + pythonRecompileBytecodePhase () { 10 + # TODO: consider other outputs than $out 11 + 12 + items="$(find "$out" -name "@bytecodeName@")" 13 + if [[ -n $items ]]; then 14 + for pycache in $items; do 15 + rm -rf "$pycache" 16 + done 17 + fi 18 + 19 + find "$out"/@pythonSitePackages@ -name "*.py" -exec @pythonInterpreter@ -OO -m compileall @compileArgs@ {} + 20 + } 21 + 22 + if [ -z "${dontUsePythonRecompileBytecode-}" ]; then 23 + postPhases+=" pythonRecompileBytecodePhase" 24 + fi
+2
pkgs/development/interpreters/python/mk-python-derivation.nix
··· 17 17 , pythonCatchConflictsHook 18 18 , pythonImportsCheckHook 19 19 , pythonNamespacesHook 20 + , pythonRecompileBytecodeHook 20 21 , pythonRemoveBinBytecodeHook 21 22 , pythonRemoveTestsDirHook 22 23 , setuptoolsBuildHook ··· 110 111 python 111 112 wrapPython 112 113 ensureNewerSourcesForZipFilesHook # move to wheel installer (pip) or builder (setuptools, flit, ...)? 114 + pythonRecompileBytecodeHook # Remove when solved https://github.com/NixOS/nixpkgs/issues/81441 113 115 pythonRemoveTestsDirHook 114 116 ] ++ lib.optionals catchConflicts [ 115 117 setuptools pythonCatchConflictsHook
+1
pkgs/top-level/python-packages.nix
··· 118 118 pythonCatchConflictsHook 119 119 pythonImportsCheckHook 120 120 pythonNamespacesHook 121 + pythonRecompileBytecodeHook 121 122 pythonRemoveBinBytecodeHook 122 123 pythonRemoveTestsDirHook 123 124 setuptoolsBuildHook