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

Issues with cross-compilation aarch64-darwin -> aarch64-linux #347

Open
ar3s3ru opened this issue Feb 26, 2024 · 0 comments
Open

Issues with cross-compilation aarch64-darwin -> aarch64-linux #347

ar3s3ru opened this issue Feb 26, 2024 · 0 comments

Comments

@ar3s3ru
Copy link

ar3s3ru commented Feb 26, 2024

I'm trying to build a monolithic Rust app using Nix through cargo2nix. I want to be able to:

  1. Compile on the host machine (in my personal case, aarch64-darwin),
  2. Have host-independent, cross-compilation working to produce artifacts for my target architectures (aarch64-linux & x86_64-linux).

This is how I set up my flake.nix:

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";

    rust-overlay.url = "github:oxalica/rust-overlay";
    rust-overlay.inputs.nixpkgs.follows = "nixpkgs";
    rust-overlay.inputs.flake-utils.follows = "flake-utils";

    cargo2nix.url = "github:cargo2nix/cargo2nix";
    cargo2nix.inputs.nixpkgs.follows = "nixpkgs";
    cargo2nix.inputs.flake-utils.follows = "flake-utils";
    cargo2nix.inputs.rust-overlay.follows = "rust-overlay";
  };

  outputs = { nixpkgs, flake-utils, rust-overlay, cargo2nix, ... }:
    flake-utils.lib.eachDefaultSystem
      (system:
        let
          # All of the local derivations should be added here as an overlay
          # to make them available to the devshells under `pkgs`.
          overlays = [
            (import ./tools/nix/overlays)
            (import rust-overlay)
            cargo2nix.overlays.default
          ];

          rustConfig = {
            rustVersion = "1.76.0";
            packageFun = import ./Cargo.nix;
          };

          pkgs = import nixpkgs {
            inherit system overlays;
            config.allowUnfree = true;
          };

          pkgsLinuxAmd64 = pkgs // {
            crossSystem.config = "x86_64-unknown-linux-musl";
          };

          pkgsLinuxArm64 = pkgs // {
            crossSystem.config = "aarch64-unknown-linux-gnu";
          };

          rustPkgs = pkgs.rustBuilder.makePackageSet rustConfig;

          rustPkgsLinuxAmd64 = pkgsLinuxAmd64.rustBuilder.makePackageSet (rustConfig // {
            target = "x86_64-unknown-linux-musl";
          });

          rustPkgsLinuxArm64 = pkgsLinuxArm64.rustBuilder.makePackageSet (rustConfig // {
            target = "aarch64-unknown-linux-gnu";
          });
        in
        rec {
          packages.cargo2nix = cargo2nix.outputs.packages."${system}".cargo2nix;
          packages.backend-app = (rustPkgs.workspace.backend-app { });
          packages.backend-app-linux-amd64 = (rustPkgsLinuxAmd64.workspace.backend-app { });
          packages.backend-app-linux-arm64 = (rustPkgsLinuxArm64.workspace.backend-app { });

          apps.backend-app = flake-utils.lib.mkApp {
            name = "app";
            drv = packages.backend-app;
          };
        }
      );
}

However, when running nix build .#backend-app-linux-arm64 or nix build .#backend-app-linux-amd64 I get this error:

warning: The interpretation of store paths arguments ending in `.drv` recently changed. If this command is now failing try again with '/nix/store/bpqmcm3gk60biz91lvpwrqws0r8m07n6-crate-rustls-native-certs-0.6.3.drv^*'
@nix { "action": "setPhase", "phase": "unpackPhase" }
Running phase: unpackPhase
unpacking source archive /nix/store/142ybpb7afscdgapf9xr6961smxjky6i-rustls-native-certs-0.6.3.tar.gz
source root is rustls-native-certs-0.6.3
setting SOURCE_DATE_EPOCH to timestamp 1153704088 of file rustls-native-certs-0.6.3/tests/smoketests.rs
@nix { "action": "setPhase", "phase": "patchPhase" }
Running phase: patchPhase
@nix { "action": "setPhase", "phase": "updateAutotoolsGnuConfigScriptsPhase" }
Running phase: updateAutotoolsGnuConfigScriptsPhase
@nix { "action": "setPhase", "phase": "configurePhase" }
Running phase: configurePhase
@nix { "action": "setPhase", "phase": "buildPhase" }
Running phase: buildPhase
[target."cfg(all(unix, not(target_os = \"macos\")))".dependencies.openssl-probe]
[target."cfg(target_os = \"macos\")".dependencies.security-framework]
   Compiling rustls-native-certs v0.6.3 (/private/tmp/nix-build-crate-rustls-native-certs-0.6.3.drv-0/rustls-native-certs-0.6.3)
error[E0433]: failed to resolve: use of undeclared crate or module `openssl_probe`
 --> src/unix.rs:7:28
  |
7 |     let likely_locations = openssl_probe::probe();
  |                            ^^^^^^^^^^^^^ use of undeclared crate or module `openssl_probe`


[target."cfg(all(unix, not(target_os = \"macos\")))".dependencies.openssl-probe]
[target."cfg(target_os = \"macos\")".dependencies.security-framework]
   Compiling rustls-native-certs v0.6.3 (/private/tmp/nix-build-crate-rustls-native-certs-0.6.3.drv-0/rustls-native-certs-0.6.3)
error[E0433]: failed to resolve: use of undeclared crate or module `openssl_probe`
 --> src/unix.rs:7:28
  |
7 |     let likely_locations = openssl_probe::probe();
  |                            ^^^^^^^^^^^^^ use of undeclared crate or module `openssl_probe`


error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
    --> src/unix.rs:10:14
     |
10   |         Some(cert_file) => load_pem_certs(&cert_file),
     |              ^^^^^^^^^ doesn't have a size known at compile-time
     |
     = help: within `Path`, the trait `Sized` is not implemented for `[u8]`
note: required because it appears within the type `Path`
    --> /nix/store/airkdq5n6dfrkqpcsfckvfl7lqm6411l-rust-minimal-1.76.0/lib/rustlib/src/rust/library/std/src/path.rs:1992:12
     |
1992 | pub struct Path {
     |            ^^^^
     = note: all local variables must have a statically known size
     = help: unsized locals are gated as an unstable feature

I am probably doing something wrong. Any idea what that is?
Thank you folks 🙏🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant