Skip to content

Build failure: zigbee2mqtt (buildNpmPackage cross compilation) #355165

Description

@kazikcz

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 node-gyp@10.1.0
npm error gyp info using node@20.18.0 | 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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    0.kind: build failureA package fails to build2.status: stalehttps://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md6.topic: cross-compilationBuilding packages on a different platform than they will be used on6.topic: nodejsNode.js is a free, open-source, cross-platform JavaScript runtime environment

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions