diff --git a/.github/workflows/abi_wasm.yml b/.github/workflows/abi_wasm.yml index 8f219a77977..fbbfeba2331 100644 --- a/.github/workflows/abi_wasm.yml +++ b/.github/workflows/abi_wasm.yml @@ -47,8 +47,8 @@ jobs: - name: Build noirc_abi_wasm run: | nix build -L .#noirc_abi_wasm - cp -r ./result/nodejs ./tooling/noirc_abi_wasm - cp -r ./result/web ./tooling/noirc_abi_wasm + cp -r ./result/noirc_abi_wasm/nodejs ./tooling/noirc_abi_wasm + cp -r ./result/noirc_abi_wasm/web ./tooling/noirc_abi_wasm - name: Export cache from nix store if: ${{ steps.cache.outputs.cache-hit != 'true' && github.event_name != 'merge_group' }} @@ -63,7 +63,7 @@ jobs: key: ${{ steps.cache.outputs.cache-primary-key }} - name: Dereference symlink - run: echo "UPLOAD_PATH=$(readlink -f result)" >> $GITHUB_ENV + run: echo "UPLOAD_PATH=$(readlink -f ./result/noirc_abi_wasm)" >> $GITHUB_ENV - name: Upload artifact uses: actions/upload-artifact@v3 @@ -75,7 +75,7 @@ jobs: test: runs-on: ubuntu-latest needs: [build] - + steps: - name: Checkout sources uses: actions/checkout@v3 @@ -85,7 +85,7 @@ jobs: with: name: noirc_abi_wasm path: ./tooling/noirc_abi_wasm - + - name: Install Yarn dependencies uses: ./.github/actions/setup @@ -94,7 +94,7 @@ jobs: - name: Install Playwright uses: ./.github/actions/install-playwright - + - name: Run browser tests run: yarn workspace @noir-lang/noirc_abi test:browser diff --git a/.github/workflows/publish-abi_wasm.yml b/.github/workflows/publish-abi_wasm.yml index 3ca7b77f747..1769b4d771c 100644 --- a/.github/workflows/publish-abi_wasm.yml +++ b/.github/workflows/publish-abi_wasm.yml @@ -36,7 +36,7 @@ jobs: nix build -L .#noirc_abi_wasm - name: Discover Build Output Path - run: echo "BUILD_OUTPUT_PATH=$(readlink -f ./result)" >> $GITHUB_ENV + run: echo "BUILD_OUTPUT_PATH=$(readlink -f ./result/noirc_abi_wasm)" >> $GITHUB_ENV - name: Copy Build Output to Temporary Directory run: | diff --git a/.github/workflows/test-integration.yml b/.github/workflows/test-integration.yml index 5f43493fbce..e72e7d81498 100644 --- a/.github/workflows/test-integration.yml +++ b/.github/workflows/test-integration.yml @@ -54,10 +54,10 @@ jobs: - name: Build noir_wasm package run: | - nix build -L .#wasm - echo "UPLOAD_PATH=$(readlink -f result)" >> $GITHUB_ENV - cp -r ./result/nodejs ./compiler/wasm - cp -r ./result/web ./compiler/wasm + nix build -L .#noir_wasm + echo "UPLOAD_PATH=$(readlink -f ./result/noir_wasm)" >> $GITHUB_ENV + cp -r ./result/noir_wasm/nodejs ./compiler/wasm + cp -r ./result/noir_wasm/web ./compiler/wasm - name: Upload `noir_wasm` artifact uses: actions/upload-artifact@v3 @@ -69,9 +69,9 @@ jobs: - name: Build noirc_abi_wasm package run: | nix build -L .#noirc_abi_wasm - echo "UPLOAD_PATH=$(readlink -f result)" >> $GITHUB_ENV - cp -r ./result/nodejs ./tooling/noirc_abi_wasm - cp -r ./result/web ./tooling/noirc_abi_wasm + echo "UPLOAD_PATH=$(readlink -f ./result/noirc_abi_wasm)" >> $GITHUB_ENV + cp -r ./result/noirc_abi_wasm/nodejs ./tooling/noirc_abi_wasm + cp -r ./result/noirc_abi_wasm/web ./tooling/noirc_abi_wasm - name: Upload `noirc_abi_wasm` artifact uses: actions/upload-artifact@v3 @@ -90,8 +90,8 @@ jobs: - name: Run `integration-tests` run: | yarn test:integration - - - name: Alert on nightly test failure + + - name: Alert on nightly test failure uses: JasonEtco/create-an-issue@v2 if: ${{ failure() && github.event_name == 'schedule' }} env: diff --git a/.github/workflows/wasm.yml b/.github/workflows/wasm.yml index f7b36738482..71972d5e5c5 100644 --- a/.github/workflows/wasm.yml +++ b/.github/workflows/wasm.yml @@ -17,7 +17,7 @@ jobs: strategy: matrix: target: [x86_64-unknown-linux-gnu] - + steps: - name: Checkout Noir repo uses: actions/checkout@v4 @@ -81,7 +81,7 @@ jobs: - name: Build wasm package run: | - nix build -L .#wasm + nix build -L .#noir_wasm - name: Export cache from nix store if: ${{ steps.cache.outputs.cache-hit != 'true' && github.event_name != 'merge_group' }} @@ -96,7 +96,7 @@ jobs: key: ${{ steps.cache.outputs.cache-primary-key }} - name: Dereference symlink - run: echo "UPLOAD_PATH=$(readlink -f result)" >> $GITHUB_ENV + run: echo "UPLOAD_PATH=$(readlink -f ./result/noir_wasm)" >> $GITHUB_ENV - name: Upload artifact uses: actions/upload-artifact@v3 diff --git a/compiler/wasm/README.md b/compiler/wasm/README.md index c5e8d54a836..0b2d92b0815 100644 --- a/compiler/wasm/README.md +++ b/compiler/wasm/README.md @@ -9,11 +9,11 @@ The package also handles dependency management like how Nargo (Noir's CLI tool) Outside of the [noir repo](https://github.com/noir-lang/noir), this package can be built using the command below: ```bash -nix build -L github:noir-lang/noir/master#wasm +nix build -L github:noir-lang/noir/master#noir_wasm ``` If you are within the noir repo and would like to build local changes, you can use: ```bash -nix build -L #wasm +nix build -L #noir_wasm ``` diff --git a/compiler/wasm/installPhase.sh b/compiler/wasm/installPhase.sh index a71b3f8cd0e..e5be98a3339 100755 --- a/compiler/wasm/installPhase.sh +++ b/compiler/wasm/installPhase.sh @@ -1,8 +1,10 @@ #!/usr/bin/env bash export self_path=$(dirname "$(readlink -f "$0")") -mkdir -p $out -cp $self_path/README.md $out/ -cp $self_path/package.json $out/ -cp -r $self_path/nodejs $out/ -cp -r $self_path/web $out/ +export out_path=$out/noir_wasm + +mkdir -p $out_path +cp $self_path/README.md $out_path/ +cp $self_path/package.json $out_path/ +cp -r $self_path/nodejs $out_path/ +cp -r $self_path/web $out_path/ diff --git a/flake.lock b/flake.lock index 659daec5d3a..e779473f0ec 100644 --- a/flake.lock +++ b/flake.lock @@ -1,28 +1,5 @@ { "nodes": { - "barretenberg": { - "inputs": { - "flake-utils": [ - "flake-utils" - ], - "nixpkgs": [ - "nixpkgs" - ] - }, - "locked": { - "lastModified": 1688820427, - "narHash": "sha256-w7yMeYp50KrlTn23TTKfYmLOQL4uIgw0wSX67v2tvvc=", - "owner": "AztecProtocol", - "repo": "barretenberg", - "rev": "fdd46f77531a6fcc9d9b24a698c56590d54d487e", - "type": "github" - }, - "original": { - "owner": "AztecProtocol", - "repo": "barretenberg", - "type": "github" - } - }, "crane": { "inputs": { "flake-compat": [ @@ -34,9 +11,7 @@ "nixpkgs": [ "nixpkgs" ], - "rust-overlay": [ - "rust-overlay" - ] + "rust-overlay": "rust-overlay" }, "locked": { "lastModified": 1681177078, @@ -52,6 +27,27 @@ "type": "github" } }, + "fenix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ], + "rust-analyzer-src": "rust-analyzer-src" + }, + "locked": { + "lastModified": 1694499657, + "narHash": "sha256-u/fZtLtN7VcDrMMVrdsFy93PEkaiK+tNpJT9on4SGdU=", + "owner": "nix-community", + "repo": "fenix", + "rev": "2895ff377cbb3cb6f5dd92066734b0447cb04e20", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "fenix", + "type": "github" + } + }, "flake-compat": { "flake": false, "locked": { @@ -104,29 +100,47 @@ }, "root": { "inputs": { - "barretenberg": "barretenberg", "crane": "crane", + "fenix": "fenix", "flake-compat": "flake-compat", "flake-utils": "flake-utils", - "nixpkgs": "nixpkgs", - "rust-overlay": "rust-overlay" + "nixpkgs": "nixpkgs" + } + }, + "rust-analyzer-src": { + "flake": false, + "locked": { + "lastModified": 1694421477, + "narHash": "sha256-df6YZzR57VFzkOPwIohJfC0fRwgq6yUPbMJkKAtQyAE=", + "owner": "rust-lang", + "repo": "rust-analyzer", + "rev": "cc6c8209cbaf7df55013977cf5cc8488d6b7ff1c", + "type": "github" + }, + "original": { + "owner": "rust-lang", + "ref": "nightly", + "repo": "rust-analyzer", + "type": "github" } }, "rust-overlay": { "inputs": { "flake-utils": [ + "crane", "flake-utils" ], "nixpkgs": [ + "crane", "nixpkgs" ] }, "locked": { - "lastModified": 1681352318, - "narHash": "sha256-+kwy7bTsuW8GYrRqWRQ8T5hg6duZb5IJiHlKo1J+v9g=", + "lastModified": 1694484610, + "narHash": "sha256-aeSDkp7fkAqtVjW3QUn7vq7BKNlFul/BiGgdv7rK+mA=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "aeaa11c65a5c5cebaa51652353ab3c497b9a7bbf", + "rev": "c5b977a7e6a295697fa1f9c42174fd6313b38df4", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index f4c986f75da..d4d1f4c54fa 100644 --- a/flake.nix +++ b/flake.nix @@ -1,6 +1,8 @@ { description = "Build the Noir programming language"; + # All of these inputs (a.k.a. dependencies) need to align with inputs we + # use so they use the `inputs.*.follows` syntax to reference our inputs inputs = { nixpkgs = { url = "github:NixOS/nixpkgs/nixos-22.11"; @@ -15,131 +17,71 @@ flake = false; }; - rust-overlay = { - url = "github:oxalica/rust-overlay"; - # All of these inputs (a.k.a. dependencies) need to align with inputs we - # use so they use the `inputs.*.follows` syntax to reference our inputs + fenix = { + url = "github:nix-community/fenix"; inputs = { nixpkgs.follows = "nixpkgs"; - flake-utils.follows = "flake-utils"; }; }; crane = { url = "github:ipetkov/crane"; - # All of these inputs (a.k.a. dependencies) need to align with inputs we - # use so they use the `inputs.*.follows` syntax to reference our inputs inputs = { nixpkgs.follows = "nixpkgs"; flake-utils.follows = "flake-utils"; flake-compat.follows = "flake-compat"; - rust-overlay.follows = "rust-overlay"; - }; - }; - - barretenberg = { - url = "github:AztecProtocol/barretenberg"; - # All of these inputs (a.k.a. dependencies) need to align with inputs we - # use so they use the `inputs.*.follows` syntax to reference our inputs - inputs = { - nixpkgs.follows = "nixpkgs"; - flake-utils.follows = "flake-utils"; }; }; }; outputs = - { self, nixpkgs, crane, flake-utils, rust-overlay, barretenberg, ... }: + { self, nixpkgs, crane, flake-utils, fenix, ... }: flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs { inherit system; - overlays = [ - rust-overlay.overlays.default - barretenberg.overlays.default - ]; }; - rustToolchain = pkgs.rust-bin.stable."1.66.0".default.override { - # We include rust-src to ensure rust-analyzer works. - # See https://discourse.nixos.org/t/rust-src-not-found-and-other-misadventures-of-developing-rust-on-nixos/11570/4 - extensions = [ "rust-src" ]; - targets = [ "wasm32-unknown-unknown" ] - ++ pkgs.lib.optional (pkgs.hostPlatform.isx86_64 && pkgs.hostPlatform.isLinux) "x86_64-unknown-linux-gnu" - ++ pkgs.lib.optional (pkgs.hostPlatform.isAarch64 && pkgs.hostPlatform.isLinux) "aarch64-unknown-linux-gnu" - ++ pkgs.lib.optional (pkgs.hostPlatform.isx86_64 && pkgs.hostPlatform.isDarwin) "x86_64-apple-darwin" - ++ pkgs.lib.optional (pkgs.hostPlatform.isAarch64 && pkgs.hostPlatform.isDarwin) "aarch64-apple-darwin"; + rustToolchain = fenix.packages.${system}.fromToolchainFile { + file = ./rust-toolchain.toml; + sha256 = "sha256-Zk2rxv6vwKFkTTidgjPm6gDsseVmmljVt201H7zuDkk="; }; craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain; - sharedEnvironment = { - # We enable backtraces on any failure for help with debugging - RUST_BACKTRACE = "1"; - - BARRETENBERG_ARCHIVE = builtins.fetchurl { - url = "https://github.com/AztecProtocol/barretenberg/releases/download/barretenberg-v0.4.5/acvm_backend.wasm.tar.gz"; - sha256 = "sha256:0z24yhvxc0dr13xj7y4xs9p42lzxwpazrmsrdpcgynfajkk6vqy4"; - }; - }; - - nativeEnvironment = sharedEnvironment // { - # rust-bindgen needs to know the location of libclang - LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib"; - }; - - wasmEnvironment = sharedEnvironment // { - # We set the environment variable because barretenberg must be compiled in a special way for wasm - BARRETENBERG_BIN_DIR = "${pkgs.barretenberg-wasm}/bin"; - }; - - testEnvironment = sharedEnvironment // { }; - # The `self.rev` property is only available when the working tree is not dirty GIT_COMMIT = if (self ? rev) then self.rev else "unknown"; GIT_DIRTY = if (self ? rev) then "false" else "true"; - # We use `include_str!` macro to embed the solidity verifier template so we need to create a special - # source filter to include .sol files in addition to usual rust/cargo source files. - solidityFilter = path: _type: builtins.match ".*sol$" path != null; - # We use `.bytecode` and `.tr` files to test interactions with `bb` so we add a source filter to include these. - bytecodeFilter = path: _type: builtins.match ".*bytecode$" path != null; - witnessFilter = path: _type: builtins.match ".*tr$" path != null; - # We use `.nr` and `.toml` files in tests so we need to create a special source - # filter to include those files in addition to usual rust/cargo source files - noirFilter = path: _type: builtins.match ".*nr$" path != null; - tomlFilter = path: _type: builtins.match ".*toml$" path != null; - sourceFilter = path: type: - (solidityFilter path type) || (bytecodeFilter path type)|| (witnessFilter path type) || (noirFilter path type) || (tomlFilter path type) || (craneLib.filterCargoSources path type); - - # As per https://discourse.nixos.org/t/gcc11stdenv-and-clang/17734/7 since it seems that aarch64-linux uses - # gcc9 instead of gcc11 for the C++ stdlib, while all other targets we support provide the correct libstdc++ - stdenv = - if (pkgs.stdenv.targetPlatform.isGnu && pkgs.stdenv.targetPlatform.isAarch64) then - pkgs.overrideCC pkgs.llvmPackages.stdenv (pkgs.llvmPackages.clang.override { gccForLibs = pkgs.gcc11.cc; }) - else - pkgs.llvmPackages.stdenv; - - extraBuildInputs = pkgs.lib.optionals pkgs.stdenv.isDarwin [ + extraBuildInputs = [ ] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [ # Need libiconv and apple Security on Darwin. See https://github.com/ipetkov/crane/issues/156 pkgs.libiconv pkgs.darwin.apple_sdk.frameworks.Security - ] ++ [ - # Need to install various packages used by the `bb` binary. - pkgs.curl - stdenv.cc.cc.lib - pkgs.gcc.cc.lib - pkgs.gzip ]; - sharedArgs = { + environment = { + # We enable backtraces on any failure for help with debugging + RUST_BACKTRACE = "1"; + + # We download the Wasm version of `acvm_backend` in the barretenberg releases for the ACVM `blackbox_solver` + BARRETENBERG_ARCHIVE = pkgs.fetchurl { + url = "https://github.com/AztecProtocol/barretenberg/releases/download/barretenberg-v0.4.5/acvm_backend.wasm.tar.gz"; + sha256 = "sha256-xONt5pTKWf/YbVnX/NXl/VNBbtKd+CP7CLkB1jf0RHw="; + }; + }; + + # Configuration shared between builds + config = { # x-release-please-start-version version = "0.12.0"; # x-release-please-end src = pkgs.lib.cleanSourceWith { src = craneLib.path ./.; - filter = sourceFilter; + # Custom filter with various file extensions that we rely upon to build packages + # Currently: `.nr`, `.sol`, `.sh`, `.json`, `.md` + filter = path: type: + (builtins.match ".*\.(nr|sol|sh|json|md)$" path != null) || (craneLib.filterCargoSources path type); }; # TODO(#1198): It'd be nice to include these flags when running `cargo clippy` in a devShell. @@ -149,131 +91,91 @@ cargoTestExtraArgs = "--workspace"; }; - # Combine the environment and other configuration needed for crane to build our Rust packages - nativeArgs = nativeEnvironment // sharedArgs // { - pname = "noir-native"; - - # Use our custom stdenv to build and test our Rust project - inherit stdenv; - - nativeBuildInputs = [ - # This provides the pkg-config tool to find barretenberg & other native libraries - pkgs.pkg-config - # This provides the `lld` linker to cargo - pkgs.llvmPackages.bintools - ] ++ pkgs.lib.optionals stdenv.isLinux [ - # This is linux specific and used to patch the rpath and interpreter of the bb binary - pkgs.patchelf - ]; + # Combine the environment and other configuration needed for Crane to build our Rust packages + nativeConfig = environment // config // { + nativeBuildInputs = [ ]; - buildInputs = [ - ] ++ extraBuildInputs; + buildInputs = [ ] ++ extraBuildInputs; }; - # Combine the environmnet with cargo args needed to build wasm package - noirWasmArgs = sharedEnvironment // sharedArgs // { - pname = "noir_wasm"; - - src = ./.; + # Combine the environmnet and other configuration needed for Crane to build our Wasm packages + wasmConfig = environment // config // { + CARGO_TARGET_DIR = "./target"; - cargoExtraArgs = "--package noir_wasm --target wasm32-unknown-unknown"; + nativeBuildInputs = with pkgs; [ + which + git + jq + rustToolchain + wasm-bindgen-cli + binaryen + toml2json + ]; buildInputs = [ ] ++ extraBuildInputs; - - doCheck = false; }; - # Combine the environment with cargo args needed to build wasm package - noirc_abi_WasmArgs = sharedEnvironment // sharedArgs // { + # Build *just* the cargo dependencies, so we can reuse all of that work between runs + native-cargo-artifacts = craneLib.buildDepsOnly (nativeConfig // { + pname = "nargo"; + }); + noir-wasm-cargo-artifacts = craneLib.buildDepsOnly (wasmConfig // { + pname = "noir_wasm"; + }); + noirc-abi-wasm-cargo-artifacts = craneLib.buildDepsOnly (wasmConfig // { pname = "noirc_abi_wasm"; + }); - src = ./.; + nargo = craneLib.buildPackage (nativeConfig // { + pname = "nargo"; - cargoExtraArgs = "--package noirc_abi_wasm --target wasm32-unknown-unknown"; + inherit GIT_COMMIT GIT_DIRTY; - buildInputs = [ ] ++ extraBuildInputs; + cargoArtifacts = native-cargo-artifacts; + # We don't want to run tests because they don't work in the Nix sandbox doCheck = false; - }; - - # Conditionally download the binary based on whether it is linux or mac - bb_binary = let - platformSpecificUrl = if stdenv.hostPlatform.isLinux then - "https://github.com/AztecProtocol/barretenberg/releases/download/barretenberg-v0.4.3/bb-ubuntu.tar.gz" - else if stdenv.hostPlatform.isDarwin then - "https://github.com/AztecProtocol/barretenberg/releases/download/barretenberg-v0.4.3/barretenberg-x86_64-apple-darwin.tar.gz" - else - throw "Unsupported platform"; - - platformSpecificHash = if stdenv.hostPlatform.isLinux then - "sha256:0rcsjws87f4v28cw9734c10pg7c49apigf4lg3m0ji5vbhhmfnhr" - else if stdenv.hostPlatform.isDarwin then - "sha256:0pnsd56z0vkai7m0advawfgcvq9jbnpqm7lk98n5flqj583x3w35" - else - throw "Unsupported platform"; - in builtins.fetchurl { - url = platformSpecificUrl; - sha256 = platformSpecificHash; - }; + }); + + noir_wasm = craneLib.buildPackage (wasmConfig // rec { + pname = "noir_wasm"; + + inherit GIT_COMMIT GIT_DIRTY; + + cargoArtifacts = noir-wasm-cargo-artifacts; - # The `port` is parameterized to support parallel test runs without colliding static servers - testArgs = port: testEnvironment // { - BB_BINARY_PATH = "/tmp/backend_binary"; - - BB_BINARY_URL = "http://0.0.0.0:${toString port}/${builtins.baseNameOf bb_binary}"; - - # We provide `barretenberg-transcript00` from the overlay to the tests as a URL hosted via a static server - # This is necessary because the Nix sandbox has no network access and downloading during tests would fail - BARRETENBERG_TRANSCRIPT_URL = "http://0.0.0.0:${toString port}/${builtins.baseNameOf pkgs.barretenberg-transcript00}"; - - # This copies the `barretenberg-transcript00` from the Nix store into this sandbox - # which avoids exposing the entire Nix store to the static server it starts - # The static server is moved to the background and killed after checks are completed - # - # We also set the NARGO_BACKEND_CACHE_DIR environment variable to the $TMP directory so we can successfully cache - # the transcript; which isn't possible with the default path because the Nix sandbox disabled $HOME - preCheck = '' - echo "Extracting bb binary" - mkdir extracted - tar -xf ${bb_binary} -C extracted - - # Conditionally patch the binary for Linux - ${if stdenv.hostPlatform.isLinux then '' - - cp extracted/cpp/build/bin/bb /tmp/backend_binary - - echo "Patching bb binary for Linux" - patchelf --set-rpath "${stdenv.cc.cc.lib}/lib:${pkgs.gcc.cc.lib}/lib" /tmp/backend_binary - patchelf --set-interpreter ${stdenv.cc.libc}/lib/ld-linux-x86-64.so.2 /tmp/backend_binary - '' else if stdenv.hostPlatform.isDarwin then '' - cp extracted/bb /tmp/backend_binary - '' else - throw "Unsupported platform" - } - - export NARGO_BACKEND_CACHE_DIR=$TMP - cp ${pkgs.barretenberg-transcript00} . - echo "Starting simple static server" - ${pkgs.simple-http-server}/bin/simple-http-server --port ${toString port} --silent & - HTTP_SERVER_PID=$! + cargoExtraArgs = "--package ${pname} --target wasm32-unknown-unknown"; + + buildPhaseCargoCommand = '' + bash compiler/wasm/buildPhaseCargoCommand.sh release ''; - postCheck = '' - kill $HTTP_SERVER_PID + installPhase = '' + bash compiler/wasm/installPhase.sh ''; - }; - # Build *just* the cargo dependencies, so we can reuse all of that work between runs - native-cargo-artifacts = craneLib.buildDepsOnly nativeArgs; - noir-wasm-cargo-artifacts = craneLib.buildDepsOnly noirWasmArgs; - noirc-abi-wasm-cargo-artifacts = craneLib.buildDepsOnly noirc_abi_WasmArgs; + # We don't want to run tests because they don't work in the Nix sandbox + doCheck = false; + }); + + noirc_abi_wasm = craneLib.buildPackage (wasmConfig // rec { + pname = "noirc_abi_wasm"; - noir-native = craneLib.buildPackage (nativeArgs // { inherit GIT_COMMIT GIT_DIRTY; - cargoArtifacts = native-cargo-artifacts; + cargoArtifacts = noirc-abi-wasm-cargo-artifacts; - # We don't want to run checks or tests when just building the project + cargoExtraArgs = "--package ${pname} --target wasm32-unknown-unknown"; + + buildPhaseCargoCommand = '' + bash tooling/noirc_abi_wasm/buildPhaseCargoCommand.sh release + ''; + + installPhase = '' + bash tooling/noirc_abi_wasm/installPhase.sh + ''; + + # We don't want to run tests because they don't work in the Nix sandbox doCheck = false; }); @@ -284,22 +186,20 @@ }; }; in - rec { + { + # We use `checks` to run `cargo clippy` and `cargo fmt` since we disable checks in the primary derivations checks = { - cargo-clippy = craneLib.cargoClippy (nativeArgs // { - inherit GIT_COMMIT GIT_DIRTY; - - cargoArtifacts = native-cargo-artifacts; - }); + cargo-clippy = craneLib.cargoClippy (nativeConfig // { + pname = "noir"; - cargo-fmt = craneLib.cargoFmt (nativeArgs // { inherit GIT_COMMIT GIT_DIRTY; cargoArtifacts = native-cargo-artifacts; - doCheck = true; }); - cargo-test = craneLib.cargoTest (nativeArgs // (testArgs 8000) // { + cargo-fmt = craneLib.cargoFmt (nativeConfig // { + pname = "noir"; + inherit GIT_COMMIT GIT_DIRTY; cargoArtifacts = native-cargo-artifacts; @@ -307,9 +207,16 @@ }; packages = { - default = noir-native; + default = nargo; - inherit noir-native; + # Nix flakes cannot build more than one derivation in one command (see https://github.com/NixOS/nix/issues/5591) + # so we use `symlinkJoin` to build everything as the "all" package. + all = pkgs.symlinkJoin { name = "all"; paths = [ nargo noir_wasm noirc_abi_wasm ]; }; + + # We also export individual packages to enable `nix build .#nargo -L`, etc. + inherit nargo; + inherit noir_wasm; + inherit noirc_abi_wasm; # We expose the `*-cargo-artifacts` derivations so we can cache our cargo dependencies in CI inherit native-cargo-artifacts; @@ -317,111 +224,37 @@ inherit noirc-abi-wasm-cargo-artifacts; }; - # TODO(#1197): Look into installable apps with Nix flakes - # apps.default = flake-utils.lib.mkApp { drv = nargo; }; - - # 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, + # Setup the environment to match the environment settings, the inputs from our checks derivations, # and extra tooling via `nativeBuildInputs` - devShells.default = pkgs.mkShell.override { inherit stdenv; } (nativeEnvironment // wasmEnvironment // testEnvironment // { - inputsFrom = builtins.attrValues checks; + devShells.default = pkgs.mkShell (environment // { + inputsFrom = [ + nargo + noir_wasm + noirc_abi_wasm + ]; + # Additional tools that weren't included as `nativeBuildInputs` of any of the derivations in `inputsFrom` nativeBuildInputs = with pkgs; [ + # Rust toolchain + rustToolchain + # Other tools + starship + yarn + nodejs-18_x + # Used by the `bb` binary curl gzip - which - starship - git + # This ensures the right lldb is in the environment for running rust-lldb + llvmPackages.lldb + # Nix tools nil nixpkgs-fmt - toml2json - llvmPackages.lldb # This ensures the right lldb is in the environment for running rust-lldb - wasm-bindgen-cli - jq - binaryen - yarn - # rust-bin.stable."1.66.1".default - rustToolchain - rust-analyzer - rustup - nodejs-18_x ]; shellHook = '' eval "$(starship init bash)" ''; }); - - # TODO: This fails with a "section too large" error on MacOS so we should limit to linux targets - # or fix the failure - packages.wasm = craneLib.buildPackage (noirWasmArgs // { - - inherit GIT_COMMIT; - inherit GIT_DIRTY; - doCheck = false; - - cargoArtifacts = noir-wasm-cargo-artifacts; - - COMMIT_SHORT = builtins.substring 0 7 GIT_COMMIT; - VERSION_APPENDIX = if GIT_DIRTY == "true" then "-dirty" else ""; - PKG_PATH = "./pkg"; - CARGO_TARGET_DIR = "./target"; - - nativeBuildInputs = with pkgs; [ - which - git - jq - rustToolchain - wasm-bindgen-cli - binaryen - toml2json - ]; - - buildPhaseCargoCommand = '' - bash compiler/wasm/buildPhaseCargoCommand.sh release - ''; - - installPhase = '' - bash compiler/wasm/installPhase.sh - ''; - - }); - - # TODO: This fails with a "section too large" error on MacOS so we should limit to linux targets - # or fix the failure - packages.noirc_abi_wasm = craneLib.buildPackage (noirc_abi_WasmArgs // { - - inherit GIT_COMMIT; - inherit GIT_DIRTY; - doCheck = false; - - cargoArtifacts = noirc-abi-wasm-cargo-artifacts; - - COMMIT_SHORT = builtins.substring 0 7 GIT_COMMIT; - VERSION_APPENDIX = if GIT_DIRTY == "true" then "-dirty" else ""; - PKG_PATH = "./pkg"; - CARGO_TARGET_DIR = "./target"; - - nativeBuildInputs = with pkgs; [ - which - git - jq - rustToolchain - wasm-bindgen-cli - binaryen - toml2json - ]; - - buildPhaseCargoCommand = '' - bash tooling/noirc_abi_wasm/buildPhaseCargoCommand.sh release - ''; - - installPhase = '' - bash tooling/noirc_abi_wasm/installPhase.sh - ''; - - }); - }); } diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 00000000000..870d0089548 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,5 @@ +[toolchain] +channel = "1.66.1" +components = [ "rust-src" ] +targets = [ "wasm32-unknown-unknown", "aarch64-apple-darwin" ] +profile = "default" diff --git a/tooling/noirc_abi_wasm/installPhase.sh b/tooling/noirc_abi_wasm/installPhase.sh index a71b3f8cd0e..d9b94f2d171 100755 --- a/tooling/noirc_abi_wasm/installPhase.sh +++ b/tooling/noirc_abi_wasm/installPhase.sh @@ -1,8 +1,10 @@ #!/usr/bin/env bash export self_path=$(dirname "$(readlink -f "$0")") -mkdir -p $out -cp $self_path/README.md $out/ -cp $self_path/package.json $out/ -cp -r $self_path/nodejs $out/ -cp -r $self_path/web $out/ +export out_path=$out/noirc_abi_wasm + +mkdir -p $out_path +cp $self_path/README.md $out_path/ +cp $self_path/package.json $out_path/ +cp -r $self_path/nodejs $out_path/ +cp -r $self_path/web $out_path/ diff --git a/wasm-bindgen-cli.nix b/wasm-bindgen-cli.nix index 38b7b0a79c1..7c3910f032e 100644 --- a/wasm-bindgen-cli.nix +++ b/wasm-bindgen-cli.nix @@ -7,6 +7,7 @@ , stdenv , curl , darwin +, libiconv , runCommand }: @@ -23,7 +24,12 @@ rustPlatform.buildRustPackage rec { nativeBuildInputs = [ pkg-config ]; - buildInputs = [ openssl ] ++ lib.optionals stdenv.isDarwin [ curl darwin.apple_sdk.frameworks.Security ]; + buildInputs = [ openssl ] ++ lib.optionals stdenv.isDarwin [ + curl + # Need libiconv and apple Security on Darwin. See https://github.com/ipetkov/crane/issues/156 + libiconv + darwin.apple_sdk.frameworks.Security + ]; doCheck = false; @@ -34,4 +40,4 @@ rustPlatform.buildRustPackage rec { maintainers = with maintainers; [ nitsky rizary ]; mainProgram = "wasm-bindgen"; }; -} \ No newline at end of file +}