-
Notifications
You must be signed in to change notification settings - Fork 6
/
flake.nix
223 lines (192 loc) · 8.05 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
{
description = "A Nix-flake-based React Native development environment";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
android-nixpkgs.url = "github:tadfisher/android-nixpkgs";
android-nixpkgs.inputs.nixpkgs.follows = "nixpkgs";
utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, android-nixpkgs, utils }:
utils.lib.eachDefaultSystem (system:
let
# utility function for string replace
replace = rec {
replacer = a: b: str:
nixpkgs.lib.strings.stringAsChars (x: if x == a then b else x) str;
dotToDash = replacer "." "-";
semicolonToDash = replacer ";" "-";
underscoreToDash = replacer "_" "-";
withDoubleQuote = str: ''"${str}"'';
};
# react-native dependencies versions
nodejsVersion = 18;
javaVersion = 11;
androidBuildToolsVersion = "33.0.0";
androidPlatformsVersion = 33;
androidNDKVersion = "23.1.7779620";
# references https://gist.github.com/mrk-han/66ac1a724456cadf1c93f4218c6060ae
createEmulator = pkgs:
let
inherit (pkgs.stdenv) isAarch64;
arch = if isAarch64 then "arm64-v8a" else "x86_64";
cpuArch = if isAarch64 then "arm64" else "x86_64";
in
rec
{
inherit cpuArch;
name = "dvt_${toString androidPlatformsVersion}";
abi.type = arch;
packages = "system-images;android-${toString androidPlatformsVersion};${tag.id};${abi.type}";
sysdir = "$ANDROID_SDK_ROOT/system-images/android-${toString androidPlatformsVersion}/${tag.id}/${abi.type}/";
tag.id = "google_apis_playstore";
avdRootPath = "$HOME/.android/avd";
avdPath = "${avdRootPath}/${name}.avd";
avdConfigIni = "${avdPath}/config.ini";
};
overlays = [
android-nixpkgs.overlays.default
(final: prev: rec {
nodejs = prev."nodejs-${toString nodejsVersion}_x";
yarn = prev.yarn.override { inherit nodejs; };
jdk = pkgs."jdk${toString javaVersion}";
gradle = prev.gradle.override {
java = jdk;
};
ruby = prev.ruby.withPackages (p: [ p.cocoapods ]);
androidSdk = let emulator = createEmulator prev; in
prev.androidSdk (s: [
# common android sdk package
s.platform-tools
s.cmdline-tools-latest
s.tools
s.patcher-v4
s.extras-google-google-play-services
s.emulator
# specified android sdk package version
s."build-tools-${replace.dotToDash androidBuildToolsVersion}"
s."ndk-${replace.dotToDash androidNDKVersion}"
s."platforms-android-${toString androidPlatformsVersion}"
s."${replace.underscoreToDash (replace.semicolonToDash emulator.packages)}"
]);
})
];
pkgs = import nixpkgs { inherit overlays system; };
run = pkg: "${pkgs.${pkg}}/bin/${pkg}";
scripts = let emulator = createEmulator pkgs; in
with pkgs; [
# help commands
(writeScriptBin "helpme" ''
__usage="
👋 Welcome to react-native development environment. 🚀
If you see this message, it means your are inside the Nix shell ❄️.
[Info]===============================================================>
Env:
- JAVA_HOME: $JAVA_HOME
- ANDROID_HOME: $ANDROID_HOME
- ANDROID_SDK_ROOT: $ANDROID_SDK_ROOT
- ANDROID_NDK_ROOT: $ANDROID_NDK_ROOT
Android Emulator:
- name: ${emulator.name}
- packages: ${emulator.packages}
Android SDK packages:
- build-tools: ${androidBuildToolsVersion}
- platforms-android: ${toString androidPlatformsVersion}
- ndk: ${androidNDKVersion}
Command available:
- start: start the project 🛹
- create-emulator: create emulator (default: android)
- align-emulator: align dev environment with AVD configurations
- gen-properties: generate local.properties in <currentDir>/android
- helpme: show this messages
Report/Help:
- https://github.com/eFishery/dvt
[Info]===============================================================>
"
echo "$__usage"
'')
# check dependencies
(writeScriptBin "check-react-native" ''
until ${run "yarn"} react-native -v > /dev/null 2>&1; do
echo "install dependencies..."
${run "yarn"} install || (echo failed instll dependencies with yarn; exit 1)
echo ""
read -p "Do you want to create an android emulator? [y/n]: " __is_create_emulator
[[ $__is_create_emulator == "y" ]] && create-emulator && align-emulator
sleep 0.5
done
'')
# align emulator configuration for sync system-images location and relateds.
(writeScriptBin "align-emulator" ''
[[ -f ${emulator.avdConfigIni} ]] && (
cat <<EOF > ${emulator.avdConfigIni}
PlayStore.enabled = enable
abi.type = ${emulator.abi.type}
avd.ini.encoding = UTF-8
skin.name = 1080x1920
hw.lcd.density = 480
hw.keyboard = yes
hw.cpu.arch = ${emulator.cpuArch}
image.sysdir.1 = ${emulator.sysdir}
tag.display = Google Play
tag.id = ${emulator.tag.id}
disk.dataPartition.size = 6442450944
EOF
echo "🚀 Updated emulator (${emulator.name}) configurations."
)
'')
# create emulator
(writeScriptBin "create-emulator" ''
echo "no" | ${androidSdk}/bin/avdmanager \
--clear-cache -s \
create avd -f \
-n ${emulator.name} \
-p ${emulator.avdPath} \
-g ${emulator.tag.id} \
-b ${emulator.abi.type} \
-k ${replace.withDoubleQuote emulator.packages} \
|| (echo "❌ Fail: to create emulator"; exit 1)
'')
# TODO: delete emulator
# (writeScriptBin "delete-emulator" ''
# Try your self here!
# '')
# start react-native metro bundler
(writeScriptBin "start" ''
check-react-native
align-emulator
$PWD/node_modules/.bin/react-native start $@
'')
# gen-properties
(writeScriptBin "gen-properties" ''
cat <<EOF > ./android/local.properties
sdk.dir=$ANDROID_SDK_ROOT
ndk.dir=$ANDROID_NDK_ROOT
EOF || (echo "are you sure in the right project directory?"; exit 1)
'')
];
in
{
devShells.default = pkgs.mkShell {
buildInputs = with pkgs; [
# nodejs 18 (specified by overlay)
nodejs
# yarn with nodejs 18 (specified by overlay)
yarn
# java 11 (specified by overlay)
jdk
# gradle with java 11 (specified by overlay)
gradle
# ruby with cocoapods (specified by overlay)
ruby
# androidSdk (specified by overlay)
androidSdk
# react-native development dependencies
watchman
] ++ scripts;
ANDROID_NDK_ROOT = "${pkgs.androidSdk.outPath}/share/android-sdk/ndk/${androidNDKVersion}";
shellHook = ''
helpme
'';
};
});
}