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