forked from systeminit/si
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
338 lines (313 loc) · 10.9 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
{
description = "System Initiative 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";
flake-utils.follows = "flake-utils";
};
};
};
outputs = {
self,
nixpkgs,
flake-utils,
rust-overlay,
...
}:
flake-utils.lib.eachDefaultSystem (system: let
overlays = [
(self: super: {
nodejs = super.nodejs-18_x;
pnpm = super.nodePackages.pnpm;
})
(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"];
};
buck2NativeBuildInputs = with pkgs;
[
bash
buck2
cacert
clang
gitMinimal
lld
makeWrapper
nodejs
pnpm
protobuf
python3
ripgrep
rust-toolchain
# breakpointHook
]
++ lib.optionals pkgs.stdenv.isDarwin [
darwin.sigtool
];
buck2BuildInputs = with pkgs;
[]
++ lib.optionals pkgs.stdenv.isLinux [
glibc
glibc.libgcc
]
++ lib.optionals pkgs.stdenv.isDarwin [
libiconv
darwin.apple_sdk.frameworks.Security
darwin.apple_sdk.frameworks.SystemConfiguration
];
# This isn't an exact science, but confirming 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
skopeo
];
buck2Derivation = {
pathPrefix,
pkgName,
extraBuildInputs ? [],
stdBuildPhase ? ''
buck2 build "$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
'';
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 = {
council = binDerivation {pkgName = "council";};
cyclone = binDerivation {pkgName = "cyclone";};
# This one's awful: we don't have a stanalone binary here, we have a
# directory of stuff with a `bin/$name` entrypoint shell script to
# invoke this beast. Additionally there are `node_modules` symlinks
# everywhere and Buck2's normal `buck2 build ... --out ...` option
# dies on some symlinks referring to directories and not files.
# Instead we'll have to parse a build report JSON to get the output
# path and copy it ourselves.
lang-js = buck2Derivation {
pathPrefix = "bin";
pkgName = "lang-js";
extraBuildInputs = [pkgs.jq];
stdBuildPhase = ''
buck2 build "$buck2_target" --verbose 8 --build-report report.log
dist_dir="$(jq -r \
'.results | to_entries | map(.value)[0].outputs.DEFAULT[0]' \
<report.log
)"
cp -rpv "$dist_dir" "build/$name-$system"
'';
installPhase = ''
mkdir -pv "$out"
cp -rpv "build/$name-$system"/* "$out"/
mv -v "$out/bin/lang-js" "$out/bin/.lang-js"
# 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 litteral string which happens to be a shell
# variable. Nuclear arms race of quoting and escaping special
# characters to make this work...
substituteInPlace "$out/bin/.lang-js" \
--replace "#!${pkgs.coreutils}/bin/env sh" "#!${pkgs.bash}/bin/sh" \
--replace "\''${0%/*}/../lib/" "$out/lib/" \
--replace "exec node" "exec ${pkgs.nodejs}/bin/node"
makeWrapper "$out/bin/.lang-js" "$out/bin/lang-js" \
--prefix PATH : ${pkgs.lib.makeBinPath langJsExtraPkgs}
'';
};
module-index = binDerivation {pkgName = "module-index";};
pinga = binDerivation {pkgName = "pinga";};
sdf = binDerivation {pkgName = "sdf";};
si = binDerivation {pkgName = "si";};
# Builds a standalone binary that is divorced from the Nix build and
# runtime environment
si-binary = buck2Derivation {
pathPrefix = "bin";
pkgName = "si";
stdBuildPhase = ''
# TODO(fnichol): we currently have a dynamically linked library of
# libc++abi being looked for in a `/nix/store` path when this
# binary is compiled on macOS, so for the meantime until this is
# fixed, we're going to perform a Cargo build of the binary (it
# does not suffer from this issue).
cargo build --bin "$name" --release
cp -pv "target/release/$name" "build/$name-$system"
'';
installPhase = ''
mkdir -pv "$out/bin"
cp -pv "build/$name-$system" "$out/bin/$name"
'';
dontPatchELF = true;
dontAutoPatchELF = true;
postFixup =
""
+ pkgs.lib.optionalString (pkgs.stdenv.isDarwin) ''
nix_lib="$(otool -L "$out/bin/$name" \
| grep libiconv.dylib \
| awk '{print $1}'
)"
install_name_tool \
-change \
"$nix_lib" \
/usr/lib/libiconv.2.dylib \
"$out/bin/$name" \
2>/dev/null
''
+ pkgs.lib.optionalString (pkgs.stdenv.isLinux) ''
patchelf \
--set-interpreter "${systemInterpreter}" \
--remove-rpath \
"$out/bin/$name"
''
+ ''
mkdir -pv "$out/tarballs"
tar cvf "$out/tarballs/$name-$system.tar" -C "$out/bin" "$name"
gzip -9 "$out/tarballs/$name-$system.tar"
'';
};
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 {
packages =
[
alejandra
docker-compose
gh
jq
# pgcli
reindeer
shellcheck
shfmt
tilt
]
# 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;
});
}