generated from tweag/project
-
Notifications
You must be signed in to change notification settings - Fork 29
/
default.nix
192 lines (166 loc) · 5.56 KB
/
default.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
{ pkgs
, system
, advisory-db
, crane
, rust-overlay
, nix-filter
, craneLib
# tree-sitter-Nickel is packaged in Nixpkgs, but it's an older version at the
# time of writing. Since updating it seems non trivial, and we need Topiary to
# be compatible with Nickel urgently (it is currently blocking for the CI), we
# use the tree-sitter-nickel flake directly.
, tree-sitter-nickel
}:
let
wasmRustVersion = "1.77.2";
wasmTarget = "wasm32-unknown-unknown";
rustWithWasmTarget = pkgs.rust-bin.stable.${wasmRustVersion}.default.override {
targets = [ wasmTarget ];
};
commonArgs = {
pname = "topiary";
src = nix-filter.lib.filter {
root = ./.;
include = [
"benches"
"Cargo.lock"
"Cargo.toml"
"languages.ncl"
"languages_nix.ncl"
"tests"
"topiary-core"
"topiary-cli"
"topiary-config"
"topiary-playground"
"topiary-queries"
"topiary-tree-sitter-facade"
"topiary-web-tree-sitter-sys"
"examples"
];
};
nativeBuildInputs = with pkgs;
[
binaryen
wasm-bindgen-cli
pkg-config
]
++ lib.optionals stdenv.isDarwin [
libiconv
];
buildInputs = with pkgs;
[
openssl.dev
]
++ lib.optionals stdenv.isDarwin [
darwin.apple_sdk.frameworks.Security
];
};
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
# NB: we don't need to overlay our custom toolchain for the *entire*
# pkgs (which would require rebuidling anything else which uses rust).
# Instead, we just want to update the scope that crane will use by appending
# our specific toolchain there.
craneLibWasm = craneLib.overrideToolchain rustWithWasmTarget;
in
{
passtru = {
inherit craneLibWasm;
};
clippy = craneLib.cargoClippy (commonArgs
// {
inherit cargoArtifacts;
cargoClippyExtraArgs = "-- --deny warnings";
});
clippy-wasm = craneLibWasm.cargoClippy (commonArgs
// {
inherit cargoArtifacts;
cargoClippyExtraArgs = "-p topiary-playground --target ${wasmTarget} -- --deny warnings";
});
fmt = craneLib.cargoFmt commonArgs;
audit = craneLib.cargoAudit (commonArgs
// {
inherit advisory-db;
});
benchmark = craneLib.buildPackage (commonArgs
// {
inherit cargoArtifacts;
cargoTestCommand = "cargo bench --profile release";
});
client-app = craneLib.buildPackage (commonArgs // {
inherit cargoArtifacts;
pname = "client-app";
cargoExtraArgs = "-p client-app";
});
topiary-core = craneLib.buildPackage (commonArgs
// {
inherit cargoArtifacts;
pname = "topiary-core";
cargoExtraArgs = "-p topiary-core";
});
topiary-cli = { nixSupport ? false }: craneLib.buildPackage (commonArgs
// {
inherit cargoArtifacts;
pname = "topiary";
cargoExtraArgs = "-p topiary-cli";
cargoTestExtraArgs = "--no-default-features";
preConfigurePhases = pkgs.lib.optional nixSupport "useNixConfiguration";
# ocamllex is not (yet) packaged in nixpkgs
# ocamllex="${pkgs.tree-sitter-grammars.tree-sitter-ocamllex}/parser" \
useNixConfiguration = ''
bash="${pkgs.tree-sitter-grammars.tree-sitter-bash}/parser" \
css="${pkgs.tree-sitter-grammars.tree-sitter-css}/parser" \
json="${pkgs.tree-sitter-grammars.tree-sitter-json}/parser" \
nickel="${tree-sitter-nickel}/parser" \
ocaml="${pkgs.tree-sitter-grammars.tree-sitter-ocaml}/parser" \
ocaml_interface="${pkgs.tree-sitter-grammars.tree-sitter-ocaml-interface}/parser" \
rust="${pkgs.tree-sitter-grammars.tree-sitter-rust}/parser" \
toml="${pkgs.tree-sitter-grammars.tree-sitter-toml}/parser" \
tree_sitter_query="${pkgs.tree-sitter-grammars.tree-sitter-query}/parser" \
substituteAllInPlace topiary-config/languages_nix.ncl
mv topiary-config/languages_nix.ncl topiary-config/languages.ncl
'';
postInstall = ''
install -Dm444 topiary-queries/queries/* -t $out/share/queries
'';
# Set TOPIARY_LANGUAGE_DIR to the Nix store
# for the build
TOPIARY_LANGUAGE_DIR = "${placeholder "out"}/share/queries";
# Set TOPIARY_LANGUAGE_DIR to the working directory
# in a development shell
shellHook = ''
export TOPIARY_LANGUAGE_DIR=$PWD/queries
'';
});
topiary-queries = craneLib.buildPackage (commonArgs
// {
pname = "topiary-queries";
cargoExtraArgs = "-p topiary-queries";
postInstall = ''
install -Dm444 topiary-queries/queries/* -t $out/share/queries
'';
});
topiary-playground = craneLibWasm.buildPackage (commonArgs
// {
inherit cargoArtifacts;
pname = "topiary-playground";
cargoExtraArgs = "-p topiary-playground --no-default-features --target ${wasmTarget}";
# Tests currently need to be run via `cargo wasi` which
# isn't packaged in nixpkgs yet...
doCheck = false;
postInstall = ''
echo 'Removing unneeded dir'
rm -rf $out/lib
echo 'Running wasm-bindgen'
wasm-bindgen --version
wasm-bindgen --target web --out-dir $out target/wasm32-unknown-unknown/release/topiary_playground.wasm;
echo 'Running wasm-opt'
wasm-opt --version
wasm-opt -Oz -o $out/output.wasm $out/topiary_playground_bg.wasm
echo 'Overwriting topiary_playground_bg.wasm with the optimized file'
mv $out/output.wasm $out/topiary_playground_bg.wasm
echo 'Extracting custom build outputs'
export LANGUAGES_EXPORT="$(ls -t target/wasm32-unknown-unknown/release/build/topiary-playground-*/out/languages_export.ts | head -1)"
cp $LANGUAGES_EXPORT $out/
'';
});
}