-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
232 lines (203 loc) · 7.13 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
{
description = "A libary that contains various utils for developing web apps with yew-rs";
inputs.nixpkgs.url = "nixpkgs/release-23.05";
inputs.nixpkgs-unstable.url = "nixpkgs/nixpkgs-unstable";
inputs.nix-rust-utils.url = "git+https://git.vdx.hu/voidcontext/nix-rust-utils.git?ref=refs/tags/v0.10.0";
inputs.nix-rust-utils.inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-utils.url = "github:numtide/flake-utils";
inputs.rust-overlay.url = "github:oxalica/rust-overlay";
outputs = {
nixpkgs,
nixpkgs-unstable,
flake-utils,
nix-rust-utils,
rust-overlay,
...
}:
flake-utils.lib.eachDefaultSystem (
system: let
pkgs = import nixpkgs {
inherit system;
overlays = [
rust-overlay.overlays.default
(final: prev: {unstable = nixpkgs-unstable.legacyPackages.${system};})
];
};
rustWithWasm32 = pkgs.rust-bin.stable."1.69.0".default.override {
targets = ["wasm32-unknown-unknown"];
};
nru = nix-rust-utils.mkLib {
inherit pkgs;
toolchain = rustWithWasm32;
};
src = ./.;
index-html = import ./index.html.nix {inherit pkgs;};
node-packages = pkgs.callPackage ./nix/node {};
plainViewCss = ''
.autocomplete-item {
padding: 0.3rem 0.5rem;
display: block;
}
.autocomplete-item.highlighted {
background: #ebfffc;;
}
'';
commonArgs = {
inherit src;
buildInputs = [pkgs.unstable.wasm-bindgen-cli];
# Nodejs is need by wasm-bindgen-test
checkInputs = [pkgs.nodejs];
};
yew-components = nru.mkWasmCrate commonArgs;
example = name:
# TODO: find a better way to build examples
nru.mkWasmCrate (commonArgs
// {
# Build examples in the preBuild step and copy them into the release directory so that
# the postBuild script picks them up and generates their JS bindings
preBuild = nru.snippets.wasm.buildExample name;
});
serve-example-demo = name: port: let
demo-src = pkgs.symlinkJoin {
name = "${name}-demo";
paths = [(index-html name plainViewCss) (example name)];
};
in
pkgs.writeShellScriptBin "serve-${name}-demo"
(nru.snippets.utils.serve demo-src port);
serve-autocomplete-demo = serve-example-demo "autocomplete" 9001;
check-nix-formatting = pkgs.stdenv.mkDerivation {
name = "nix-formatting-check";
src = pkgs.lib.cleanSourceWith {
src = ./.;
# prevent re-checking the formatting when only non nix files changed
filter = path: _type: builtins.match ".*\.nix$" path != null;
};
checkInputs = [pkgs.alejandra];
checkPhase = ''
alejandra . --check \
-e nix/node # this directory contains generated code
'';
doCheck = true;
installPhase = ''mkdir -p $out'';
};
gen-node-packages = pkgs.writeShellScriptBin "gen-node-packages" ''
cd $WORKSPACE/nix/node
${pkgs.node2nix}/bin/node2nix -i node-packages.json -o node-packages.nix
'';
fmt = pkgs.writeShellScriptBin "fmt-nix" ''
${pkgs.alejandra}/bin/alejandra -e $WORKSPACE/nix/node $WORKSPACE
'';
cypress-bin = pkgs.cypress;
cypress = node-packages."cypress-${pkgs.cypress.version}".overrideAttrs (old:
{
buildInputs = old.buildInputs ++ (pkgs.lib.optionals (system == flake-utils.lib.system.x86_64-linux) [cypress-bin]);
}
// (
if system == flake-utils.lib.system.x86_64-linux
then {
CYPRESS_INSTALL_BINARY = 0;
}
else {}
));
watch-autocomplete-demo = let
watches = nru.utils.watch {
"$WORKSPACE/yew-autocomplete/src $WORKSPACE/yew-autocomplete/examples/autocomplete" = ''
${nru.snippets.wasm.buildExample "autocomplete"}
${nru.snippets.wasm.bindgen {outDir = "$WORKSPACE/dist/lib";}}
'';
};
in
pkgs.writeShellScriptBin "watch-autocomplete-demo"
(nru.snippets.utils.cleanupWrapper ''
out=$WORKSPACE/dist
mkdir -p $out/lib
cp ${index-html "autocomplete" plainViewCss}/index.html $out
${watches}
${nru.snippets.utils.serve "$out" 9001}
'');
mkRunE2eTests = suffix: wrapper: let
prefix =
if system == flake-utils.lib.system.x86_64-linux
then "CYPRESS_RUN_BINARY=${cypress-bin}/bin/Cypress xvfb-run "
else "";
in
pkgs.writeShellApplication {
name = "run-e2e-tests${suffix}";
runtimeInputs =
[
pkgs.coreutils # timeout
pkgs.netcat
pkgs.procps #pkill
serve-autocomplete-demo
cypress
]
++ (pkgs.lib.optionals pkgs.stdenv.isLinux [pkgs.xvfb-run]);
text = wrapper ''
set -x -e -o pipefail
serve-autocomplete-demo&
echo $! >> server.pid
# shellcheck disable=SC2016
timeout 30 sh -c 'until nc -z $0 $1; do sleep 1; done' 0.0.0.0 9001
${prefix}cypress run
# shellcheck disable=SC2046
pkill -TERM -P $(cat server.pid)
'';
};
run-e2e-tests = mkRunE2eTests "" nru.snippets.utils.cleanupWrapper;
run-e2e-tests-ci = mkRunE2eTests "-ci" (text: text);
mkApp = derivation: name: {
${name} = {
type = "app";
program = "${derivation}/bin/${name}";
};
};
checks =
(nru.mkWasmChecks {
inherit src;
crate = yew-components;
})
// {
inherit
run-e2e-tests
run-e2e-tests-ci
serve-autocomplete-demo
;
nix-formatting = check-nix-formatting;
};
apps = let
derivations = [
(mkApp serve-autocomplete-demo "serve-autocomplete-demo")
(mkApp run-e2e-tests "run-e2e-tests")
(mkApp run-e2e-tests-ci "run-e2e-tests-ci")
];
in
builtins.foldl' pkgs.lib.recursiveUpdate {} derivations;
in {
inherit apps checks;
packages = {
default = yew-components;
inherit
run-e2e-tests
run-e2e-tests-ci
serve-autocomplete-demo
check-nix-formatting
;
};
devShells.default = nru.mkDevShell {
inputsFrom = [yew-components];
inherit checks;
buildInputs = [
pkgs.node2nix
pkgs.gnused
pkgs.cargo-edit
gen-node-packages
watch-autocomplete-demo
cypress
fmt
rustWithWasm32
];
};
}
);
}