Building a package using gtk2 and cmake

I’m trying to make a package for a C project of mine, but I’m having trouble getting the code to build on NixOS.

As an example, I have the following CMakeLists.txt

cmake_minimum_required(VERSION 2.8.12)
project( test )

find_package(GTK2 2.6 REQUIRED gtk)

if(GTK2_FOUND)
	message( STATUS "GTK-2 found")
else()
	message(FATAL_ERROR "GTK-2 not found")
endif()

My default.nix is

{mkDerivation, cmake, gtk2, pkg-config} :

mkDerivation {
  name = "test";
  src = ./.;
  nativeBuildInputs = [ cmake pkg-config ];
  buildInputs = [ gtk2 ];
}

and I also have a shell.nix to make things easier:

with import <nixpkgs> {};
stdenv.mkDerivation {
  name = "test";
  buildInputs = [ 
    (callPackage ./default.nix {mkDerivation = stdenv.mkDerivation ; })
    ];
}

When calling nix-shell I obtain the following error:

this derivation will be built:
  /nix/store/cywgm8c8r367wx0jj51z4isl58nvp4d3-test.drv
building '/nix/store/cywgm8c8r367wx0jj51z4isl58nvp4d3-test.drv'...
unpacking sources
unpacking source archive /nix/store/lp9pxrrzh6rxyiamh8dqvnpj7r407133-gtk-test
source root is gtk-test
patching sources
configuring
fixing cmake files...
cmake flags: -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/jkpp4hvcwrndgznf75nvbbbxr5jnziaa-test/share/locale -DCMAKE_INSTALL_LIBEXECDIR=/nix/store/jkpp4hvcwrndgznf75nvbbbxr5jnziaa-test/libexec -DCMAKE_INSTALL_LIBDIR=/nix/store/jkpp4hvcwrndgznf75nvbbbxr5jnziaa-test/lib -DCMAKE_INSTALL_DOCDIR=/nix/store/jkpp4hvcwrndgznf75nvbbbxr5jnziaa-test/share/doc/test -DCMAKE_INSTALL_INFODIR=/nix/store/jkpp4hvcwrndgznf75nvbbbxr5jnziaa-test/share/info -DCMAKE_INSTALL_MANDIR=/nix/store/jkpp4hvcwrndgznf75nvbbbxr5jnziaa-test/share/man -DCMAKE_INSTALL_OLDINCLUDEDIR=/nix/store/jkpp4hvcwrndgznf75nvbbbxr5jnziaa-test/include -DCMAKE_INSTALL_INCLUDEDIR=/nix/store/jkpp4hvcwrndgznf75nvbbbxr5jnziaa-test/include -DCMAKE_INSTALL_SBINDIR=/nix/store/jkpp4hvcwrndgznf75nvbbbxr5jnziaa-test/sbin -DCMAKE_INSTALL_BINDIR=/nix/store/jkpp4hvcwrndgznf75nvbbbxr5jnziaa-test/bin -DCMAKE_INSTALL_NAME_DIR=/nix/store/jkpp4hvcwrndgznf75nvbbbxr5jnziaa-test/lib -DCMAKE_POLICY_DEFAULT_CMP0025=NEW -DCMAKE_OSX_SYSROOT= -DCMAKE_FIND_FRAMEWORK=LAST -DCMAKE_STRIP=/nix/store/3d9zjv5vaqjxb9kwbdqsd194alwm97x1-gcc-wrapper-11.3.0/bin/strip -DCMAKE_RANLIB=/nix/store/3d9zjv5vaqjxb9kwbdqsd194alwm97x1-gcc-wrapper-11.3.0/bin/ranlib -DCMAKE_AR=/nix/store/3d9zjv5vaqjxb9kwbdqsd194alwm97x1-gcc-wrapper-11.3.0/bin/ar -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_INSTALL_PREFIX=/nix/store/jkpp4hvcwrndgznf75nvbbbxr5jnziaa-test  
-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /nix/store/3d9zjv5vaqjxb9kwbdqsd194alwm97x1-gcc-wrapper-11.3.0/bin/gcc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /nix/store/3d9zjv5vaqjxb9kwbdqsd194alwm97x1-gcc-wrapper-11.3.0/bin/g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Some or all of the gtk libraries were not found. (missing: GTK2_GDKCONFIG_INCLUDE_DIR GTK2_GLIBCONFIG_INCLUDE_DIR) 
CMake Error at CMakeLists.txt:9 (message):
  GTK-2 not found


-- Configuring incomplete, errors occurred!
See also "/build/gtk-test/build/CMakeFiles/CMakeOutput.log".
error: builder for '/nix/store/cywgm8c8r367wx0jj51z4isl58nvp4d3-test.drv' failed with exit code 1;
       last 10 log lines:
       > -- Check for working CXX compiler: /nix/store/3d9zjv5vaqjxb9kwbdqsd194alwm97x1-gcc-wrapper-11.3.0/bin/g++ - skipped
       > -- Detecting CXX compile features
       > -- Detecting CXX compile features - done
       > -- Some or all of the gtk libraries were not found. (missing: GTK2_GDKCONFIG_INCLUDE_DIR GTK2_GLIBCONFIG_INCLUDE_DIR)
       > CMake Error at CMakeLists.txt:9 (message):
       >   GTK-2 not found
       >
       > 
       > -- Configuring incomplete, errors occurred!
       > See also "/build/gtk-test/build/CMakeFiles/CMakeOutput.log".
       For full logs, run 'nix log /nix/store/cywgm8c8r367wx0jj51z4isl58nvp4d3-test.drv'.

It seems cmake find some parts of GTK2, but not all, and so it fails on the if(GTK2_FOUND) instead of on find_package(GTK2 2.6 REQUIRED gtk) (which is where it fails if I remove gtk2 from buildInputs).

Am I doing something wrong in cmake? Or do I need to install additional packages other than gtk2? Any help or pointers would be appreciated.

The default.nix should look like this

{ stdenv, cmake, gtk2, pkg-config }:

stdenv.mkDerivation {
  name = "test";
  src = ./.;
  nativeBuildInputs = [ cmake pkg-config ];
  buildInputs = [ gtk2 ];
}

to fix the compile error I would try https://github.com/NixOS/nixpkgs/blob/2bd7266a48fffdd8f26eba4c5533bbdf078d3db6/pkgs/applications/emulators/dolphin-emu/default.nix#L66-L67

1 Like

Thanks, so I changed my default.nix to

{stdenv, cmake, gtk2, pkg-config, glib} :

stdenv.mkDerivation {
  name = "test";
  src = ./.;
  nativeBuildInputs = [ cmake pkg-config ];
  buildInputs = [ gtk2 glib ];
  cmakeFlags = 
    [ "-DGTK2_GLIBCONFIG_INCLUDE_DIR=${glib.out}/lib/glib-2.0/include"
      "-DGTK2_GDKCONFIG_INCLUDE_DIR=${gtk2.out}/lib/gtk-2.0/include"
    ];
}

and it now works!