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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
{ callPackage, stdenv, lib, fetchurl, ruby, writeText
, licenseAccepted ? false
}:
{ cmdLineToolsVersion ? "13.0"
, toolsVersion ? "26.1.1"
, platformToolsVersion ? "35.0.1"
, buildToolsVersions ? [ "34.0.0" ]
, includeEmulator ? false
, emulatorVersion ? "35.1.4"
, platformVersions ? []
, includeSources ? false
, includeSystemImages ? false
, systemImageTypes ? [ "google_apis" "google_apis_playstore" ]
, abiVersions ? [ "x86" "x86_64" "armeabi-v7a" "arm64-v8a" ]
, cmakeVersions ? [ ]
, includeNDK ? false
, ndkVersion ? "26.3.11579264"
, ndkVersions ? [ndkVersion]
, useGoogleAPIs ? false
, useGoogleTVAddOns ? false
, includeExtras ? []
, repoJson ? ./repo.json
, repoXmls ? null
, extraLicenses ? []
}:
let
# Determine the Android os identifier from Nix's system identifier
os = if stdenv.isLinux then "linux"
else if stdenv.isDarwin then "macosx"
else throw "No Android SDK tarballs are available for system architecture: ${stdenv.system}";
# Uses mkrepo.rb to create a repo spec.
mkRepoJson = { packages ? [], images ? [], addons ? [] }: let
mkRepoRuby = (ruby.withPackages (pkgs: with pkgs; [ slop nokogiri ]));
mkRepoRubyArguments = lib.lists.flatten [
(builtins.map (package: ["--packages" "${package}"]) packages)
(builtins.map (image: ["--images" "${image}"]) images)
(builtins.map (addon: ["--addons" "${addon}"]) addons)
];
in
stdenv.mkDerivation {
name = "androidenv-repo-json";
buildInputs = [ mkRepoRuby ];
preferLocalBuild = true;
unpackPhase = "true";
buildPhase = ''
ruby ${./mkrepo.rb} ${lib.escapeShellArgs mkRepoRubyArguments} > repo.json
'';
installPhase = ''
mv repo.json $out
'';
};
# Reads the repo JSON. If repoXmls is provided, will build a repo JSON into the Nix store.
repo = if repoXmls != null then
let
repoXmlSpec = {
packages = repoXmls.packages or [];
images = repoXmls.images or [];
addons = repoXmls.addons or [];
};
in
lib.importJSON "${mkRepoJson repoXmlSpec}"
else
lib.importJSON repoJson;
# Converts all 'archives' keys in a repo spec to fetchurl calls.
fetchArchives = attrSet:
lib.attrsets.mapAttrsRecursive
(path: value:
if (builtins.elemAt path ((builtins.length path) - 1)) == "archives" then
(builtins.listToAttrs
(builtins.map
(archive: lib.attrsets.nameValuePair archive.os (fetchurl { inherit (archive) url sha1; })) value))
else value
)
attrSet;
# Converts the repo attrset into fetch calls
packages = fetchArchives repo.packages;
system-images-packages = fetchArchives repo.images;
addons = {
addons = fetchArchives repo.addons;
extras = fetchArchives repo.extras;
};
# Converts a license name to a list of license texts.
mkLicenses = licenseName: repo.licenses.${licenseName};
# Converts a list of license names to a flattened list of license texts.
# Just used for displaying licenses.
mkLicenseTexts = licenseNames:
lib.lists.flatten
(builtins.map
(licenseName:
builtins.map
(licenseText: "--- ${licenseName} ---\n${licenseText}")
(mkLicenses licenseName))
licenseNames);
# Converts a license name to a list of license hashes.
mkLicenseHashes = licenseName:
builtins.map
(licenseText: builtins.hashString "sha1" licenseText)
(mkLicenses licenseName);
# The list of all license names we're accepting. Put android-sdk-license there
# by default.
licenseNames = lib.lists.unique ([
"android-sdk-license"
] ++ extraLicenses);
in
rec {
deployAndroidPackages = callPackage ./deploy-androidpackages.nix {
inherit stdenv lib mkLicenses;
};
deployAndroidPackage = ({package, os ? null, buildInputs ? [], patchInstructions ? "", meta ? {}, ...}@args:
let
extraParams = removeAttrs args [ "package" "os" "buildInputs" "patchInstructions" ];
in
deployAndroidPackages ({
inherit os buildInputs meta;
packages = [ package ];
patchesInstructions = { "${package.name}" = patchInstructions; };
} // extraParams
));
# put a much nicer error message that includes the available options.
check-version = packages: package: version:
if lib.hasAttrByPath [ package version ] packages then
packages.${package}.${version}
else
throw ''
The version ${version} is missing in package ${package}.
The only available versions are ${builtins.concatStringsSep ", " (builtins.attrNames packages.${package})}.
'';
platform-tools = callPackage ./platform-tools.nix {
inherit deployAndroidPackage;
os = if stdenv.system == "aarch64-darwin" then "macosx" else os; # "macosx" is a universal binary here
package = check-version packages "platform-tools" platformToolsVersion;
};
tools = callPackage ./tools.nix {
inherit deployAndroidPackage os;
package = check-version packages "tools" toolsVersion;
postInstall = ''
${linkPlugin { name = "platform-tools"; plugin = platform-tools; }}
${linkPlugin { name = "patcher"; plugin = patcher; }}
${linkPlugin { name = "emulator"; plugin = emulator; check = includeEmulator; }}
'';
};
patcher = callPackage ./patcher.nix {
inherit deployAndroidPackage os;
package = packages.patcher."1";
};
build-tools = map (version:
callPackage ./build-tools.nix {
inherit deployAndroidPackage os;
package = check-version packages "build-tools" version;
postInstall = ''
${linkPlugin { name = "tools"; plugin = tools; check = toolsVersion != null; }}
'';
}
) buildToolsVersions;
emulator = lib.optionals includeEmulator (callPackage ./emulator.nix {
inherit deployAndroidPackage os;
package = check-version packages "emulator" emulatorVersion;
postInstall = ''
${linkSystemImages { images = system-images; check = includeSystemImages; }}
'';
});
platforms = map (version:
deployAndroidPackage {
inherit os;
package = check-version packages "platforms" version;
}
) platformVersions;
sources = map (version:
deployAndroidPackage {
inherit os;
package = check-version packages "sources" version;
}
) platformVersions;
system-images = lib.flatten (map (apiVersion:
map (type:
# Deploy all system images with the same systemImageType in one derivation to avoid the `null` problem below
# with avdmanager when trying to create an avd!
#
# ```
# $ yes "" | avdmanager create avd --force --name testAVD --package 'system-images;android-33;google_apis;x86_64'
# Error: Package path is not valid. Valid system image paths are:
# null
# ```
let
availablePackages = map (abiVersion:
system-images-packages.${apiVersion}.${type}.${abiVersion}
) (builtins.filter (abiVersion:
lib.hasAttrByPath [apiVersion type abiVersion] system-images-packages
) abiVersions);
instructions = builtins.listToAttrs (map (package: {
name = package.name;
value = lib.optionalString (lib.hasPrefix "google_apis" type) ''
# Patch 'google_apis' system images so they're recognized by the sdk.
# Without this, `android list targets` shows 'Tag/ABIs : no ABIs' instead
# of 'Tag/ABIs : google_apis*/*' and the emulator fails with an ABI-related error.
sed -i '/^Addon.Vendor/d' source.properties
'';
}) availablePackages
);
in
lib.optionals (availablePackages != [])
(deployAndroidPackages {
inherit os;
packages = availablePackages;
patchesInstructions = instructions;
})
) systemImageTypes
) platformVersions);
cmake = map (version:
callPackage ./cmake.nix {
inherit deployAndroidPackage os;
package = check-version packages "cmake" version;
}
) cmakeVersions;
# Creates a NDK bundle.
makeNdkBundle = ndkVersion:
callPackage ./ndk-bundle {
inherit deployAndroidPackage os platform-tools;
package = packages.ndk-bundle.${ndkVersion} or packages.ndk.${ndkVersion};
};
# All NDK bundles.
ndk-bundles = lib.optionals includeNDK (map makeNdkBundle ndkVersions);
# The "default" NDK bundle.
ndk-bundle = if includeNDK then lib.findFirst (x: x != null) null ndk-bundles else null;
google-apis = map (version:
deployAndroidPackage {
inherit os;
package = (check-version addons "addons" version).google_apis;
}
) (builtins.filter (platformVersion: platformVersion < "26") platformVersions); # API level 26 and higher include Google APIs by default
google-tv-addons = map (version:
deployAndroidPackage {
inherit os;
package = (check-version addons "addons" version).google_tv_addon;
}
) platformVersions;
# Function that automatically links all plugins for which multiple versions can coexist
linkPlugins = {name, plugins}:
lib.optionalString (plugins != []) ''
mkdir -p ${name}
${lib.concatMapStrings (plugin: ''
ln -s ${plugin}/libexec/android-sdk/${name}/* ${name}
'') plugins}
'';
# Function that automatically links all NDK plugins.
linkNdkPlugins = {name, plugins, rootName ? name}:
lib.optionalString (plugins != []) ''
mkdir -p ${rootName}
${lib.concatMapStrings (plugin: ''
ln -s ${plugin}/libexec/android-sdk/${name} ${rootName}/${plugin.version}
'') plugins}
'';
# Function that automatically links the default NDK plugin.
linkNdkPlugin = {name, plugin, check}:
lib.optionalString check ''
ln -s ${plugin}/libexec/android-sdk/${name} ${name}
'';
# Function that automatically links a plugin for which only one version exists
linkPlugin = {name, plugin, check ? true}:
lib.optionalString check ''
ln -s ${plugin}/libexec/android-sdk/${name} ${name}
'';
linkSystemImages = { images, check }: lib.optionalString check ''
mkdir -p system-images
${lib.concatMapStrings (system-image: ''
apiVersion=$(basename $(echo ${system-image}/libexec/android-sdk/system-images/*))
type=$(basename $(echo ${system-image}/libexec/android-sdk/system-images/*/*))
mkdir -p system-images/$apiVersion
ln -s ${system-image}/libexec/android-sdk/system-images/$apiVersion/$type system-images/$apiVersion/$type
'') images}
'';
# Links all plugins related to a requested platform
linkPlatformPlugins = {name, plugins, check}:
lib.optionalString check ''
mkdir -p ${name}
${lib.concatMapStrings (plugin: ''
ln -s ${plugin}/libexec/android-sdk/${name}/* ${name}
'') plugins}
''; # */
cmdline-tools-package = check-version packages "cmdline-tools" cmdLineToolsVersion;
# This derivation deploys the tools package and symlinks all the desired
# plugins that we want to use. If the license isn't accepted, prints all the licenses
# requested and throws.
androidsdk = if !licenseAccepted then throw ''
${builtins.concatStringsSep "\n\n" (mkLicenseTexts licenseNames)}
You must accept the following licenses:
${lib.concatMapStringsSep "\n" (str: " - ${str}") licenseNames}
a)
by setting nixpkgs config option 'android_sdk.accept_license = true;'.
b)
by an environment variable for a single invocation of the nix tools.
$ export NIXPKGS_ACCEPT_ANDROID_SDK_LICENSE=1
'' else callPackage ./cmdline-tools.nix {
inherit deployAndroidPackage os;
package = cmdline-tools-package;
postInstall = ''
# Symlink all requested plugins
${linkPlugin { name = "platform-tools"; plugin = platform-tools; }}
${linkPlugin { name = "tools"; plugin = tools; check = toolsVersion != null; }}
${linkPlugin { name = "patcher"; plugin = patcher; }}
${linkPlugins { name = "build-tools"; plugins = build-tools; }}
${linkPlugin { name = "emulator"; plugin = emulator; check = includeEmulator; }}
${linkPlugins { name = "platforms"; plugins = platforms; }}
${linkPlatformPlugins { name = "sources"; plugins = sources; check = includeSources; }}
${linkPlugins { name = "cmake"; plugins = cmake; }}
${linkNdkPlugins { name = "ndk-bundle"; rootName = "ndk"; plugins = ndk-bundles; }}
${linkNdkPlugin { name = "ndk-bundle"; plugin = ndk-bundle; check = includeNDK; }}
${linkSystemImages { images = system-images; check = includeSystemImages; }}
${linkPlatformPlugins { name = "add-ons"; plugins = google-apis; check = useGoogleAPIs; }}
${linkPlatformPlugins { name = "add-ons"; plugins = google-apis; check = useGoogleTVAddOns; }}
# Link extras
${lib.concatMapStrings (identifier:
let
path = addons.extras.${identifier}.path;
addon = deployAndroidPackage {
inherit os;
package = addons.extras.${identifier};
};
in
''
targetDir=$(dirname ${path})
mkdir -p $targetDir
ln -s ${addon}/libexec/android-sdk/${path} $targetDir
'') includeExtras}
# Expose common executables in bin/
mkdir -p $out/bin
for i in ${platform-tools}/bin/*; do
ln -s $i $out/bin
done
${lib.optionalString includeEmulator ''
for i in ${emulator}/bin/*; do
ln -s $i $out/bin
done
''}
find $ANDROID_SDK_ROOT/${cmdline-tools-package.path}/bin -type f -executable | while read i; do
ln -s $i $out/bin
done
# Write licenses
mkdir -p licenses
${lib.concatMapStrings (licenseName:
let
licenseHashes = builtins.concatStringsSep "\n" (mkLicenseHashes licenseName);
licenseHashFile = writeText "androidenv-${licenseName}" licenseHashes;
in
''
ln -s ${licenseHashFile} licenses/${licenseName}
'') licenseNames}
'';
};
}
|