2 , 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
19 , enableUnfree ? false
21 , enablePython ? false, pythonPackages ? null
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, bzip2
37 assert blas.implementation == "openblas" && lapack.implementation == "openblas";
39 assert enablePython -> pythonPackages != null;
44 src = fetchFromGitHub {
48 sha256 = "1dnz3gfj70lm1gbrk8pz28apinlqi2x6nvd6xcy5hs08505nqnjp";
51 contribSrc = fetchFromGitHub {
53 repo = "opencv_contrib";
55 sha256 = "0psaa1yx36n34l09zd1y8jxgf8q4jzxd3vn06fqmzwzy85hcqn8i";
58 # Contrib must be built in order to enable Tesseract support:
59 buildContrib = enableContrib || enableTesseract;
61 useSystemProtobuf = ! stdenv.isDarwin;
63 # See opencv/3rdparty/ippicv/ippicv.cmake
65 src = fetchFromGitHub {
67 repo = "opencv_3rdparty";
68 rev = "32e315a5b106a7b89dbed51c28f8120a48b368b4";
69 sha256 = "19w9f0r16072s59diqxsr5q6nmwyz9gnxjs49nglzhd66p3ddbkp";
71 files = let name = platform : "ippicv_2019_${platform}_general_20180723.tgz"; in
72 if stdenv.hostPlatform.system == "x86_64-linux" then
73 { ${name "lnx_intel64"} = "c0bd78adb4156bbf552c1dfe90599607"; }
74 else if stdenv.hostPlatform.system == "i686-linux" then
75 { ${name "lnx_ia32"} = "4f38432c30bfd6423164b7a24bbc98a0"; }
76 else if stdenv.hostPlatform.system == "x86_64-darwin" then
77 { ${name "mac_intel64"} = "fe6b2bb75ae0e3f19ad3ae1a31dfa4a2"; }
79 throw "ICV is not available for this platform (or not yet supported by this package)";
80 dst = ".cache/ippicv";
83 # See opencv_contrib/modules/xfeatures2d/cmake/download_vgg.cmake
85 src = fetchFromGitHub {
87 repo = "opencv_3rdparty";
88 rev = "fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d";
89 sha256 = "0r9fam8dplyqqsd3qgpnnfgf9l7lj44di19rxwbm8mxiw0rlcdvy";
92 "vgg_generated_48.i" = "e8d0dcd54d1bcfdc29203d011a797179";
93 "vgg_generated_64.i" = "7126a5d9a8884ebca5aea5d63d677225";
94 "vgg_generated_80.i" = "7cd47228edec52b6d82f46511af325c5";
95 "vgg_generated_120.i" = "151805e03568c9f490a5e3a872777b75";
97 dst = ".cache/xfeatures2d/vgg";
100 # See opencv_contrib/modules/xfeatures2d/cmake/download_boostdesc.cmake
102 src = fetchFromGitHub {
104 repo = "opencv_3rdparty";
105 rev = "34e4206aef44d50e6bbcd0ab06354b52e7466d26";
106 sha256 = "13yig1xhvgghvxspxmdidss5lqiikpjr0ddm83jsi0k85j92sn62";
109 "boostdesc_bgm.i" = "0ea90e7a8f3f7876d450e4149c97c74f";
110 "boostdesc_bgm_bi.i" = "232c966b13651bd0e46a1497b0852191";
111 "boostdesc_bgm_hd.i" = "324426a24fa56ad9c5b8e3e0b3e5303e";
112 "boostdesc_binboost_064.i" = "202e1b3e9fec871b04da31f7f016679f";
113 "boostdesc_binboost_128.i" = "98ea99d399965c03d555cef3ea502a0b";
114 "boostdesc_binboost_256.i" = "e6dcfa9f647779eb1ce446a8d759b6ea";
115 "boostdesc_lbgm.i" = "0ae0675534aa318d9668f2a179c2a052";
117 dst = ".cache/xfeatures2d/boostdesc";
120 # See opencv_contrib/modules/face/CMakeLists.txt
122 src = fetchFromGitHub {
124 repo = "opencv_3rdparty";
125 rev = "8afa57abc8229d611c4937165d20e2a2d9fc5a12";
126 sha256 = "061lsvqdidq9xa2hwrcvwi9ixflr2c2lfpc8drr159g68zi8bp4v";
129 "face_landmark_model.dat" = "7505c44ca4eb54b4ab1e4777cb96ac05";
134 # See opencv/cmake/OpenCVDownload.cmake
135 installExtraFiles = extra : with lib; ''
136 mkdir -p "${extra.dst}"
137 '' + concatStrings (mapAttrsToList (name : md5 : ''
138 ln -s "${extra.src}/${name}" "${extra.dst}/${md5}-${name}"
141 opencvFlag = name: enabled: "-DWITH_${name}=${printEnabled enabled}";
143 printEnabled = enabled : if enabled then "ON" else "OFF";
146 stdenv.mkDerivation {
150 postUnpack = lib.optionalString buildContrib ''
151 cp --no-preserve=mode -r "${contribSrc}/modules" "$NIX_BUILD_TOP/opencv_contrib"
154 # This prevents cmake from using libraries in impure paths (which
155 # causes build failure on non NixOS)
156 # Also, work around https://github.com/NixOS/nixpkgs/issues/26304 with
157 # what appears to be some stray headers in dnn/misc/tensorflow
158 # in contrib when generating the Python bindings:
160 sed -i '/Add these standard paths to the search paths for FIND_LIBRARY/,/^\s*$/{d}' CMakeLists.txt
161 sed -i -e 's|if len(decls) == 0:|if len(decls) == 0 or "opencv2/" not in hdr:|' ./modules/python/src2/gen2.py
165 lib.optionalString enableIpp (installExtraFiles ippicv) + (
166 lib.optionalString buildContrib ''
167 cmakeFlagsArray+=("-DOPENCV_EXTRA_MODULES_PATH=$NIX_BUILD_TOP/opencv_contrib")
169 ${installExtraFiles vgg}
170 ${installExtraFiles boostdesc}
171 ${installExtraFiles face}
175 [ -e modules/core/version_string.inc ]
176 echo '"(build info elided)"' > modules/core/version_string.inc
180 [ zlib pcre hdf5 glog boost gflags ]
181 ++ lib.optional useSystemProtobuf protobuf
182 ++ lib.optional enablePython pythonPackages.python
183 ++ lib.optional enableGtk2 gtk2
184 ++ lib.optional enableGtk3 gtk3
185 ++ lib.optional enableVtk vtk
186 ++ lib.optional enableJPEG libjpeg
187 ++ lib.optional enablePNG libpng
188 ++ lib.optional enableTIFF libtiff
189 ++ lib.optional enableWebP libwebp
190 ++ lib.optionals enableEXR [ openexr ilmbase ]
191 ++ lib.optional enableFfmpeg ffmpeg_3
192 ++ lib.optionals (enableFfmpeg && stdenv.isDarwin)
193 [ VideoDecodeAcceleration bzip2 ]
194 ++ lib.optionals enableGStreamer (with gst_all_1; [ gstreamer gst-plugins-base ])
195 ++ lib.optional enableOvis ogre
196 ++ lib.optional enableGPhoto2 libgphoto2
197 ++ lib.optional enableDC1394 libdc1394
198 ++ lib.optional enableEigen eigen
199 ++ lib.optional enableOpenblas openblas
200 # There is seemingly no compile-time flag for Tesseract. It's
201 # simply enabled automatically if contrib is built, and it detects
202 # tesseract & leptonica.
203 ++ lib.optionals enableTesseract [ tesseract leptonica ]
204 ++ lib.optional enableTbb tbb
205 ++ lib.optional enableCuda cudatoolkit
206 ++ lib.optionals stdenv.isDarwin [ bzip2 AVFoundation Cocoa VideoDecodeAcceleration ]
207 ++ lib.optionals enableDocs [ doxygen graphviz-nox ];
209 propagatedBuildInputs = lib.optional enablePython pythonPackages.numpy;
211 nativeBuildInputs = [ cmake pkg-config unzip ];
213 NIX_CFLAGS_COMPILE = lib.optionalString enableEXR "-I${ilmbase.dev}/include/OpenEXR";
215 # Configure can't find the library without this.
216 OpenBLAS_HOME = lib.optionalString enableOpenblas openblas;
220 "-DBUILD_PROTOBUF=${printEnabled (!useSystemProtobuf)}"
221 "-DPROTOBUF_UPDATE_FILES=${printEnabled useSystemProtobuf}"
222 "-DOPENCV_ENABLE_NONFREE=${printEnabled enableUnfree}"
224 "-DBUILD_PERF_TESTS=OFF"
225 "-DBUILD_DOCS=${printEnabled enableDocs}"
226 (opencvFlag "IPP" enableIpp)
227 (opencvFlag "TIFF" enableTIFF)
228 (opencvFlag "WEBP" enableWebP)
229 (opencvFlag "JPEG" enableJPEG)
230 (opencvFlag "PNG" enablePNG)
231 (opencvFlag "OPENEXR" enableEXR)
232 (opencvFlag "CUDA" enableCuda)
233 (opencvFlag "CUBLAS" enableCuda)
234 (opencvFlag "TBB" enableTbb)
235 ] ++ lib.optionals enableCuda [
236 "-DCUDA_FAST_MATH=ON"
237 "-DCUDA_HOST_COMPILER=${cudatoolkit.cc}/bin/cc"
238 "-DCUDA_NVCC_FLAGS=--expt-relaxed-constexpr"
239 ] ++ lib.optionals stdenv.isDarwin [
242 "-DBUILD_opencv_videoio=OFF"
243 ] ++ lib.optionals enablePython [
244 "-DOPENCV_SKIP_PYTHON_LOADER=ON"
245 ] ++ lib.optional enableEigen [
246 # Autodetection broken by https://github.com/opencv/opencv/pull/13337
247 "-DEIGEN_INCLUDE_PATH=${eigen}/include/eigen3"
250 postBuild = lib.optionalString enableDocs ''
254 # By default $out/lib/pkgconfig/opencv.pc looks something like this:
256 # prefix=/nix/store/10pzq1a8fkh8q4sysj8n6mv0w0nl0miq-opencv-3.4.1
257 # exec_prefix=${prefix}
258 # libdir=${exec_prefix}//nix/store/10pzq1a8fkh8q4sysj8n6mv0w0nl0miq-opencv-3.4.1/lib
260 # Libs: -L${exec_prefix}//nix/store/10pzq1a8fkh8q4sysj8n6mv0w0nl0miq-opencv-3.4.1/lib ...
262 # Note that ${exec_prefix} is set to $out but that $out is also appended to
263 # ${exec_prefix}. This causes linker errors in downstream packages so we strip
264 # of $out after the ${exec_prefix} prefix:
266 sed -i "s|{exec_prefix}/$out|{exec_prefix}|" \
267 "$out/lib/pkgconfig/opencv.pc"
270 hardeningDisable = [ "bindnow" "relro" ];
272 passthru = lib.optionalAttrs enablePython { pythonPath = []; };
275 description = "Open Computer Vision Library with more than 500 algorithms";
276 homepage = "https://opencv.org/";
277 license = with licenses; if enableUnfree then unfree else bsd3;
278 maintainers = with maintainers; [mdaiter basvandijk];
279 platforms = with platforms; linux ++ darwin;