From 824dcc2a8d140851fe222b0d3d6dd8676b676108 Mon Sep 17 00:00:00 2001
From: Jean-Paul Calderone <exarkun@twistedmatrix.com>
Date: Tue, 6 Dec 2022 05:42:13 -0500
Subject: [PATCH] a crossenv-based attempt

---
 crossenv/flake.lock | 26 ++++++++++++++++++
 crossenv/flake.nix  | 66 ++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 85 insertions(+), 7 deletions(-)
 create mode 100644 crossenv/flake.lock

diff --git a/crossenv/flake.lock b/crossenv/flake.lock
new file mode 100644
index 0000000..5f75025
--- /dev/null
+++ b/crossenv/flake.lock
@@ -0,0 +1,26 @@
+{
+  "nodes": {
+    "nixpkgs": {
+      "locked": {
+        "lastModified": 1670318372,
+        "narHash": "sha256-o/jKGceUG4MfoUWUTaijSayW8l+fzBVFGUC/f6QsF1A=",
+        "owner": "NixOS",
+        "repo": "nixpkgs",
+        "rev": "bad09cd10c343277b8e8a125d562e13154d03296",
+        "type": "github"
+      },
+      "original": {
+        "owner": "NixOS",
+        "repo": "nixpkgs",
+        "type": "github"
+      }
+    },
+    "root": {
+      "inputs": {
+        "nixpkgs": "nixpkgs"
+      }
+    }
+  },
+  "root": "root",
+  "version": 7
+}
diff --git a/crossenv/flake.nix b/crossenv/flake.nix
index 7179373..e733c70 100644
--- a/crossenv/flake.nix
+++ b/crossenv/flake.nix
@@ -1,17 +1,29 @@
 {
-  inputs.nixpkgs.url = "github:NixOS/nixpkgs?rev=3d58e3a31ee55554fe26650bb59db7d6d0e97625";
-  outputs = { self, nixpkgs }:
+  # inputs.nixpkgs.url = "github:NixOS/nixpkgs?rev=3d58e3a31ee55554fe26650bb59db7d6d0e97625";
+  # inputs.nixpkgs.url = "github:NixOS/nixpkgs?rev=fd83da2b640e05c6dd14fad4b741714b069ae0d7";
+  inputs.nixpkgs.url = "github:NixOS/nixpkgs";
+  # inputs.nixpkgs.url = "github:NixOS/nixpkgs?ref=nixos-22.05";
+  # inputs.nixpkgs.url = "github:NixOS/nixpkgs?ref=nixos-22.11";
+  # inputs.mach-nix.url = "github:DavHau/mach-nix";
+
+  outputs =
+    { self
+    , nixpkgs
+    # , mach-nix
+    }:
     let
       native-pkgs = import nixpkgs {
         system = "x86_64-linux";
-        allowUnfree = true;
-        android_sdk.accept_license = true;
+        config = {
+          allowUnfree = true;
+          android_sdk.accept_license = true;
+        };
       };
       cross-pkgs = native-pkgs.pkgsCross.aarch64-android;
 
       platformVersion = "31";
       cmakeVersion = "3.10.2";
-      buildToolsVersion = "31.0.0";
+      buildToolsVersion = "30.0.0";
       # Check out pkgs/development/mobile/androidenv/repo.json for
       # nixpkgs-supported versions.
       ndkVersion = "23.0.7123448-rc1";
@@ -19,14 +31,54 @@
         inherit cmakeVersion buildToolsVersion platformVersion ndkVersion;
       };
 
+      # mach-nix' = native-pkgs.callPackage mach-nix {
+      #   python = "python3Minimal";
+      # };
+
     in {
       devShells.x86_64-linux.default = native-pkgs.mkShell rec {
-        BUILD_PYTHON = "${native-pkgs.python3Minimal}/bin/python";
+        BUILD_PYTHON = "${native-pkgs.python3}/bin/python";
         HOST_PYTHON = "${cross-pkgs.python3Minimal}/bin/python";
         ANDROIDSDK = "${androidComposition.androidsdk}/libexec/android-sdk";
         ANDROIDNDK = "${ANDROIDSDK}/ndk-bundle";
+        CROSSENV_WHEEL = native-pkgs.fetchurl {
+          url = "https://files.pythonhosted.org/packages/46/11/8bdda7df08c5d7566a518bb422e305f11980d2be1d1ca9b3fb6806097762/crossenv-1.3.0-py3-none-any.whl";
+          sha256 = "sha256-bdkjYJChMU+810cd8MN5/b82KCsMzQJ7WPoayKTwRyE=";
+        };
+
+        nativeBuildInputs = [
+          # expose
+          cross-pkgs.libffi
+        ];
+
+        setupCrossEnv = ''
+          # Create a virtualenv with software we can run on the build system.
+          $BUILD_PYTHON -m venv build-env
+
+          # Put crossenv into that build environment so we can use it to
+          # manage the cross build.
+          ./build-env/bin/pip install crossenv
+
+          # Create a cross build environment with crossenv.  It doesn't find
+          # CC on its own, so help it out.  Also tell it where a Python
+          # configured for the host system can be found, so it can extra more
+          # details from it.
+          ./build-env/bin/python -m crossenv --cc $CC $HOST_PYTHON ./cross-env
+
+          # Activate the cross build environment for interactive use.
+          . ./cross-env/bin/activate
+        '';
+
         shellHook = ''
-          export PATH="$(echo "$ANDROIDSDK/cmake/${cmakeVersion}".*/bin):$PATH"
+          # Point at the compiler that can build for the host platform.  This
+          # seems like a plausible choice but it doesn't actually match the
+          # compiler used to build the host Python so maybe it is wrong.
+          export CC="${ANDROIDNDK}/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android${platformVersion}-clang"
+
+          # Let cffi build find ffi.h
+          export CFLAGS=$NIX_CFLAGS_COMPILE
+          # Let cffi build find libffi.so
+          export LDFLAGS=$NIX_LDFLAGS
         '';
       };
     };
-- 
GitLab