2025-12-04 02:52:18 -05:00

201 lines
5.0 KiB
Nix

{
description = "YTBN Graphing Software";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
{
self,
nixpkgs,
flake-utils,
rust-overlay,
}:
flake-utils.lib.eachDefaultSystem (
system:
let
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs {
inherit system overlays;
};
# Use nightly rust with wasm32 target
rustToolchain = pkgs.rust-bin.stable.latest.default.override {
targets = [ "wasm32-unknown-unknown" ];
};
rustPlatform = pkgs.makeRustPlatform {
cargo = rustToolchain;
rustc = rustToolchain;
};
# Build the wasm library using rustPlatform
wasmLib = rustPlatform.buildRustPackage {
pname = "ytbn-graphing-software-wasm";
version = "0.1.0";
src = ./.;
cargoLock = {
lockFile = ./Cargo.lock;
outputHashes = {
"ecolor-0.33.2" = "sha256-jdQK55yKZptadwosrJXIhoQDGNeELQmPExWRsGc0VG0=";
"egui_plot-0.34.0" = "sha256-CfqrpAnLNcB3StuZ9YSDTWih8OUVEa9SJi9RwS1i4ok=";
};
};
nativeBuildInputs = with pkgs; [
python3Packages.fonttools
pkg-config
clang
];
buildInputs = with pkgs; [
openssl
zstd
];
# Run all tests on native target before building wasm
# Note: Tests run without --release because the release profile uses
# panic=abort which is incompatible with the test harness.
checkPhase =
let
libPath = pkgs.lib.makeLibraryPath (
with pkgs;
[
libxkbcommon
libGL
wayland
]
);
in
''
runHook preCheck
export LD_LIBRARY_PATH="${libPath}:$LD_LIBRARY_PATH"
cargo test --workspace --features native-test
runHook postCheck
'';
buildPhase = ''
runHook preBuild
cargo build \
--release \
--lib \
--target wasm32-unknown-unknown
runHook postBuild
'';
installPhase = ''
runHook preInstall
mkdir -p $out/lib
cp target/wasm32-unknown-unknown/release/*.wasm $out/lib/
runHook postInstall
'';
doCheck = true;
};
# Final web package with wasm-bindgen processing
ytbn-graphing-software-web = pkgs.stdenv.mkDerivation {
pname = "ytbn-graphing-software-web";
version = "0.1.0";
src = ./.;
nativeBuildInputs = with pkgs; [
wasm-bindgen-cli
binaryen
];
buildPhase = ''
runHook preBuild
# Generate JS bindings
wasm-bindgen ${wasmLib}/lib/ytbn_graphing_software.wasm \
--out-dir out \
--out-name ytbn_graphing_software \
--target web \
--no-typescript
# Optimize wasm (enable features used by modern rust wasm targets)
wasm-opt out/ytbn_graphing_software_bg.wasm \
-Oz \
--enable-bulk-memory \
--enable-nontrapping-float-to-int \
--enable-sign-ext \
--enable-mutable-globals \
-o out/ytbn_graphing_software_bg.wasm
runHook postBuild
'';
installPhase = ''
runHook preInstall
mkdir -p $out
# Copy wasm and js files
cp out/ytbn_graphing_software_bg.wasm $out/
cp out/ytbn_graphing_software.js $out/
# Copy static web assets
cp www/index.html $out/
cp www/manifest.json $out/
cp www/sw.js $out/
# Copy logo
cp assets/logo.svg $out/
runHook postInstall
'';
};
in
{
packages = {
default = ytbn-graphing-software-web;
web = ytbn-graphing-software-web;
wasm = wasmLib;
};
devShells.default = pkgs.mkShell {
nativeBuildInputs = with pkgs; [
rustToolchain
wasm-bindgen-cli
binaryen
python3Packages.fonttools
rust-analyzer
pkg-config
clang
# Runtime deps for native builds
libxkbcommon
libGL
wayland
];
buildInputs = with pkgs; [
openssl
zstd
];
LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath (
with pkgs;
[
libxkbcommon
libGL
wayland
]
);
};
}
);
}