-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflake.nix
305 lines (262 loc) · 10.4 KB
/
flake.nix
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
{
description = "Anki SRS Kai";
inputs = {
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
crane.url = "github:ipetkov/crane";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
advisory-db = {
url = "github:rustsec/advisory-db";
flake = false;
};
};
outputs =
{
self,
flake-utils,
nixpkgs,
crane,
rust-overlay,
advisory-db,
...
}:
flake-utils.lib.eachDefaultSystem (
system:
let
targetSystem = "wasm32-unknown-unknown";
pkgs = import nixpkgs {
inherit system;
overlays = [ (import rust-overlay) ];
};
craneLib = (crane.mkLib pkgs).overrideToolchain (
p: p.rust-bin.stable.latest.default.override { targets = [ targetSystem ]; }
);
src = craneLib.cleanCargoSource ./.;
commonArgs = {
inherit src;
strictDeps = true;
};
cargoArtifacts = craneLib.buildDepsOnly (
commonArgs
// {
buildPhaseCargoCommand = ''
cargo check --profile release --frozen --all-targets
cargo build --profile release --frozen --workspace --exclude wasm
cargo build --profile release --frozen --target ${targetSystem} --workspace --exclude xtask
'';
checkPhaseCargoCommand = ''
cargo test --profile release --frozen --workspace --exclude wasm --no-run
cargo test --profile release --frozen --target ${targetSystem} --workspace --exclude xtask --no-run
'';
}
);
anki_srs_kai = craneLib.buildPackage (
commonArgs
// {
inherit cargoArtifacts;
cargoExtraArgs = "--frozen --target ${targetSystem} --workspace --exclude xtask";
nativeBuildInputs = with pkgs; [
binaryen
wasm-bindgen-cli
];
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER = "wasm-bindgen-test-runner";
nativeCheckInputs = with pkgs; [ nodePackages_latest.nodejs ];
postInstall = ''
cargo run --release --package xtask --
'';
}
);
addon = pkgs.stdenv.mkDerivation (finalAttrs: {
pname = "AnkiSrsKaiAddon";
version = anki_srs_kai.version;
strictDeps = true;
src = pkgs.lib.fileset.toSource {
root = ./.;
fileset = pkgs.lib.fileset.unions [ ./addon ];
};
nativeBuildInputs = (with pkgs; [ zip ]);
buildPhase = ''
mkdir -p $out
cd addon
zip $out/addon.ankiaddon *
'';
});
androidPkgs = import nixpkgs {
inherit system;
config = {
android_sdk.accept_license = true;
allowUnfree = true;
};
};
buildToolsVersion = "34.0.0";
platformVersion = "35";
systemImageType = "google_apis";
abiVersion = "x86_64";
androidComposition = androidPkgs.androidenv.composeAndroidPackages {
buildToolsVersions = [ buildToolsVersion ];
platformToolsVersion = "35.0.2";
platformVersions = [ platformVersion ];
emulatorVersion = "35.2.5";
includeEmulator = true;
includeSources = false;
includeSystemImages = true;
systemImageTypes = [ systemImageType ];
abiVersions = [ abiVersion ];
includeNDK = false;
useGoogleAPIs = false;
useGoogleTVAddOns = false;
includeExtras = [ ];
};
androidSdk = androidComposition.androidsdk;
ankidroid = pkgs.stdenv.mkDerivation (finalAttrs: {
pname = "AnkiDroid";
version = "v2.20.1";
strictDeps = true;
ankiDroidSource = pkgs.fetchFromGitHub {
owner = "ankidroid";
repo = "Anki-Android";
rev = "${finalAttrs.version}";
# IMPORTANT: To properly update the hash after updating the version
# above, first set the hash to an empty string (""), build the
# derivation, extract the calculated hash from the error message,
# and replace the hash below
#
# If only the version was updated and not the hash, the build will
# not fetch the new version or check that the hash has changed since
# it is a fixed output derivation
#
# https://nixos.org/manual/nixpkgs/unstable/index.html#chap-pkgs-fetchers-caveats
# https://nixos.org/manual/nixpkgs/unstable/index.html#sec-pkgs-fetchers-updating-source-hashes
hash = "sha256-CPCczLovuQTjUTWgjVjgZ8PL4idlcTzWZ2wKwqsF+cg=";
name = finalAttrs.pname;
};
localDirectory = pkgs.lib.fileset.toSource {
root = ./.;
fileset = pkgs.lib.fileset.unions [
./AnkiDroid
./addon/update_custom_data.sql
];
};
srcs = [
finalAttrs.ankiDroidSource
finalAttrs.localDirectory
];
sourceRoot = finalAttrs.pname;
override_anki_srs_kai = anki_srs_kai.overrideAttrs (oldAttrs: {
INCLUDE_SCHEDULER_HEADER = "false";
});
nativeBuildInputs =
(with pkgs; [
git
gradle
temurin-bin
keepBuildTree
])
++ [
androidSdk
finalAttrs.override_anki_srs_kai
];
mitmCache = pkgs.gradle.fetchDeps {
pkg = ankidroid;
data = ./deps.json;
};
ANDROID_HOME = "${androidSdk}/libexec/android-sdk";
gradleFlags = "-Dorg.gradle.project.android.aapt2FromMavenOverride=${finalAttrs.ANDROID_HOME}/build-tools/${buildToolsVersion}/aapt2";
requiredSystemFeatures = [ "kvm" ];
deviceName = "device";
port = "5554";
postUnpack = ''
cp -r source/AnkiDroid ${finalAttrs.pname}
cp source/addon/update_custom_data.sql ${finalAttrs.pname}/AnkiDroid/src/androidTest/assets
cp ${finalAttrs.override_anki_srs_kai}/dist/anki_srs_kai.js ${finalAttrs.pname}/AnkiDroid/src/androidTest/assets
'';
# Reference
# https://github.com/NixOS/nixpkgs/blob/a551cfdc3e3381211ff060093a9977c6d3935032/pkgs/development/mobile/androidenv/emulate-app.nix
postConfigure = ''
export LOGS_DIR=$(mktemp --directory --tmpdir logs-XXXX)
export ANDROID_USER_HOME=$(mktemp --directory --tmpdir android-user-home-XXXX)
export ANDROID_AVD_HOME=$ANDROID_USER_HOME/avd
export ANDROID_SDK_ROOT=$ANDROID_HOME
export HOME=$ANDROID_USER_HOME
export ANDROID_SERIAL="emulator-${finalAttrs.port}"
${androidSdk}/bin/avdmanager create avd --force --name ${finalAttrs.deviceName} --package "system-images;android-${platformVersion};${systemImageType};${abiVersion}" --path $ANDROID_AVD_HOME/${finalAttrs.deviceName}.avd < <(yes "")
echo "hw.gpu.enabled=yes" >> $ANDROID_AVD_HOME/${finalAttrs.deviceName}.avd/config.ini
$ANDROID_SDK_ROOT/emulator/emulator -avd ${finalAttrs.deviceName} -no-boot-anim -port ${finalAttrs.port} -no-window &
${androidSdk}/libexec/android-sdk/platform-tools/adb -s emulator-${finalAttrs.port} wait-for-device
${androidSdk}/libexec/android-sdk/platform-tools/adb logcat '*:D' >> $LOGS_DIR/adb-log.txt &
${androidSdk}/libexec/android-sdk/platform-tools/adb emu screenrecord start --time-limit 1800 $LOGS_DIR/video.webm
'';
# Only capture the dependencies required to run our tests when
# initializing or updating deps.json
gradleUpdateTask = "AnkiDroid:connectedPlayDebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.package=com.ichi2.anki.ankisrskai";
gradleBuildTask = finalAttrs.gradleUpdateTask;
installPhase = ''
cp $LOGS_DIR/* $out
'';
});
docs = pkgs.stdenv.mkDerivation (finalAttrs: {
pname = "AnkiSrsKaiDocs";
version = anki_srs_kai.version;
strictDeps = true;
src = pkgs.lib.fileset.toSource {
root = ./.;
fileset = pkgs.lib.fileset.unions [ ./CHANGELOG.md ./guide ];
};
nativeBuildInputs = (with pkgs; [ mdbook ]);
buildPhase = ''
mkdir -p $out
mdbook build guide
cp -r guide/book/* $out
'';
});
in
{
formatter = pkgs.nixfmt-rfc-style;
packages.default = anki_srs_kai;
checks = {
inherit anki_srs_kai;
clippy = craneLib.cargoClippy (
commonArgs
// {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
}
);
format = craneLib.cargoFmt { inherit src; };
toml_format = craneLib.taploFmt { src = pkgs.lib.sources.sourceFilesBySuffices src [ ".toml" ]; };
audit = craneLib.cargoAudit { inherit src advisory-db; };
deny = craneLib.cargoDeny { inherit src; };
};
devShells.default = craneLib.devShell {
checks = self.checks.${system};
packages = with pkgs; [
mdbook
rust-analyzer
];
# fixes: the cargo feature `public-dependency` requires a nightly
# version of Cargo, but this is the `stable` channel
#
# This enables unstable features with the stable compiler
# Remove once this is fixed in stable
#
# https://github.com/rust-lang/rust/issues/112391
# https://github.com/rust-lang/rust-analyzer/issues/15046
RUSTC_BOOTSTRAP = 1;
};
devShells.ankidroid = pkgs.mkShell { packages = with androidPkgs; [ android-studio ]; };
# Initialize or update deps.json
# nix build .#ankidroid.mitmCache.updateScript
# BWRAP_FLAGS="--dev-bind /dev/kvm /dev/kvm" ./result
#
# Then run tests with
# nix build .#ankidroid
packages.ankidroid = ankidroid;
devShells.addon = pkgs.mkShell { packages = with pkgs; [ black ]; };
packages.addon = addon;
packages.docs = docs;
}
);
}