From 0645ba7e22afa121143434209d0f57f46dbc15c8 Mon Sep 17 00:00:00 2001 From: Koby Date: Wed, 2 Aug 2023 12:32:32 +0200 Subject: [PATCH] fix: utilize nix for clippy/test/fmt --- .github/workflows/rust.yml | 66 ++++++++++++++++++------------ flake.nix | 84 ++++++++++++++++++++++++-------------- 2 files changed, 93 insertions(+), 57 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 989f5f0e5..f194a8a7b 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -3,9 +3,23 @@ name: Rust on: [push, pull_request] jobs: - barretenberg-job: - runs-on: ubuntu-latest + clippy-n-test: + name: Test on ${{ matrix.os }} + runs-on: ${{ matrix.runner }} + timeout-minutes: 30 + + strategy: + fail-fast: false + matrix: + include: + - os: ubuntu + runner: ubuntu-latest + target: x86_64-linux + steps: + - name: Checkout + uses: actions/checkout@v3 + - uses: cachix/install-nix-action@v22 with: nix_path: nixpkgs=channel:nixos-22.11 @@ -15,28 +29,28 @@ jobs: with: name: barretenberg - - name: Checkout sources - uses: actions/checkout@v3 - - # barretenberg#wasm32 is needed by acvm_js crate so we need to build it first - # and ensure it's in $workspace_root/acvm_js/result/bin - # so acvm_js build.rs script can pick it up - - name: Run `nix build bb` - working-directory: ./acvm_js + - name: Restore nix store cache + id: nix-store-cache + uses: actions/cache@v3 + with: + path: /tmp/nix-cache + key: ${{ runner.os }}-flake-${{ hashFiles('*.lock') }} + + # Based on https://github.com/marigold-dev/deku/blob/b5016f0cf4bf6ac48db9111b70dd7fb49b969dfd/.github/workflows/build.yml#L26 + - name: Copy cache into nix store + if: steps.nix-store-cache.outputs.cache-hit == 'true' + # We don't check the signature because we're the one that created the cache + run: | + for narinfo in /tmp/nix-cache/*.narinfo; do + path=$(head -n 1 "$narinfo" | awk '{print $2}') + nix copy --no-check-sigs --from "file:///tmp/nix-cache" "$path" + done + + - name: Run `nix flake check` + run: | + nix flake check -L + + - name: Export cache from nix store + if: steps.nix-store-cache.outputs.cache-hit != 'true' run: | - nix build -L "github:AztecProtocol/barretenberg?rev=65e651d04c6092cb5ca079cd9e12ed9b5846fa3a#wasm32" - - check_n_test: - needs: barretenberg-job - name: cargo check & test - uses: noir-lang/.github/.github/workflows/rust-test.yml@main - - clippy: - needs: barretenberg-job - name: cargo clippy - uses: noir-lang/.github/.github/workflows/rust-clippy.yml@main - - format: - needs: barretenberg-job - name: cargo fmt - uses: noir-lang/.github/.github/workflows/rust-format.yml@main \ No newline at end of file + nix copy --to "file:///tmp/nix-cache?compression=zstd¶llel-compression=true" .#cargo-artifacts \ No newline at end of file diff --git a/flake.nix b/flake.nix index e1effe1df..9025f5e5b 100644 --- a/flake.nix +++ b/flake.nix @@ -76,13 +76,18 @@ cargoToml = ./acvm_js/Cargo.toml; }; + crateACVMDefinitions = craneLib.crateNameFromCargoToml { + cargoToml = ./acvm/Cargo.toml; + }; + + sharedEnvironment = { # Barretenberg fails if tests are run on multiple threads, so we set the test thread # count to 1 throughout the entire project # # Note: Setting this allows for consistent behavior across build and shells, but is mostly # hidden from the developer - i.e. when they see the command being run via `nix flake check` - RUST_TEST_THREADS = "1"; + # RUST_TEST_THREADS = "1"; BARRETENBERG_BIN_DIR = "${pkgs.barretenberg-wasm}/bin"; }; @@ -99,17 +104,16 @@ GIT_DIRTY = if (self ? rev) then "false" else "true"; commonArgs = { - + inherit (crateACVMDefinitions) pname version; src = pkgs.lib.cleanSourceWith { src = craneLib.path { path = ./.; - # name = "acvm_js"; }; filter = sourceFilter; }; - # cargoClippyExtraArgs = "--package acvm_js --all-targets -- -D warnings"; - # cargoTestExtraArgs = "--workspace"; + cargoClippyExtraArgs = "--package acvm_js --all-targets -- -D warnings"; + cargoTestExtraArgs = "--workspace"; # We don't want to run checks or tests when just building the project doCheck = false; @@ -131,7 +135,7 @@ }; # Build *just* the cargo dependencies, so we can reuse all of that work between runs - cargoArtifacts = craneLib.buildDepsOnly acvmjsWasmArgs; + cargo-artifacts = craneLib.buildDepsOnly commonArgs; wasm-bindgen-cli = pkgs.callPackage ./acvm_js/nix/wasm-bindgen-cli/default.nix { rustPlatform = pkgs.makeRustPlatform { @@ -143,45 +147,63 @@ in rec { checks = { - cargo-clippy = craneLib.cargoClippy (acvmjsWasmArgs // { - inherit cargoArtifacts - ; + + cargo-clippy = craneLib.cargoClippy (commonArgs // sharedEnvironment // { + inherit GIT_COMMIT GIT_DIRTY; + + cargoArtifacts = cargo-artifacts; + doCheck = true; + }); + + cargo-test = craneLib.cargoTest (commonArgs // sharedEnvironment // { + inherit GIT_COMMIT GIT_DIRTY; + + cargoArtifacts = cargo-artifacts; + doCheck = true; + }); + + cargo-fmt = craneLib.cargoFmt (commonArgs // sharedEnvironment // { inherit GIT_COMMIT GIT_DIRTY; + cargoArtifacts = cargo-artifacts; doCheck = true; }); + }; - packages.default = craneLib.mkCargoDerivation (acvmjsWasmArgs // rec { + packages = { + default = craneLib.mkCargoDerivation (acvmjsWasmArgs // rec { - inherit cargoArtifacts; + cargoArtifacts = cargo-artifacts; - inherit GIT_COMMIT; - inherit GIT_DIRTY; + inherit GIT_COMMIT; + inherit GIT_DIRTY; - COMMIT_SHORT = builtins.substring 0 7 GIT_COMMIT; - VERSION_APPENDIX = if GIT_DIRTY == "true" then "-dirty" else ""; + COMMIT_SHORT = builtins.substring 0 7 GIT_COMMIT; + VERSION_APPENDIX = if GIT_DIRTY == "true" then "-dirty" else ""; - src = ./.; #craneLib.cleanCargoSource (craneLib.path ./.); + src = ./.; #craneLib.cleanCargoSource (craneLib.path ./.); - nativeBuildInputs = with pkgs; [ - binaryen - which - git - jq - rustToolchain - wasm-bindgen-cli - ]; + nativeBuildInputs = with pkgs; [ + binaryen + which + git + jq + rustToolchain + wasm-bindgen-cli + ]; - buildPhaseCargoCommand = '' - bash ./acvm_js/buildPhaseCargoCommand.sh - ''; + buildPhaseCargoCommand = '' + bash ./acvm_js/buildPhaseCargoCommand.sh + ''; - installPhase = '' - bash ./acvm_js/installPhase.sh - ''; + installPhase = '' + bash ./acvm_js/installPhase.sh + ''; - }); + }); + inherit cargo-artifacts; + }; # Setup the environment to match the stdenv from `nix build` & `nix flake check`, and # combine it with the environment settings, the inputs from our checks derivations,