-
Notifications
You must be signed in to change notification settings - Fork 143
/
flake.nix
360 lines (307 loc) · 11.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
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
{
description = "Galoy dev environment";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";
concourse-shared.url = "github:galoymoney/concourse-shared";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs = {
nixpkgs.follows = "nixpkgs";
flake-utils.follows = "flake-utils";
};
};
};
outputs = {
self,
nixpkgs,
flake-utils,
concourse-shared,
rust-overlay,
}:
flake-utils.lib.eachDefaultSystem (system: let
overlays = [
(self: super: {
nodejs = super.nodejs_20;
pnpm = super.nodePackages.pnpm;
})
(import rust-overlay)
];
pkgs = import nixpkgs {inherit overlays system;};
rustVersion = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
rust-toolchain = rustVersion.override {
extensions = ["rust-analyzer" "rust-src"];
};
buck2NativeBuildInputs = with pkgs; [
buck2
protobuf
nodejs
pnpm
python3
ripgrep
cacert
clang
lld
rust-toolchain
];
nativeBuildInputs = with pkgs;
[
envsubst
nodejs
tilt
typescript
bats
postgresql
alejandra
gnumake
docker
docker-compose
shellcheck
shfmt
vendir
jq
ytt
sqlx-cli
cargo-nextest
cargo-audit
cargo-watch
reindeer
gitMinimal
grpcurl
buf
netcat
]
++ buck2NativeBuildInputs
++ lib.optionals pkgs.stdenv.isLinux [
xvfb-run
cypress
];
buck2BuildInputs = with pkgs;
[]
++ lib.optionals pkgs.stdenv.isDarwin [
darwin.apple_sdk.frameworks.SystemConfiguration
];
buck2Version = pkgs.buck2.version;
postPatch = with pkgs; ''
rg -l '#!(/usr/bin/env|/bin/bash|/bin/sh)' prelude toolchains \
| while read -r file; do
patchShebangs --build "$file"
done
rg -l '(/usr/bin/env|/bin/bash)' prelude toolchains \
| while read -r file; do
substituteInPlace "$file" \
--replace /usr/bin/env "${coreutils}/bin/env" \
--replace /bin/bash "${bash}/bin/bash"
done
'';
tscDerivation = {
pkgName,
pathPrefix ? "core",
}:
pkgs.stdenv.mkDerivation {
bin_target = pkgName;
deps_target = "prod_build";
name = pkgName;
buck2_target = "//${pathPrefix}/${pkgName}";
__impure = true;
src = ./.;
nativeBuildInputs = buck2NativeBuildInputs;
inherit postPatch;
buildPhase = ''
export HOME="$(dirname $(pwd))/home"
buck2 build "$buck2_target" --verbose 8
deps_result=$(buck2 build --show-simple-output "$buck2_target:$deps_target" 2> /dev/null)
bin_result=$(buck2 build --show-simple-output "$buck2_target:$bin_target" 2> /dev/null)
mkdir -p build/$name-$system/bin
echo "$(pwd)/$deps_result" > build/$name-$system/buck2-deps-path
cp -rpv $deps_result build/$name-$system/lib
cp -rpv $bin_result build/$name-$system/bin/
'';
installPhase = ''
mkdir -pv "$out"
cp -rpv "build/$name-$system/lib" "$out/"
cp -rpv "build/$name-$system/bin" "$out/"
substituteInPlace "$out/bin/run" \
--replace "#!${pkgs.coreutils}/bin/env sh" "#!${pkgs.bash}/bin/sh" \
--replace "$(cat build/$name-$system/buck2-deps-path)" "$out/lib" \
--replace "exec node" "exec ${pkgs.nodejs}/bin/node"
'';
};
nextDerivation = {
pkgName,
pathPrefix ? "apps",
}:
pkgs.stdenv.mkDerivation {
bin_target = pkgName;
name = pkgName;
buck2_target = "//${pathPrefix}/${pkgName}";
__impure = true;
src = ./.;
nativeBuildInputs = buck2NativeBuildInputs;
inherit postPatch;
buildPhase = ''
export HOME="$(dirname $(pwd))/home"
mkdir -p build
buck2 build "$buck2_target" --verbose 8
result=$(buck2 build --show-simple-output "$buck2_target" 2> /dev/null)
mkdir -p "build/$name-$system"
cp -rpv "$result" "build/$name-$system/"
'';
installPhase = ''
mkdir -pv "$out"
cp -rpv build/$name-$system/app/* "$out/"
# Need to escape this shell variable which should not be
# iterpreted in Nix as a variable nor a shell variable when run
# but rather a literal string which happens to be a shell
# variable. Nuclear arms race of quoting and escaping special
# characters to make this work...
substituteInPlace "$out/bin/run" \
--replace "#!${pkgs.coreutils}/bin/env sh" "#!${pkgs.bash}/bin/sh" \
--replace "\''${0%/*}/../lib/" "$out/lib/" \
--replace "exec node" "exec ${pkgs.nodejs}/bin/node"
'';
};
npmDerivation = {
pkgName,
binTarget,
npmBinTarget,
pathPrefix ? "core",
}:
pkgs.stdenv.mkDerivation {
name = "${binTarget}-${pkgName}";
buck2_target = "//${pathPrefix}/${pkgName}";
bin_target = binTarget;
npm_bin_target = npmBinTarget;
__impure = true;
src = ./.;
nativeBuildInputs = buck2NativeBuildInputs;
inherit postPatch;
buildPhase = ''
export HOME="$(dirname $(pwd))/home"
buck2 build "$buck2_target" --verbose 8
npm_bin_result=$(buck2 build --show-simple-output "$buck2_target:$npm_bin_target" 2> /dev/null)
bin_result=$(buck2 build --show-simple-output "$buck2_target:$bin_target" 2> /dev/null)
bin_parent_path=$(dirname $(dirname $bin_result))
node_modules_result=$(buck2 build --show-simple-output "$buck2_target:node_modules" 2> /dev/null)
mkdir -p build/$name-$system/bin
echo "$(pwd)/$npm_bin_result" > build/$name-$system/buck2-npm-bin-path
echo "$(pwd)/$node_modules_result" > build/$name-$system/buck2-node-modules-path
echo "$(pwd)/$bin_parent_path/__workspace" > build/$name-$system/buck2-deps-path
mv $bin_parent_path/__workspace build/$name-$system/lib
cp -rpv $bin_result build/$name-$system/bin/
cp -rpv $npm_bin_result build/$name-$system/bin/
'';
installPhase = ''
mkdir -pv "$out"
cp -rpv "build/$name-$system/lib" "$out/"
cp -rpv "build/$name-$system/bin" "$out/"
npm_bin=$(cat build/$name-$system/buck2-npm-bin-path)
npm_bin_parent_path=$(dirname $npm_bin)
substituteInPlace "$out/bin/run" \
--replace "#!${pkgs.coreutils}/bin/env sh" "#!${pkgs.bash}/bin/sh" \
--replace "$npm_bin_parent_path" "$out/bin" \
--replace "$(cat build/$name-$system/buck2-deps-path)" "$out/lib"
npm_bin_name=$(basename $npm_bin)
substituteInPlace "$out/bin/$npm_bin_name" \
--replace "#!${pkgs.coreutils}/bin/env sh" "#!${pkgs.bash}/bin/sh" \
--replace "$(cat build/$name-$system/buck2-node-modules-path)" "$out/lib"
npm_bin_source_file=$(cat "$out/bin/$npm_bin_name" | grep "exec" | awk '{print $2}')
substituteInPlace "$npm_bin_source_file" \
--replace "$(cat build/$name-$system/buck2-node-modules-path)" "$out/lib" \
--replace "exec node" "exec ${pkgs.nodejs}/bin/node" \
--replace " sed " " ${pkgs.gnused}/bin/sed " \
--replace "dirname" "${pkgs.coreutils}/bin/dirname" \
--replace "uname" "${pkgs.coreutils}/bin/uname"
'';
};
rustDerivation = {
pkgName,
pathPrefix ? "core",
}:
pkgs.stdenv.mkDerivation {
bin_target = pkgName;
name = pkgName;
buck2_target = "//${pathPrefix}/${pkgName}";
src = ./.;
nativeBuildInputs = buck2NativeBuildInputs;
inherit postPatch;
buildPhase = ''
export HOME="$(dirname $(pwd))/home"
buck2 build "$buck2_target" --verbose 8
result=$(buck2 build --show-simple-output "$buck2_target:$bin_target" 2> /dev/null)
mkdir -p build/$name-$system/bin
cp -rpv $result build/$name-$system/bin/
'';
installPhase = ''
mkdir -pv "$out"
cp -rpv "build/$name-$system/bin" "$out/"
'';
};
gh-token = concourse-shared.packages.${system}.gh-token;
in
with pkgs; {
packages = {
api = tscDerivation {pkgName = "api";};
api-trigger = tscDerivation {pkgName = "api-trigger";};
api-ws-server = tscDerivation {pkgName = "api-ws-server";};
api-exporter = tscDerivation {pkgName = "api-exporter";};
api-cron = tscDerivation {pkgName = "api-cron";};
consent = nextDerivation {pkgName = "consent";};
dashboard = nextDerivation {pkgName = "dashboard";};
pay = nextDerivation {pkgName = "pay";};
admin-panel = nextDerivation {pkgName = "admin-panel";};
map = nextDerivation {pkgName = "map";};
voucher = nextDerivation {pkgName = "voucher";};
migrate-mongo = npmDerivation {
pkgName = "api";
binTarget = "migrate-mongo-up";
npmBinTarget = "migrate_mongo_bin";
};
api-keys = rustDerivation {pkgName = "api-keys";};
notifications = rustDerivation {pkgName = "notifications";};
dockerImage = dockerTools.buildImage {
name = "galoy-dev";
tag = "latest";
# Optional base image to bring in extra binaries for debugging etc.
fromImage = dockerTools.pullImage {
imageName = "ubuntu";
imageDigest = "sha256:496a9a44971eb4ac7aa9a218867b7eec98bdef452246c037aa206c841b653e08";
sha256 = "sha256-LYdoE40tYih0XXJoJ8/b1e/IAkO94Jrs2C8oXWTeUTg=";
finalImageTag = "mantic-20240122";
finalImageName = "ubuntu";
};
config = {
Cmd = ["bash"];
Env = [
"GIT_SSL_CAINFO=${cacert}/etc/ssl/certs/ca-bundle.crt"
"SSL_CERT_FILE=${cacert}/etc/ssl/certs/ca-bundle.crt"
];
};
copyToRoot = buildEnv {
name = "image-root";
paths =
nativeBuildInputs
++ [
bash
yq-go
google-cloud-sdk
gh
gh-token
openssh
rsync
git-cliff
unixtools.xxd
];
pathsToLink = ["/bin"];
};
};
};
devShells.default = mkShell {
inherit nativeBuildInputs;
buildInputs = buck2BuildInputs;
BUCK2_VERSION = buck2Version;
COMPOSE_PROJECT_NAME = "galoy-dev";
};
formatter = alejandra;
});
}