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
- PR about contrib attribute for opencv
- Failing to override network dependency for bson
- The official documentation for
.override
- How to override a Haskell package in shell.nix
- Override a transitive emacs dependency - #4 by codygman
- Nix Haskell Development (2020)
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)
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:
>>> 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
/nix/store/37ig12v4y2v349azyia6q7lh4nx0qbyq-opencv-4.3.0
/nix/store/d83bjc8rgg04j47v6k6yfx5j42mxf2h5-opencv-4.3.0
/nix/store/dw2smyxhr51vfn0jq3nf027v6rfhppx2-opencv-4.3.0
Questions:
- Am I doing the override correctly? or am I just setting random attributes on
opencv
and building multiple variants of stockopencv
- 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