generated from tweag/project
-
Notifications
You must be signed in to change notification settings - Fork 17
/
default.nix
134 lines (113 loc) · 3.9 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
{ pkgs, nixpkgs, system, advisory-db, crane, rust-overlay, nix-filter }:
let
rustPkgs = import nixpkgs {
inherit system;
overlays = [ (import rust-overlay) ];
};
wasmRustVersion = "1.67.1";
wasmTarget = "wasm32-unknown-unknown";
rustWithWasmTarget = rustPkgs.rust-bin.stable.${wasmRustVersion}.default.override {
targets = [ wasmTarget ];
};
craneLib = crane.mkLib pkgs;
commonArgs = {
src = nix-filter.lib.filter {
root = ./.;
include = [
"benches"
"Cargo.lock"
"Cargo.toml"
"languages.toml"
"languages"
"topiary"
"topiary-cli"
"topiary-playground"
"tests"
];
};
nativeBuildInputs = [
pkgs.binaryen pkgs.wasm-bindgen-cli
] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
pkgs.libiconv
];
};
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;
topiaryName = craneLib.crateNameFromCargoToml {
cargoToml = ./topiary/Cargo.toml;
};
topiaryCLIName = craneLib.crateNameFromCargoToml {
cargoToml = ./topiary-cli/Cargo.toml;
};
topiaryPlaygroundName = craneLib.crateNameFromCargoToml {
cargoToml = ./topiary-playground/Cargo.toml;
};
in
{
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";
});
topiary-lib = craneLib.buildPackage (commonArgs // {
inherit cargoArtifacts;
inherit (topiaryName) version;
pname = "topiary-lib";
cargoExtraArgs = "-p topiary";
});
topiary-cli = craneLib.buildPackage (commonArgs // {
inherit cargoArtifacts;
inherit (topiaryCLIName) version;
pname = "topiary";
cargoExtraArgs = "-p topiary-cli";
postInstall = ''
install -Dm444 languages/* -t $out/share/languages
'';
# Set TOPIARY_LANGUAGE_DIR to the Nix store
# for the build
TOPIARY_LANGUAGE_DIR = "${placeholder "out"}/share/languages";
# Set TOPIARY_LANGUAGE_DIR to the working directory
# in a development shell
shellHook = ''
export TOPIARY_LANGUAGE_DIR=$PWD/languages
'';
});
topiary-playground = craneLibWasm.buildPackage (commonArgs // {
inherit cargoArtifacts;
inherit (topiaryPlaygroundName) pname version;
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/
'';
});
}