Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build failure: zigbee2mqtt (buildNpmPackage cross compilation) #355165

Open
kazikcz opened this issue Nov 11, 2024 · 0 comments
Open

Build failure: zigbee2mqtt (buildNpmPackage cross compilation) #355165

kazikcz opened this issue Nov 11, 2024 · 0 comments
Labels
0.kind: build failure A package fails to build 6.topic: cross-compilation Building packages on a different platform than they will be used on 6.topic: nodejs

Comments

@kazikcz
Copy link
Contributor

kazikcz commented Nov 11, 2024

Steps To Reproduce

# default.nix:
let
        # Sun Nov 10 14:30:04 2024 +0000
        pkgs = import (fetchTarball https://github.com/nixos/nixpkgs/archive/d40ed47baac04ed2ba8d4b0d12e0bc7bd582eec9.tar.gz) {
                overlays = [
                        (final: prev: {
                                zigbee2mqtt = prev.zigbee2mqtt.overrideAttrs (attrs: {
                                        # Without this, -m64 is passed on x86_64 to an arm cross-gcc resulting in an error
                                        npmFlags = (attrs.npmFlags or []) ++ [ "--arch=arm" ];
                                });
                        })
                ];
        };
        z2m = pkgs.pkgsCross.raspberryPi.zigbee2mqtt;
        file = pkgs.file;
in
        # with npmFlags --arch=arm hack, this produces a script that refers to the buildPlatform nodejs (x86_64), instead of targetPlatform nodejs (arm)
        pkgs.runCommand "" {} ''eval ${file}/bin/file ''$(grep exec ${z2m}/bin/zigbee2mqtt | cut -d" " -f2)''

nix-build

Build log

Without the npmFlags hack:

npm error gyp info using [email protected]
npm error gyp info using [email protected] | linux | x64
...
       > npm error gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
       > npm error armv6l-unknown-linux-gnueabihf-g++: error: unrecognized command-line option '-m64'
       > npm error make: *** [notify.target.mk:102: Release/obj.target/notify/notify.o] Error 1

With npmFlags hack, the package builds, but (as per runCommand) refers to the wrong nodejs and ends up being non-executable on ARM:

/nix/store/jz54v1m0ldqivx5i45q8i05pz5clf842-nodejs-20.18.0/bin/node: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), dynamically linked, interpreter /nix/store/3bvxjkkmwlymr0fssczhgi39c3aj1l7i-glibc-2.40-36/lib/ld-linux-x86-64.so.2, for GNU/Linux 3.10.0, not stripped

Additional context

I'm trying to build an sdImage for Raspberry Pi 0 (armv6l) on a x86_64 system. Wanted to include zigbee2mqtt.

I've found #327653 which fixed nodejs building itself. It looks like this alone may not be sufficient.

I've done some digging and it looks to me buildNpmPackage is not cross-compile aware/ready. It pulls in nodejs in both buildInputs and nativeBuildInputs. The npm used during setup is ofcourse the nativeBuildInputs one, so x86_64 specific (explains -m64 presence, wrong script exec wrapper). It seems the buildInputs one is not present in any way during the build.

Interestingly, when inspecting rootfs I found that both nodejs outputs (x86_64 and arm) are in /nix/store/, with zigbee2mqtt being the only one as "referer" (nix-store).

FWIW If I do this (+ npmFlags) to the zigbee2mqtt override I can get it actually start (didn't check beyond that) on the resulting rootfs, but it's a nasty hack:

 postInstall = ''sed -i "s@^exec.*node\"@exec ${prev.nodejs}/bin/node@" $out/bin/zigbee2mqtt'';

Plenty of files in the zigbee2mqtt output in store still refer to the wrong nodejs in shebangs.

Notify maintainers

@aduh95
@tie
@Cynerd
@sweber83

Metadata

$ nix-shell -p nix-info --run "nix-info -m"
 - system: `"x86_64-linux"`
 - host os: `Linux 6.11.6, NixOS, 24.11 (Vicuna), 24.11pre703931.4aa36568d413`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.24.10`
 - channels(root): `"nixos"`
 - nixpkgs: `/nix/var/nix/profiles/per-user/root/channels/nixos`

Add a 👍 reaction to issues you find important.

@kazikcz kazikcz added the 0.kind: build failure A package fails to build label Nov 11, 2024
@FliegendeWurst FliegendeWurst added 6.topic: cross-compilation Building packages on a different platform than they will be used on 6.topic: nodejs labels Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.kind: build failure A package fails to build 6.topic: cross-compilation Building packages on a different platform than they will be used on 6.topic: nodejs
Projects
None yet
Development

No branches or pull requests

2 participants