I need java bindings for opencv to be able to use nix in a java/scala project at work. I tried many things and none of them work. Here is the nix flake I am using:
{
description = "A template that shows all standard flake outputs";
inputs.nixpkgs.url = "github:NixOS/nixpkgs";
outputs = {
self,
nixpkgs,
...
}: let
system = "x86_64-linux";
pkgs = import nixpkgs {inherit system;};
in {
packages.${system}.default = (
pkgs.opencv4.overrideAttrs
(oldAttr: {
buildInputs = oldAttr.buildInputs ++ (with pkgs; [ant jdk jre]);
cmakeFlags = oldAttr.cmakeFlags ++ ["-DWITH_JAVA=ON"];
})
);
};
}
In the package definition in nixpkgs there is no option like enableJava. Also, I didnāt find the java bindings in the default build (cached on cache.nixos.org) version of the package.
Can anyone tell me how to get a opencv build with java bindings? I also would appreciate any tips which would push me in the right direction. Thank you!
Can you tell me how you found this option? I did not find this option in package definition for the opencv derivation.
I tried it with enableJava, but unfortunately it did not build the java bindings. I am not sure what is happening there. I also added openjdk and ant to the buildInputs, however it still did not do the trick. I also created a nix shell with all the dependencies and tried doing it manually, however the cmake output says, that it does not find Java, although ant and JNI are found. I am not sure if it is an issue is here ⦠Do you have any further tips? I really appreciate your help!
This is a good way to figure out what other packages are doing. Chances are usually pretty good that others have run into similar problems.
By this - I mean that you should see what that package is doing, and how itās setting and using the enableJava option. It uses it to add openjdk into nativeBuildInputs ā native is important, it makes the dependency available during build time.
It would also be helpful to see what the most recent version of your code is now.
I see, that actually is quite helpful. Thanks @aos !
Here is the latest version of my flake.nix file for building OpenCV, the devShell is just for testing and debugging, but so far I havenāt figured out how to build with java bindings:
I followed you advice and added java and ant to the build inputs. Here I added corretto21 instead of openjdk, but it does not work with openjdk either.
In the devShell, I did some testing and found out that the issue most likely comes from CMake. OpenCV uses the two modules FindJNI and FindJava from CMake. As I understand, the CMake modules define some base directories to search, however this is a collection for different Linux distributions such as Ubuntu, OpenSUSE, etc. The patches I tried havenāt worked. I also, did not find anything in nixpkgs which has done this before.
Do you have any other suggestions or hints to get this to work? I really do appreciate you taking the time to help me out!
no unfortunately I havenāt. I tried patching CMake, as I think it is a CMake module which cannot find the java binary, but I have not had any success there.
@me-it-is Yes, I managed to solve the issue, but have not had any time to make a pull request for nixpkgs. It was really difficult to find the problem. I had to dig deep into the source code. But fortunately the solution is really easy!
OpenCV needs python to build the java bindings, so just adding python, ant, and openjdk to the nativeBuildInputs makes the java bindings build! This is a minimal working flake.nix example with numpy. You probably dontā even need numpy, but here it is:
{
description = "A template that shows all standard flake outputs";
inputs.nixpkgs.url = "github:NixOS/nixpkgs";
outputs = {
self,
nixpkgs,
...
}: let
system = "x86_64-linux";
pkgs = import nixpkgs {
inherit system;
};
in {
packages.${system}.default = (
pkgs.opencv4.overrideAttrs
(oldAttr: {
nativeBuildInputs =
oldAttr.nativeBuildInputs
++ (with pkgs; [
ant
openjdk # or corretto21, or ...
python3
python3Packages.numpy
]);
cmakeFlags =
oldAttr.cmakeFlags
++ [
"-DBUILD_JAVA=ON"
];
})
);
};
}
After doing a nix build -L -vvv you should get an output like this, which indicates the java bindings are building: