# Overrides potentially not being applied

**URL:** https://discourse.nixos.org/t/overrides-potentially-not-being-applied/8890
**Category:** Guides
**Created:** [September 4, 2020, 10:40pm UTC](https://discourse.nixos.org/t/overrides-potentially-not-being-applied/8890 "2020-09-04T22:40:55Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jeff-hykin](https://discourse.nixos.org/user_avatar/discourse.nixos.org/jeff-hykin/32/2827_2.png) [@jeff-hykin](https://discourse.nixos.org/u/jeff-hykin)
#### Post date: [September 4, 2020, 10:40pm UTC](https://discourse.nixos.org/t/overrides-potentially-not-being-applied/8890/1 "2020-09-04T22:40:55Z")

</div>

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:

- [OpenCV with Cuda in nix-shell](https://discourse.nixos.org/t/opencv-with-cuda-in-nix-shell/7358)
- [PR about contrib attribute for opencv](https://github.com/NixOS/nixpkgs/pull/30946)
- [Failing to override network dependency for bson](https://discourse.nixos.org/t/failing-to-override-network-dependency-for-bson/6588)
- [The official documentation for `.override`](https://nixos.org/manual/nixpkgs/stable/#sec-pkg-override)
- [How to override a Haskell package in shell.nix](https://discourse.nixos.org/t/how-to-override-a-haskell-package-in-shell-nix/2907)
- [Override a transitive emacs dependency - #4 by codygman](https://discourse.nixos.org/t/override-a-transitive-emacs-dependency/6700/4)
- [Nix Haskell Development (2020)](https://discourse.nixos.org/t/nix-haskell-development-2020/6170)

An inline example of the opencv-contrib override from [here](https://github.com/NixOS/nixpkgs/pull/30946) is shown as:

```auto
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](https://discourse.nixos.org/t/failing-to-override-network-dependency-for-bson/6588/2) that helped me get this far)

```auto
let
    # 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];
    };
in
    nixpkgs.mkShell {
        buildInputs = [
            nixpkgs.cmake
            nixpkgs.python37
            nixpkgs.python37Packages.setuptools
            nixpkgs.python37Packages.pip
            nixpkgs.opencv4Custom
        ];
    }

```

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:

```python
>>> 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__'/nix/store/37ig12v4y2v349azyia6q7lh4nx0qbyq-opencv-4.3.0/lib/python3.7/site-packages'
# sanity check^ that its importing the correct cv2 

```

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

```auto
/nix/store/37ig12v4y2v349azyia6q7lh4nx0qbyq-opencv-4.3.0
/nix/store/d83bjc8rgg04j47v6k6yfx5j42mxf2h5-opencv-4.3.0
/nix/store/dw2smyxhr51vfn0jq3nf027v6rfhppx2-opencv-4.3.0

```

## Questions:

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

---

<div class="post-metadata">

### Author: ![dsxmachina](https://discourse.nixos.org/user_avatar/discourse.nixos.org/dsxmachina/32/2499_2.png) [@dsxmachina](https://discourse.nixos.org/u/dsxmachina)
#### Post date: [September 6, 2020, 8:59am UTC](https://discourse.nixos.org/t/overrides-potentially-not-being-applied/8890/2 "2020-09-06T08:59:58Z")

</div>

Hmm,  
I have not worked with overlays so far, so I can not help you with that.  
But:  
The default value for ‘enableContrib’ is true according to the [default.nix](https://github.com/NixOS/nixpkgs/blob/193a6a2307b7b29aa11bee309d4aa41840686ab0/pkgs/development/libraries/opencv/4.x.nix) of opencv.

I can work with the contrib modules like in your example

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

```

With just having the following in my configuration.nix:

```auto
 my-python-packages = python-packages: with python-packages; [ 
        # ...
        opencv4
        numpy
    ];
python-with-my-packages = python3.withPackages my-python-packages;

```

You can also use this inside a nix shell:

```auto
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 😃 )

---

<div class="post-metadata">

### Author: ![jeff-hykin](https://discourse.nixos.org/user_avatar/discourse.nixos.org/jeff-hykin/32/2827_2.png) [@jeff-hykin](https://discourse.nixos.org/u/jeff-hykin)
#### Post date: [September 6, 2020, 9:14pm UTC](https://discourse.nixos.org/t/overrides-potentially-not-being-applied/8890/3 "2020-09-06T21:14:12Z")

</div>

> [@dsxmachina](#):
>
> I have not worked with overlays so far,

Me either haha, your post is helpful though.

> [@dsxmachina](#):
>
> The default value for ‘enableContrib’ is true according to the [default.nix](https://github.com/NixOS/nixpkgs/blob/193a6a2307b7b29aa11bee309d4aa41840686ab0/pkgs/development/libraries/opencv/4.x.nix) of opencv.

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 😐💢 no idea why.

> <https://github.com/NixOS/nixpkgs/blob/193a6a2307b7b29aa11bee309d4aa41840686ab0/pkgs/development/libraries/opencv/4.x.nix#L258>

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

---

<div class="post-metadata">

### Author: ![dsxmachina](https://discourse.nixos.org/user_avatar/discourse.nixos.org/dsxmachina/32/2499_2.png) [@dsxmachina](https://discourse.nixos.org/u/dsxmachina)
#### Post date: [September 7, 2020, 8:39am UTC](https://discourse.nixos.org/t/overrides-potentially-not-being-applied/8890/4 "2020-09-07T08:39:13Z")

</div>

Nice.  
Do you have opencv installed only via nix ?  
I found [this stackoverflow post](https://stackoverflow.com/questions/29117034/opencv-videowriter-write-function-fails-on-mac-os-in-c) that says, that there is no VideoWriter-Object on mac,  
where [this one](https://stackoverflow.com/questions/10605163/opencv-videowriter-under-osx-producing-no-output) suggests the opposite.

Nevertheless, I would also be interested in the overlay solution 😃

---

<div class="post-metadata">

### Author: ![jeff-hykin](https://discourse.nixos.org/user_avatar/discourse.nixos.org/jeff-hykin/32/2827_2.png) [@jeff-hykin](https://discourse.nixos.org/u/jeff-hykin)
#### Post date: [September 7, 2020, 12:08pm UTC](https://discourse.nixos.org/t/overrides-potentially-not-being-applied/8890/5 "2020-09-07T12:08:54Z")

</div>

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.
