about summary refs log tree commit diff
path: root/web/tvixbolt/default.nix
diff options
context:
space:
mode:
authorIlan Joselevich <personal@ilanjoselevich.com>2024-07-04T18·05+0300
committerIlan Joselevich <personal@ilanjoselevich.com>2024-07-05T20·19+0000
commit7ca32d9f0baf2548ec32d75d2872e8b82d4d8921 (patch)
tree791a20db7c7c1c3cc7da36088ac3b4f11af4f0c0 /web/tvixbolt/default.nix
parent6a7069904e9d29f05638c37a52b640bc9eb43a63 (diff)
refactor(web/tvixbolt): buildRustPackage -> crate2nix r/8349
With the recent changes to crate2nix and buildRustCrate in nixpkgs it is
now possible to build tvixbolt via crate2nix like we do for other tvix
crates. We can reuse a lot of the customizations done in //tvix in
tvixbolt to avoid repeating ourselves.

A script for serving tvixbolt locally for testing purposes is also
available now through the .serve attribute of tvixbolt.

This change supersedes https://cl.tvl.fyi/c/depot/+/11821.

Change-Id: I4864df8b75aec73cf5fee2428924ed4cfbb32902
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11952
Tested-by: BuildkiteCI
Autosubmit: Ilan Joselevich <personal@ilanjoselevich.com>
Reviewed-by: flokli <flokli@flokli.de>
Diffstat (limited to 'web/tvixbolt/default.nix')
-rw-r--r--web/tvixbolt/default.nix94
1 files changed, 25 insertions, 69 deletions
diff --git a/web/tvixbolt/default.nix b/web/tvixbolt/default.nix
index d0dd8f37c4a7..33c6622717d5 100644
--- a/web/tvixbolt/default.nix
+++ b/web/tvixbolt/default.nix
@@ -1,74 +1,30 @@
-{ depot, lib, pkgs, ... }:
-
+{ pkgs, lib, depot, ... }:
 let
-  wasmRust = pkgs.rust-bin.stable.latest.default.override {
-    targets = [ "wasm32-unknown-unknown" ];
+  pkgsCross = pkgs.pkgsCross.wasm32-unknown-none;
+in
+(pkgsCross.callPackage ./Cargo.nix {
+  defaultCrateOverrides = (depot.tvix.utils.defaultCrateOverridesForPkgs pkgsCross) // {
+    tvixbolt = prev: {
+      src = depot.tvix.utils.filterRustCrateSrc { root = prev.src.origSrc; };
+    };
   };
-
-  cargoToml = with builtins; fromTOML (readFile ./Cargo.toml);
-
-  wasmBindgenMatch =
-    cargoToml.dependencies.wasm-bindgen == "= ${pkgs.wasm-bindgen-cli.version}";
-
-  assertWasmBindgen = assert (lib.assertMsg wasmBindgenMatch ''
-    Due to instability in the Rust WASM ecosystem, the trunk build
-    tool enforces that the Cargo-dependency version of `wasm-bindgen`
-    MUST match the version of the CLI supplied in the environment.
-
-    This can get out of sync when nixpkgs is updated. To resolve it,
-    wasm-bindgen must be bumped in the Cargo.toml file and cargo needs
-    to be run to resolve the dependencies.
-
-    Versions of `wasm-bindgen` in Cargo.toml:
-
-      Expected: '= ${pkgs.wasm-bindgen-cli.version}'
-      Actual:   '${cargoToml.dependencies.wasm-bindgen}'
-  ''); pkgs.wasm-bindgen-cli;
-
-  deps = [
-    pkgs.binaryen
-    pkgs.sass
-    pkgs.trunk
-
-    wasmRust
-    assertWasmBindgen
-  ];
-
-  # Cargo.toml needs to be patched with the /nix/store source path of
-  # tvix-eval.
-  cargoTomlPatch = pkgs.writeText "tvix-eval-src.patch" ''
-    diff --git a/Cargo.toml b/Cargo.toml
-    index 75006bec18..6ca244bbb2 100644
-    --- a/Cargo.toml
-    +++ b/Cargo.toml
-    @@ -16,7 +16,7 @@ rnix = "0.11.0"
-     wasm-bindgen = "= 0.2.83"
-
-     [dependencies.tvix-eval]
-    -path = "../../tvix/eval"
-    +path = "${../../tvix/eval}"
-     default-features = false
-
-     [dependencies.serde]
+}).rootCrate.build.overrideAttrs (oldAttrs: {
+  installPhase = ''
+    ${lib.getExe pkgs.wasm-bindgen-cli} \
+      --target web \
+      --out-dir $out \
+      --out-name ${oldAttrs.crateName} \
+      --no-typescript \
+      target/lib/${oldAttrs.crateName}-${oldAttrs.metadata}.wasm
+
+      mv src/*.{html,css} $out
   '';
-in
-pkgs.rustPlatform.buildRustPackage rec {
-  pname = "tvixbolt";
-  version = "canon";
-  src = lib.cleanSource ./.;
-
-  cargoLock.lockFile = ./Cargo.lock;
 
-  patches = [
-    cargoTomlPatch
-  ];
-
-  buildPhase = ''
-    export PATH=${lib.makeBinPath deps}:$PATH
-    mkdir home
-    export HOME=$PWD/home
-    trunk build --release -d $out
+  passthru.serve = pkgs.writeShellScriptBin "tvixbolt-serve" ''
+    ${lib.getExe pkgs.simple-http-server} \
+        --index \
+        --nocache \
+        "$@" \
+        ${depot.web.tvixbolt}
   '';
-
-  dontInstall = true;
-}
+})