lol
1{
2 cmake,
3 fetchFromGitHub,
4 fetchpatch,
5 makeWrapper,
6 lib,
7 stdenv,
8 which,
9
10 # build dependencies
11 boost,
12 curl,
13 eigen,
14 fftwFloat,
15 gdal,
16 itk,
17 libsvm,
18 libgeotiff,
19 muparser,
20 muparserx,
21 opencv,
22 perl,
23 python3,
24 shark,
25 swig,
26 tinyxml,
27
28 # otb modules
29 enableFFTW ? false,
30 enableFeatureExtraction ? true,
31 enableHyperspectral ? true,
32 enableLearning ? true,
33 enableMiscellaneous ? true,
34 enableOpenMP ? false,
35 enablePython ? true,
36 extraPythonPackages ? ps: with ps; [ ],
37 enableRemote ? true,
38 enableShark ? true,
39 enableSAR ? true,
40 enableSegmentation ? true,
41 enableStereoProcessing ? true,
42 enableThirdParty ? true,
43}:
44let
45 inherit (lib) optionals;
46 pythonInputs =
47 optionals enablePython (with python3.pkgs; [ numpy ]) ++ (extraPythonPackages python3.pkgs);
48
49 # ITK configs for OTB requires 5.3.0 and
50 # filter out gdcm, libminc from list of ITK deps as it's not needed for OTB
51 itkVersion = "5.3.0";
52 itkMajorMinorVersion = lib.versions.majorMinor itkVersion;
53 itkDepsToRemove = [
54 "gdcm"
55 "libminc"
56 ]
57 ++ optionals (!enableFFTW) [
58 # remove fftw to avoid GPL contamination
59 # https://gitlab.orfeo-toolbox.org/orfeotoolbox/otb/-/issues/2454#note_112821
60 "fftw"
61 ];
62 itkIsInDepsToRemove = dep: builtins.any (d: d == dep.name) itkDepsToRemove;
63
64 # remove after https://gitlab.orfeo-toolbox.org/orfeotoolbox/otb/-/issues/2451
65 otbSwig = swig.overrideAttrs (oldArgs: {
66 version = "4.2.1";
67 src = fetchFromGitHub {
68 owner = "swig";
69 repo = "swig";
70 tag = "v4.2.1";
71 hash = "sha256-VlUsiRZLScmbC7hZDzKqUr9481YXVwo0eXT/jy6Fda8=";
72 };
73 });
74
75 # override the ITK version with OTB version
76 # https://gitlab.orfeo-toolbox.org/orfeotoolbox/otb/-/blob/develop/SuperBuild/CMake/External_itk.cmake?ref_type=heads#L145
77 otb-itk = (itk.override { enableRtk = false; }).overrideAttrs (oldArgs: {
78 version = itkVersion;
79 src = fetchFromGitHub {
80 owner = "InsightSoftwareConsortium";
81 repo = "ITK";
82 tag = "v${itkVersion}";
83 hash = "sha256-+qCd8Jzpl5fEPTUpLyjjFBkfgCn3+Lf4pi8QnjCwofs=";
84 };
85
86 # https://gitlab.orfeo-toolbox.org/orfeotoolbox/otb/-/tree/develop/SuperBuild/patches/ITK?ref_type=heads
87 patches = oldArgs.patches or [ ] ++ [
88 ./itk-1-fftw-all.diff
89 ./itk-2-totalprogress-all.diff
90 # add gcc13 patch for itk 5.3.0 as well
91 (fetchpatch {
92 name = "fix-gcc13-build";
93 url = "https://github.com/InsightSoftwareConsortium/ITK/commit/9a719a0d2f5f489eeb9351b0ef913c3693147a4f.patch";
94 hash = "sha256-dDyqYOzo91afR8W7k2N64X6l7t6Ws1C9iuRkWHUe0fg=";
95 })
96 ];
97
98 # fix the CMake config files for ITK which contains double slashes
99 postInstall = (oldArgs.postInstall or "") + ''
100 sed -i 's|''${ITK_INSTALL_PREFIX}//nix/store|/nix/store|g' $out/lib/cmake/ITK-${itkMajorMinorVersion}/ITKConfig.cmake
101 '';
102
103 cmakeFlags = oldArgs.cmakeFlags or [ ] ++ [
104 (lib.cmakeBool "ITK_USE_SYSTEM_EIGEN" true)
105
106 # turn off all the itk modules from nixpkgs
107 (lib.cmakeBool "ITK_USE_SYSTEM_GDCM" false)
108 (lib.cmakeBool "ITK_USE_SYSTEM_MINC" false)
109 (lib.cmakeBool "Module_ITKMINC" false)
110 (lib.cmakeBool "Module_ITKIOMINC" false)
111 (lib.cmakeBool "Module_ITKIOTransformMINC" false)
112 (lib.cmakeBool "Module_SimpleITKFilters" false)
113 (lib.cmakeBool "Module_ITKReview" false)
114 (lib.cmakeBool "Module_MGHIO" false)
115 (lib.cmakeBool "Module_AdaptiveDenoising" false)
116 (lib.cmakeBool "Module_GenericLabelInterpolator" false)
117
118 # enable itk modules for otb
119 # https://gitlab.orfeo-toolbox.org/orfeotoolbox/otb/-/blob/develop/SuperBuild/CMake/External_itk.cmake?ref_type=heads
120 # https://gitlab.orfeo-toolbox.org/orfeotoolbox/otb/-/blob/develop/SuperBuild/CMake/External_itk.cmake?ref_type=heads#L143
121 (lib.cmakeBool "BUILD_TESTING" false)
122 (lib.cmakeBool "ITK_BUILD_DEFAULT_MODULES" false)
123 (lib.cmakeBool "ITKGroup_Core" false)
124 (lib.cmakeBool "Module_ITKCommon" true)
125 (lib.cmakeBool "Module_ITKFiniteDifference" true)
126 (lib.cmakeBool "Module_ITKGPUCommon" true)
127 (lib.cmakeBool "Module_ITKGPUFiniteDifference" true)
128 (lib.cmakeBool "Module_ITKImageAdaptors" true)
129 (lib.cmakeBool "Module_ITKImageFunction" true)
130 (lib.cmakeBool "Module_ITKMesh" true)
131 (lib.cmakeBool "Module_ITKQuadEdgeMesh" true)
132 (lib.cmakeBool "Module_ITKSpatialObjects" true)
133 (lib.cmakeBool "Module_ITKTransform" true)
134 (lib.cmakeBool "Module_ITKAnisotropicSmoothing" true)
135 (lib.cmakeBool "Module_ITKAntiAlias" true)
136 (lib.cmakeBool "Module_ITKBiasCorrection" true)
137 (lib.cmakeBool "Module_ITKBinaryMathematicalMorphology" true)
138 (lib.cmakeBool "Module_ITKColormap" true)
139 (lib.cmakeBool "Module_ITKConvolution" true)
140 (lib.cmakeBool "Module_ITKCurvatureFlow" true)
141 (lib.cmakeBool "Module_ITKDeconvolution" true)
142 (lib.cmakeBool "Module_ITKDenoising" true)
143 (lib.cmakeBool "Module_ITKDisplacementField" true)
144 (lib.cmakeBool "Module_ITKDistanceMap" true)
145 (lib.cmakeBool "Module_ITKFastMarching" true)
146 (lib.cmakeBool "Module_ITKFFT" true)
147 (lib.cmakeBool "Module_ITKGPUAnisotropicSmoothing" true)
148 (lib.cmakeBool "Module_ITKGPUImageFilterBase" true)
149 (lib.cmakeBool "Module_ITKGPUSmoothing" true)
150 (lib.cmakeBool "Module_ITKGPUThresholding" true)
151 (lib.cmakeBool "Module_ITKImageCompare" true)
152 (lib.cmakeBool "Module_ITKImageCompose" true)
153 (lib.cmakeBool "Module_ITKImageFeature" true)
154 (lib.cmakeBool "Module_ITKImageFilterBase" true)
155 (lib.cmakeBool "Module_ITKImageFusion" true)
156 (lib.cmakeBool "Module_ITKImageGradient" true)
157 (lib.cmakeBool "Module_ITKImageGrid" true)
158 (lib.cmakeBool "Module_ITKImageIntensity" true)
159 (lib.cmakeBool "Module_ITKImageLabel" true)
160 (lib.cmakeBool "Module_ITKImageSources" true)
161 (lib.cmakeBool "Module_ITKImageStatistics" true)
162 (lib.cmakeBool "Module_ITKLabelMap" true)
163 (lib.cmakeBool "Module_ITKMathematicalMorphology" true)
164 (lib.cmakeBool "Module_ITKPath" true)
165 (lib.cmakeBool "Module_ITKQuadEdgeMeshFiltering" true)
166 (lib.cmakeBool "Module_ITKSmoothing" true)
167 (lib.cmakeBool "Module_ITKSpatialFunction" true)
168 (lib.cmakeBool "Module_ITKThresholding" true)
169 (lib.cmakeBool "Module_ITKEigen" true)
170 (lib.cmakeBool "Module_ITKNarrowBand" true)
171 (lib.cmakeBool "Module_ITKOptimizers" true)
172 (lib.cmakeBool "Module_ITKOptimizersv4" true)
173 (lib.cmakeBool "Module_ITKPolynomials" true)
174 (lib.cmakeBool "Module_ITKStatistics" true)
175 (lib.cmakeBool "Module_ITKRegistrationCommon" true)
176 (lib.cmakeBool "Module_ITKGPURegistrationCommon" true)
177 (lib.cmakeBool "Module_ITKGPUPDEDeformableRegistration" true)
178 (lib.cmakeBool "Module_ITKMetricsv4" true)
179 (lib.cmakeBool "Module_ITKPDEDeformableRegistration" true)
180 (lib.cmakeBool "Module_ITKRegistrationMethodsv4" true)
181 (lib.cmakeBool "Module_ITKClassifiers" true)
182 (lib.cmakeBool "Module_ITKConnectedComponents" true)
183 (lib.cmakeBool "Module_ITKDeformableMesh" true)
184 (lib.cmakeBool "Module_ITKKLMRegionGrowing" true)
185 (lib.cmakeBool "Module_ITKLabelVoting" true)
186 (lib.cmakeBool "Module_ITKLevelSets" true)
187 (lib.cmakeBool "Module_ITKLevelSetsv4" true)
188 (lib.cmakeBool "Module_ITKMarkovRandomFieldsClassifiers" true)
189 (lib.cmakeBool "Module_ITKRegionGrowing" true)
190 (lib.cmakeBool "Module_ITKSignedDistanceFunction" true)
191 (lib.cmakeBool "Module_ITKVoronoi" true)
192 (lib.cmakeBool "Module_ITKWatersheds" true)
193 ];
194
195 buildInputs = oldArgs.buildInputs or [ ] ++ [
196 # add eigen as well as itk nixpkgs doesn't add it for version lower than 5.4.0
197 eigen
198 ];
199
200 propagatedBuildInputs =
201 lib.lists.filter (pkg: !(itkIsInDepsToRemove pkg)) oldArgs.propagatedBuildInputs or [ ]
202 ++ lib.optionals enableFFTW [
203 # the only missing dependency for OTB from itk propagated list if FFTW option is enabled
204 fftwFloat
205 ];
206
207 });
208
209 otb-shark = shark.override { enableOpenMP = enableOpenMP; };
210
211in
212stdenv.mkDerivation (finalAttrs: {
213 pname = "otb";
214 version = "10.0-unstable-2025-04-03";
215
216 src = fetchFromGitHub {
217 owner = "orfeotoolbox";
218 repo = "otb";
219 rev = "93649b68f54975a1a48a0acd49f2602a55fc8032";
220 hash = "sha256-S6yhV//qlKdWWcT9J1p64WuVS0QNepIYTr/t4JvyEwE=";
221 };
222
223 patches = [
224 # fixes for gdal 10
225 # https://gitlab.orfeo-toolbox.org/orfeotoolbox/otb/-/merge_requests/1056
226 (fetchpatch {
227 url = "https://gitlab.orfeo-toolbox.org/orfeotoolbox/otb/-/merge_requests/1056/diffs.patch";
228 hash = "sha256-Zj/wkx0vxn5vqj0hszn7NxoYW1yf63G3HPVKbSdZIOY=";
229 })
230 ./1-otb-swig-include-itk.diff
231 ];
232
233 postPatch = ''
234 substituteInPlace Modules/Core/Wrappers/SWIG/src/python/CMakeLists.txt \
235 --replace-fail ''\'''${ITK_INCLUDE_DIRS}' "${otb-itk}/include/ITK-${itkMajorMinorVersion}"
236 ''
237 # Add the header file "vcl_legacy_aliases.h", which defines the legacy vcl_* functions.
238 # This patch fixes the unreproducible build of OTB.
239 # See https://gitlab.orfeo-toolbox.org/orfeotoolbox/otb/-/issues/2484.
240 + ''
241 sed -i '/#include "vcl_compiler.h"/a #include "vcl_legacy_aliases.h"' Modules/Core/Mosaic/include/otbMosaicFunctors.h
242 '';
243
244 nativeBuildInputs = [
245 cmake
246 makeWrapper
247 which
248 ];
249
250 # https://www.orfeo-toolbox.org/CookBook/CompilingOTBFromSource.html#native-build-with-system-dependencies
251 # activates all modules and python by default
252 cmakeFlags = [
253 (lib.cmakeBool "OTBGroup_Core" true)
254 (lib.cmakeBool "OTB_BUILD_FeaturesExtraction" enableFeatureExtraction)
255 (lib.cmakeBool "OTB_BUILD_Hyperspectral" enableHyperspectral)
256 (lib.cmakeBool "OTB_BUILD_Learning" enableLearning)
257 (lib.cmakeBool "OTB_BUILD_Miscellaneous" enableMiscellaneous)
258 (lib.cmakeBool "OTB_USE_OPENMP" enableOpenMP)
259 (lib.cmakeBool "OTB_BUILD_RemoteModules" enableRemote)
260 (lib.cmakeBool "OTB_BUILD_SAR" enableSAR)
261 (lib.cmakeBool "OTB_USE_SHARK" enableShark)
262 (lib.cmakeBool "OTB_BUILD_Segmentation" enableSegmentation)
263 (lib.cmakeBool "OTB_BUILD_StereoProcessing" enableStereoProcessing)
264 (lib.cmakeBool "OTBGroup_ThirdParty" enableThirdParty)
265 (lib.cmakeBool "OTB_WRAP_PYTHON" enablePython)
266 (lib.cmakeBool "BUILD_TESTING" finalAttrs.doInstallCheck)
267 (lib.cmakeBool "OTB_USE_FFTW" enableFFTW)
268 ];
269
270 propagatedBuildInputs = [
271 boost
272 curl
273 gdal
274 libgeotiff
275 libsvm
276 muparser
277 muparserx
278 opencv
279 otb-itk
280 perl
281 tinyxml
282 ]
283 ++ otb-itk.propagatedBuildInputs
284 ++ optionals enablePython (
285 [
286 python3
287 otbSwig
288 ]
289 ++ pythonInputs
290 )
291 ++ optionals enableShark [ otb-shark ];
292
293 doInstallCheck = true;
294
295 postInstall = ''
296 wrapProgram $out/bin/otbcli \
297 --set OTB_INSTALL_DIR "$out" \
298 --set OTB_APPLICATION_PATH "$out/lib/otb/applications"
299 '';
300
301 meta = {
302 description = "Open Source processing of remote sensing images";
303 homepage = "https://www.orfeo-toolbox.org/";
304 license = lib.licenses.asl20;
305 maintainers = with lib.maintainers; [ daspk04 ];
306 teams = [ lib.teams.geospatial ];
307 platforms = lib.platforms.linux;
308 };
309})