CMake can't find package

Hello,
I’m new to Nix and NixOS. I got through all the nix.dev tutorials and I’m now trying to apply nix to my own project. I’m using CMake and it’s find_package function to find installed packages on my system. I thought that it should be as easy as adding my dependencies to either the packages attribute in the mkShell or to buildInputs if im trying to build my package with mkDeviation. But I had no success on either. The packages i tried it with was eigen and nlopt.

My current idea is to create a develop environment with nix, the following dev.nix file is how i thought i might achive that, but so far no success. This file gets called from default.nix via pkgs.callPackage

{
  mkShell,
  lib,
  cmake,
  ninja,
  nlopt,
}:

mkShell {
  NAME = "mog-slam";
  VERSION = "0.1.0";

  packages = [
    cmake
    ninja
    nlopt
  ];

  shellHook = ''
    echo "Building $NAME $VERSION with"
    echo -n "- GCC "
    gcc --version | grep -oP '\d+\.\d+\.\d+'
    echo -n "- CMake "
    cmake --version | grep -oP '\d+\.\d+\.\d+'
    echo -n "- ninja "
    ninja --version | grep -oP '\d+\.\d+\.\d+'
  '';
}

Thanks for your help.

To do this, I usually package the project first with nix (stdenv.mkDerivation with nativeBuildInputs = [ cmake ]), and then declare a shell with mkShell { inputsFrom = [ <your derivation> ]}

I am currently working on examples for this:
example-adder/default.nix at 0068ffe3f627044d2ac3206146984439fd7c994d · nim65s/example-adder · GitHub for the derivation, and
example-adder/flake.nix at 0068ffe3f627044d2ac3206146984439fd7c994d · nim65s/example-adder · GitHub for the shells: one with the package already built to directly use it, and another with only the dependencies of the package to build it.

4 Likes

TIL inputsFrom is a thing, very useful. Thank you for mentioning that.

Thanks that help alot. And another point, coming from windows, was that I had to change the casing of some libraries to find them, e.g. find_package(nlopt REQUIRED) worked fine on windows, but on unix/NixOs I had to change it to find_package(NLopt REQUIRE).

1 Like