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

cross compile backend from x86_64 to aarch64-multiplatform #718

Closed
jafonsor opened this issue Jan 21, 2021 · 6 comments
Closed

cross compile backend from x86_64 to aarch64-multiplatform #718

jafonsor opened this issue Jan 21, 2021 · 6 comments

Comments

@jafonsor
Copy link

jafonsor commented Jan 21, 2021

Hey. I'm new to nix and reflex-platform. I'm trying to get a reflex-platform project to run on my raspberry pi. I've already managed to cross compile a cabal project to aarch64-multiplatform with this:

let
  nixpkgs-func = import <nixpkgs>;
  lib = import <nixpkgs/lib>;
  raspberryPi64 = nixpkgs-func {
    crossSystem = lib.systems.examples.aarch64-multiplatform;
  };
  drv-raspberryPi64 =  raspberryPi64.haskell.packages.ghc865.callCabal2nix "cabal-project" ./cabal-project {};
  drv-raspberryPi64-justStatic = raspberryPi64.haskell.lib.justStaticExecutables drv-raspberryPi64;
in drv-raspberryPi64-justStatic

However I don't know how to do the same on a reflex-platform project like this:

{ reflex-platform ? import ./reflex-platform {} }:

reflex-platform.project ({ pkgs, ... }: {
  packages = {
    common = ./common;
    backend = ./backend;
    frontend = ./frontend;
  };

  android.frontend = {
    executableName = "frontend";
    applicationId = "org.example.frontend";
    displayName = "Example Android App";
  };

  ios.frontend = {
    executableName = "frontend";
    bundleIdentifier = "org.example.frontend";
    bundleName = "Example iOS App";
  };

  shells = {
    ghc = ["common" "backend" "frontend"];
    ghcjs = ["common" "frontend"];
  };
})

I'm a bit lost, so any pointers would really help.

Edit 1: Just to be clear I just want to produce a backend executable that runs on aarch64-platform so that I can run the server on a raspberry pi instead of my development machine.

@matthewbauer
Copy link
Member

I think you'll need to add aarch64-platform to the attrset here:

https://github.com/reflex-frp/reflex-platform/blob/develop/default.nix#L121-L163

then make a ghcLinuxAarch64 package set. You can then build your binary with nix-build -A ghcLinuxAarch64.backend.

@jafonsor
Copy link
Author

Thanks for you quick response. I'll try that.

@jafonsor
Copy link
Author

I'm using the reflex-project-skeleton to test this. After some changes on the reflex-platform/default.nix I got this:

[nix-shell:/out]# cd reflex-project-skeleton/

[nix-shell:/out/reflex-project-skeleton]# nix-build -A ghcLinuxAarch64.backend
error: attribute 'ghcLinuxAarch64' in selection path 'ghcLinuxAarch64.backend' not found

I must be missing adding the ghcLinuxAarch64 in some place.

I edited the reflex-platform/default.nix in 3 places:

  1. added aarch64-platform to nixpkgsCross
...
nixpkgsCross = {
      android = lib.mapAttrs (_: args: nixpkgsFunc (nixpkgsArgs // args)) rec {
        aarch64 = {
          crossSystem = lib.systems.examples.aarch64-android-prebuilt;
        };
        aarch32 = {
          crossSystem = lib.systems.examples.armv7a-android-prebuilt // {
            # Hard to find newer 32-bit phone to test with that's newer than
            # this. Concretely, doing so resulted in:
            # https://android.googlesource.com/platform/bionic/+/master/libc/arch-common/bionic/pthread_atfork.h#19
            sdkVer = "22";
          };
        };
        # Back compat
        arm64 = lib.warn "nixpkgsCross.android.arm64 has been deprecated, using nixpkgsCross.android.aarch64 instead." aarch64;
        armv7a = lib.warn "nixpkgsCross.android.armv7a has been deprecated, using nixpkgsCross.android.aarch32 instead." aarch32;
        arm64Impure = lib.warn "nixpkgsCross.android.arm64Impure has been deprecated, using nixpkgsCross.android.aarch64 instead." aarch64;
        armv7aImpure = lib.warn "nixpkgsCross.android.armv7aImpure has been deprecated, using nixpkgsCross.android.aarch32 instead." aarch32;
      };
      ios = lib.mapAttrs (_: args: nixpkgsFunc (nixpkgsArgs // args)) rec {
        simulator64 = {
          crossSystem = lib.systems.examples.iphone64-simulator // {
            sdkVer = iosSdkVersion;
          };
        };
        aarch64 = {
          crossSystem = lib.systems.examples.iphone64 // {
            sdkVer = iosSdkVersion;
          };
        };
        aarch32 = {
          crossSystem = lib.systems.examples.iphone32 // {
            sdkVer = iosSdkVersion;
          };
        };
        # Back compat
        arm64 = lib.warn "nixpkgsCross.ios.arm64 has been deprecated, using nixpkgsCross.ios.aarch64 instead." aarch64;
      };
      aarch64-multiplatform = {
        crossSystem = lib.systems.examples.aarch64-multiplatform;
      };
    };
...
  1. defined ghcLinuxAarch64 next to the other ghc's
...
  ghc8_2 = (makeRecursivelyOverridable nixpkgs.haskell.packages.ghc822).override {
    overrides = nixpkgs.haskell.overlays.combined;
  };
  ghc8_0 = (makeRecursivelyOverridable nixpkgs.haskell.packages.ghc802).override {
    overrides = nixpkgs.haskell.overlays.combined;
  };
  ghc7 = (makeRecursivelyOverridable nixpkgs.haskell.packages.ghc7103).override {
    overrides = nixpkgs.haskell.overlays.combined;
  };

  ghcLinuxAarch64 = (makeRecursivelyOverridable nixpkgsCross.aarch64-multiplatform.haskell.packages.ghc843).override {
    overrides = nixpkgs.haskell.overlays.combined;
  };


  # Takes a package set with `makeRecursivelyOverridable` and ensures that any
  # future overrides will be applied to both the package set itself and it's
  # build-time package set (`buildHaskellPackages`).
  makeRecursivelyOverridableBHPToo = x: x // {
    override = new: makeRecursivelyOverridableBHPToo (x.override
      (combineOverrides
        {
          overrides = self: super: {
            buildHaskellPackages = super.buildHaskellPackages.override new;
          };
        }
        new));
  };
...
  1. on this list of ghc's
...
in let this = rec {
  inherit nixpkgs
          nixpkgsCross
          overrideCabal
          hackGet
          foreignLibSmuggleHeaders
          stage2Script
          ghc
          ghcHEAD
          ghc8_4
          ghc8_2
          ghc8_0
          ghc7

          ghcLinuxAarch64
          
          ghcIosSimulator64
          ghcIosAarch64
          ghcIosAarch64-8_4
          ghcIosAarch64-8_2
          ghcIosAarch32
          ghcIosAarch32-8_4
          ghcIosAarch32-8_2
          ghcAndroidAarch64
          ghcAndroidAarch64-8_4
          ghcAndroidAarch64-8_2
          ghcAndroidAarch32
          ghcAndroidAarch32-8_4
          ghcAndroidAarch32-8_2
          ghcjs
          ghcjs8_0
          ghcjs8_2
          ghcjs8_4
          ghcSavedSplices
          android
          androidWithHaskellPackages
          iosAarch32
          iosAarch64
          iosWithHaskellPackages
          filterGit;

  # Back compat
  ios = iosAarch64;
...

@jafonsor
Copy link
Author

Oh. My bad. On the nixpkgsCross I should have done this:

      aarch64-multiplatform = nixpkgsFunc (nixpkgsArgs // {
        crossSystem = lib.systems.examples.aarch64-multiplatform;
      });

and then on the reflex-platform project file should have added one more shell:

  shells = {
    ghcLinuxAarch64 = ["backend"];
    ghc = ["common" "backend" "frontend"];
    ghcjs = ["common" "frontend"];
  };

@jafonsor jafonsor reopened this Feb 4, 2021
@jafonsor
Copy link
Author

jafonsor commented Feb 4, 2021

Wait a second @matthewbauer . I don't want to create a shell for ghcLinuxAarch64. Why can't I compile to raspberry pi like I compile to android without creating a specific shell?

This is what I get if I remove ghcLinuxAarch64 = ["backend"]; from the shells:

# nix-build -A ghcLinuxAarch64.backend
unpacking 'https://github.com/NixOS/nixpkgs/archive/3e55299b41428ec92516568120a31f79b13f878e.tar.gz'...
error: attribute 'ghcLinuxAarch64' in selection path 'ghcLinuxAarch64.backend' not found

@ali-abrar
Copy link
Member

We recently added some aarch64 support here: 1.2.0.0

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

3 participants