Nix-shell doesn't put libmysqlclient into NIX_LDFLAGS

I’m trying to compile a C++ application. I came up with this shell.nix:

{ pkgs ? import <nixpkgs> {} }:
  pkgs.mkShell {
    nativeBuildInputs = with pkgs.buildPackages; [ scons gcc13 boost libmysqlclient libxcrypt ];
}

In the shell, I have Boost and libcrypt in NIX_LDFLAGS:

$ echo $NIX_LDFLAGS | sed 's/ /\n/g' | sort | uniq
-L/nix/store/08n25j4vxyjidjf93fyc15icxwrxm2p8-libidn2-2.3.4/lib
-L/nix/store/1ryrrnjsxnj48hic5bk03hgvcp38sbli-zstd-1.5.5/lib
-L/nix/store/4vqq3gb4qwr1s2vwgx09bl6slqxkh6v6-libssh2-1.11.0/lib
-L/nix/store/5ii12aznjvbg2m427nvp9sf9shisggz3-boost-1.81.0/lib
-L/nix/store/7d2qkskb5yg9p5lgvspapw15dkc3y3zx-libkrb5-1.20.2/lib
-L/nix/store/8xgb8phqmfn9h971q7dg369h647i1aa0-zlib-1.3/lib
-L/nix/store/fs9zd6sa5akbvc4d46war0402lbwxh5n-curl-8.4.0/lib
-L/nix/store/ixvka49i2mjnxzk8l8anykc1idilws1r-libxcrypt-4.4.36/lib
-L/nix/store/k3d7ny5h682kixy91iw7sm653kxyqr8d-openssl-3.0.12/lib
-L/nix/store/ksdrjk9519bx41hbam04xpgmgg4s6q5i-nghttp2-1.57.0-lib/lib
-L/nix/store/qmaqgf4cj8mkfc98d6pd3ardz092f146-brotli-1.1.0-lib/lib
-L/nix/store/qp5zys77biz7imbk6yy85q5pdv7qk84j-python3-3.11.6/lib
/nix/store/ywh5siz0c4q5wxipnqkkn81s63h9rcni-nix-shell/lib
-rpath

However, /nix/store/48prp9546sqyg3mg6vylb011dxbz05qg-mariadb-connector-c-3.3.5/lib/mariadb (or whichever) is not added to the shell, so my linking fails, unless I add it manually to my g++ command line.

What am I doing wrong?

You should not rely on NIX_LDFLAGS – that is more of a fallback hack. The app’s build system should use pkg-config to discover the proper compiler flags instead:

https://dev.mysql.com/blog-archive/the-client-library-part-3-building-mysql-client-applications/

Also, mkShell will already pull in a compiler so you do not need to specify it explicitly (I would not be surprised if it caused some issues). If you need a specific GCC version, you can override the stdenv used by mkShell with gcc13Stdenv.

Finally, even though mkShell currently does not enforce the distinction, runtime dependencies (that will execute on the host platform) go to buildInputs, nativeBuildInputs should contain dependencies that will execute on the build platform (scons and pkg-config in this case).

Thank you so much! Everything makes sense now.

1 Like