mtp/flake.nix
Alois 89a20044a5
Some checks failed
CI / rustfmt (push) Successful in 17s
CI / clippy (push) Failing after 1m16s
CI / wasm build (push) Successful in 1m17s
CI / example (push) Successful in 1m29s
CI / test (push) Successful in 1m49s
CI / duplicate code (push) Successful in 12s
CI / web client (push) Failing after 27s
CI / cargo-machete (push) Successful in 1m10s
CI / cargo-deny (push) Failing after 2m20s
(feat): rename example-usage to just example
(feat): add the example's web-client dist folder to a gitignore
(fix): format issues
(fix): a lot of duplicate code
2026-06-27 03:20:25 +02:00

182 lines
5.9 KiB
Nix

{
description = "MTP - Methanium Transport Protocol";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
rust-overlay.url = "github:oxalica/rust-overlay";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = {
self,
nixpkgs,
rust-overlay,
flake-utils,
}:
flake-utils.lib.eachDefaultSystem (
system: let
overlays = [rust-overlay.overlays.default];
pkgs = import nixpkgs {inherit system overlays;};
rustToolchain = pkgs.rust-bin.stable.latest.default.override {
extensions = [
"rust-src"
"clippy"
"rustfmt"
];
targets = ["wasm32-unknown-unknown"];
};
clippyCheck = pkgs.writeShellApplication {
name = "mtp-clippy";
runtimeInputs = [rustToolchain];
text = ''
export MTP_TYPE_MAPS="''${MTP_TYPE_MAPS:-$PWD/example/type-maps.yaml}"
cargo clippy --workspace --exclude mtp-wasm --all-targets --all-features -- -D warnings \
-W unreachable-pub \
-W clippy::cognitive_complexity \
-W clippy::missing_docs_in_private_items \
-W clippy::missing_errors_doc \
-W clippy::missing_panics_doc \
-W clippy::missing_safety_doc \
-W clippy::undocumented_unsafe_blocks \
-W clippy::pedantic \
-W clippy::restriction \
-A clippy::blanket_clippy_restriction_lints
'';
};
macheteCheck = pkgs.writeShellApplication {
name = "mtp-machete";
runtimeInputs = [pkgs.cargo-machete];
text = ''
cargo machete "$@"
'';
};
buildAll = pkgs.writeShellApplication {
name = "mtp-build-all";
runtimeInputs = [rustToolchain pkgs.wasm-pack pkgs.bun clippyCheck macheteCheck];
text = ''
export MTP_TYPE_MAPS="''${MTP_TYPE_MAPS:-$PWD/example/type-maps.yaml}"
bun install --frozen-lockfile
cargo fmt --all --check
cargo b
cargo test --workspace --exclude mtp-wasm --all-features
cargo check --manifest-path example/Cargo.toml --workspace --all-targets --all-features
mtp-clippy
mtp-machete
bun run dup
RUSTFLAGS='--cfg web_sys_unstable_apis' wasm-pack build wasm --target web --out-dir pkg --release
bun install --cwd example/web-client --frozen-lockfile
bun run --cwd example/web-client build
'';
};
healthCheck = pkgs.writeShellApplication {
name = "mtp-health";
runtimeInputs = [clippyCheck macheteCheck];
text = ''
mtp-clippy
mtp-machete
'';
};
in {
devShells = {
default = pkgs.mkShell {
name = "mtp-dev";
buildInputs = with pkgs; [
rustToolchain
cargo-machete
wasm-pack
pkg-config
openssl
];
MTP_TYPE_MAPS = "${toString ./example/type-maps.yaml}";
shellHook = ''
repo_root="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
cert_dir="$repo_root/example/dev-cert"
public_dir="$repo_root/example/web-client/public"
cert_key="$cert_dir/key.pem"
cert_pem="$cert_dir/cert.pem"
cert_hash="$cert_dir/sha256.txt"
mkdir -p "$cert_dir"
mkdir -p "$public_dir"
if [ ! -f "$cert_key" ] || [ ! -f "$cert_pem" ]; then
openssl ecparam -name prime256v1 -genkey -noout -out "$cert_key"
openssl req -new -x509 \
-sha256 \
-key "$cert_key" \
-out "$cert_pem" \
-days 13 \
-subj "/CN=localhost" \
-addext "subjectAltName=DNS:localhost,IP:127.0.0.1" \
-addext "basicConstraints=critical,CA:FALSE" \
-addext "keyUsage=critical,digitalSignature" \
-addext "extendedKeyUsage=serverAuth"
cert_status="generated"
else
cert_status="cached"
fi
openssl x509 -in "$cert_pem" -outform der \
| openssl dgst -sha256 -binary \
| od -An -tx1 -v \
| tr -d ' \n' > "$cert_hash"
cp "$cert_hash" "$public_dir/mtp_dev_cert_hash.txt"
export MTP_DEV_CERT="$cert_pem"
export MTP_DEV_KEY="$cert_key"
export MTP_DEV_CERT_HASH="$(cat "$cert_hash")"
'';
};
autoStart = pkgs.mkShell {
name = "autoStart";
buildInputs = with pkgs; [
mprocs
];
shellHook = ''
nix develop --command bash -c "mprocs 'cd wasm && wasm-pack build --target web --out-dir pkg && cd ../example/web-client && bun dev' 'cargo b && cd example && cargo r --bin server' 'cd example && cargo r --bin client'"
exit
'';
};
};
# Ad-hoc WASM build using wasm-pack
apps = {
clippy = {
type = "app";
program = "${clippyCheck}/bin/mtp-clippy";
};
machete = {
type = "app";
program = "${macheteCheck}/bin/mtp-machete";
};
build-all = {
type = "app";
program = "${buildAll}/bin/mtp-build-all";
};
health = {
type = "app";
program = "${healthCheck}/bin/mtp-health";
};
wasm-build = {
type = "app";
program = "${pkgs.wasm-pack}/bin/wasm-pack";
args = [
"build"
"wasm"
"--target"
"web"
];
};
};
}
);
}