From 8e6b8b1ff181470b458d1c656db2ea7bb241f896 Mon Sep 17 00:00:00 2001 From: Pedro Pombeiro Date: Thu, 11 Apr 2019 14:48:53 +0200 Subject: [PATCH] Use Android SDK from nixpkgs instead of downloading --- Makefile | 2 +- derivation.nix | 30 ++---------- nix/mobile/android.nix | 63 +++++++++++++++++++++++++ nix/mobile/default.nix | 23 ++++----- nix/shell.sh | 2 + scripts/lib/setup/installers.sh | 84 --------------------------------- scripts/setup | 16 +++++-- 7 files changed, 91 insertions(+), 129 deletions(-) create mode 100644 nix/mobile/android.nix delete mode 100755 scripts/lib/setup/installers.sh diff --git a/Makefile b/Makefile index 2dafc0a11f..bc271a6d33 100644 --- a/Makefile +++ b/Makefile @@ -48,7 +48,7 @@ clean: ##@prepare Remove all output folders clean-nix: SHELL := /bin/sh clean-nix: ##@prepare Remove complete nix setup - sudo rm -rf /nix ~/.cache/nix + sudo rm -rf /nix ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.cache/nix ~/.status shell: ##@prepare Enter into a pre-configured shell ifndef IN_NIX_SHELL diff --git a/derivation.nix b/derivation.nix index 6ed6ef92df..fb49dc6624 100644 --- a/derivation.nix +++ b/derivation.nix @@ -1,6 +1,5 @@ -# target-os = [ 'windows' 'linux' 'macos' 'darwin' 'android' 'ios' ] { system ? builtins.currentSystem -, config ? {}, overlays ? [] +, config ? { android_sdk.accept_license = true; }, overlays ? [] , pkgs ? (import { inherit system config overlays; }) , target-os }: @@ -27,8 +26,8 @@ with pkgs; # TODO: Try to use stdenv for iOS. The problem is with building iOS as the build is trying to pass parameters to Apple's ld that are meant for GNU's ld (e.g. -dynamiclib) _stdenv = stdenvNoCC; statusDesktop = callPackage ./nix/desktop { inherit target-os; stdenv = _stdenv; }; - statusMobile = callPackage ./nix/mobile { inherit target-os status-go; androidPkgs = androidComposition; stdenv = _stdenv; }; - status-go = callPackage ./nix/status-go { inherit (xcodeenv) composeXcodeWrapper; inherit xcodewrapperArgs; androidPkgs = androidComposition; }; + statusMobile = callPackage ./nix/mobile { inherit target-os config status-go; stdenv = _stdenv; }; + status-go = callPackage ./nix/status-go { inherit (xcodeenv) composeXcodeWrapper; inherit (statusMobile) xcodewrapperArgs; androidPkgs = statusMobile.androidComposition; }; nodejs' = pkgs.nodejs-10_x; yarn' = yarn.override { nodejs = nodejs'; }; nodeInputs = import ./nix/global-node-packages/output { @@ -41,29 +40,6 @@ with pkgs; python27 # for e.g. gyp yarn' ] ++ (map (x: nodeInputs."${x}") (builtins.attrNames nodeInputs)); - xcodewrapperArgs = { - version = "10.1"; - }; - xcodeWrapper = xcodeenv.composeXcodeWrapper xcodewrapperArgs; - androidComposition = androidenv.composeAndroidPackages { - toolsVersion = "26.1.1"; - platformToolsVersion = "28.0.2"; - buildToolsVersions = [ "28.0.3" ]; - includeEmulator = false; - platformVersions = [ "26" "27" ]; - includeSources = false; - includeDocs = false; - includeSystemImages = false; - systemImageTypes = [ "default" ]; - abiVersions = [ "armeabi-v7a" ]; - lldbVersions = [ "2.0.2558144" ]; - cmakeVersions = [ "3.6.4111459" ]; - includeNDK = true; - ndkVersion = "19.2.5345600"; - useGoogleAPIs = false; - useGoogleTVAddOns = false; - includeExtras = [ "extras;android;m2repository" "extras;google;m2repository" ]; - }; in _stdenv.mkDerivation rec { name = "status-react-build-env"; diff --git a/nix/mobile/android.nix b/nix/mobile/android.nix new file mode 100644 index 0000000000..ef1bdfba67 --- /dev/null +++ b/nix/mobile/android.nix @@ -0,0 +1,63 @@ +{ config, stdenv, pkgs }: + +with pkgs; +with stdenv; + +let + gradle = gradle_4_10; + androidComposition = androidenv.composeAndroidPackages { + toolsVersion = "26.1.1"; + platformToolsVersion = "28.0.2"; + buildToolsVersions = [ "28.0.3" ]; + includeEmulator = false; + platformVersions = [ "28" ]; + includeSources = false; + includeDocs = false; + includeSystemImages = false; + systemImageTypes = [ "default" ]; + abiVersions = [ "armeabi-v7a" ]; + lldbVersions = [ "2.0.2558144" ]; + cmakeVersions = [ "3.6.4111459" ]; + includeNDK = true; + ndkVersion = "19.2.5345600"; + useGoogleAPIs = false; + useGoogleTVAddOns = false; + includeExtras = [ "extras;android;m2repository" "extras;google;m2repository" ]; + }; + +in + { + inherit androidComposition; + + buildInputs = [ openjdk gradle ]; + shellHook = '' + export JAVA_HOME="${openjdk}" + export ANDROID_HOME=~/.status/Android/Sdk + export ANDROID_SDK_ROOT="$ANDROID_HOME" + export ANDROID_NDK_ROOT="${androidComposition.androidsdk}/libexec/android-sdk/ndk-bundle" + export ANDROID_NDK_HOME="$ANDROID_NDK_ROOT" + export ANDROID_NDK="$ANDROID_NDK_ROOT" + export PATH="$ANDROID_HOME/bin/$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin:$ANDROID_HOME/platform-tools:$ANDROID_HOME/build-tools:$PATH" + '' + + ## We need to make a writeable copy of the Android SDK so that we can accept the license (which causes files to be written to the SDK folders) + ## since the nix store is immutable by nature, we can't license the SDK from there. + '' + if ! [ -d $ANDROID_HOME ]; then + echo "=> pulling the Android SDK out of the nix store and into a writeable directory" + + mkdir -p $ANDROID_HOME + cp -rL ${androidComposition.androidsdk}/bin $ANDROID_HOME + cp -rL ${androidComposition.androidsdk}/libexec/android-sdk/* $ANDROID_HOME/ + chmod -R 755 $ANDROID_HOME/ + '' + lib.optionalString config.android_sdk.accept_license '' + echo "=> accepting Android SDK licenses" + pushd $ANDROID_HOME + yes | $PWD/bin/sdkmanager --licenses || if [ $? -ne '141' ]; then exit $?; fi; #Captures SIGPIPE 141 error but still allow repeating "y" to accept all licenses + popd + '' + + '' + echo "=> generating keystore" + $PWD/scripts/generate-keystore.sh + fi + ''; + } diff --git a/nix/mobile/default.nix b/nix/mobile/default.nix index cbcbe57af0..efbf896692 100644 --- a/nix/mobile/default.nix +++ b/nix/mobile/default.nix @@ -1,4 +1,4 @@ -{ stdenv, pkgs, target-os ? "all", status-go, androidPkgs }: +{ config, stdenv, pkgs, target-os ? "all", status-go }: with pkgs; with stdenv; @@ -13,24 +13,21 @@ let "ios" = true; "all" = true; }.${target-os} or false; + xcodewrapperArgs = { + version = "10.1"; + }; + android = callPackage ./android.nix { inherit config; }; in { + inherit (android) androidComposition; + inherit xcodewrapperArgs; + buildInputs = - lib.optionals targetAndroid [ - openjdk gradle - ]; + lib.optional targetAndroid android.buildInputs; shellHook = lib.optionalString targetIOS '' export RCTSTATUS_FILEPATH=${status-go}/lib/ios/Statusgo.framework '' + - lib.optionalString targetAndroid '' - export JAVA_HOME="${openjdk}" - export ANDROID_HOME=~/.status/Android/Sdk - export ANDROID_SDK_ROOT="$ANDROID_HOME" - export ANDROID_NDK_ROOT="${androidPkgs.ndk-bundle}/libexec/android-sdk/ndk-bundle" - export ANDROID_NDK_HOME="$ANDROID_NDK_ROOT" - export ANDROID_NDK="$ANDROID_NDK_ROOT" - export PATH="$ANDROID_HOME/bin:$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin:$ANDROID_HOME/platform-tools:$ANDROID_HOME/build-tools:$PATH" - ''; + lib.optionalString targetAndroid android.shellHook; } diff --git a/nix/shell.sh b/nix/shell.sh index c105a89bf9..cd9833863a 100755 --- a/nix/shell.sh +++ b/nix/shell.sh @@ -16,6 +16,8 @@ if ! command -v "nix" >/dev/null 2>&1; then else echo -e "${GREEN}Setting up environment...${NC}" ./scripts/setup + + . ~/.nix-profile/etc/profile.d/nix.sh fi fi diff --git a/scripts/lib/setup/installers.sh b/scripts/lib/setup/installers.sh deleted file mode 100755 index f7eb139254..0000000000 --- a/scripts/lib/setup/installers.sh +++ /dev/null @@ -1,84 +0,0 @@ -#!/usr/bin/env bash - -GIT_ROOT=$(git rev-parse --show-toplevel) - -function download_url() { - if program_exists "aria2c"; then - aria2c --max-connection-per-server=16 --split=16 --dir="$1" -o "$2" "$3" - else - wget --show-progress --output-document="$1/$2" "$3" - fi -} - -function install_nix() { - if ! program_exists nix; then - local required_version=$(toolversion nix) - NIX_INSTALLER_NO_MODIFY_PROFILE=1 bash <(curl https://nixos.org/releases/nix/nix-${required_version}/install) --no-daemon - if [ $? -eq 0 ]; then - echo -e "${YELLOW}**********************************************************************************************************" - echo "The Nix package manager was successfully installed." - echo -e "**********************************************************************************************************${NC}" - else - echo "Please see https://nixos.org/nix/manual/#chap-installation" - fi - exit - fi -} - -function install_android_sdk() { - if [ -z "$ANDROID_SDK_ROOT" ]; then - return 0 - fi - - if [ -d "$ANDROID_SDK_ROOT" ]; then - cecho "@green[[Android SDK already installed.]]" - else - local required_version=$(toolversion android-sdk) - mkdir -p $ANDROID_SDK_ROOT - cecho "@cyan[[Downloading Android SDK.]]" - - local PLATFORM=$(echo "$OS" | tr '[:upper:]' '[:lower:]') - download_url . sdk-tools-${PLATFORM}.zip https://dl.google.com/android/repository/sdk-tools-${PLATFORM}-${required_version}.zip && \ - cecho "@cyan[[Extracting Android SDK to $ANDROID_SDK_ROOT.]]" && \ - unzip -q -o ./sdk-tools-${PLATFORM}.zip -d "$ANDROID_SDK_ROOT" && \ - rm -f ./sdk-tools-${PLATFORM}.zip && \ - cecho "@blue[[Android SDK installation completed in $ANDROID_SDK_ROOT.]]" || \ - exit $? - fi - - [ $? -eq 0 ] && use_android_sdk - - return 0 -} - -function dependency_setup() { - cecho "@b@blue[[\$ $@]]" - echo - - cd "$(repo_path)" - eval "$@" || (cecho "@b@red[[Error running dependency install '$@']]" && exit 1) - - echo - echo " + done" - echo -} - -function use_android_sdk() { - if [ -d "$ANDROID_SDK_ROOT" ]; then - local ANDROID_BUILD_TOOLS_VERSION=$(toolversion android-sdk-build-tools) - local ANDROID_PLATFORM_VERSION=$(toolversion android-sdk-platform) - touch ~/.android/repositories.cfg - echo y | sdkmanager "platform-tools" "build-tools;$ANDROID_BUILD_TOOLS_VERSION" "platforms;$ANDROID_PLATFORM_VERSION" - yes | sdkmanager --licenses - else - local _docUrl="https://status.im/build_status/" - cecho "@yellow[[ANDROID_SDK_ROOT environment variable not defined, please install the Android SDK.]]" - cecho "@yellow[[(see $_docUrl).]]" - - echo - - exit 1 - fi - - scripts/generate-keystore.sh -} diff --git a/scripts/setup b/scripts/setup index b9aff2672e..9989ae1ac8 100755 --- a/scripts/setup +++ b/scripts/setup @@ -13,7 +13,6 @@ source "$_current_dir/lib/setup/path-support.sh" source_lib "output.sh" source_lib "packages.sh" source_lib "platform.sh" -source_lib "installers.sh" exit_unless_os_supported @@ -46,9 +45,18 @@ fi #### setup_header "Installing requirements..." -install_nix && \ -install_android_sdk || \ -exit $? +if ! program_exists nix; then + required_version=$(toolversion nix) + NIX_INSTALLER_NO_MODIFY_PROFILE=1 bash <(curl https://nixos.org/releases/nix/nix-${required_version}/install) --no-daemon + if [ $? -eq 0 ]; then + echo -e "${YELLOW}**********************************************************************************************************" + echo "The Nix package manager was successfully installed." + echo -e "**********************************************************************************************************${NC}" + else + echo "Please see https://nixos.org/nix/manual/#chap-installation" + exit + fi +fi #### setup_complete