Pkgs.gtk3 cannot find Xlib.h, even with the package present

I am trying to build a Flutter desktop application with a nix-shell environment. I’ve met this error message in the process, and tried a bunch of packages, and manually using C_INCLUDE_PATH before posting here.
The issue can be replicated by

$ flutter create myapp
# write flake file to flake.nix
$ nix develop
$ flutter build linux

this throws the error:

/nix/store/nlhshd5j3wls7wgl97mn7bmd72nba1dd-gtk+3-3.24.34-dev/include/gtk-3.0/gdk/gdkx.h:30:10: fatal error: 'X11/Xlib.h' file not found
Building Linux application...                                           
Build process failed

Here’s my flake.nix:

Interestingly enough, flutter does know a thing about this issue: flutter doctor does add some info:

$ flutter doctor

[✗] Linux toolchain - develop for Linux desktop
    ✗ GTK 3.0 development libraries are required for Linux development.
      They are likely available from your distribution (e.g.: apt install libgtk-3-dev)

This is interesting, because if you do check the flake, gtk3 and gtk3-x11 are both installed.
Any help would be appreciated.

1 Like

I notice few issues with your shell:

  • You are mixing dependencies for build platform and for the runtime (host) platform in the packages (nativeBuildInputs) attribute. Build time dependencies go to nativeBuildInputs and runtime to buildInputs.
  • On Linux gtk3-x11 is the same thing as gtk3.

But neither should have an effect here.

The error appears to come from this line (string origin) so maybe try running pkg-config --exists $library for each of _requiredGtkLibraries.

Is that really true? I would expect stdenv not to include nativeBuildInputs in the header search path.

Also, you may want to add pkg-config to nativeBuildInputs.

pkgs.mkShell {
  buildInputs = with pkgs; [
    at-spi2-core.dev
    clang
    cmake
    dart
    dbus.dev
    flutter
    gtk3
    libdatrie
    libepoxy.dev
    libselinux
    libsepol
    libthai
    libxkbcommon
    ninja
    pcre
    pkg-config
    util-linux.dev
    xorg.libXdmcp
    xorg.libXtst
  ];
  shellHook = ''
    export LD_LIBRARY_PATH=${pkgs.libepoxy}/lib
  '';
};

This shell worked for me. I also had to delete the ./build directory.

Seems to be broken again with latest flutter:

Multiple devices found:
Linux (desktop) • linux  • linux-x64      • NixOS 23.05 (Stoat) 6.1.7
Chrome (web)    • chrome • web-javascript • Chromium 109.0.5414.119
[1]: Linux (linux)
[2]: Chrome (chrome)
Please choose one (To quit, press "q/Q"): 1
Launching lib/main.dart on Linux in debug mode...
Building Linux application...
Package libdeflate was not found in the pkg-config search path.
Perhaps you should add the directory containing `libdeflate.pc'
to the PKG_CONFIG_PATH environment variable
Package 'libdeflate', required by 'libtiff-4', not found
Package libdeflate was not found in the pkg-config search path.
Perhaps you should add the directory containing `libdeflate.pc'
to the PKG_CONFIG_PATH environment variable
Package 'libdeflate', required by 'libtiff-4', not found
Package libdeflate was not found in the pkg-config search path.
Perhaps you should add the directory containing `libdeflate.pc'
to the PKG_CONFIG_PATH environment variable
Package 'libdeflate', required by 'libtiff-4', not found
Package libdeflate was not found in the pkg-config search path.
Perhaps you should add the directory containing `libdeflate.pc'
to the PKG_CONFIG_PATH environment variable
Package 'libdeflate', required by 'libtiff-4', not found
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libgmodule-2.0.so.0, needed by /nix/store/kv7ih63pn9hn8p00wwi1mvg400ahsrc2-gtk+3-3.24.36/lib/libgtk-3.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libpangoft2-1.0.so.0, needed by /nix/store/kv7ih63pn9hn8p00wwi1mvg400ahsrc2-gtk+3-3.24.36/lib/libgtk-3.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libfontconfig.so.1, needed by /nix/store/kv7ih63pn9hn8p00wwi1mvg400ahsrc2-gtk+3-3.24.36/lib/libgtk-3.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libfribidi.so.0, needed by /nix/store/kv7ih63pn9hn8p00wwi1mvg400ahsrc2-gtk+3-3.24.36/lib/libgtk-3.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libatk-bridge-2.0.so.0, needed by /nix/store/kv7ih63pn9hn8p00wwi1mvg400ahsrc2-gtk+3-3.24.36/lib/libgtk-3.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libtracker-sparql-3.0.so.0, needed by /nix/store/kv7ih63pn9hn8p00wwi1mvg400ahsrc2-gtk+3-3.24.36/lib/libgtk-3.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libxkbcommon.so.0, needed by /nix/store/kv7ih63pn9hn8p00wwi1mvg400ahsrc2-gtk+3-3.24.36/lib/libgdk-3.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libwayland-client.so.0, needed by /nix/store/kv7ih63pn9hn8p00wwi1mvg400ahsrc2-gtk+3-3.24.36/lib/libgdk-3.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libwayland-cursor.so.0, needed by /nix/store/kv7ih63pn9hn8p00wwi1mvg400ahsrc2-gtk+3-3.24.36/lib/libgdk-3.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libwayland-egl.so.1, needed by /nix/store/kv7ih63pn9hn8p00wwi1mvg400ahsrc2-gtk+3-3.24.36/lib/libgdk-3.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libXrandr.so.2, needed by /nix/store/kv7ih63pn9hn8p00wwi1mvg400ahsrc2-gtk+3-3.24.36/lib/libgdk-3.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libXinerama.so.1, needed by /nix/store/kv7ih63pn9hn8p00wwi1mvg400ahsrc2-gtk+3-3.24.36/lib/libgdk-3.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libthai.so.0, needed by /nix/store/cd96gaj987p7n9kc5y3fm1fcsw774kja-pango-1.50.12/lib/libpango-1.0.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libfreetype.so.6, needed by /nix/store/liwyhk6q2j82db1p8ywl7ylq429vzrra-harfbuzz-6.0.0/lib/libharfbuzz.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libgraphite2.so.3, needed by /nix/store/liwyhk6q2j82db1p8ywl7ylq429vzrra-harfbuzz-6.0.0/lib/libharfbuzz.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libpixman-1.so.0, needed by /nix/store/yh7577a49x6y4hfngmii3clhzknq1k36-cairo-1.16.0/lib/libcairo-gobject.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libpng16.so.16, needed by /nix/store/yh7577a49x6y4hfngmii3clhzknq1k36-cairo-1.16.0/lib/libcairo-gobject.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libjpeg.so.62, needed by /nix/store/bqf7gag0r4k8g8fak9ss3wpg32ahk8i6-gdk-pixbuf-2.42.10/lib/libgdk_pixbuf-2.0.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libselinux.so.1, needed by /nix/store/0mcsib9r75kzr8lnwrfi3wm0jbs4751l-glib-2.74.3/lib/libgio-2.0.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libffi.so.8, needed by /nix/store/0mcsib9r75kzr8lnwrfi3wm0jbs4751l-glib-2.74.3/lib/libgobject-2.0.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libpcre2-8.so.0, needed by /nix/store/0mcsib9r75kzr8lnwrfi3wm0jbs4751l-glib-2.74.3/lib/libglib-2.0.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libXau.so.6, needed by /usr/lib/libxcb-shm.so.0, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libXdmcp.so.6, needed by /usr/lib/libxcb-shm.so.0, not found (try using -rpath or -rpath-link)
clang-11: error: linker command failed with exit code 1 (use -v to see invocation)
Exception: Build process failed