This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathflake.nix
411 lines (345 loc) · 11.6 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
{
description = "Enarx Cod(e) Ex(amples)";
inputs.cryptle.url = github:enarx/cryptle;
inputs.enarx.url = github:enarx/enarx;
inputs.fenix.url = github:nix-community/fenix;
inputs.flake-utils.url = github:numtide/flake-utils;
inputs.naersk.inputs.nixpkgs.follows = "nixpkgs";
inputs.naersk.url = github:nix-community/naersk;
inputs.nixpkgs.url = github:profianinc/nixpkgs;
outputs = {
self,
cryptle,
enarx,
fenix,
flake-utils,
naersk,
nixpkgs,
}: let
cargoPackage = toml: (builtins.fromTOML (builtins.readFile toml)).package;
demos.chat-client.conf = ./demos/chat-client/Enarx.toml;
demos.chat-client.c.src = ./demos/chat-client/c/main.c;
demos.chat-client.cpp.src = ./demos/chat-client/c++;
demos.chat-client.rust.src = ./demos/chat-client/rust;
demos.chat-client.rust.package = cargoPackage ./demos/chat-client/rust/Cargo.toml;
demos.chat-server.conf = ./demos/chat-server/Enarx.toml;
demos.chat-server.rust.src = ./demos/chat-server/rust;
demos.chat-server.rust.package = cargoPackage ./demos/chat-server/rust/Cargo.toml;
demos.fibonacci.conf = ./demos/fibonacci/Enarx.toml;
demos.fibonacci.c.src = ./demos/fibonacci/c/fibonacci.c;
demos.fibonacci.cpp.src = ./demos/fibonacci/c++/fibonacci.cpp;
demos.fibonacci.go.src = ./demos/fibonacci/go;
demos.fibonacci.rust.package = cargoPackage ./demos/fibonacci/rust/Cargo.toml;
demos.fibonacci.rust.src = ./demos/fibonacci/rust;
demos.fibonacci.zig.src = ./demos/fibonacci/zig;
CARGO_BUILD_TARGET = "wasm32-wasi";
buildEnarxPackage = {
pkgs,
pname,
version,
wasm,
conf,
}:
pkgs.stdenv.mkDerivation {
inherit pname version;
dontUnpack = true;
installPhase = ''
mkdir -p $out
cp ${conf} $out/Enarx.toml
cp ${wasm} $out/main.wasm
'';
};
buildFibonacciPackage = {
pkgs,
wasm,
...
} @ args: let
args' =
args
// {
inherit (demos.fibonacci) conf;
inherit (wasm) version;
wasm = "${wasm}/bin/fibonacci.wasm";
pname = "fibonacci";
};
in
(buildEnarxPackage args').overrideAttrs (attrs: {
doCheck = true;
checkInputs = [
enarx.packages.${pkgs.system}.enarx
];
checkPhase = ''
${self}/tests/run.sh ${self}/tests/fibonacci/golden/default --wasmcfgfile ${demos.fibonacci.conf} ${wasm}/bin/fibonacci.wasm
cat ${self}/tests/fibonacci/stdin | ${self}/tests/run.sh ${self}/tests/fibonacci/golden/stdin ${wasm}/bin/fibonacci.wasm
'';
});
codex = final: prev: let
rust = with fenix.packages.${final.system};
combine [
stable.cargo
stable.rustc
targets.wasm32-wasi.stable.rust-std
];
naersk-lib = naersk.lib.${final.system}.override {
cargo = rust;
rustc = rust;
};
in {
chat-client-c-wasm =
final.pkgsCross.wasi32.runCommandCC "chat-client" {
pname = "chat-client-c";
version = "0.1.0";
}
''
mkdir -p "$out/bin"
$CC -Wall -pedantic ${demos.chat-client.c.src} \
-o "$out/bin/chat-client.wasm"
'';
chat-client-c = buildEnarxPackage {
inherit (demos.chat-client) conf;
inherit (final) pkgs;
inherit (final.chat-client-c-wasm) pname version;
wasm = "${final.chat-client-c-wasm}/bin/chat-client.wasm";
};
chat-client-cpp-wasm =
final.pkgsCross.wasi32.runCommandCC "chat-client" {
pname = "chat-client-cpp";
version = "0.1.0";
}
''
mkdir -p "$out/bin"
$CXX -Wall -I"${demos.chat-client.cpp.src}/assets.h" -pedantic ${demos.chat-client.cpp.src}/main.cpp \
-o "$out/bin/chat-client.wasm"
'';
chat-client-cpp = buildEnarxPackage {
inherit (demos.chat-client) conf;
inherit (final) pkgs;
inherit (final.chat-client-cpp-wasm) pname version;
wasm = "${final.chat-client-cpp-wasm}/bin/chat-client.wasm";
};
chat-client-rust-wasm = naersk-lib.buildPackage {
inherit (demos.chat-client.rust) src;
inherit (demos.chat-client.rust.package) version;
inherit CARGO_BUILD_TARGET;
pname = "chat-client-rust";
};
chat-client-rust = buildEnarxPackage {
inherit (demos.chat-client) conf;
inherit (final) pkgs;
inherit (final.chat-client-rust-wasm) pname version;
wasm = "${final.chat-client-rust-wasm}/bin/${demos.chat-client.rust.package.name}.wasm";
};
chat-server-rust-wasm = naersk-lib.buildPackage {
inherit (demos.chat-server.rust) src;
inherit (demos.chat-server.rust.package) version;
inherit CARGO_BUILD_TARGET;
pname = "chat-server-rust";
};
chat-server-rust = buildEnarxPackage {
inherit (demos.chat-server) conf;
inherit (final) pkgs;
inherit (final.chat-server-rust-wasm) pname version;
wasm = "${final.chat-server-rust-wasm}/bin/${demos.chat-server.rust.package.name}.wasm";
};
fibonacci-c-wasm =
final.pkgsCross.wasi32.runCommandCC "fibonacci" {
pname = "fibonacci-c";
version = "0.3.0";
}
''
mkdir -p "$out/bin"
$CC -Wall -pedantic ${demos.fibonacci.c.src} \
-o "$out/bin/fibonacci.wasm"
'';
fibonacci-c = buildFibonacciPackage {
inherit (final) pkgs;
wasm = final.fibonacci-c-wasm;
};
fibonacci-cpp-wasm =
final.pkgsCross.wasi32.runCommandCC "fibonacci" {
pname = "fibonacci-cpp";
version = "0.3.0";
}
''
mkdir -p "$out/bin"
$CXX -Wall -pedantic ${demos.fibonacci.cpp.src} \
-o "$out/bin/fibonacci.wasm"
'';
fibonacci-cpp = buildFibonacciPackage {
inherit (final) pkgs;
wasm = final.fibonacci-cpp-wasm;
};
fibonacci-go-wasm = final.stdenv.mkDerivation rec {
inherit (demos.fibonacci.go) src;
pname = "fibonacci-go";
version = "0.3.0";
nativeBuildInputs = with final; [tinygo];
configurePhase = ''
export HOME=$TMPDIR/home
export GOCACHE=$TMPDIR/go-cache
'';
buildPhase = ''
tinygo build -target wasi main.go
'';
installPhase = ''
mkdir -p $out/bin
cp main.wasm $out/bin/fibonacci.wasm
'';
};
fibonacci-go = buildFibonacciPackage {
inherit (final) pkgs;
wasm = final.fibonacci-go-wasm;
};
fibonacci-rust-wasm = naersk-lib.buildPackage {
inherit (demos.fibonacci.rust) src;
inherit (demos.fibonacci.rust.package) version;
inherit CARGO_BUILD_TARGET;
pname = "fibonacci-rust";
};
fibonacci-rust = buildFibonacciPackage {
inherit (final) pkgs;
wasm = final.fibonacci-rust-wasm;
};
fibonacci-zig-wasm = final.stdenv.mkDerivation {
inherit (demos.fibonacci.zig) src;
pname = "fibonacci-zig";
version = "0.4.0";
nativeBuildInputs = with final; [zig];
configurePhase = ''
export HOME=$TMPDIR
'';
installPhase = ''
zig build -Dtarget=wasm32-wasi -Drelease-safe -Dcpu=baseline --prefix $out install
'';
};
fibonacci-zig = buildFibonacciPackage {
inherit (final) pkgs;
wasm = final.fibonacci-zig-wasm;
};
};
credentialHelpers = final: prev: {
enarx-credential-helper-gopass = final.writeShellScriptBin "enarx-credential-helper-gopass" ''
set -e
if [ "''${1}" = "insert" ]; then
exec ${final.gopass}/bin/gopass insert -f "misc/enarx/''${2}"
elif [ "''${1}" = "show" ]; then
${final.gopass}/bin/gopass find misc/enarx 1>/dev/null 2>/dev/null
exec ${final.gopass}/bin/gopass show -n -o "misc/enarx/''${2}"
else
echo "Unknown command `''${1}`"
exit 1
fi
'';
enarx-credential-helper-pass = final.writeShellScriptBin "enarx-credential-helper-pass" ''
set -e
if [ "''${1}" = "insert" ]; then
exec ${final.pass}/bin/pass insert -f -m "misc/enarx/''${2}" 1> /dev/null
elif [ "''${1}" = "show" ]; then
exec ${final.pass}/bin/pass show "misc/enarx/''${2}"
else
echo "Unknown command `''${1}`"
exit 1
fi
'';
};
in
with flake-utils.lib.system;
{
overlays.codex = codex;
overlays.credentialHelpers = credentialHelpers;
overlays.default = codex;
}
// flake-utils.lib.eachSystem [
aarch64-darwin
aarch64-linux
x86_64-darwin
x86_64-linux
] (system: let
pkgs = import nixpkgs {
inherit system;
overlays = [
codex
credentialHelpers
cryptle.overlays.default
fenix.overlay
];
};
packages = with pkgs;
{
inherit
chat-client-c
chat-client-c-wasm
chat-client-cpp
chat-client-cpp-wasm
chat-client-rust
chat-client-rust-wasm
chat-server-rust
chat-server-rust-wasm
enarx-credential-helper-gopass
enarx-credential-helper-pass
fibonacci-c
fibonacci-c-wasm
fibonacci-cpp
fibonacci-cpp-wasm
fibonacci-rust
fibonacci-rust-wasm
;
cryptle-rust = cryptle-enarx;
cryptle-rust-wasm = cryptle-wasm;
}
// lib.optionalAttrs (!tinygo.meta.broken) {
# NOTE: TinyGo is broken on some platforms, only add Go packages on platforms where it works
inherit
fibonacci-go
fibonacci-go-wasm
;
}
// lib.optionalAttrs (!zig.meta.broken) {
# NOTE: Zig is broken on some platforms, only add Zig packages on platforms where it works
inherit
fibonacci-zig
fibonacci-zig-wasm
;
};
devShells = let
rust = with pkgs.fenix;
combine [
stable.cargo
stable.clippy
stable.rustc
stable.rustfmt
targets.wasm32-wasi.stable.rust-std
];
in
{
default = pkgs.mkShell {
buildInputs = [
enarx.packages.${system}.enarx
rust
];
};
rust = devShells.default.overrideAttrs (attrs: {
buildInputs = attrs.buildInputs ++ [rust];
});
}
// pkgs.lib.optionalAttrs (!pkgs.tinygo.meta.broken) {
# NOTE: TinyGo is broken on some platforms, only add Go shell on platforms where it works
go = devShells.default.overrideAttrs (attrs: {
buildInputs = attrs.buildInputs ++ [pkgs.tinygo];
});
}
// pkgs.lib.optionalAttrs (!pkgs.zig.meta.broken) {
# NOTE: Zig is broken on some platforms, only add Zig shell on platforms where it works
zig = devShells.default.overrideAttrs (attrs: {
buildInputs = attrs.buildInputs ++ [pkgs.zig];
});
};
in {
inherit
devShells
packages
;
checks = packages;
formatter = pkgs.alejandra;
});
}