Cannot find library on statically-linked builds

Thanks a lot. Sorry, again random guess (hopefully someone qualified will jump in at some points ^^), but after a quick inspection of the libiio derivation, I found this patch:

$ cat pkgs/development/libraries/libiio/cmake-fix-libxml2-find-package.patch
diff --color -ur a/CMakeLists.txt b/CMakeLists.txt
--- a/CMakeLists.txt    2022-06-02 02:57:01.503340155 +0300
+++ b/CMakeLists.txt    2022-06-02 02:54:33.726941188 +0300
@@ -378,7 +378,7 @@
        # So, try first to find the CMake module provided by libxml2 package, then fallback
        # on the CMake's FindLibXml2.cmake module (which can lack some definition, especially
        # in static build case).
-       find_package(LibXml2 QUIET NO_MODULE)
+       find_package(LibXml2 QUIET MODULE)
        if(DEFINED LIBXML2_VERSION_STRING)
                set(LIBXML2_FOUND ON)
                set(LIBXML2_INCLUDE_DIR ${LIBXML2_INCLUDE_DIRS})
Seulement dans b: good.patch

which clearly mentions that things could go wrong in the static build case (which is exactly where things break down in your case) since it uses instead the CMake version of FindLibXml instead of the libxml2 package, which, apparently, contains less stuff. The derivation say that this patch could be removed once libxml2.dev: cmake module doesn't have a correct LIBXML2_LIBRARY_DIR · Issue #125008 · NixOS/nixpkgs · GitHub is properly fixed.

That being sayed, I’m not so sure why you get the error message cannot find -liio, and what is this /home/hftsai/CytoRepo/pifds/target/aarch64-unknown-linux-gnu/debug/deps/liblibiio_sys-dcd8fe422efbc76a.rlib (why is it not coming from /nix/store?). This seems to be related to the static version of iio

Also, if you want to force a -L, you can add options using something like:

NIX_LDFLAGS = "-L${lib.getLib json_c}/lib"

so maybe setting:

NIX_LDFLAGS = "-L${lib.getLib libiio}/lib"

or something like:

PKG_CONFIG_PATH = "${pkgs.libiio.dev}/lib/pkgconfig";

could help nix to find the proper libraries… But to be honest I don’t know if it will help.

Also, why do you say that it is the wrong architecture? It is not clear from the path you provide since when using the hack, you use now the aarch64 package which is not cross compiled.

Also, I tried to run your example, I can enter into a shell with:

$ nix develop

but then if I create a file:

fn main() {
    // Statements here are executed when the compiled binary is called.

    // Print text to the console.
    println!("Hello World!");
}

and compile statically, I get a different error:


[leo@bestos:/tmp/tt]$ rustc -C target-feature=+crt-static hello.rs 
error: linking with `cc` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/nix/store/s1mgys5x5rxm28801yby886a8byk231z-rust-complete-1.74.0/lib/rustlib/x86_64-unknown-linux-gnu/bin:/nix/store/90h6k8ylkgn81k10190v5c9ldyjpzgl9-gcc-wrapper-12.3.0/bin:/nix/store/hf2gy3km07d5m0p1lwmja0rg9wlnmyr7-gcc-12.3.0/bin:/nix/store/cx01qk0qyylvkgisbwc7d3pk8sliccgh-glibc-2.38-27-bin/bin:/nix/store/bblyj5b3ii8n6v4ra0nb37cmi3lf8rz9-coreutils-9.3/bin:/nix/store/1alqjnr40dsk7cl15l5sn5y2zdxidc1v-binutils-wrapper-2.40/bin:/nix/store/1fn92b0783crypjcxvdv6ycmvi27by0j-binutils-2.40/bin:/nix/store/s1mgys5x5rxm28801yby886a8byk231z-rust-complete-1.74.0/bin:/nix/store/gwvk9jj6ygnq8b0xnysrd18rb8kc25jj-aarch64-unknown-li
[…]
  = note: /nix/store/1fn92b0783crypjcxvdv6ycmvi27by0j-binutils-2.40/bin/ld: cannot find -lutil: No such file or directory
          /nix/store/1fn92b0783crypjcxvdv6ycmvi27by0j-binutils-2.40/bin/ld: cannot find -lrt: No such file or directory
          /nix/store/1fn92b0783crypjcxvdv6ycmvi27by0j-binutils-2.40/bin/ld: cannot find -lpthread: No such file or directory
          /nix/store/1fn92b0783crypjcxvdv6ycmvi27by0j-binutils-2.40/bin/ld: cannot find -lm: No such file or directory
          /nix/store/1fn92b0783crypjcxvdv6ycmvi27by0j-binutils-2.40/bin/ld: cannot find -ldl: No such file or directory
          /nix/store/1fn92b0783crypjcxvdv6ycmvi27by0j-binutils-2.40/bin/ld: cannot find -lc: No such file or directory
          /nix/store/1fn92b0783crypjcxvdv6ycmvi27by0j-binutils-2.40/bin/ld: cannot find -lc: No such file or directory
          collect2: error: ld returned 1 exit status

it it the procedure you followed?

Sorry for not being more useful…