From 06ca784b25937242822aeacb3451d842f5f1c0fc Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Sat, 31 Dec 2022 11:07:23 -0500
Subject: [PATCH 1/2] vtk: Generate egg-info

VTK's cmake build system fails to generate an `.egg-info` file, which
various libraries in the Python package system (e.g. `pkg_resources`)
would use to identify the package. Work around this by generating an
`egg-info` file ourselves.

This will allow us to drop a workaround in `mayavi`.
---
 pkgs/development/libraries/vtk/generic.nix  | 13 ++++++++++---
 pkgs/development/libraries/vtk/vtk.egg-info |  4 ++++
 pkgs/top-level/python-packages.nix          |  2 +-
 3 files changed, 15 insertions(+), 4 deletions(-)
 create mode 100644 pkgs/development/libraries/vtk/vtk.egg-info

diff --git a/pkgs/development/libraries/vtk/generic.nix b/pkgs/development/libraries/vtk/generic.nix
index 1994e4ace114..327e848d89ee 100644
--- a/pkgs/development/libraries/vtk/generic.nix
+++ b/pkgs/development/libraries/vtk/generic.nix
@@ -2,7 +2,7 @@
 { stdenv, lib, fetchurl, cmake, libGLU, libGL, libX11, xorgproto, libXt, libpng, libtiff
 , fetchpatch
 , enableQt ? false, qtbase, qtx11extras, qttools, qtdeclarative, qtEnv
-, enablePython ? false, pythonInterpreter ? throw "vtk: Python support requested, but no python interpreter was given."
+, enablePython ? false, python ? throw "vtk: Python support requested, but no python interpreter was given."
 # Darwin support
 , Cocoa, CoreServices, DiskArbitration, IOKit, CFNetwork, Security, GLUT, OpenGL
 , ApplicationServices, CoreText, IOSurface, ImageIO, xpc, libobjc
@@ -11,7 +11,7 @@
 let
   inherit (lib) optionalString optionals optional;
 
-  pythonMajor = lib.substring 0 1 pythonInterpreter.pythonVersion;
+  pythonMajor = lib.substring 0 1 python.pythonVersion;
 
 in stdenv.mkDerivation rec {
   pname = "vtk${optionalString enableQt "-qvtk"}";
@@ -47,7 +47,7 @@ in stdenv.mkDerivation rec {
       OpenGL
       GLUT
     ] ++ optionals enablePython [
-      pythonInterpreter
+      python
     ];
   propagatedBuildInputs = optionals stdenv.isDarwin [ libobjc ]
     ++ optionals stdenv.isLinux [ libX11 libGL ];
@@ -89,6 +89,13 @@ in stdenv.mkDerivation rec {
     sed -i 's/fprintf(output, shift)/fprintf(output, "%s", shift)/g' ./ThirdParty/libxml2/vtklibxml2/xpath.c
   '';
 
+  postInstall = optionalString enablePython ''
+    substitute \
+      ${./vtk.egg-info} \
+      $out/lib/python${python.pythonVersion}/site-packages/vtk-${version}.egg-info \
+      --subst-var-by VTK_VER "${version}"
+  '';
+
   meta = with lib; {
     description = "Open source libraries for 3D computer graphics, image processing and visualization";
     homepage = "https://www.vtk.org/";
diff --git a/pkgs/development/libraries/vtk/vtk.egg-info b/pkgs/development/libraries/vtk/vtk.egg-info
new file mode 100644
index 000000000000..a244450ad767
--- /dev/null
+++ b/pkgs/development/libraries/vtk/vtk.egg-info
@@ -0,0 +1,4 @@
+Metadata-Version: 2.1
+Version: @VTK_VER@
+Summary: VTK is an open-source toolkit for 3D computer graphics, image processing, and visualization
+Platform: UNKNOWN
diff --git a/pkgs/top-level/python-packages.nix b/pkgs/top-level/python-packages.nix
index 1ebe6312d91b..653f8d841185 100644
--- a/pkgs/top-level/python-packages.nix
+++ b/pkgs/top-level/python-packages.nix
@@ -11905,7 +11905,7 @@ self: super: with self; {
   vt-py = callPackage ../development/python-modules/vt-py { };
 
   vtk = toPythonModule (pkgs.vtk_9.override {
-    pythonInterpreter = python;
+    inherit python;
     enablePython = true;
   });
 

From 57556157c98736e44d1222ee67cfe4cd454de65d Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Sat, 31 Dec 2022 11:09:46 -0500
Subject: [PATCH 2/2] mayavi: Drop workaround of vtk's lacking egg-info file

nixpkgs now generates an egg-info file for vtk.
---
 pkgs/development/python-modules/mayavi/default.nix | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/pkgs/development/python-modules/mayavi/default.nix b/pkgs/development/python-modules/mayavi/default.nix
index 736fc1d87952..b0ddfeeefcf3 100644
--- a/pkgs/development/python-modules/mayavi/default.nix
+++ b/pkgs/development/python-modules/mayavi/default.nix
@@ -27,10 +27,6 @@ buildPythonPackage rec {
   };
 
   postPatch = ''
-    # Discovery of 'vtk' in setuptools is not working properly, due to a missing
-    # .egg-info in the vtk package. It does however import and run just fine.
-    substituteInPlace mayavi/__init__.py --replace "'vtk'" ""
-
     # building the docs fails with the usual Qt xcb error, so skip:
     substituteInPlace setup.py \
       --replace "build.build.run(self)" "build.build.run(self); return"