Skip to content

Commit e005675

Browse files
committed
nix + web stuff
1 parent e62be30 commit e005675

3 files changed

Lines changed: 333 additions & 7 deletions

File tree

flake.lock

Lines changed: 100 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
{
2+
description = "YTBN Graphing Software - Web-compatible graphing calculator";
3+
4+
inputs = {
5+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
6+
flake-utils.url = "github:numtide/flake-utils";
7+
rust-overlay = {
8+
url = "github:oxalica/rust-overlay";
9+
inputs.nixpkgs.follows = "nixpkgs";
10+
};
11+
simon-egui = {
12+
url = "github:Titaniumtown/egui/b63c21d70150f1b414370f0f9a8af56e886662f4";
13+
flake = false;
14+
};
15+
};
16+
17+
outputs = { self, nixpkgs, flake-utils, rust-overlay, simon-egui }:
18+
flake-utils.lib.eachDefaultSystem (system:
19+
let
20+
overlays = [ (import rust-overlay) ];
21+
pkgs = import nixpkgs {
22+
inherit system overlays;
23+
};
24+
25+
# Use nightly rust with wasm32 target
26+
rustToolchain = pkgs.rust-bin.nightly."2025-05-01".default.override {
27+
targets = [ "wasm32-unknown-unknown" ];
28+
};
29+
30+
rustPlatform = pkgs.makeRustPlatform {
31+
cargo = rustToolchain;
32+
rustc = rustToolchain;
33+
};
34+
35+
# Build wasm-bindgen-cli matching the version in Cargo.lock (0.2.106)
36+
wasm-bindgen-cli = rustPlatform.buildRustPackage rec {
37+
pname = "wasm-bindgen-cli";
38+
version = "0.2.106";
39+
40+
src = pkgs.fetchCrate {
41+
inherit pname version;
42+
hash = "sha256-M6WuGl7EruNopHZbqBpucu4RWz44/MSdv6f0zkYw+44=";
43+
};
44+
45+
cargoHash = "sha256-ElDatyOwdKwHg3bNH/1pcxKI7LXkhsotlDPQjiLHBwA=";
46+
47+
nativeBuildInputs = [ pkgs.pkg-config ];
48+
buildInputs = [ pkgs.openssl ] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
49+
pkgs.curl
50+
pkgs.darwin.apple_sdk.frameworks.Security
51+
];
52+
53+
# Tests require network access
54+
doCheck = false;
55+
};
56+
57+
# Create a combined source with the main project and dependencies
58+
combinedSrc = pkgs.stdenv.mkDerivation {
59+
name = "ytbn-combined-src";
60+
phases = [ "installPhase" ];
61+
installPhase = ''
62+
mkdir -p $out/integral_site_rust
63+
mkdir -p $out/simon-egui
64+
65+
cp -r ${./.}/* $out/integral_site_rust/
66+
cp -r ${simon-egui}/* $out/simon-egui/
67+
68+
chmod -R u+w $out
69+
'';
70+
};
71+
72+
# Build the wasm library using rustPlatform
73+
wasmLib = rustPlatform.buildRustPackage {
74+
pname = "ytbn-graphing-software-wasm";
75+
version = "0.1.0";
76+
77+
src = combinedSrc;
78+
sourceRoot = "${combinedSrc.name}/integral_site_rust";
79+
80+
cargoLock = {
81+
lockFile = ./Cargo.lock;
82+
outputHashes = {
83+
"egui_plot-0.34.0" = "sha256-lk0yeljsvkHzF0eLD5llQ+05DycPqG2jGzhBvQ0X6Qw=";
84+
};
85+
};
86+
87+
nativeBuildInputs = with pkgs; [
88+
python3Packages.fonttools
89+
pkg-config
90+
clang
91+
];
92+
93+
buildInputs = with pkgs; [
94+
openssl
95+
zstd
96+
];
97+
98+
buildPhase = ''
99+
runHook preBuild
100+
101+
export HOME=$TMPDIR
102+
103+
cargo build \
104+
--release \
105+
--lib \
106+
--target wasm32-unknown-unknown
107+
108+
runHook postBuild
109+
'';
110+
111+
installPhase = ''
112+
runHook preInstall
113+
mkdir -p $out/lib
114+
cp target/wasm32-unknown-unknown/release/*.wasm $out/lib/
115+
runHook postInstall
116+
'';
117+
118+
doCheck = false;
119+
};
120+
121+
# Final web package with wasm-bindgen processing
122+
ytbn-graphing-software-web = pkgs.stdenv.mkDerivation {
123+
pname = "ytbn-graphing-software-web";
124+
version = "0.1.0";
125+
126+
src = ./.;
127+
128+
nativeBuildInputs = [
129+
wasm-bindgen-cli
130+
pkgs.binaryen
131+
];
132+
133+
buildPhase = ''
134+
runHook preBuild
135+
136+
# Generate JS bindings
137+
wasm-bindgen ${wasmLib}/lib/ytbn_graphing_software.wasm \
138+
--out-dir out \
139+
--out-name ytbn_graphing_software \
140+
--target web \
141+
--no-typescript
142+
143+
# Optimize wasm (enable features used by modern rust wasm targets)
144+
wasm-opt out/ytbn_graphing_software_bg.wasm \
145+
-O2 --fast-math \
146+
--enable-bulk-memory \
147+
--enable-nontrapping-float-to-int \
148+
--enable-sign-ext \
149+
--enable-mutable-globals \
150+
-o out/ytbn_graphing_software_bg.wasm
151+
152+
runHook postBuild
153+
'';
154+
155+
installPhase = ''
156+
runHook preInstall
157+
158+
mkdir -p $out
159+
160+
# Copy wasm and js files
161+
cp out/ytbn_graphing_software_bg.wasm $out/
162+
cp out/ytbn_graphing_software.js $out/
163+
164+
# Copy static web assets
165+
cp www/index.html $out/
166+
cp www/manifest.json $out/
167+
cp www/sw.js $out/
168+
169+
# Copy logo
170+
cp assets/logo.svg $out/
171+
172+
runHook postInstall
173+
'';
174+
175+
meta = with pkgs.lib; {
176+
description = "Web-compatible graphing calculator similar to Desmos";
177+
homepage = "https://github.com/Titaniumtown/YTBN-Graphing-Software";
178+
license = licenses.agpl3Only;
179+
platforms = platforms.all;
180+
};
181+
};
182+
in
183+
{
184+
packages = {
185+
default = ytbn-graphing-software-web;
186+
web = ytbn-graphing-software-web;
187+
wasm = wasmLib;
188+
};
189+
190+
devShells.default = pkgs.mkShell {
191+
nativeBuildInputs = with pkgs; [
192+
rustToolchain
193+
wasm-bindgen-cli
194+
binaryen
195+
python3Packages.fonttools
196+
rust-analyzer
197+
pkg-config
198+
clang
199+
200+
# Runtime deps for native builds
201+
libxkbcommon
202+
libGL
203+
wayland
204+
xorg.libX11
205+
xorg.libXcursor
206+
xorg.libXi
207+
];
208+
209+
buildInputs = with pkgs; [
210+
openssl
211+
zstd
212+
];
213+
214+
LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath (with pkgs; [
215+
libxkbcommon
216+
libGL
217+
wayland
218+
xorg.libX11
219+
xorg.libXcursor
220+
xorg.libXi
221+
]);
222+
};
223+
}
224+
);
225+
}

src/math_app.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,16 @@ impl MathApp {
202202
#[cfg(target_arch = "wasm32")]
203203
{
204204
tracing::info!("Setting decompression cache");
205-
let commit: crate::misc::HashBytes = const {
206-
unsafe {
207-
std::mem::transmute::<&str, crate::misc::HashBytes>(build::SHORT_COMMIT)
208-
}
209-
};
210-
let saved_data = commit.hashed_storage_create(data);
205+
// Convert SHORT_COMMIT string to fixed-size byte array
206+
let commit_bytes = build::SHORT_COMMIT.as_bytes();
207+
let mut commit: crate::misc::HashBytes = [0u8; crate::misc::HASH_LENGTH];
208+
let len = commit_bytes.len().min(crate::misc::HASH_LENGTH);
209+
commit[..len].copy_from_slice(&commit_bytes[..len]);
210+
211+
let saved_data = crate::misc::hashed_storage_create(commit, &data);
211212
tracing::info!("Bytes: {}", saved_data.len());
212213
get_localstorage()
213-
.set_item(DATA_NAME, saved_data)
214+
.set_item(DATA_NAME, &saved_data)
214215
.expect("failed to set local storage cache");
215216
}
216217

0 commit comments

Comments
 (0)