Build a derivation with CMake?

Hello, I’m trying to build a derivation using CMake but it fails with a CXX compiler missing:

@nix { "action": "setPhase", "phase": "unpackPhase" }
unpacking sources
unpacking source archive /nix/store/26c3pksp0hga5p06mbf4y6v9y3f6j7bx-source
source root is source
@nix { "action": "setPhase", "phase": "patchPhase" }
patching sources
@nix { "action": "setPhase", "phase": "updateAutotoolsGnuConfigScriptsPhase" }
updateAutotoolsGnuConfigScriptsPhase
@nix { "action": "setPhase", "phase": "configurePhase" }
configuring
fixing cmake files...
cmake flags: -GNinja -DCMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY=OFF -DCMAKE_FIND_USE_PACKAGE_REGISTRY=OFF -DCMAKE_EXPORT_NO_PACKAGE_REGISTRY=ON -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=OFF -DCMAKE_INSTALL_LOCALEDIR=/nix/store/wqsazxsvf7pqmx7y24hp18h5hkwinppy-vcpkg-14e7bb4ae24616ec54ff6b2f6ef4e8659434ea44/share/locale -DCMAKE_INSTALL_LIBEXECDIR=/nix/store/wqsazxsvf7pqmx7y24hp18h5hkwinppy-vcpkg-14e7bb4ae24616ec54ff6b2f6ef4e8659434ea44/libexec -DCMAKE_INSTALL_LIBDIR=/nix/store/wqsazxsvf7pqmx7y24hp18h5hkwinppy-vcpkg-14e7bb4ae24616ec54ff6b2f6ef4e8659434ea44/lib -DCMAKE_INSTALL_DOCDIR=/nix/store/wqsazxsvf7pqmx7y24hp18h5hkwinppy-vcpkg-14e7bb4ae24616ec54ff6b2f6ef4e8659434ea44/share/doc/vcpkg -DCMAKE_INSTALL_INFODIR=/nix/store/wqsazxsvf7pqmx7y24hp18h5hkwinppy-vcpkg-14e7bb4ae24616ec54ff6b2f6ef4e8659434ea44/share/info -DCMAKE_INSTALL_MANDIR=/nix/store/wqsazxsvf7pqmx7y24hp18h5hkwinppy-vcpkg-14e7bb4ae24616ec54ff6b2f6ef4e8659434ea44/share/man -DCMAKE_INSTALL_OLDINCLUDEDIR=/nix/store/wqsazxsvf7pqmx7y24hp18h5hkwinppy-vcpkg-14e7bb4ae24616ec54ff6b2f6ef4e8659434ea44/include -DCMAKE_INSTALL_INCLUDEDIR=/nix/store/wqsazxsvf7pqmx7y24hp18h5hkwinppy-vcpkg-14e7bb4ae24616ec54ff6b2f6ef4e8659434ea44/include -DCMAKE_INSTALL_SBINDIR=/nix/store/wqsazxsvf7pqmx7y24hp18h5hkwinppy-vcpkg-14e7bb4ae24616ec54ff6b2f6ef4e8659434ea44/sbin -DCMAKE_INSTALL_BINDIR=/nix/store/wqsazxsvf7pqmx7y24hp18h5hkwinppy-vcpkg-14e7bb4ae24616ec54ff6b2f6ef4e8659434ea44/bin -DCMAKE_INSTALL_NAME_DIR=/nix/store/wqsazxsvf7pqmx7y24hp18h5hkwinppy-vcpkg-14e7bb4ae24616ec54ff6b2f6ef4e8659434ea44/lib -DCMAKE_POLICY_DEFAULT_CMP0025=NEW -DCMAKE_OSX_SYSROOT= -DCMAKE_FIND_FRAMEWORK=LAST -DCMAKE_STRIP= -DCMAKE_RANLIB= -DCMAKE_AR= -DCMAKE_C_COMPILER= -DCMAKE_CXX_COMPILER= -DCMAKE_INSTALL_PREFIX=/nix/store/wqsazxsvf7pqmx7y24hp18h5hkwinppy-vcpkg-14e7bb4ae24616ec54ff6b2f6ef4e8659434ea44  
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:48 (project):
  No CMAKE_CXX_COMPILER could be found.

  Tell CMake where to find the compiler by setting either the environment
  variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
  to the compiler, or to the compiler name if it is in the PATH.


-- Configuring incomplete, errors occurred!
See also "/tmp/nix-build-vcpkg-14e7bb4ae24616ec54ff6b2f6ef4e8659434ea44.drv-0/source/build/CMakeFiles/CMakeOutput.log".
See also "/tmp/nix-build-vcpkg-14e7bb4ae24616ec54ff6b2f6ef4e8659434ea44.drv-0/source/build/CMakeFiles/CMakeError.log".

As you can see, the arguments for CMAKE_C_COMPILER and CMAKE_CXX_COMPILER is missing. Here’s my default.nix file:

{
  stdenv,
  fetchFromGitHub,
  ninja,
  cmake,
  lib,
  git,
  cacert,
  runtimeShell
}:

stdenv.mkDerivation rec {
  pname = "vcpkg";
  version = "14e7bb4ae24616ec54ff6b2f6ef4e8659434ea44";

  src = fetchFromGitHub {
    owner = "microsoft";
    repo = "vcpkg-tool";
    rev = "2022-07-21";
    sha256 = "03x623vzcax2f56i30m10i2wy3zm5ny3qmj4dq2qpqwsfgfh3b89";
  };

  vcpkg_src = fetchFromGitHub {
    owner = "microsoft";
    repo = "vcpkg";
    rev = "2022.07.25";
    sha256 = "18xdnagps0fa36i7gwgrcm526lnfa4zmb5agzxxmzp7ph6pczrnj";
  };

  buildInputs = [cmake ninja git cacert];

  vcpkgScript = ''
    #!${runtimeShell}
    vcpkg_hash=$(echo -n "${placeholder "out"}" | sha256sum | cut -f1 -d ' ')
    vcpkg_root_path="$HOME/.vcpkg/roots/$vcpkg_hash"
    if [[ ! -d "$vcpkg_root_path" ]]; then
      mkdir -p "$vcpkg_root_path"
      touch "$vcpkg_root_path/.vcpkg-root"
      ln -s ${placeholder "out"}/share/vcpkg/{docs,ports,scripts,triplets,versions} "$vcpkg_root_path/"
      ln -s ${placeholder "out"}/bin/vcpkg "$vcpkg_root_path/"
    fi
    VCPKG_DOWNLOADS="$vcpkg_root_path/downloads" VCPKG_ROOT="$vcpkg_root_path" ${placeholder "out"}/share/vcpkg/vcpkg "$@"
  '';
  
  passAsFile = [ "vcpkgScript" ];
  installPhase = ''
    cmake --build . --target=install --config=Release
    mkdir -p $out/share/vcpkg
    cp --preserve=mode -r ${vcpkg_src}/{docs,ports,scripts,triplets,.vcpkg-root,versions} $out/share/vcpkg
    mv $out/bin/vcpkg $out/share/vcpkg/vcpkg
    cp $vcpkgScriptPath $out/bin/vcpkg
    chmod +x $out/bin/vcpkg
    touch $out/share/vcpkg/vcpkg.disable-metrics
  '';
  enableParallelBuilding = true;
}

Is there a build input missing or something like that?

I found the solution! You can override a CMake argument using the follwing:

  cmakeFlags = [
    "-DCMAKE_CXX_COMPILER=${clang}/bin/clang++"
  ];

Then simply add clang to the build inputs and everything seems to work!

Seems weird there are many variables that nix set as empty like CMAKE_AR, CMAKE_RANLIB and all of that. I had to set it manually.

CMake should be in nativeBuildInputs and then it should be setup better.

2 Likes

Wow!! Yeah it worked like magic:

cxx = if stdenv.isLinux then gcc else clang;
nativeBuildInputs = [cmake ninja cxx];
buildInputs = [git cacert];

stdenv.mkDerivation will expose the CXX for you, you probably only need

nativeBuildInputs = [ cmake ninja ];
buildInputs = [ git cacert ];

I have a video on nix + cmake: Nix - Packaging lib60870 with CMake. - YouTube

2 Likes