Overrides potentially not being applied

I’ve been successfully using opencv via nixpkgs.python37.opencv4
Now I need the contrib variant (opencv-contrib), but this is my first time doing an override.

Sources I tried learning from so far:

An inline example of the opencv-contrib override from here is shown as:

nix-build -E '(import ./. {}).opencv3.override {enableIpp = true; enableContrib = true;}' --option build-use-sandbox true

Based on that, here’s my current shell.nix, which builds opencv from source over the course of an hour. (Shoutout to @cdepillabout 's well-commented incredibly-helpful post that helped me get this far)

    # nixpkgs overlay, for opencv4
    overlay-for-opencv-contrib = self: super: {
        opencv4Custom = super.python37Packages.opencv4.override {
            enableContrib = true;
            enableFfmpeg = true; 
            enableGtk3 = true; 
            enableIpp = true;
            enableUnfree = true;
            # enableCuda = true; 
    # niv generates the sources.nix
    sources = import ./settings/nix/sources.nix;
    nixpkgs = import sources.nixpkgs {
        overlays = [ overlay-for-opencv-contrib ];
    nixpkgs.mkShell {
        buildInputs = [

I’ve tried several variations (opencv3, having only enableContrib = true; adding only enableFfmpeg = true; ) each of them taking an hour to compile.

They all build successfully, the issue is:

>>> import cv2

>>> cv2.VideoCapture
AttributeError: module 'cv2' has no attribute 'VideoCapture'
# VideoCapture should exist in the `contrib` variant of opencv
# but not the regular opencv (so this appears to be the regular opencv)

>>> cv2.__file__
# sanity check^ that its importing the correct cv2 

I currently have three builds of opencv-4.3.0, and I’ve tested all three



  1. Am I doing the override correctly? or am I just setting random attributes on opencv and building multiple variants of stock opencv
  2. Where do I go for package-specific issues? The opencv-related PR is on nixpkgs itself, but I’d want to create/look at issues specifically related to the opencv package

I have not worked with overlays so far, so I can not help you with that.
The default value for ‘enableContrib’ is true according to the default.nix of opencv.

I can work with the contrib modules like in your example

>>> import cv2
>>> cv2.VideoCapture
<class 'cv2.VideoCapture'>

With just having the following in my configuration.nix:

 my-python-packages = python-packages: with python-packages; [ 
        # ...
python-with-my-packages = python3.withPackages my-python-packages;

You can also use this inside a nix shell:

stdenv.mkDerivation {
   name = "python-environment";
   src  = if lib.inNixShell then null else ./.;
   buildInputs = [ python-with-my-packages ];

There seems to be no need to use a overlay to enable the contrib modules I think.

Hope this helps (although it answers none of your questions :smiley: )

1 Like

Me either haha, your post is helpful though.

Edit/Rewrite #3: After testing on Ubuntu and looking further into the file you linked I found the culprit. Even with Contrib enabled, the videoio tools are removed on Mac :neutral_face: :anger: no idea why.

Now my question is: How do I override that setting ( "-DBUILD_opencv_videoio=OFF") from a shell.nix

Do you have opencv installed only via nix ?
I found this stackoverflow post that says, that there is no VideoWriter-Object on mac,
where this one suggests the opposite.

Nevertheless, I would also be interested in the overlay solution :smiley:

I have opencv-contrib-python installed in both a pip virtual env and global pip, cv2.VideoCapture works with both. It seems I didn’t need to do a global install either on Mac, but that might not always be the case. I know however Ubuntu 18.04 needs apt install python3-opencv instead of just a pip module.

Sadly I had to switch to the system-python with opencv on Ubuntu too. While cv2.VideoCapture exists on nix-opencv, it errors out when trying to open a video.