tadfisher / android-nixpkgs

Nix-packaged Android SDK
MIT License
294 stars 44 forks source link

Usage with devbox #80

Open BenceBakos opened 11 months ago

BenceBakos commented 11 months ago

Hey!

I keep my nix system config in devbox: https://www.jetpack.io/devbox/docs/

I would like to use this repository, but I can not set the ANDROID_HOME env variable.

My current devbox.json looks something like this:

{
  "packages": [
    "neovim@latest",
    "docker@latest",
    "lazygit@latest",
    "dart@latest",
    "flutter@latest",
    "jdk11@latest",
    "openjdk11@latest",
    "android-tools@latest",
    "sdkmanager@latest",
    "github:tadfisher/android-nixpkgs#cmdline-tools-latest",
    "github:tadfisher/android-nixpkgs#build-tools-34-0-0",
    "github:tadfisher/android-nixpkgs#emulator",
    "github:tadfisher/android-nixpkgs#platforms-android-34",
    "github:tadfisher/android-nixpkgs#ndk-26-1-10909125",
    "github:tadfisher/android-nixpkgs#platform-tools"
  ],
  "shell": {
    "init_hook": [
    ],
    "scripts": {
      "test": [
        "echo \"Error: no test specified\" && exit 1"
      ]
    }
  }
}

I can set up env variables in the init_hook section, like they do in the rust setup example: https://www.jetpack.io/devbox/docs/configuration/#example-a-rust-devbox

any ideas how to make it work?

ebauger commented 9 months ago

@BenceBakos

I figured out. I'm more familiar with the nix and flake ecosystems and I used devbox for isolated my project.

There packages are there for making android-sdk package and you need to remove these lines on your devbox.json:

    "github:tadfisher/android-nixpkgs#cmdline-tools-latest",
    "github:tadfisher/android-nixpkgs#build-tools-34-0-0",
    "github:tadfisher/android-nixpkgs#emulator",
    "github:tadfisher/android-nixpkgs#platforms-android-34",
    "github:tadfisher/android-nixpkgs#ndk-26-1-10909125",
    "github:tadfisher/android-nixpkgs#platform-tools"

On the root of your project folder (same level of .devbox) create the path/file structure :

Note: I used this flake.nix from the template and comment on the sections with the mention of devshell. We don't need it with devbox.

./android-flake/flake.nix

{
  description = "My Android project";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs";
    # devshell.url = "github:numtide/devshell";
    flake-utils.url = "github:numtide/flake-utils";
    android.url = "github:tadfisher/android-nixpkgs";
  };

  outputs = { self, nixpkgs, /*devshell, */flake-utils, android }:
    {
      overlay = final: prev: {
        inherit (self.packages.${final.system}) android-SDK android-studio;
      };
    }
    //
    flake-utils.lib.eachSystem [ "aarch64-darwin" "x86_64-darwin" "x86_64-linux" ] (system:
      let
        inherit (nixpkgs) lib;
        pkgs = import nixpkgs {
          inherit system;
          config.allowUnfree = true;
          #overlays = [
            #devshell.overlays.default
            #self.overlay
          #];
        };
      in
      {
        packages = {
          android-sdk = android.sdk.${system} (sdkPkgs: with sdkPkgs; [
            # Useful packages for building and testing.
            build-tools-33-0-0
            cmdline-tools-latest
            emulator
            platform-tools
            platforms-android-33

            # Other useful packages for a development environment.
            # ndk-26-1-10909125
            # skiaparser-3
            # sources-android-34
          ]
          ++ lib.optionals (system == "aarch64-darwin") [
            # system-images-android-34-google-apis-arm64-v8a
            # system-images-android-34-google-apis-playstore-arm64-v8a
          ]
          ++ lib.optionals (system == "x86_64-darwin" || system == "x86_64-linux") [
            # system-images-android-34-google-apis-x86-64
            # system-images-android-34-google-apis-playstore-x86-64
          ]);
        } // lib.optionalAttrs (system == "x86_64-linux") {
          # Android Studio in nixpkgs is currently packaged for x86_64-linux only.
          android-studio = pkgs.androidStudioPackages.stable;
          # android-studio = pkgs.androidStudioPackages.beta;
          # android-studio = pkgs.androidStudioPackages.preview;
          # android-studio = pkgs.androidStudioPackage.canary;
        };

        #devShell = import ./devshell.nix { inherit pkgs; };
      }
    );
}

here the devbox.json should look like:

{
  "packages": [
...
    "path:./android-flake#android-sdk"
    // "path:./android-flake#android-studio" // work only with x86_64-linux
  ],

}

You can change in this flake.nix the packages to create the proper version of the SDK. Here my need was with the version 33. Feel free the change with the 34 version.

If you perform the refresh command in your devbox shell it will remove/install the packages sourced from your custom flake.

If you echo $ANDROID_HOME in your shell, it should return something like that /nix/store/<hash>-android-SDK-env/share/android-sdk.

Now you have a deterministic android SDK installation for your dev environment.

I'm using devbox version 0.9.1 on macOS 14.3.1 (M1 machine)

fdietze commented 2 months ago

This is great und should be part of the readme!