1 { lib, stdenv, fetchFromGitHub, cmake, pkg-config, unzip
3 , enablePython ? false, pythonPackages
4 , enableGtk2 ? false, gtk2
5 , enableJPEG ? true, libjpeg
6 , enablePNG ? true, libpng
7 , enableTIFF ? true, libtiff
8 , enableEXR ? (!stdenv.isDarwin), openexr, ilmbase
9 , enableFfmpeg ? false, ffmpeg_3
10 , enableGStreamer ? false, gst_all_1
11 , enableEigen ? true, eigen
16 opencvFlag = name: enabled: "-DWITH_${name}=${if enabled then "ON" else "OFF"}";
20 stdenv.mkDerivation rec {
24 src = fetchFromGitHub {
28 sha256 = "062js7zhh4ixi2wk61wyi23qp9zsk5vw24iz2i5fab2hp97y5zq3";
32 [ # Don't include a copy of the CMake status output in the
33 # build. This causes a runtime dependency on GCC.
37 # This prevents cmake from using libraries in impure paths (which causes build failure on non NixOS)
39 sed -i '/Add these standard paths to the search paths for FIND_LIBRARY/,/^\s*$/{d}' CMakeLists.txt
42 outputs = [ "out" "dev" ];
46 ++ lib.optional enablePython pythonPackages.python
47 ++ lib.optional enableGtk2 gtk2
48 ++ lib.optional enableJPEG libjpeg
49 ++ lib.optional enablePNG libpng
50 ++ lib.optional enableTIFF libtiff
51 ++ lib.optionals enableEXR [ openexr ilmbase ]
52 ++ lib.optional enableFfmpeg ffmpeg_3
53 ++ lib.optionals enableGStreamer (with gst_all_1; [ gstreamer gst-plugins-base ])
54 ++ lib.optional enableEigen eigen
55 ++ lib.optionals stdenv.isDarwin [ Cocoa QTKit ]
58 propagatedBuildInputs = lib.optional enablePython pythonPackages.numpy;
60 nativeBuildInputs = [ cmake pkg-config unzip ];
62 NIX_CFLAGS_COMPILE = lib.optionalString enableEXR "-I${ilmbase.dev}/include/OpenEXR";
65 (opencvFlag "TIFF" enableTIFF)
66 (opencvFlag "JPEG" enableJPEG)
67 (opencvFlag "PNG" enablePNG)
68 (opencvFlag "OPENEXR" enableEXR)
69 (opencvFlag "GSTREAMER" enableGStreamer)
72 hardeningDisable = [ "bindnow" "relro" ];
74 # Fix pkg-config file that gets broken with multiple outputs
76 sed -i $dev/lib/pkgconfig/opencv.pc -e "s|includedir_old=.*|includedir_old=$dev/include/opencv|"
77 sed -i $dev/lib/pkgconfig/opencv.pc -e "s|includedir_new=.*|includedir_new=$dev/include|"
80 passthru = lib.optionalAttrs enablePython { pythonPath = []; };
83 description = "Open Computer Vision Library with more than 500 algorithms";
84 homepage = "https://opencv.org/";
85 license = licenses.bsd3;
86 maintainers = with maintainers; [ ];
87 platforms = platforms.linux;