Lazbuild tries to link on the host OS libraries

Hello

I am trying to package a software built with lazarus IDE.

I can compile correctly with nixos, however when I try to compile it on nix on top of archlinux, it fails.

this is one of my first attempts to package software for nix, so maybe you’ll find it is not so clean, in that case tell me how I could improve it…

Here are the files I use:

$ cat nix/default-gtk.nix
with import <nixpkgs> {};
libsForQt5.callPackage ./build-gtk.nix { }
$ cat nix/build-gtk.nix 
{stdenv, lib, autoPatchelfHook, lazarus, fpc, pango, atk, alsa-lib, glib, gdk-pixbuf, gtk2, cairo, glibc ,libX11, fetchurl,p7zip}:
 stdenv.mkDerivation rec {

  name = "AyEmul";
  src = fetchurl {
    url = "https://bulba.untergrund.net/Ay_Emul29b32.src.7z";
    sha256 = "sha256-g4zVhpbQx9hcTfgvceoRSbN+MRV1rPzWlUnDR0lfSXY=";
  };

  nativeBuildInputs = [ autoPatchelfHook lazarus fpc glibc cairo pango atk alsa-lib glib gdk-pixbuf gtk2] ;

  NIX_LDFLAGS = "--as-needed -rpath ${lib.makeLibraryPath buildInputs}";
  sourceRoot = ".";
  unpackCmd = ''
    ${p7zip}/bin/7zr x $curSrc
  '';
    buildInputs = [ pango cairo glib atk gtk2 libX11 gdk-pixbuf p7zip ];
    buildPhase = ''
      ldir="${lazarus}/"
      HOME=/tmp/lazhome lazbuild Ay_Emul.lpi --lazarusdir="$ldir/share/lazarus/"
    '';
    installPhase = ''
      mkdir -p $out/bin/
      cp ./lib/x86_64-linux/Ay_Emul $out/bin/Ay_Emul
    '';
}

I use this command:

$ nix-env -f nix/default-gtk.nix -i

It fails with:

(9022) Compiling resource /tmp/nix-build-AyEmul.drv-0/lib/x86_64-linux/Ay_Emul.or
(9015) Linking /tmp/nix-build-AyEmul.drv-0/lib/x86_64-linux/Ay_Emul
/nix/store/rq6bh3qfrqnyqwik0w3q6w180zg3w2pa-binutils-2.38/bin/ld: /lib//libcairo.so: undefined reference to `hypot@GLIBC_2.35'
/tmp/nix-build-AyEmul.drv-0/Ay_Emul.lpr(69,1) Error: (9013) Error while linking
/tmp/nix-build-AyEmul.drv-0/Ay_Emul.lpr(69,1) Fatal: (10026) There were 1 errors compiling module, stopping

It seems that lazarus used /lib/libcairo.so from my arch and not the one from the nix store

Do you know how I could solve this ?

Thank you

Make sure the sandbox is enabled: nix.conf

Your derivation looks ok, but consider using alejandra or nixpkgs-format; you need some line breaks in there :wink:

Oh, and don’t use rec if you can possibly avoid it. I don’t think you actually use it in this case, you should be able to just remove it.

hello and thanks for your reply, the sandbox was not enabled, you’re right.
I thought there was a cleaner way to avoid my problem, even with sandbox disabled.

Thank you !

You’d hope, but ldd is very eager to pick whatever library it finds first, often even with LD_LIBRARY_PATH set.

This is a large part of why hermetic/sandboxed builds are such a big deal, just think of all the random libraries you probably on accident pull in when building without, and what people actively ship in prebuilt binaries!