-
Notifications
You must be signed in to change notification settings - Fork 285
/
Copy pathflake.nix
329 lines (302 loc) · 10.2 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
{
description = "System Initiative build and development environment";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs = {
nixpkgs.follows = "nixpkgs";
};
};
};
outputs = {
self,
nixpkgs,
flake-utils,
rust-overlay,
...
}:
flake-utils.lib.eachDefaultSystem (system: let
overlays = [
(self: super: {
nodejs = super.nodejs-18_x;
})
(import rust-overlay)
];
pkgs = import nixpkgs {inherit overlays system;};
rustVersion = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain;
rust-toolchain = rustVersion.override {
extensions = ["rust-analyzer" "rust-src"];
};
nodePkgs = pkgs.nodePackages.override {
nodejs = pkgs.nodejs_18;
};
buck2NativeBuildInputs = with pkgs;
[
b3sum
bash
buck2
cacert
clang
gitMinimal
lld
makeWrapper
nodejs
deno
nodePkgs.pnpm
protobuf
python3
ripgrep
rust-toolchain
minica
# breakpointHook
]
++ lib.optionals pkgs.stdenv.isDarwin [
darwin.sigtool
];
buck2BuildInputs = with pkgs;
[]
++ lib.optionals pkgs.stdenv.isLinux [
glibc
glibc.libgcc
lvm2
llvmPackages.libclang.lib
]
++ lib.optionals pkgs.stdenv.isDarwin [
libiconv
darwin.apple_sdk.frameworks.Security
darwin.apple_sdk.frameworks.SystemConfiguration
darwin.apple_sdk.frameworks.CoreFoundation
];
# This isn't an exact science, but confirmed the system interpreter by
# running `ldd /bin/sh` in Docker containers running:
# - debian:9-slim
# - ubuntu:rolling
# - fedora:38
# - archlinux:latest
# - amazonlinux:latest
# - rockylinux:9-minimal
systemInterpreter =
{
"x86_64-linux" = "/lib64/ld-linux-x86-64.so.2";
"aarch64-linux" = "/lib/ld-linux-aarch64.so.1";
"x86_64-darwin" = "/dev/null";
"aarch64-darwin" = "/dev/null";
}
.${system};
langJsExtraPkgs = with pkgs; [
awscli2
butane
gh
skopeo
openssh
minio-client
fastly
doctl
google-cloud-sdk
linode-cli
flyctl
azure-cli
];
buck2Derivation = {
pathPrefix,
pkgName,
extraBuildInputs ? [],
stdBuildPhase ? ''
buck2 build @//mode/release "$buck2_target" --verbose 8 --out "build/$name-$system"
'',
extraBuildPhase ? "",
installPhase,
dontStrip ? false,
dontPatchELF ? false,
dontAutoPatchELF ? false,
postFixup ? "",
}:
pkgs.stdenv.mkDerivation rec {
name = pkgName;
buck2_target = "//${pathPrefix}/${pkgName}";
__impure = true;
src = ./.;
nativeBuildInputs =
buck2NativeBuildInputs
++ pkgs.lib.optionals (
pkgs.stdenv.isLinux && !dontAutoPatchELF
) [pkgs.autoPatchelfHook];
buildInputs = buck2BuildInputs ++ extraBuildInputs;
runtimeDependencies = map pkgs.lib.getLib buck2BuildInputs;
postPatch = with pkgs; ''
rg -l '#!(/usr/bin/env|/bin/bash|/bin/sh)' prelude prelude-si \
| while read -r file; do
patchShebangs --build "$file"
done
rg -l '(/usr/bin/env|/bin/bash)' prelude prelude-si \
| while read -r file; do
substituteInPlace "$file" \
--replace /usr/bin/env "${coreutils}/bin/env" \
--replace /bin/bash "${bash}/bin/bash"
done
'';
configurePhase = ''
export LIBCLANG_PATH="${pkgs.llvmPackages.libclang.lib}/lib"
export BINDGEN_EXTRA_CLANG_ARGS="$(< ${pkgs.stdenv.cc}/nix-support/libc-crt1-cflags) \
$(< ${pkgs.stdenv.cc}/nix-support/libc-cflags) \
$(< ${pkgs.stdenv.cc}/nix-support/cc-cflags) \
$(< ${pkgs.stdenv.cc}/nix-support/libcxx-cxxflags) \
$NIX_CFLAGS_COMPILE"
export OUT=${placeholder "out"}
echo $OUT
'';
buildPhase =
''
export HOME="$(dirname $(pwd))/home"
mkdir -p build
''
+ stdBuildPhase
+ extraBuildPhase;
inherit installPhase;
inherit dontStrip;
inherit dontPatchELF;
inherit postFixup;
};
appDerivation = {
pkgName,
extraBuildPhase ? "",
extraInstallPhase ? "",
dontStrip ? false,
dontPatchELF ? false,
dontAutoPatchELF ? false,
}: (buck2Derivation {
pathPrefix = "app";
inherit pkgName;
inherit extraBuildPhase;
installPhase =
''
mkdir -pv "$out"
cp -rpv "build/$name-$system" "$out/html"
''
+ extraInstallPhase;
inherit dontStrip;
inherit dontPatchELF;
inherit dontAutoPatchELF;
});
binDerivation = {
pkgName,
extraInstallPhase ? "",
dontStrip ? false,
dontPatchELF ? false,
dontAutoPatchELF ? false,
}: (buck2Derivation {
pathPrefix = "bin";
inherit pkgName;
installPhase =
''
mkdir -pv "$out/bin"
cp -pv "build/$name-$system" "$out/bin/$name"
''
+ extraInstallPhase;
inherit dontStrip;
inherit dontPatchELF;
inherit dontAutoPatchELF;
});
in
with pkgs; rec {
packages = {
auth-api = binDerivation {pkgName = "auth-api";};
cyclone = binDerivation {pkgName = "cyclone";};
# autoPatchingElf and stripping will break deno compile'd binaries.
# We need to make sure we know where glibc and friends exist and since
# we can't patchelf, we need to ensure we drop the dynmic linker in a
# known place. Note that LD_LIBRAY_PATH is unset when using siExec to
# ensure the binaries our binaries run don't inherit it.
lang-js = binDerivation {
pkgName = "lang-js";
dontAutoPatchELF = true;
dontStrip = true;
extraInstallPhase = ''
# since we can't patchelf, we need to ensure the dynamic linker
# is where we expect it. This gets droppped into the rootfs as
# /lib64/ld-linux-x86-64.so.2 -> /nix/store/*/ld-linux-x86-64.20.2
mkdir -p $out/lib64
ln -sf ${pkgs.glibc}/lib/ld-linux-x86-64.so.2 $out/lib64/ld-linux-x86-64.so.2
ln -sf ${pkgs.glibc}/lib/ld-linux-aarch64.so.1 $out/lib64/ld-linux-aarch64.so.1
wrapProgram $out/bin/lang-js \
--set LD_LIBRARY_PATH "${pkgs.lib.makeLibraryPath [
pkgs.glibc
pkgs.gcc-unwrapped.lib
]}" \
--set SI_LANG_JS_LOG "debug" \
--prefix PATH : ${pkgs.lib.makeBinPath langJsExtraPkgs} \
--run 'cd "$(dirname "$0")"'
# ^ deno falls over trying to resolve libraries if you don't set
# the working path
'';
};
forklift = binDerivation {pkgName = "forklift";};
module-index = binDerivation {pkgName = "module-index";};
pinga = binDerivation {pkgName = "pinga";};
rebaser = binDerivation {pkgName = "rebaser";};
sdf = binDerivation {pkgName = "sdf";};
veritech = binDerivation {pkgName = "veritech";};
web = appDerivation rec {
pkgName = "web";
extraBuildPhase = ''
buck2 build app/web:nginx_src --verbose 3 --out build/nginx
buck2 build app/web:docker-entrypoint.sh \
--verbose 3 --out build/docker-entrypoint.sh
'';
extraInstallPhase = ''
patchShebangs --host build/docker-entrypoint.sh
substituteInPlace build/docker-entrypoint.sh \
--replace @@nginx@@ "${nginx}/bin/nginx" \
--replace @@conf@@ "$out/conf/nginx.conf" \
--replace @@prefix@@ "$out"
mkdir -pv "$out/bin" "$out/conf"
cp -pv build/nginx/nginx.conf "$out/conf/nginx.conf"
cp -pv "${nginx}/conf/mime.types" "$out/conf"/
cp -pv build/docker-entrypoint.sh "$out/bin/${pkgName}"
'';
};
};
devShells.default = mkShell {
# Env Vars so bindgen can find libclang
shellHook = ''
export LIBCLANG_PATH="${pkgs.llvmPackages.libclang.lib}/lib"
export BINDGEN_EXTRA_CLANG_ARGS="$(< ${pkgs.stdenv.cc}/nix-support/libc-crt1-cflags) \
$(< ${pkgs.stdenv.cc}/nix-support/libc-cflags) \
$(< ${pkgs.stdenv.cc}/nix-support/cc-cflags) \
$(< ${pkgs.stdenv.cc}/nix-support/libcxx-cxxflags) \
$NIX_CFLAGS_COMPILE"
export OUT=${placeholder "out"}
echo $OUT
'';
packages =
[
alejandra
buildkite-test-collector-rust
docker-compose
jq
nats-top
natscli
# pgcli
reindeer
shellcheck
shfmt
spicedb-zed
tilt
typos
yapf
]
# Directly add the build dependencies for the packages rather than
# use: `inputsFrom = lib.attrValues packages;`.
#
# This tweak means our flake doesn't require `impure-derivations`
# and `ca-derivations` experimental features by default--only when
# attempting to build the packages directly.
++ buck2NativeBuildInputs
++ buck2BuildInputs
++ langJsExtraPkgs;
};
formatter = alejandra;
});
}