Stream: rust-flake

Topic: Trunk + Wasm


view this post on Zulip Quinn (Jul 13 2024 at 17:41):

oh cool. NCI is frustrating me with wasm and trunk right now. so if you have an example for wasm and trunk, that might get me to switch

view this post on Zulip Srid (Jul 13 2024 at 22:45):

@Quinn rust-flake is designed to be basically a (flake-parts) wrapper over https://crane.dev/ - so if you can get something working with crane, it should map straightforwardly to rust-flake.

view this post on Zulip Shivaraj B H (Jul 17 2024 at 19:03):

oh cool. NCI is frustrating me with wasm and trunk right now. so if you have an example for wasm and trunk, that might get me to switch

Do you have an existing repo with wasm and trunk? I could try to package it using rust-flake

view this post on Zulip Quinn (Jul 17 2024 at 19:05):

this is the current deal https://github.com/quinn-dougherty/gatekept-traffic-PoC/tree/master/src.rs i haven't done a ton of debugging as to why its not working. I will say my emacs lsp is jank in that it works when i direnv to an NCI full rust environment of the project, but it all depends on rustup not being installed. it all comes crashing down if emacs finds a rustup. that's a separate issue tho-- it'd be dope if it even built in CI.

view this post on Zulip Shivaraj B H (Jul 17 2024 at 19:07):

Alright, I will give it a try tomorrow, its almost bed time here.

view this post on Zulip Shivaraj B H (Jul 18 2024 at 10:03):

@Quinn it should be simple with crane: https://crane.dev/examples/trunk-workspace.html?highlight=trunk-work#

I used the same flake in your src.rs dir and it worked fine. There are some unrelated code errors, which you will have to fix, here’s the tail of it:

error[E0599]: no function or associated item named `new` found for struct `Pixels` in the current scope
  --> site/src/components/pixels.rs:54:34
   |
54 |             let pixels = Pixels::new(width, height, gl_context).unwrap();
   |                                  ^^^ function or associated item not found in `Pixels`

view this post on Zulip Shivaraj B H (Jul 18 2024 at 10:04):

Here’s the file (src.rs/flake.nix) that worked for me:

{
  description = "Build a cargo project";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";

    # The version of wasm-bindgen-cli needs to match the version in Cargo.lock
    # Update this to include the version you need
    nixpkgs-for-wasm-bindgen.url = "github:NixOS/nixpkgs/4e6868b1aa3766ab1de169922bb3826143941973";

    crane = {
      url = "github:ipetkov/crane";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    flake-utils.url = "github:numtide/flake-utils";

    rust-overlay = {
      url = "github:oxalica/rust-overlay";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = { self, nixpkgs, crane, flake-utils, rust-overlay, nixpkgs-for-wasm-bindgen, ... }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = import nixpkgs {
          inherit system;
          overlays = [ (import rust-overlay) ];
        };

        inherit (pkgs) lib;

        rustToolchainFor = p: p.rust-bin.stable.latest.default.override {
          # Set the build targets supported by the toolchain,
          # wasm32-unknown-unknown is required for trunk.
          targets = [ "wasm32-unknown-unknown" ];
        };
        craneLib = ((crane.mkLib pkgs).overrideToolchain rustToolchainFor).overrideScope (_final: _prev: {
          # The version of wasm-bindgen-cli needs to match the version in Cargo.lock. You
          # can unpin this if your nixpkgs commit contains the appropriate wasm-bindgen-cli version
          inherit (import nixpkgs-for-wasm-bindgen { inherit system; }) wasm-bindgen-cli;
        });

        # When filtering sources, we want to allow assets other than .rs files
        src = lib.cleanSourceWith {
          src = ./.; # The original, unfiltered source
          filter = path: type:
            (lib.hasSuffix "\.html" path) ||
            (lib.hasSuffix "\.scss" path) ||
            # Example of a folder for images, icons, etc
            (lib.hasInfix "/assets/" path) ||
            # Default filter from crane (allow .rs files)
            (craneLib.filterCargoSources path type)
          ;
        };

        commonArgs = {
          inherit src;
          strictDeps = true;

          buildInputs = [
            # Add additional build inputs here
          ] ++ lib.optionals pkgs.stdenv.isDarwin [
            # Additional darwin specific inputs can be set here
            pkgs.libiconv
          ];
        };

        # Wasm packages

        wasmArgs = commonArgs // {
          pname = "trunk-workspace-wasm";
          cargoExtraArgs = "--package=site";
          CARGO_BUILD_TARGET = "wasm32-unknown-unknown";
        };

        cargoArtifactsWasm = craneLib.buildDepsOnly (wasmArgs // {
          doCheck = false;
        });

        mySite = craneLib.buildTrunkPackage (wasmArgs // {
          pname = "trunk-workspace-site";
          cargoArtifacts = cargoArtifactsWasm;
          trunkIndexPath = "site/index.html";
          # The version of wasm-bindgen-cli here must match the one from Cargo.lock.
          wasm-bindgen-cli = pkgs.wasm-bindgen-cli.override {
            version = "0.2.92";
            hash = "sha256-1VwY8vQy7soKEgbki4LD+v259751kKxSxmo/gqE6yV0=";
            cargoHash = "sha256-aACJ+lYNEU8FFBs158G1/JG8sc6Rq080PeKCMnwdpH0=";
          };
        });
        serve-app = pkgs.writeShellScriptBin "serve-app" ''
          ${pkgs.python3Minimal}/bin/python3 -m http.server --directory ${mySite} 8000
        '';
      in
      {
        checks = {
          inherit mySite;
        };
        packages.default = mySite;

        apps.default = flake-utils.lib.mkApp {
          name = "server";
          drv = serve-app;
        };

        devShells.default = craneLib.devShell {
          # Inherit inputs from checks.
          checks = self.checks.${system};

          # Extra inputs can be added here; cargo and rustc are provided by default.
          packages = [
            pkgs.trunk
          ];
        };
      });
}

view this post on Zulip Notification Bot (Jul 18 2024 at 10:07):

7 messages were moved here from #nix > rust-flake (Nixify Rust projects) by Shivaraj B H.

view this post on Zulip Shivaraj B H (Jul 18 2024 at 10:27):

It should not be hard to support this in rust-flake. The only difference is the usage of buildTrunkPackage in place of buildPackage.
This is a schema I propose:

{
  rust-project = {
    crates = {
      "site" = {
           isTrunkPackage = true;
           extraBuildArgs = {
             wasm-bindgen-cli = 
             trunkIndexPath = "site/index.html";
       };
      "cli" = {  };
      "holodeck" = {  };
    };
  };
}

This would replace the 100+ line of flake.nix from above

view this post on Zulip Srid (Jul 18 2024 at 14:50):

Perhaps we should have a freeform type that can build all types of derivations, using:

(Search for -> drv in https://crane.dev/API.html to find these)

For e.g.,

{
  rust-project.crates."foo" = {
    packages.trunk = { extraBuildArgs = { ... } };
  };
}

view this post on Zulip Srid (Jul 18 2024 at 14:51):

Eventually, we can even allow the user to build a custom package.

view this post on Zulip Srid (Jul 18 2024 at 14:51):

packages.NAME maps to a known builder if NAME is known (eg: trunk maps to buildTrunkPackage). The user can define their own builders.

view this post on Zulip Srid (Jul 18 2024 at 14:53):

In the meanwhile, @Quinn can already use rust-flake to call buildTrunkPackage because we expose crane-lib anyway.

view this post on Zulip Srid (Jul 18 2024 at 14:53):

As an aside, it would be good to have a template for Trunk projects, especially as part of https://github.com/juspay/rust-flake/issues/15


Last updated: Dec 05 2024 at 02:53 UTC