2 , fetchurl, fetchFromGitHub, fetchpatch
3 , cmake, pkg-config, unzip, zlib, pcre, hdf5
4 , glog, boost, gflags, protobuf
7 , enableJPEG ? true, libjpeg
8 , enablePNG ? true, libpng
9 , enableTIFF ? true, libtiff
10 , enableWebP ? true, libwebp
11 , enableEXR ? !stdenv.isDarwin, openexr, ilmbase
12 , enableEigen ? true, eigen
13 , enableOpenblas ? true, openblas, blas, lapack
14 , enableContrib ? true
16 , enableCuda ? (config.cudaSupport or false) &&
17 stdenv.hostPlatform.isx86_64, cudatoolkit, nvidia-optical-flow-sdk
19 , enableUnfree ? false
21 , enablePython ? false, pythonPackages
22 , enableGtk2 ? false, gtk2
23 , enableGtk3 ? false, gtk3
24 , enableVtk ? false, vtk
25 , enableFfmpeg ? false, ffmpeg_3
26 , enableGStreamer ? false, gst_all_1
27 , enableTesseract ? false, tesseract, leptonica
28 , enableTbb ? false, tbb
29 , enableOvis ? false, ogre
30 , enableGPhoto2 ? false, libgphoto2
31 , enableDC1394 ? false, libdc1394
32 , enableDocs ? false, doxygen, graphviz-nox
34 , AVFoundation, Cocoa, VideoDecodeAcceleration, CoreMedia, MediaToolbox, bzip2
40 src = fetchFromGitHub {
44 sha256 = "1r9bq9p1x99g2y8jvj9428sgqvljz75dm5vrfsma7hh5wjhz9775";
47 contribSrc = fetchFromGitHub {
49 repo = "opencv_contrib";
51 sha256 = "068b4f95rlryab3mffxs2w6dnbmbhrnpsdgl007rxk4bwnz29y49";
54 # Contrib must be built in order to enable Tesseract support:
55 buildContrib = enableContrib || enableTesseract || enableOvis;
57 # See opencv/3rdparty/ippicv/ippicv.cmake
59 src = fetchFromGitHub {
61 repo = "opencv_3rdparty";
62 rev = "a56b6ac6f030c312b2dce17430eef13aed9af274";
63 sha256 = "1msbkc3zixx61rcg6a04i1bcfhw1phgsrh93glq1n80hgsk3nbjq";
65 files = let name = platform : "ippicv_2019_${platform}_general_20180723.tgz"; in
66 if stdenv.hostPlatform.system == "x86_64-linux" then
67 { ${name "lnx_intel64"} = "c0bd78adb4156bbf552c1dfe90599607"; }
68 else if stdenv.hostPlatform.system == "i686-linux" then
69 { ${name "lnx_ia32"} = "4f38432c30bfd6423164b7a24bbc98a0"; }
70 else if stdenv.hostPlatform.system == "x86_64-darwin" then
71 { ${name "mac_intel64"} = "fe6b2bb75ae0e3f19ad3ae1a31dfa4a2"; }
73 throw "ICV is not available for this platform (or not yet supported by this package)";
74 dst = ".cache/ippicv";
77 # See opencv_contrib/modules/xfeatures2d/cmake/download_vgg.cmake
79 src = fetchFromGitHub {
81 repo = "opencv_3rdparty";
82 rev = "fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d";
83 sha256 = "0r9fam8dplyqqsd3qgpnnfgf9l7lj44di19rxwbm8mxiw0rlcdvy";
86 "vgg_generated_48.i" = "e8d0dcd54d1bcfdc29203d011a797179";
87 "vgg_generated_64.i" = "7126a5d9a8884ebca5aea5d63d677225";
88 "vgg_generated_80.i" = "7cd47228edec52b6d82f46511af325c5";
89 "vgg_generated_120.i" = "151805e03568c9f490a5e3a872777b75";
91 dst = ".cache/xfeatures2d/vgg";
94 # See opencv_contrib/modules/xfeatures2d/cmake/download_boostdesc.cmake
96 src = fetchFromGitHub {
98 repo = "opencv_3rdparty";
99 rev = "34e4206aef44d50e6bbcd0ab06354b52e7466d26";
100 sha256 = "13yig1xhvgghvxspxmdidss5lqiikpjr0ddm83jsi0k85j92sn62";
103 "boostdesc_bgm.i" = "0ea90e7a8f3f7876d450e4149c97c74f";
104 "boostdesc_bgm_bi.i" = "232c966b13651bd0e46a1497b0852191";
105 "boostdesc_bgm_hd.i" = "324426a24fa56ad9c5b8e3e0b3e5303e";
106 "boostdesc_binboost_064.i" = "202e1b3e9fec871b04da31f7f016679f";
107 "boostdesc_binboost_128.i" = "98ea99d399965c03d555cef3ea502a0b";
108 "boostdesc_binboost_256.i" = "e6dcfa9f647779eb1ce446a8d759b6ea";
109 "boostdesc_lbgm.i" = "0ae0675534aa318d9668f2a179c2a052";
111 dst = ".cache/xfeatures2d/boostdesc";
114 # See opencv_contrib/modules/face/CMakeLists.txt
116 src = fetchFromGitHub {
118 repo = "opencv_3rdparty";
119 rev = "8afa57abc8229d611c4937165d20e2a2d9fc5a12";
120 sha256 = "061lsvqdidq9xa2hwrcvwi9ixflr2c2lfpc8drr159g68zi8bp4v";
123 "face_landmark_model.dat" = "7505c44ca4eb54b4ab1e4777cb96ac05";
128 # See opencv/modules/gapi/cmake/DownloadADE.cmake
131 url = "https://github.com/opencv/ade/archive/${name}";
132 sha256 = "04n9na2bph706bdxnnqfcbga4cyj8kd9s9ni7qyvnpj5v98jwvlm";
134 name = "v0.1.1f.zip";
135 md5 = "b624b995ec9c439cbc2e9e6ee940d3a2";
139 # See opencv/cmake/OpenCVDownload.cmake
140 installExtraFiles = extra : with lib; ''
141 mkdir -p "${extra.dst}"
142 '' + concatStrings (flip mapAttrsToList extra.files (name : md5 : ''
143 ln -s "${extra.src}/${name}" "${extra.dst}/${md5}-${name}"
145 installExtraFile = extra: ''
146 mkdir -p "${extra.dst}"
147 ln -s "${extra.src}" "${extra.dst}/${extra.md5}-${extra.name}"
150 opencvFlag = name: enabled: "-DWITH_${name}=${printEnabled enabled}";
152 printEnabled = enabled : if enabled then "ON" else "OFF";
155 stdenv.mkDerivation {
159 postUnpack = lib.optionalString buildContrib ''
160 cp --no-preserve=mode -r "${contribSrc}/modules" "$NIX_BUILD_TOP/source/opencv_contrib"
163 # This prevents cmake from using libraries in impure paths (which
164 # causes build failure on non NixOS)
166 ./cmake-don-t-use-OpenCVFindOpenEXR.patch
167 ] ++ lib.optional enableCuda ./cuda_opt_flow.patch;
169 sed -i '/Add these standard paths to the search paths for FIND_LIBRARY/,/^\s*$/{d}' CMakeLists.txt
173 installExtraFile ade +
174 lib.optionalString enableIpp (installExtraFiles ippicv) + (
175 lib.optionalString buildContrib ''
176 cmakeFlagsArray+=("-DOPENCV_EXTRA_MODULES_PATH=$NIX_BUILD_TOP/source/opencv_contrib")
178 ${installExtraFiles vgg}
179 ${installExtraFiles boostdesc}
180 ${installExtraFiles face}
184 [ -e modules/core/version_string.inc ]
185 echo '"(build info elided)"' > modules/core/version_string.inc
189 [ zlib pcre hdf5 glog boost gflags protobuf ]
190 ++ lib.optional enablePython pythonPackages.python
191 ++ lib.optional enableGtk2 gtk2
192 ++ lib.optional enableGtk3 gtk3
193 ++ lib.optional enableVtk vtk
194 ++ lib.optional enableJPEG libjpeg
195 ++ lib.optional enablePNG libpng
196 ++ lib.optional enableTIFF libtiff
197 ++ lib.optional enableWebP libwebp
198 ++ lib.optionals enableEXR [ openexr ilmbase ]
199 ++ lib.optional enableFfmpeg ffmpeg_3
200 ++ lib.optionals (enableFfmpeg && stdenv.isDarwin)
201 [ VideoDecodeAcceleration bzip2 ]
202 ++ lib.optionals enableGStreamer (with gst_all_1; [ gstreamer gst-plugins-base ])
203 ++ lib.optional enableOvis ogre
204 ++ lib.optional enableGPhoto2 libgphoto2
205 ++ lib.optional enableDC1394 libdc1394
206 ++ lib.optional enableEigen eigen
207 ++ lib.optional enableOpenblas openblas
208 # There is seemingly no compile-time flag for Tesseract. It's
209 # simply enabled automatically if contrib is built, and it detects
210 # tesseract & leptonica.
211 ++ lib.optionals enableTesseract [ tesseract leptonica ]
212 ++ lib.optional enableTbb tbb
213 ++ lib.optionals enableCuda [ cudatoolkit nvidia-optical-flow-sdk ]
214 ++ lib.optionals stdenv.isDarwin [ bzip2 AVFoundation Cocoa VideoDecodeAcceleration CoreMedia MediaToolbox ]
215 ++ lib.optionals enableDocs [ doxygen graphviz-nox ];
217 propagatedBuildInputs = lib.optional enablePython pythonPackages.numpy;
219 nativeBuildInputs = [ cmake pkg-config unzip ];
221 NIX_CFLAGS_COMPILE = lib.optionalString enableEXR "-I${ilmbase.dev}/include/OpenEXR";
223 # Configure can't find the library without this.
224 OpenBLAS_HOME = lib.optionalString enableOpenblas openblas;
227 "-DOPENCV_GENERATE_PKGCONFIG=ON"
229 "-DBUILD_PROTOBUF=OFF"
230 "-DPROTOBUF_UPDATE_FILES=ON"
231 "-DOPENCV_ENABLE_NONFREE=${printEnabled enableUnfree}"
233 "-DBUILD_PERF_TESTS=OFF"
234 "-DBUILD_DOCS=${printEnabled enableDocs}"
235 (opencvFlag "IPP" enableIpp)
236 (opencvFlag "TIFF" enableTIFF)
237 (opencvFlag "WEBP" enableWebP)
238 (opencvFlag "JPEG" enableJPEG)
239 (opencvFlag "PNG" enablePNG)
240 (opencvFlag "OPENEXR" enableEXR)
241 (opencvFlag "CUDA" enableCuda)
242 (opencvFlag "CUBLAS" enableCuda)
243 (opencvFlag "TBB" enableTbb)
244 ] ++ lib.optionals enableCuda [
245 "-DCUDA_FAST_MATH=ON"
246 "-DCUDA_HOST_COMPILER=${cudatoolkit.cc}/bin/cc"
247 "-DCUDA_NVCC_FLAGS=--expt-relaxed-constexpr"
248 "-DNVIDIA_OPTICAL_FLOW_1_0_HEADERS_PATH=${nvidia-optical-flow-sdk}"
249 ] ++ lib.optionals stdenv.isDarwin [
252 ] ++ lib.optionals enablePython [
253 "-DOPENCV_SKIP_PYTHON_LOADER=ON"
256 postBuild = lib.optionalString enableDocs ''
260 # By default $out/lib/pkgconfig/opencv4.pc looks something like this:
262 # prefix=/nix/store/g0wnfyjjh4rikkvp22cpkh41naa43i4i-opencv-4.0.0
263 # exec_prefix=${prefix}
264 # libdir=${exec_prefix}//nix/store/g0wnfyjjh4rikkvp22cpkh41naa43i4i-opencv-4.0.0/lib
265 # includedir_old=${prefix}//nix/store/g0wnfyjjh4rikkvp22cpkh41naa43i4i-opencv-4.0.0/include/opencv4/opencv
266 # includedir_new=${prefix}//nix/store/g0wnfyjjh4rikkvp22cpkh41naa43i4i-opencv-4.0.0/include/opencv4
268 # Libs: -L${exec_prefix}//nix/store/g0wnfyjjh4rikkvp22cpkh41naa43i4i-opencv-4.0.0/lib ...
269 # Note that ${exec_prefix} is set to $out but that $out is also appended to
270 # ${exec_prefix}. This causes linker errors in downstream packages so we strip
271 # of $out after the ${exec_prefix} and ${prefix} prefixes:
273 sed -i "s|{exec_prefix}/$out|{exec_prefix}|;s|{prefix}/$out|{prefix}|" \
274 "$out/lib/pkgconfig/opencv4.pc"
277 hardeningDisable = [ "bindnow" "relro" ];
279 passthru = lib.optionalAttrs enablePython { pythonPath = []; };
282 description = "Open Computer Vision Library with more than 500 algorithms";
283 homepage = "https://opencv.org/";
284 license = with licenses; if enableUnfree then unfree else bsd3;
285 maintainers = with maintainers; [mdaiter basvandijk];
286 platforms = with platforms; linux ++ darwin;