-
Notifications
You must be signed in to change notification settings - Fork 24
feat: Add nix flake for ffi #1319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
c34c938
feat: Add nix flake for ffi
maru-ava cd6171c
fixup: Add crane for dep artifact caching and update static lib action
maru-ava 0a7a0b0
fixup: Document flake usage
maru-ava 93479f9
fixup: Revert ci changes
maru-ava ed33a9e
fixup: Add ffi jobs for nix
maru-ava 023879c
fixup: Use dontStrip in the nix flake to match regular cargo build
maru-ava 3099e48
fixup: Limit ffi-nix job to ubuntu to minimize runtime
maru-ava b96b6c7
fixup: Add script to test for build equivalency
maru-ava 2ec10b2
fixup: Update to copy only the maxperf lib
maru-ava 6d54892
fixup: Remove deprecated darwin stubs
maru-ava ca17113
fixup: Fix macos target to apply to both buildDepsOnly and buildPackage
maru-ava d2e7488
fixup: Add build equivalency check to ffi-nix job
maru-ava 5bf2243
fixup: Update script to exit non zero if builds differ
maru-ava 66fe251
fixup: Clarify comment justifying test mode
maru-ava 18ae4dc
fixup: Add build-static-ffi cargo alias to minimize the change of div…
maru-ava eed24bd
fixup: Ensure flake output equivalent to firewood-go-ethhash
maru-ava 81f4794
fixup: Avoid unnecessary rebuilding in build equivalency test
maru-ava 4e66eb4
fixup: Update to use avalanchego's golang flake for 1.24.8
maru-ava 3b2c6ef
fixup: Force sequential jemalloc build for x86_64 reproducibility
maru-ava cda7583
fixup: Configure dependabot for security updates for github actions
maru-ava 24b54e7
fixup: Minimize duplication in build equivalency script
maru-ava File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| [alias] | ||
| build-static-ffi = "build --frozen --profile maxperf --package firewood-ffi --features ethhash,logger" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| dbtest | ||
| _obj | ||
|
|
||
| # Nix output | ||
| result |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| { | ||
| # To test with arbitrary firewood versions (alternative to firewood-go-ethhash): | ||
| # - Install nix: https://github.com/DeterminateSystems/nix-installer?tab=readme-ov-file#install-nix | ||
| # - Clone firewood locally at desired version/commit | ||
| # - Build: `cd ffi && nix build` | ||
| # - In your Go project: `go mod edit -replace github.com/ava-labs/firewood-go-ethhash/ffi=/path/to/firewood/ffi/result/ffi` | ||
|
|
||
| description = "Firewood FFI library and development environment"; | ||
|
|
||
| inputs = { | ||
| nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.2505.*.tar.gz"; | ||
| rust-overlay.url = "github:oxalica/rust-overlay"; | ||
| crane.url = "github:ipetkov/crane"; | ||
| flake-utils.url = "github:numtide/flake-utils"; | ||
| golang.url = "github:ava-labs/avalanchego?dir=nix/go&ref=f10757d594eedf0f016bc1400739788c542f005f"; | ||
| }; | ||
|
|
||
| outputs = { self, nixpkgs, rust-overlay, crane, flake-utils, golang }: | ||
| flake-utils.lib.eachDefaultSystem (system: | ||
| let | ||
| overlays = [ (import rust-overlay) ]; | ||
| pkgs = import nixpkgs { inherit system overlays; }; | ||
| inherit (pkgs) lib; | ||
|
|
||
| go = golang.packages.${system}.default; | ||
|
|
||
| rustToolchain = pkgs.rust-bin.stable.latest.default.override { | ||
| extensions = [ "rust-src" "rustfmt" "clippy" ]; | ||
| }; | ||
|
|
||
| craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain; | ||
|
|
||
| # Extract crate info from Cargo.toml files | ||
| ffiCargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml); | ||
| workspaceCargoToml = builtins.fromTOML (builtins.readFile ../Cargo.toml); | ||
|
|
||
| src = lib.cleanSourceWith { | ||
| src = craneLib.path ./..; | ||
| filter = path: type: | ||
| (lib.hasSuffix "\.md" path) || | ||
| (lib.hasSuffix "\.go" path) || | ||
| (lib.hasSuffix "go.mod" path) || | ||
| (lib.hasSuffix "go.sum" path) || | ||
| (lib.hasSuffix "firewood.h" path) || | ||
| (craneLib.filterCargoSources path type); | ||
| }; | ||
|
|
||
| commonArgs = { | ||
| inherit src; | ||
| strictDeps = true; | ||
| dontStrip = true; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nix strips debugging details by default, which was resulting in the .a file being ~6MB instead of ~50MB. |
||
|
|
||
| # Build only the firewood-ffi crate | ||
| pname = ffiCargoToml.package.name; | ||
| version = workspaceCargoToml.workspace.package.version; | ||
|
|
||
| nativeBuildInputs = with pkgs; [ | ||
| pkg-config | ||
| ]; | ||
|
|
||
| # Force sequential build of vendored jemalloc to avoid race conditions | ||
| # that cause non-deterministic symbol generation on x86_64 | ||
| # MAKEFLAGS only affects make invocations (jemalloc), not cargo parallelism | ||
| # See: https://github.com/NixOS/nixpkgs/issues/380852 | ||
| MAKEFLAGS = "-j1"; | ||
| } // lib.optionalAttrs pkgs.stdenv.isDarwin { | ||
| # Set macOS deployment target for Darwin builds | ||
| MACOSX_DEPLOYMENT_TARGET = "13.0"; | ||
| }; | ||
|
|
||
| cargoArtifacts = craneLib.buildDepsOnly (commonArgs // { | ||
| # Use cargo alias defined in .cargo/config.toml | ||
| cargoBuildCommand = "cargo build-static-ffi"; | ||
| }); | ||
|
|
||
| firewood-ffi = craneLib.buildPackage (commonArgs // { | ||
| inherit cargoArtifacts; | ||
| # Use cargo alias defined in .cargo/config.toml | ||
| cargoBuildCommand = "cargo build-static-ffi"; | ||
|
|
||
| # Disable tests - we only need to build the static library | ||
| doCheck = false; | ||
|
|
||
| # Install the static library and header | ||
| postInstall = '' | ||
| # Create a package structure compatible with FIREWOOD_LD_MODE=STATIC_LIBS | ||
| mkdir -p $out/ffi | ||
| cp -R ./ffi/* $out/ffi/ | ||
| mkdir -p $out/ffi/libs/${pkgs.stdenv.hostPlatform.config} | ||
| cp target/maxperf/libfirewood_ffi.a $out/ffi/libs/${pkgs.stdenv.hostPlatform.config}/ | ||
|
|
||
| # Run go generate to switch CGO directives to STATIC_LIBS mode | ||
| cd $out/ffi | ||
| HOME=$TMPDIR GOTOOLCHAIN=local FIREWOOD_LD_MODE=STATIC_LIBS ${go}/bin/go generate | ||
| ''; | ||
|
|
||
| meta = with lib; { | ||
| description = "C FFI bindings for Firewood, an embedded key-value store"; | ||
| homepage = "https://github.com/ava-labs/firewood"; | ||
| license = { | ||
| fullName = "Ava Labs Ecosystem License 1.1"; | ||
| url = "https://github.com/ava-labs/firewood/blob/main/LICENSE.md"; | ||
| }; | ||
| platforms = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]; | ||
| }; | ||
| }); | ||
| in | ||
| { | ||
| packages = { | ||
| inherit firewood-ffi; | ||
| default = firewood-ffi; | ||
| }; | ||
|
|
||
| apps.go = { | ||
| type = "app"; | ||
| program = "${go}/bin/go"; | ||
| }; | ||
|
|
||
| devShells.default = craneLib.devShell { | ||
| inputsFrom = [ firewood-ffi ]; | ||
|
|
||
| packages = with pkgs; [ | ||
| firewood-ffi | ||
| rustToolchain | ||
| go | ||
| ]; | ||
|
|
||
| shellHook = '' | ||
| # Ensure golang bin is in the path | ||
| GOBIN="$(go env GOPATH)/bin" | ||
| if [[ ":$PATH:" != *":$GOBIN:"* ]]; then | ||
| export PATH="$GOBIN:$PATH" | ||
| fi | ||
|
|
||
| # Force sequential build of vendored jemalloc for reproducibility | ||
| export MAKEFLAGS="-j1" | ||
| ''; | ||
| }; | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Questions: How and when are these hardcoded shas updated? I'm sure dependabot won't tell us when they update. Should we add something to the release checklist to see if they should be updated?
What happens if someone finds a security vulnerability in these packages?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dependabot can, in fact, tell you to update. I just checked, though, and it's not yet configured for this repo. This is what needs to be in
.github/dependabot.yml:As per ava-labs/avalanchego#3822, specifying SHAs for 3rd party actions (i.e. not provided by github) is best practice to avoid supply chain attack. A high-profile incident earlier this year galvanized awareness of the problem.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated dependabot config: dc0f60b