-
Notifications
You must be signed in to change notification settings - Fork 25
/
flake.nix
402 lines (355 loc) · 15.2 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
{
description = "homestar"; # Thanks to @walkah
inputs = {
nixpkgs.url = "nixpkgs/nixos-23.11";
# we leverage unstable due to wasm-tools/wasm updates
nixos-unstable.url = "nixpkgs/nixos-unstable-small";
flake-utils.url = "github:numtide/flake-utils";
nixlib.url = "github:nix-community/nixpkgs.lib";
nixify = {
url = "github:rvolosatovs/nixify";
inputs.nixlib.follows = "nixlib";
};
flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
wit-deps = {
url = "github:bytecodealliance/wit-deps/v0.3.5";
inputs.nixlib.follows = "nixlib";
inputs.nixify.follows = "nixify";
};
# Needed due to wit-deps not existing in nixpkgs.
# TODO: Remove once wit-deps is in nixpkgs or move to fenix? maybe?
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = {
self,
nixpkgs,
nixos-unstable,
fenix,
flake-utils,
wit-deps,
...
}:
flake-utils.lib.eachDefaultSystem (
system: let
overlays = [fenix.overlays.default wit-deps.overlays.default];
pkgs = import nixpkgs {inherit system overlays;};
unstable = import nixos-unstable {inherit system overlays;};
file-toolchain = fenix.packages.${system}.fromToolchainFile {
file = ./rust-toolchain.toml;
# sha256 = pkgs.lib.fakeSha256;
sha256 = "sha256-+syqAd2kX8KVa8/U2gz3blIQTTsYYt3U63xBWaGOSc8=";
};
default-toolchain = fenix.packages.${system}.complete.withComponents [
"cargo"
"clippy"
"llvm-tools-preview"
"rustfmt"
"rust-src"
"rust-std"
];
rust-toolchain = fenix.packages.${system}.combine [file-toolchain default-toolchain];
rustPlatform = pkgs.makeRustPlatform {
cargo = rust-toolchain;
rustc = rust-toolchain;
};
nightly-rustfmt =
(fenix.packages.${system}.toolchainOf {
channel = "nightly";
date = "2024-03-26";
sha256 = "sha256-kHuQWRjPWmulZWG7tka+rX+VjZOnP7pZ2xF2qxV8784=";
# sha256 = pkgs.lib.fakeSha256;
})
.rustfmt;
format-pkgs = with pkgs; [
nixpkgs-fmt
alejandra
taplo
];
cargo-installs = with pkgs; [
cargo-deb
cargo-deny
cargo-cross
cargo-expand
cargo-hakari
cargo-machete
cargo-nextest
cargo-outdated
cargo-sort
cargo-unused-features
cargo-udeps
cargo-watch
rustup
tokio-console
twiggy
unstable.cargo-component
unstable.wasm-tools
# TODO: Return to this
# unstable.warg
# unstable.wit
];
ci = pkgs.writeScriptBin "ci" ''
#!${pkgs.stdenv.shell}
cargo fmt --check
cargo clippy
cargo build --release
nx-test
nx-test-0
'';
db = pkgs.writeScriptBin "db" ''
#!${pkgs.stdenv.shell}
diesel setup
diesel migration run
'';
dbReset = pkgs.writeScriptBin "db-reset" ''
#!${pkgs.stdenv.shell}
diesel database reset
diesel setup
diesel migration run
'';
devBuild = pkgs.writeScriptBin "cargo-build-dev" ''
#!${pkgs.stdenv.shell}
RUSTFLAGS="--cfg tokio_unstable" cargo build --no-default-features --features dev
'';
devCheck = pkgs.writeScriptBin "cargo-check-dev" ''
#!${pkgs.stdenv.shell}
RUSTFLAGS="--cfg tokio_unstable" cargo build --no-default-features --features dev
'';
devRunServer = pkgs.writeScriptBin "cargo-run-dev" ''
#!${pkgs.stdenv.shell}
cargo run --no-default-features --features dev -- start
'';
doc = pkgs.writeScriptBin "doc" ''
#!${pkgs.stdenv.shell}
cargo doc --workspace --all-features --no-deps --document-private-items --open
'';
docAll = pkgs.writeScriptBin "doc-all" ''
#!${pkgs.stdenv.shell}
cargo doc --workspace --all-features --document-private-items --open
'';
dockerBuild = arch:
pkgs.writeScriptBin "docker-${arch}" ''
#!${pkgs.stdenv.shell}
docker buildx build --build-arg git_sha=$(git rev-parse HEAD) \
--build-arg git_timestamp=$(git log -1 --pretty=format:'%cI') \
--file docker/Dockerfile --platform=linux/${arch} -t homestar --progress=plain .
'';
xFunc = cmd:
pkgs.writeScriptBin "x-${cmd}" ''
#!${pkgs.stdenv.shell}
cargo watch -c -x ${cmd}
'';
xFuncAll = cmd:
pkgs.writeScriptBin "x-${cmd}-all" ''
#!${pkgs.stdenv.shell}
cargo watch -c -s "cargo ${cmd} --workspace --all-features"
'';
xFuncNoDefault = cmd:
pkgs.writeScriptBin "x-${cmd}-0" ''
#!${pkgs.stdenv.shell}
cargo watch -c -s "cargo ${cmd} --no-default-features"
'';
xFuncPackage = cmd: crate:
pkgs.writeScriptBin "x-${cmd}-${crate}" ''
#!${pkgs.stdenv.shell}
cargo watch -c -s "cargo ${cmd} -p homestar-${crate} --all-features"
'';
xFuncTest = pkgs.writeScriptBin "x-test" ''
#!${pkgs.stdenv.shell}
cargo watch -c -s "cargo nextest run --workspace --no-capture && cargo test --doc"
'';
xFuncTestAll = pkgs.writeScriptBin "x-test-all" ''
#!${pkgs.stdenv.shell}
cargo watch -c -s "cargo nextest run --workspace --all-features --no-capture \
&& cargo test --workspace --doc --all-features"
'';
xFuncTestNoDefault = pkgs.writeScriptBin "x-test-0" ''
#!${pkgs.stdenv.shell}
cargo watch -c -s "cargo nextest run --no-default-features --no-capture \
&& cargo test --doc --no-default-features"
'';
xFuncTestPackage = crate:
pkgs.writeScriptBin "x-test-${crate}" ''
#!${pkgs.stdenv.shell}
cargo watch -c -s "cargo nextest run -p homestar-${crate} --all-features \
&& cargo test --doc -p homestar-${crate} --all-features"
'';
nxTest = pkgs.writeScriptBin "nx-test" ''
#!${pkgs.stdenv.shell}
cargo nextest run --workspace
cargo test --workspace --doc
'';
nxTestAll = pkgs.writeScriptBin "nx-test-all" ''
#!${pkgs.stdenv.shell}
cargo nextest run --workspace --all-features --no-capture
cargo test --workspace --doc --all-features
'';
nxTestNoDefault = pkgs.writeScriptBin "nx-test-0" ''
#!${pkgs.stdenv.shell}
cargo nextest run --no-default-features --no-capture
cargo test --doc --no-default-features
'';
wasmTest = pkgs.writeScriptBin "wasm-ex-test" ''
#!${pkgs.stdenv.shell}
cargo component build -p homestar-functions-test --target wasm32-unknown-unknown --profile release-wasm-fn
cp target/wasm32-unknown-unknown/release-wasm-fn/homestar_functions_test.wasm homestar-wasm/fixtures/example_test_cargo_component.wasm
cargo component build -p homestar-functions-test --profile release-wasm-fn
cp target/wasm32-wasi/release-wasm-fn/homestar_functions_test.wasm homestar-wasm/fixtures/example_test_cargo_component_wasi.wasm
cargo build -p homestar-functions-test --target wasm32-unknown-unknown --profile release-wasm-fn
cp target/wasm32-unknown-unknown/release-wasm-fn/homestar_functions_test.wasm homestar-wasm/fixtures/example_test.wasm
wasm-tools component new homestar-wasm/fixtures/example_test.wasm -o homestar-wasm/fixtures/example_test_component.wasm
cargo build -p homestar-functions-test --target wasm32-wasi --profile release-wasm-fn
cp target/wasm32-wasi/release-wasm-fn/homestar_functions_test.wasm homestar-wasm/fixtures/example_test_wasi.wasm
wasm-tools component new homestar-wasm/fixtures/example_test_wasi.wasm -o homestar-wasm/fixtures/example_test_wasi_component.wasm --adapt homestar-functions/wasi_snapshot_preview1.wasm
cp homestar-wasm/fixtures/example_test.wasm examples/websocket-relay/example_test.wasm
wasm-tools component wit homestar-wasm/wit -o homestar-wasm/fixtures/host.wasm --wasm
'';
wasmAdd = pkgs.writeScriptBin "wasm-ex-add" ''
#!${pkgs.stdenv.shell}
cargo component build -p homestar-functions-add --profile release-wasm-fn
cp target/wasm32-wasi/release-wasm-fn/homestar_functions_add.wasm homestar-wasm/fixtures/example_add_cargo_component_wasi.wasm
wasm-tools print homestar-wasm/fixtures/example_add_cargo_component_wasi.wasm -o homestar-wasm/fixtures/example_add_cargo_component_wasi.wat
cargo build -p homestar-functions-add --target wasm32-unknown-unknown --profile release-wasm-fn
cp target/wasm32-unknown-unknown/release-wasm-fn/homestar_functions_add.wasm homestar-wasm/fixtures/example_add.wasm
wasm-tools component new homestar-wasm/fixtures/example_add.wasm -o homestar-wasm/fixtures/example_add_component.wasm
wasm-tools print homestar-wasm/fixtures/example_add.wasm -o homestar-wasm/fixtures/example_add.wat
wasm-tools print homestar-wasm/fixtures/example_add_component.wasm -o homestar-wasm/fixtures/example_add_component.wat
'';
wasmSubtract = pkgs.writeScriptBin "wasm-ex-subtract" ''
#!${pkgs.stdenv.shell}
cargo component build -p homestar-functions-subtract --profile release-wasm-fn
cp target/wasm32-wasi/release-wasm-fn/homestar_functions_subtract.wasm homestar-wasm/fixtures/example_subtract_cargo_component_wasi.wasm
wasm-tools print homestar-wasm/fixtures/example_subtract_cargo_component_wasi.wasm -o homestar-wasm/fixtures/example_subtract_cargo_component_wasi.wat
cargo build -p homestar-functions-subtract --target wasm32-unknown-unknown --profile release-wasm-fn
cp target/wasm32-unknown-unknown/release-wasm-fn/homestar_functions_subtract.wasm homestar-wasm/fixtures/example_subtract.wasm
wasm-tools component new homestar-wasm/fixtures/example_subtract.wasm -o homestar-wasm/fixtures/example_subtract_component.wasm
wasm-tools print homestar-wasm/fixtures/example_subtract.wasm -o homestar-wasm/fixtures/example_subtract.wat
wasm-tools print homestar-wasm/fixtures/example_subtract_component.wasm -o homestar-wasm/fixtures/example_subtract_component.wat
'';
runIpfs = pkgs.writeScriptBin "run-ipfs" ''
#!${pkgs.stdenv.shell}
ipfs --repo-dir ./.ipfs --offline daemon
'';
testCleanup = pkgs.writeScriptBin "test-clean" ''
#!${pkgs.stdenv.shell}
rm -rf homestar-runtime/tests/fixtures/*.db*
rm -rf homestar-runtime/tests/fixtures/*.toml
'';
scripts = [
ci
db
dbReset
devCheck
devBuild
devRunServer
doc
docAll
(builtins.map (arch: dockerBuild arch) ["amd64" "arm64"])
(builtins.map (cmd: xFunc cmd) ["build" "check" "run" "clippy"])
(builtins.map (cmd: xFuncAll cmd) ["build" "check" "run" "clippy"])
(builtins.map (cmd: xFuncNoDefault cmd) ["build" "check" "run" "clippy"])
(builtins.map (cmd: xFuncPackage cmd "core") ["build" "check" "run" "clippy"])
(builtins.map (cmd: xFuncPackage cmd "wasm") ["build" "check" "run" "clippy"])
(builtins.map (cmd: xFuncPackage cmd "runtime") ["build" "check" "run" "clippy"])
xFuncTest
xFuncTestAll
xFuncTestNoDefault
(builtins.map (crate: xFuncTestPackage crate) ["core" "wasm" "guest-wasm" "runtime"])
nxTest
nxTestAll
nxTestNoDefault
runIpfs
testCleanup
wasmTest
wasmAdd
wasmSubtract
];
in {
devShells.default = pkgs.mkShell {
name = "homestar";
nativeBuildInputs = with pkgs;
[
# The ordering of these two items is important. For nightly rustfmt to be used instead of
# the rustfmt provided by `rust-toolchain`, it must appear first in the list. This is
# because native build inputs are added to $PATH in the order they're listed here.
nightly-rustfmt
rust-toolchain
pkgs.wit-deps
pkg-config
pre-commit
diesel-cli
direnv
unstable.nodejs_20
unstable.nodePackages.pnpm
action-validator
kubo
self.packages.${system}.irust
]
++ format-pkgs
++ cargo-installs
++ scripts
++ lib.optionals stdenv.isDarwin [
libiconv
darwin.apple_sdk.frameworks.Security
darwin.apple_sdk.frameworks.CoreFoundation
darwin.apple_sdk.frameworks.Foundation
];
NIX_PATH = "nixpkgs=" + pkgs.path;
RUST_BACKTRACE = 1;
shellHook =
''
[ -e .git/hooks/pre-commit ] || pre-commit install --install-hooks && pre-commit install --hook-type commit-msg
# Setup local Kubo config
if [ ! -e ./.ipfs ]; then
ipfs --repo-dir ./.ipfs --offline init
fi
unset SOURCE_DATE_EPOCH
# Run Kubo / IPFS
echo -e "To run Kubo as a local IPFS node, use the following command:"
echo -e "ipfs --repo-dir ./.ipfs --offline daemon"
''
# See https://github.com/nextest-rs/nextest/issues/267
+ (pkgs.lib.strings.optionalString pkgs.stdenv.isDarwin ''
export DYLD_FALLBACK_LIBRARY_PATH="$(rustc --print sysroot)/lib"
export NIX_LDFLAGS="-F${pkgs.darwin.apple_sdk.frameworks.CoreFoundation}/Library/Frameworks -framework CoreFoundation $NIX_LDFLAGS";
'');
};
packages.irust = rustPlatform.buildRustPackage rec {
pname = "IRust";
version = "1.71.20";
src = pkgs.fetchFromGitHub {
owner = "sigmaSd";
repo = pname;
rev = "v${version}";
sha256 = "sha256-PokyKNHiZqOo1DXLQ2NAkuax4BXayNko9nQMighuh4Q=";
};
doCheck = false;
cargoSha256 = "sha256-0CMrA6k4OEXM9MJ9Iyk0yMuulvhT2pXty4OIU5Z6a9U=";
};
packages.default =
rustPlatform.buildRustPackage
{
name = "homestar";
src = ./.;
cargoLock = {
lockFile = ./Cargo.lock;
};
buildInputs = with pkgs;
[]
++ lib.optionals stdenv.isDarwin [
libiconv
darwin.apple_sdk.frameworks.Security
darwin.apple_sdk.frameworks.CoreFoundation
darwin.apple_sdk.frameworks.Foundation
];
doCheck = false;
};
formatter = pkgs.alejandra;
}
);
}