1 { lib, stdenv, fetchFromGitHub, cmake, pkg-config, unzip
3 , enableGtk2 ? false, gtk2
4 , enableJPEG ? true, libjpeg
5 , enablePNG ? true, libpng
6 , enableTIFF ? true, libtiff
7 , enableEXR ? (!stdenv.isDarwin), openexr, ilmbase
8 , enableFfmpeg ? false, ffmpeg
9 , enableGStreamer ? false, gst_all_1
10 , enableEigen ? true, eigen
11 , enableUnfree ? false
12 , AVFoundation, Cocoa, QTKit, Accelerate
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 enableGtk2 gtk2
47 ++ lib.optional enableJPEG libjpeg
48 ++ lib.optional enablePNG libpng
49 ++ lib.optional enableTIFF libtiff
50 ++ lib.optionals enableEXR [ openexr ilmbase ]
51 ++ lib.optional enableFfmpeg ffmpeg
52 ++ lib.optionals enableGStreamer (with gst_all_1; [ gstreamer gst-plugins-base ])
53 ++ lib.optional enableEigen eigen
54 ++ lib.optionals stdenv.isDarwin [ AVFoundation Cocoa QTKit Accelerate ]
57 nativeBuildInputs = [ cmake pkg-config unzip ];
59 env.NIX_CFLAGS_COMPILE = lib.optionalString enableEXR "-I${ilmbase.dev}/include/OpenEXR";
62 (opencvFlag "TIFF" enableTIFF)
63 (opencvFlag "JPEG" enableJPEG)
64 (opencvFlag "PNG" enablePNG)
65 (opencvFlag "OPENEXR" enableEXR)
66 (opencvFlag "GSTREAMER" enableGStreamer)
67 ] ++ lib.optional (!enableUnfree) "-DBUILD_opencv_nonfree=OFF";
69 hardeningDisable = [ "bindnow" "relro" ];
71 # Fix pkg-config file that gets broken with multiple outputs
73 sed -i $dev/lib/pkgconfig/opencv.pc -e "s|includedir_old=.*|includedir_old=$dev/include/opencv|"
74 sed -i $dev/lib/pkgconfig/opencv.pc -e "s|includedir_new=.*|includedir_new=$dev/include|"
78 description = "Open Computer Vision Library with more than 500 algorithms";
79 homepage = "https://opencv.org/";
80 license = if enableUnfree then licenses.unfree else licenses.bsd3;
81 maintainers = with maintainers; [ ];
82 platforms = platforms.unix;