Nix-shell build failure

Hi everyone,

I’m having a slight problem getting a nix-shell environment to build a library. I’m trying to make a cross-compiling environment (target aarch64-unknown-linux-gnu) with SDL2. I’ve tried tweaking my nix-shell by imitating various people online, and it might not be correct, but in its current state looks like this:

with import <nixpkgs> {
  crossSystem = {
    config = "aarch64-unknown-linux-gnu";
  };
};

mkShell {
  nativeBuildInputs = with buildPackages; [ ];
  buildInputs = [ SDL2 ];
}

Running $ nix-shell shell.nix, I obtain the following error message:

builder for '/nix/store/55daf92lqa0k9i8hmnix8ds64d2rqvsj-libXScrnSaver-1.2.3-aarch64-unknown-linux-gnu.drv' failed with exit code 1
cannot build derivation '/nix/store/ak4ph4pnay0c30apd3m4g8ls7mw7dnbx-SDL2-2.0.10-aarch64-unknown-linux-gnu.drv': 1 dependencies couldn't be built
error: build of '/nix/store/ak4ph4pnay0c30apd3m4g8ls7mw7dnbx-SDL2-2.0.10-aarch64-unknown-linux-gnu.drv' failed

Is this due to a problem with the SDL2 derivation (is that the right word?), or is my shell.nix misconfigured? It looks like similar issues in the past have been resolved by fixing broken packages. Could that be the case here, as well? If so, should I open an issue on Github?

Thank you!

:thinking: Not sure about nix-shell but I can help with nix-build for crosscompiled.

Assuming you are going to nix-build this expression:

let pkgs = import <nixpkgs> {
       crossSystem = {
         config = "aarch64-unknown-linux-gnu";
       };
     };
in pkgs.SDL

the error is not in SDL, but in it’s dependency libXScrnSaver.

Depending on error and dependency it may be easier to side-step cross-compiling problem for this dependency. The trick is to reuse aarch64-built package from Hydra cache. I went ahead and replaced most problematic deps with native binaries:

let
    # this will use aarch64 binaries from binary cache, so no need to build those
    pkgsArm = import <nixpkgs> {
        config = {};
        overlays = [];
        system = "aarch64-linux";
    };

    # these will be your cross packages
    pkgsCross = import <nixpkgs> {

       overlays = [(self: super: {

         inherit (pkgsArm)
           xorg libpulseaudio libGL guile systemd wayland libxkbcommon wayland-protocols
         ;
 
       })];
       crossSystem = {
         config = "aarch64-unknown-linux-gnu";
       };
     };

in pkgsCross.SDL2

Unfotunately, this results into another error:

/nix/store/czx8vkrb9jdgjyz8qfksh10vrnqa723l-bash-4.4-p23/bin/bash: /nix/store/j8ybzzmpsv7qc9lp0v7jcmhhdaawj933-wayland-1.16.0/bin/wayland-scanner: cannot execute binary file: Exec format error

which means it tries to run arm binary on x86. Huh, just replace those deps from buildPackages (which are in reality non-cross pkgs):

...
in pkgsCross.SDL2.override { 
      inherit (pkgsCross.buildPackages) 
         wayland wayland-protocols
      ;
   }

Hope that helps!

1 Like

actually, looks like I misunderstood the problem. You don’t want to crosscompile SDL2, but use it as part of cross toolchain.

In that case you can go straight and use pkgsArm.SDL2 directly, no need to crosscompile it.

Oh wow, thank you for putting all this time into this! I really appreciate it.

If I understand your second reply correctly, I should have something like the following:

with import <nixpkgs> {
  crossSystem = {
    config = "aarch64-unknown-linux-gnu";
  };
};

let
  pkgsArm = import <nixpkgs> {
    config = { };
    overlays = [ ];
    system = "aarch64-linux";
  };
in
mkShell {
  buildInputs = [ pkgsArm.SDL2 ];

Nice! I can run $ nix-shell shell.nix, enter this shell, perfect. But when I try to build something with SDL, say by $ aarch64-unknown-linux-gnu-gcc -lSDL2 example.c, it looks like I don’t actually have the library:

/nix/store/91wnxl4b6bksgaa410fnnp195clwmwp6-aarch64-unknown-linux-gnu-binutils-2.31.1/bin/aarch64-unknown-linux-gnu-ld: cannot find -lSDL
collect2: error: ld returned 1 exit status

I must be doing something wrong. Did I misunderstand?

Edit: never mind! As you can see in the error message, I tried linking to libsdl, not libsdl2! All is well! Thanks again :slight_smile:

2 Likes