summaryrefslogtreecommitdiffhomepage
path: root/android/flake.nix
blob: b48befe9410c92a3e4d9becc065b73dca9ce8697 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
{
  description = "Mullvad Android app build flake";

  inputs = {
    # Unstable is currently needed for protoc-gen-grpc-java.
    # We should switch to a stable channel once it's avaiable on those.
    nixpkgs.url = "nixpkgs/nixos-unstable";
    devshell.url = "github:numtide/devshell";
    flake-utils.url = "github:numtide/flake-utils";
    rust-overlay = {
      url = "github:oxalica/rust-overlay";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    android-nixpkgs = {
      url = "github:tadfisher/android-nixpkgs";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = {
    self,
    nixpkgs,
    android-nixpkgs,
    rust-overlay,
    flake-utils,
    devshell,
  }:
    flake-utils.lib.eachDefaultSystem (system: let
      pkgs = import nixpkgs {
        inherit system;
        overlays = [
          (import rust-overlay)
          devshell.overlays.default
          # Fix that disables autoPatchelfHook on macOS.
          # Should be addressed upstream in nixpkgs.
          (final: prev: {
            protoc-gen-grpc-java = prev.protoc-gen-grpc-java.overrideAttrs (old: {
              nativeBuildInputs =
                pkgs.lib.remove prev.autoPatchelfHook (old.nativeBuildInputs or [])
                ++ pkgs.lib.optionals prev.stdenv.isLinux [prev.autoPatchelfHook];
            });
          })
        ];
      };

      rust-toolchain = (pkgs.buildPackages.rust-bin.fromRustupToolchainFile
        ../rust-toolchain.toml).override {
        extensions = ["rust-analyzer"];
        targets = [
          "aarch64-linux-android"
          "armv7-linux-androideabi"
          "x86_64-linux-android"
          "i686-linux-android"
        ];
      };

      patchedGo_1_21_3 =
        (import (fetchTarball {
          url = "https://github.com/NixOS/nixpkgs/archive/b392079f5fd051926a834c878d27ceec4f139dce.tar.gz";
          sha256 = "16dkk98fs9pw2amz0vpjsc7ks85cw3hc5rlpbp27llq6x7lwpjaz";
        }) {inherit system;}).go_1_21.overrideAttrs (oldAttrs: {
          patches = (oldAttrs.patches or []) ++ [./docker/goruntime-boottime-over-monotonic.diff];
        });

      versions =
        (builtins.fromTOML (
          builtins.concatStringsSep "\n" (
            builtins.filter (line: !(builtins.match "^[[:space:]]*#" line != null))
            (nixpkgs.lib.splitString "\n" (builtins.readFile ./gradle/libs.versions.toml))
          )
        )).versions;

      compileSdkVersion = versions."compile-sdk";
      buildToolsVersion = versions."build-tools";
      minSdkVersion = versions."min-sdk";
      ndkVersion = versions.ndk;

      android-sdk = android-nixpkgs.sdk.${system} (sdkPkgs:
        with sdkPkgs; [
          (builtins.getAttr "platforms-android-${compileSdkVersion}" sdkPkgs)
          (builtins.getAttr "build-tools-${builtins.replaceStrings ["."] ["-"] buildToolsVersion}" sdkPkgs)
          (builtins.getAttr "ndk-${builtins.replaceStrings ["."] ["-"] ndkVersion}" sdkPkgs)
          cmdline-tools-latest
          platform-tools
        ]);
    in {
      overlay = final: prev: {
        inherit (self.packages.${system}) android-sdk;
      };

      packages = {
        inherit android-sdk;
      };

      devShells.default = pkgs.devshell.mkShell {
        name = "mullvad-android-devshell";
        packages =
          [
            android-sdk
            rust-toolchain
            patchedGo_1_21_3
            pkgs.protoc-gen-grpc-java
            pkgs.gcc
            pkgs.gnumake
            pkgs.protobuf
            pkgs.jdk17
            pkgs.python3Full
          ]
          ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [pkgs.libiconv];

        env = import ./nix/env-vars.nix {
          inherit pkgs android-sdk buildToolsVersion ndkVersion minSdkVersion;
        };
        # Unfortunately rich menus with package, description and category
        # is only supported using the TOML format and not when using mkShell.
        # The two cannot be combined and TOML format by itself doesn't support
        # the way we dynamically configure the devshell.
        commands = [
          {
            name = "tasks";
            command = "./gradlew tasks";
          }
          {
            name = "build";
            command = "./gradlew assembleOssProdDebug";
          }
        ];
      };
    });
}