Building ring crate for Rust-based function

@sei0o and others,

If you are interested in using Netlify for the crate cross compile builds on your Rust functions, I created a solution that works fairly well for now using cargo-zigbuild.

tldr;

Basically write a script to install zig binary using yarn (or npm) globally (6s install and only one line);
Check to see if the binary install for cargo-zigbuild already exists, if not cargo install otherwise ignore;
Compile your functions using cargo zigbuild for the correct target and copy to the functions folder. :rocket:

Script and Repo:

Here’s my example repo

Bash script to run inline with your build (./build.sh)

#!/bin/bash
# https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -euxo pipefail

which zig || yarn global add @ziglang/cli
# /opt/buildhome/.yarn/bin/zig
which cargo-zigbuild || cargo install cargo-zigbuild
mkdir -p netlify/functions
cargo zigbuild --target x86_64-unknown-linux-gnu.2.26 --release
# add copy commands as you add functions
cp target/x86_64-unknown-linux-gnu/release/hello netlify/functions/

Installing @ziglang/cli will install the zig binary for use by cargo-zigbuild

cargo-zigbuild is installed into the .cargo directory and gets cached by Netlify so there isn’t a repeat of the install after the first time. I thought the target directory was cached also, but not sure why that’s not true (I see a message, but a different question for the Netlify devs).

NOTE: I first used cargo-lambda instead, but it’s not really needed and wraps cargo-zigbuild with the same required dependency on zig, so I decided to use a smaller crate install until there is a need to use cargo-lambda.

1 Like