1{
2 version,
3 tag,
4 sourceSha256,
5}:
6
7{
8 lib,
9 stdenv,
10 fetchFromGitHub,
11 fetchpatch,
12 cmake,
13 castxml,
14 swig,
15 expat,
16 eigen,
17 fftw,
18 gdcm,
19 hdf5-cpp,
20 libjpeg,
21 libminc,
22 libtiff,
23 libpng,
24 libX11,
25 libuuid,
26 patchelf,
27 python ? null,
28 numpy ? null,
29 xz,
30 vtk,
31 which,
32 zlib,
33 enablePython ? false,
34 enableRtk ? true,
35}:
36
37let
38 # Python wrapper contains its own VTK support incompatible with MODULE_ITKVtkGlue
39 withVtk = !enablePython;
40
41 itkGenericLabelInterpolatorSrc = fetchFromGitHub {
42 owner = "InsightSoftwareConsortium";
43 repo = "ITKGenericLabelInterpolator";
44 rev = "2f3768110ffe160c00c533a1450a49a16f4452d9";
45 hash = "sha256-Cm3jg14MMnbr/sP+gqR2Rh25xJjoRvpmY/jP/DKH978=";
46 };
47
48 itkAdaptiveDenoisingSrc = fetchFromGitHub {
49 owner = "ntustison";
50 repo = "ITKAdaptiveDenoising";
51 rev = "24825c8d246e941334f47968553f0ae388851f0c";
52 hash = "sha256-deJbza36c0Ohf9oKpO2T4po37pkyI+2wCSeGL4r17Go=";
53 };
54
55 itkSimpleITKFiltersSrc = fetchFromGitHub {
56 owner = "InsightSoftwareConsortium";
57 repo = "ITKSimpleITKFilters";
58 rev = "bb896868fc6480835495d0da4356d5db009592a6";
59 hash = "sha256-MfaIA0xxA/pzUBSwnAevr17iR23Bo5iQO2cSyknS3o4=";
60 };
61
62 rtkSrc = fetchFromGitHub {
63 owner = "RTKConsortium";
64 repo = "RTK";
65 rev = "583288b1898dedcfb5e4d602e31020b452971383";
66 hash = "sha256-1ItsLCRwRzGDSRe4xUDg09Hksu1nKichbWuM0YSVkbM=";
67 };
68in
69
70stdenv.mkDerivation {
71 pname = "itk";
72 inherit version;
73
74 src = fetchFromGitHub {
75 owner = "InsightSoftwareConsortium";
76 repo = "ITK";
77 inherit tag;
78 sha256 = sourceSha256;
79 };
80
81 patches = lib.optionals (lib.versionOlder version "5.4") [
82 (fetchpatch {
83 name = "fix-gcc13-build";
84 url = "https://github.com/InsightSoftwareConsortium/ITK/commit/9a719a0d2f5f489eeb9351b0ef913c3693147a4f.patch";
85 hash = "sha256-dDyqYOzo91afR8W7k2N64X6l7t6Ws1C9iuRkWHUe0fg=";
86 })
87 ];
88
89 postPatch = ''
90 substituteInPlace CMake/ITKSetStandardCompilerFlags.cmake \
91 --replace "-march=corei7" "" \
92 --replace "-mtune=native" ""
93 ln -sr ${itkGenericLabelInterpolatorSrc} Modules/External/ITKGenericLabelInterpolator
94 ln -sr ${itkAdaptiveDenoisingSrc} Modules/External/ITKAdaptiveDenoising
95 ln -sr ${itkSimpleITKFiltersSrc} Modules/External/ITKSimpleITKFilters
96 ln -sr ${rtkSrc} Modules/Remote/RTK
97 '';
98
99 cmakeFlags = [
100 "-DBUILD_EXAMPLES=OFF"
101 "-DBUILD_SHARED_LIBS=ON"
102 "-DITK_FORBID_DOWNLOADS=ON"
103 "-DITK_USE_SYSTEM_LIBRARIES=ON" # finds common libraries e.g. hdf5, libpng, libtiff, zlib, but not GDCM, NIFTI, MINC, etc.
104 (lib.cmakeBool "ITK_USE_SYSTEM_EIGEN" (lib.versionAtLeast version "5.4"))
105 "-DITK_USE_SYSTEM_GOOGLETEST=OFF" # ANTs build failure due to https://github.com/ANTsX/ANTs/issues/1489
106 "-DITK_USE_SYSTEM_GDCM=ON"
107 "-DITK_USE_SYSTEM_MINC=ON"
108 "-DLIBMINC_DIR=${libminc}/lib/cmake"
109 "-DModule_ITKMINC=ON"
110 "-DModule_ITKIOMINC=ON"
111 "-DModule_ITKIOTransformMINC=ON"
112 "-DModule_SimpleITKFilters=ON"
113 "-DModule_ITKReview=ON"
114 "-DModule_MGHIO=ON"
115 "-DModule_AdaptiveDenoising=ON"
116 "-DModule_GenericLabelInterpolator=ON"
117 ]
118 ++ lib.optionals enableRtk [
119 "-DModule_RTK=ON"
120 ]
121 ++ lib.optionals enablePython [
122 "-DITK_WRAP_PYTHON=ON"
123 "-DITK_USE_SYSTEM_CASTXML=ON"
124 "-DITK_USE_SYSTEM_SWIG=ON"
125 "-DPY_SITE_PACKAGES_PATH=${placeholder "out"}/${python.sitePackages}"
126 ]
127 ++ lib.optionals withVtk [ "-DModule_ITKVtkGlue=ON" ];
128
129 nativeBuildInputs = [
130 cmake
131 xz
132 ]
133 ++ lib.optionals enablePython [
134 castxml
135 swig
136 which
137 ];
138
139 buildInputs = [
140 libX11
141 libuuid
142 ]
143 ++ lib.optionals (lib.versionAtLeast version "5.4") [ eigen ]
144 ++ lib.optionals enablePython [ python ]
145 ++ lib.optionals withVtk [ vtk ];
146 # Due to ITKVtkGlue=ON and the additional dependencies needed to configure VTK 9
147 # (specifically libGL and libX11 on Linux),
148 # it's now seemingly necessary for packages that configure ITK to
149 # also include configuration deps of VTK, even if VTK is not required or available.
150 # These deps were propagated from VTK 9 in https://github.com/NixOS/nixpkgs/pull/206935,
151 # so we simply propagate them again from ITK.
152 # This admittedly is a hack and seems like an issue with VTK 9's CMake configuration.
153 propagatedBuildInputs = [
154 # The dependencies we've un-vendored from ITK, such as GDCM, must be propagated,
155 # otherwise other software built against ITK fails to configure since ITK headers
156 # refer to these previously vendored libraries:
157 expat
158 fftw
159 gdcm
160 hdf5-cpp
161 libjpeg
162 libminc
163 libpng
164 libtiff
165 zlib
166 ]
167 ++ lib.optionals withVtk vtk.propagatedBuildInputs
168 ++ lib.optionals enablePython [ numpy ];
169
170 postInstall = lib.optionalString enablePython ''
171 substitute \
172 ${./itk.egg-info} \
173 $out/${python.sitePackages}/itk-${version}.egg-info \
174 --subst-var-by ITK_VER "${version}"
175 '';
176
177 # remove forbidden reference to /build which occur when building the Python wrapping
178 # (also remove a copy of itkTestDriver with incorrect permissions/RPATH):
179 preFixup = lib.optionals enablePython ''
180 rm $out/${python.sitePackages}/itk/itkTestDriver
181 find $out/${python.sitePackages}/itk -type f -name '*.so*' -exec \
182 patchelf {} --shrink-rpath --allowed-rpath-prefixes "$NIX_STORE" \;
183 '';
184
185 meta = {
186 description = "Insight Segmentation and Registration Toolkit";
187 homepage = "https://www.itk.org";
188 license = lib.licenses.asl20;
189 maintainers = with lib.maintainers; [ bcdarwin ];
190 # aarch64-linux Python wrapping fails with "error: unknown type name '_Float128'" and similar;
191 # compilation runs slowly and times out on Darwin
192 platforms = with lib.platforms; if enablePython then [ "x86_64-linux" ] else unix;
193 };
194}