diff --git a/.envrc b/.envrc index 1d953f4bd7..b2f868b189 100644 --- a/.envrc +++ b/.envrc @@ -1 +1,20 @@ -use nix +# Based on https://github.com/direnv/direnv-vscode/blob/158e8302c2594cc0eaa5f8b4f0cafedd4e1c0315/.envrc + +# You can define your system-specific logic (like Git settings or GH tokens) in .envrc.local +# If that logic is usable by other people and might improve development environment, consider +# contributing it to this file! + +source_env_if_exists .envrc.local + +if [[ -z "${SKIP_NIX:-}" ]] && has nix; then + + if nix flake metadata &>/dev/null && has use_flake; then + # use flakes if possible + use flake + + else + # Otherwise fall back to pure nix + use nix + fi + +fi diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 75278dcee5..c7eb6df168 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -26,6 +26,7 @@ jobs: pull-request-title-pattern: "chore(noir): Release ${version}" extra-files: | Cargo.toml + flake.nix update-lockfile: name: Update lockfile diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml deleted file mode 100644 index 8363858fa8..0000000000 --- a/.github/workflows/rust.yml +++ /dev/null @@ -1,21 +0,0 @@ -name: Rust - -on: [push, pull_request] - -# This will cancel previous runs when a branch or PR is updated -concurrency: - group: ${{ github.workflow }}-${{ github.head_ref || github.ref || github.run_id }} - cancel-in-progress: true - -jobs: - test: - name: Cargo test - uses: noir-lang/.github/.github/workflows/rust-test.yml@main - - clippy: - name: Cargo clippy - uses: noir-lang/.github/.github/workflows/rust-clippy.yml@main - - format: - name: Cargo fmt - uses: noir-lang/.github/.github/workflows/rust-format.yml@main \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000000..220985d800 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,35 @@ +name: Test + +on: [push, pull_request] + +# This will cancel previous runs when a branch or PR is updated +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.ref || github.run_id }} + cancel-in-progress: true + +jobs: + test: + name: Test on ${{ matrix.os }} + runs-on: ${{ matrix.os }} + + strategy: + fail-fast: false + matrix: + include: + - os: ubuntu-latest + target: x86_64-linux + - os: macos-latest + target: x86_64-darwin + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - uses: cachix/install-nix-action@v20 + with: + nix_path: nixpkgs=channel:nixos-22.11 + github_access_token: ${{ secrets.GITHUB_TOKEN }} + + - name: Run `nix flake check` + run: | + nix flake check diff --git a/.gitignore b/.gitignore index 10988465b2..351f5e16a7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,10 +2,14 @@ .DS_Store examples/**/target/ examples/9 -.vscode node_modules pkg/ +# Nix stuff +result +.envrc.local +.direnv/ + # Nargo output *.proof *.acir @@ -13,4 +17,4 @@ pkg/ *.tr *.pk *.vk -**/Verifier.toml \ No newline at end of file +**/Verifier.toml diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000000..64ae238015 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,13 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations. + // Extension identifier format: ${publisher}.${name}. Example: vscode.csharp + // List of extensions which should be recommended for users of this workspace. + "recommendations": [ + "mkhl.direnv", + "jnoortheen.nix-ide", + "rust-lang.rust-analyzer", + "redhat.vscode-yaml" + ], + // List of extensions recommended by VS Code that should not be recommended for users of this workspace. + "unwantedRecommendations": [] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000000..78bfd88c35 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,16 @@ +{ + "direnv.restart.automatic": true, + "redhat.telemetry.enabled": false, + "yaml.recommendations.show": false, + "nix.serverPath": "nil", + "nix.enableLanguageServer": true, + "nix.serverSettings": { + "nil": { + "formatting": { + "command": [ + "nixpkgs-fmt" + ] + } + } + }, +} diff --git a/Cargo.lock b/Cargo.lock index 7416537e24..85b5c12aa4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "acir" -version = "0.8.0" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f764b474e341efc3e8ee3d5054840b2fd2ac002f764fc2f4cd3569ce76badd1" +checksum = "018148d69bf14422b1c1d62909a241af2a7f51fec064feb2b01de88fb02b11b8" dependencies = [ "acir_field", "flate2", @@ -16,9 +16,9 @@ dependencies = [ [[package]] name = "acir_field" -version = "0.8.0" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cbca7df5192c7823d4108d2c34cadcfd30dca94506b9e9861f85f0ea747ddedc" +checksum = "4d40dac25cf6be6335dd86286caeac859afd0dc74a4a75c64eed041b0f00a278" dependencies = [ "ark-bn254", "ark-ff", @@ -30,9 +30,9 @@ dependencies = [ [[package]] name = "acvm" -version = "0.8.0" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92d5df175b6923bf9bb05ba973b017b0fa1356066be8f0ebadd3d2dbbc48bd5b" +checksum = "e17b7bc8f2b2215075b8e080ba3a0b8b7d759f04bc44b27e5bb8d845f4c77f20" dependencies = [ "acir", "acvm_stdlib", @@ -48,9 +48,9 @@ dependencies = [ [[package]] name = "acvm_stdlib" -version = "0.8.0" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa2bbc18fe9732ca3d93a2bf8f1a1ad99a003b565e7bc1ad5c67f69867449e8f" +checksum = "33ce2d19a9d1e7ff1bf415ed909b43031e33ef6df21be70e470bb1817b3e6989" dependencies = [ "acir", ] @@ -270,17 +270,6 @@ dependencies = [ "tempfile", ] -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi 0.1.19", - "libc", - "winapi", -] - [[package]] name = "autocfg" version = "1.1.0" @@ -303,32 +292,38 @@ dependencies = [ ] [[package]] -name = "barretenberg_static_lib" -version = "0.1.0" -source = "git+https://github.com/noir-lang/aztec_backend?rev=26178359a2251e885f15f0a4d1a686afda04aec9#26178359a2251e885f15f0a4d1a686afda04aec9" +name = "barretenberg-sys" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bc96e40cc45e7d5622cbc57a140aee926a7fb6e4f7d07dda758daa9256f3317" dependencies = [ - "barretenberg_wrapper", - "common", + "bindgen", + "cc", + "color-eyre", + "link-cplusplus", + "pkg-config", + "thiserror", ] [[package]] -name = "barretenberg_wasm" +name = "barretenberg_static_lib" version = "0.1.0" -source = "git+https://github.com/noir-lang/aztec_backend?rev=26178359a2251e885f15f0a4d1a686afda04aec9#26178359a2251e885f15f0a4d1a686afda04aec9" +source = "git+https://github.com/noir-lang/aztec_backend?rev=e3d4504f15e1295e637c4da80b1d08c87c267c45#e3d4504f15e1295e637c4da80b1d08c87c267c45" dependencies = [ + "barretenberg-sys", "common", - "wasmer", ] [[package]] -name = "barretenberg_wrapper" +name = "barretenberg_wasm" version = "0.1.0" -source = "git+https://github.com/noir-lang/aztec-connect?branch=kw/noir-dsl#dbd544318819cf710dede8ffb7b7eafa112f5aed" +source = "git+https://github.com/noir-lang/aztec_backend?rev=e3d4504f15e1295e637c4da80b1d08c87c267c45#e3d4504f15e1295e637c4da80b1d08c87c267c45" dependencies = [ - "bindgen", - "cmake", - "hex", - "num_cpus 0.2.13", + "common", + "getrandom", + "pkg-config", + "rust-embed", + "wasmer", ] [[package]] @@ -339,15 +334,13 @@ checksum = "a4a4ddaa51a5bc52a6948f74c06d20aaaddb71924eab79b8c97a8c556e942d6a" [[package]] name = "bindgen" -version = "0.60.1" +version = "0.64.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "062dddbc1ba4aca46de6338e2bf87771414c335f7b2f2036e8f3e9befebf88e6" +checksum = "c4243e6031260db77ede97ad86c27e501d646a27ab57b59a574f725d98ab1fb4" dependencies = [ "bitflags", "cexpr", "clang-sys", - "clap 3.2.23", - "env_logger", "lazy_static", "lazycell", "log", @@ -357,6 +350,7 @@ dependencies = [ "regex", "rustc-hash", "shlex", + "syn", "which", ] @@ -531,21 +525,6 @@ dependencies = [ "libloading", ] -[[package]] -name = "clap" -version = "3.2.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71655c45cb9845d3270c9d6df84ebe72b4dad3c2ba3f7023ad47c144e4e473a5" -dependencies = [ - "atty", - "bitflags", - "clap_lex 0.2.4", - "indexmap", - "strsim", - "termcolor", - "textwrap", -] - [[package]] name = "clap" version = "4.1.8" @@ -554,7 +533,7 @@ checksum = "c3d7ae14b20b94cb02149ed21a86c423859cbe18dc7ed69845cace50e52b40a5" dependencies = [ "bitflags", "clap_derive", - "clap_lex 0.3.2", + "clap_lex", "is-terminal", "once_cell", "strsim", @@ -574,15 +553,6 @@ dependencies = [ "syn", ] -[[package]] -name = "clap_lex" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" -dependencies = [ - "os_str_bytes", -] - [[package]] name = "clap_lex" version = "0.3.2" @@ -592,15 +562,6 @@ dependencies = [ "os_str_bytes", ] -[[package]] -name = "cmake" -version = "0.1.49" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db34956e100b30725f2eb215f90d4871051239535632f84fea3bc92722c66b7c" -dependencies = [ - "cc", -] - [[package]] name = "codespan" version = "0.9.5" @@ -660,15 +621,16 @@ dependencies = [ [[package]] name = "common" version = "0.1.0" -source = "git+https://github.com/noir-lang/aztec_backend?rev=26178359a2251e885f15f0a4d1a686afda04aec9#26178359a2251e885f15f0a4d1a686afda04aec9" +source = "git+https://github.com/noir-lang/aztec_backend?rev=e3d4504f15e1295e637c4da80b1d08c87c267c45#e3d4504f15e1295e637c4da80b1d08c87c267c45" dependencies = [ "acvm", "blake2", "dirs 3.0.2", - "downloader", + "futures-util", "indicatif", - "regex", + "reqwest", "sled", + "tokio", ] [[package]] @@ -1047,19 +1009,6 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" -[[package]] -name = "downloader" -version = "0.2.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d05213e96f184578b5f70105d4d0a644a168e99e12d7bea0b200c15d67b5c182" -dependencies = [ - "futures", - "rand 0.8.5", - "reqwest", - "thiserror", - "tokio", -] - [[package]] name = "ecdsa" version = "0.10.2" @@ -1151,19 +1100,6 @@ dependencies = [ "syn", ] -[[package]] -name = "env_logger" -version = "0.9.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a12e6657c4c97ebab115a42dcee77225f7f482cdd841cf7088c657a42e9e00e7" -dependencies = [ - "atty", - "humantime", - "log", - "regex", - "termcolor", -] - [[package]] name = "errno" version = "0.2.8" @@ -1288,21 +1224,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fed34cd105917e91daa4da6b3728c47b068749d6a62c59811f06ed2ac71d9da7" -[[package]] -name = "futures" -version = "0.3.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "531ac96c6ff5fd7c62263c5e3c67a603af4fcaee2e1a0ae5565ba3a11e69e549" -dependencies = [ - "futures-channel", - "futures-core", - "futures-executor", - "futures-io", - "futures-sink", - "futures-task", - "futures-util", -] - [[package]] name = "futures-channel" version = "0.3.27" @@ -1310,7 +1231,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "164713a5a0dcc3e7b4b1ed7d3b433cabc18025386f9339346e8daf15963cf7ac" dependencies = [ "futures-core", - "futures-sink", ] [[package]] @@ -1319,17 +1239,6 @@ version = "0.3.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "86d7a0c1aa76363dac491de0ee99faf6941128376f1cf96f07db7603b7de69dd" -[[package]] -name = "futures-executor" -version = "0.3.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1997dd9df74cdac935c76252744c1ed5794fac083242ea4fe77ef3ed60ba0f83" -dependencies = [ - "futures-core", - "futures-task", - "futures-util", -] - [[package]] name = "futures-io" version = "0.3.27" @@ -1365,7 +1274,6 @@ version = "0.3.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ef6b17e481503ec85211fed8f39d1970f128935ca1f814cd32ac4a6842e84ab" dependencies = [ - "futures-channel", "futures-core", "futures-io", "futures-macro", @@ -1539,15 +1447,6 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" -[[package]] -name = "hermit-abi" -version = "0.1.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" -dependencies = [ - "libc", -] - [[package]] name = "hermit-abi" version = "0.2.6" @@ -1613,12 +1512,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" -[[package]] -name = "humantime" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" - [[package]] name = "hyper" version = "0.14.25" @@ -1732,14 +1625,14 @@ dependencies = [ [[package]] name = "indicatif" -version = "0.15.0" +version = "0.17.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7baab56125e25686df467fe470785512329883aab42696d661247aca2a2896e4" +checksum = "cef509aa9bc73864d6756f0d34d35504af3cf0844373afe9b8669a5b8005a729" dependencies = [ "console", - "lazy_static", "number_prefix", - "regex", + "portable-atomic", + "unicode-width", ] [[package]] @@ -2014,7 +1907,7 @@ dependencies = [ "barretenberg_wasm", "build-data", "cfg-if 1.0.0", - "clap 4.1.8", + "clap", "color-eyre", "const_format", "dirs 4.0.0", @@ -2068,7 +1961,7 @@ name = "noirc_driver" version = "0.4.1" dependencies = [ "acvm", - "clap 4.1.8", + "clap", "fm", "iter-extended", "noirc_abi", @@ -2170,15 +2063,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "num_cpus" -version = "0.2.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cee7e88156f3f9e19bdd598f8d6c9db7bf4078f99f8381f43a55b09648d1a6e3" -dependencies = [ - "libc", -] - [[package]] name = "num_cpus" version = "1.15.0" @@ -2191,9 +2075,9 @@ dependencies = [ [[package]] name = "number_prefix" -version = "0.3.0" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17b02fc0ff9a9e4b35b3342880f48e896ebf69f2967921fe8646bf5b7125956a" +checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" [[package]] name = "object" @@ -2304,6 +2188,18 @@ dependencies = [ "der", ] +[[package]] +name = "pkg-config" +version = "0.3.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160" + +[[package]] +name = "portable-atomic" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26f6a7b87c2e435a3241addceeeff740ff8b7e76b74c13bf9acb17fa454ea00b" + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -2512,7 +2408,7 @@ dependencies = [ "crossbeam-channel", "crossbeam-deque", "crossbeam-utils", - "num_cpus 1.15.0", + "num_cpus", ] [[package]] @@ -2610,9 +2506,9 @@ dependencies = [ [[package]] name = "reqwest" -version = "0.11.14" +version = "0.11.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21eed90ec8570952d53b772ecf8f206aa1ec9a3d76b2521c56c42973f2d91ee9" +checksum = "27b71749df584b7f4cac2c426c127a7c785a5106cc98f7a8feb044115f0fa254" dependencies = [ "base64", "bytes", @@ -2638,10 +2534,12 @@ dependencies = [ "serde_urlencoded", "tokio", "tokio-rustls", + "tokio-util", "tower-service", "url", "wasm-bindgen", "wasm-bindgen-futures", + "wasm-streams", "web-sys", "webpki-roots", "winreg", @@ -2729,6 +2627,7 @@ dependencies = [ "proc-macro2", "quote", "rust-embed-utils", + "shellexpand", "syn", "walkdir", ] @@ -2739,6 +2638,7 @@ version = "7.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512b0ab6853f7e14e3c8754acb43d6f748bb9ced66aa5915a6553ac8213f7731" dependencies = [ + "globset", "sha2 0.10.6", "walkdir", ] @@ -3001,6 +2901,15 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "shellexpand" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ccc8076840c4da029af4f87e4e8daeb0fca6b87bbb02e10cb60b791450e11e4" +dependencies = [ + "dirs 4.0.0", +] + [[package]] name = "shlex" version = "1.1.0" @@ -3196,12 +3105,6 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" -[[package]] -name = "textwrap" -version = "0.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" - [[package]] name = "thiserror" version = "1.0.39" @@ -3269,7 +3172,6 @@ dependencies = [ "libc", "memchr", "mio", - "num_cpus 1.15.0", "pin-project-lite", "socket2", "windows-sys 0.45.0", @@ -3599,6 +3501,19 @@ dependencies = [ "web-sys", ] +[[package]] +name = "wasm-streams" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bbae3363c08332cadccd13b67db371814cd214c2524020932f0804b8cf7c078" +dependencies = [ + "futures-util", + "js-sys", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + [[package]] name = "wasmer" version = "2.3.0" diff --git a/Cargo.toml b/Cargo.toml index 746c3e6399..26eec846ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ edition = "2021" rust-version = "1.66" [workspace.dependencies] -acvm = "0.8.0" +acvm = "0.9.0" arena = { path = "crates/arena" } fm = { path = "crates/fm" } iter-extended = { path = "crates/iter-extended" } diff --git a/README.md b/README.md index bef2e9a0a8..1830e43016 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,81 @@ Concretely the following items are on the road map: This crate's minimum supported rustc version is 1.66.0. +## Working on this project + +Due to the large number of native dependencies, this project uses [Nix](https://nixos.org/) and [direnv](https://direnv.net/) to streamline the development experience. + +### Setting up your environment + +For the best experience, please follow these instructions to setup your environment: +1. Install Nix following [their guide](https://nixos.org/download.html) for your operating system +2. Create the file `~/.config/nix/nix.conf` with the contents: +```ini +experimental-features = nix-command +extra-experimental-features = flakes +``` +3. Install direnv into your Nix profile by running: +```sh +nix profile install nixpkgs#direnv +``` +4. Add direnv to your shell following [their guide](https://direnv.net/docs/hook.html) +5. Restart your shell + +### Shell & editor experience + +Now that your environment is set up, you can get to work on the project. + +1. Clone the repository, such as: +```sh +git clone git@github.com:noir-lang/noir +``` +2. Navigate to the directory: +```sh +cd noir +``` +3. You should see a __direnv error__ because projects aren't allowed by default. Make sure you've reviewed and trust our `.envrc` file, then you need to run: +```sh +direnv allow +``` +4. Now, wait awhile for all the native dependencies to be built. This will take some time and direnv will warn you that it is taking a long time, but we just need to let it run. +5. Once you are presented with your prompt again, you can start your editor within the project directory (we recommend [VSCode](https://code.visualstudio.com/)): +```sh +code . +``` +6. (Recommended) When launching VSCode for the first time, you should be prompted to install our recommended plugins. We highly recommend installing these for the best development experience. + +### Building and testing + +Assuming you are using `direnv` to populate your environment, building and testing the project can be done +with the typical `cargo build`, `cargo test`, and `cargo clippy` commands. You'll notice that the `cargo` version matches the version we specify in [flake.nix](./flake.nix), which is 1.66.0 at the time of this writing. + +If you want to build the entire project in an isolated sandbox, you can use Nix commands: +1. `nix build .` (or `nix build . -L` for verbose output) to build the project in a Nix sandbox +2. `nix flake check` (or `nix flake check -L` for verbose output) to run clippy and tests in a Nix sandbox + +### Building against a different local/remote version of Barretenberg + +If you are working on this project and want a different version of Barretenberg (instead of the version this project is pinned against), you'll want to replace the lockfile version with your version. This can be done by running: + +```sh +nix flake lock --override-input barretenberg /absolute/path/to/your/barretenberg +``` + +You can also point at a fork and/or branch on GitHub using: + +```sh +nix flake lock --override-input barretenberg github:username/barretenberg/branch_name +``` + +__Note:__ You don't want to commit the updated lockfile, as it will fail in CI! + +### Without direnv + +If you have hesitations with using `direnv`, you can launch a subshell with `nix develop` and then launch your editor +from within the subshell. However, if VSCode was already launched in the project directory, the environment won't be updated. + +__Advanced:__ If you aren't using `direnv` nor launching your editor within the subshell, you can try to install Barretenberg and other global dependencies the package needs. This is an advanced workflow and likely won't receive support! + ## License Noir is free and open source. It is distributed under a dual license. (MIT/APACHE) diff --git a/crates/nargo/Cargo.toml b/crates/nargo/Cargo.toml index 430d926cc9..8d3c9fbd3c 100644 --- a/crates/nargo/Cargo.toml +++ b/crates/nargo/Cargo.toml @@ -18,4 +18,3 @@ iter-extended.workspace = true toml.workspace = true serde.workspace = true thiserror.workspace = true - diff --git a/crates/nargo_cli/Cargo.toml b/crates/nargo_cli/Cargo.toml index 49c9428f54..40ab4b9245 100644 --- a/crates/nargo_cli/Cargo.toml +++ b/crates/nargo_cli/Cargo.toml @@ -37,8 +37,8 @@ termcolor = "1.1.2" color-eyre = "0.6.2" # Backends -aztec_backend = { optional = true, package = "barretenberg_static_lib", git = "https://github.com/noir-lang/aztec_backend", rev = "26178359a2251e885f15f0a4d1a686afda04aec9" } -aztec_wasm_backend = { optional = true, package = "barretenberg_wasm", git = "https://github.com/noir-lang/aztec_backend", rev = "26178359a2251e885f15f0a4d1a686afda04aec9" } +aztec_backend = { optional = true, package = "barretenberg_static_lib", git = "https://github.com/noir-lang/aztec_backend", rev = "e3d4504f15e1295e637c4da80b1d08c87c267c45" } +aztec_wasm_backend = { optional = true, package = "barretenberg_wasm", git = "https://github.com/noir-lang/aztec_backend", rev = "e3d4504f15e1295e637c4da80b1d08c87c267c45" } [dev-dependencies] tempdir = "0.3.7" diff --git a/crates/nargo_cli/build.rs b/crates/nargo_cli/build.rs index 0745d31840..8e8ff02b90 100644 --- a/crates/nargo_cli/build.rs +++ b/crates/nargo_cli/build.rs @@ -7,10 +7,16 @@ fn check_rustc_version() { ); } +const GIT_COMMIT: &&str = &"GIT_COMMIT"; + fn main() { check_rustc_version(); - build_data::set_GIT_COMMIT(); - build_data::set_GIT_DIRTY(); - build_data::no_debug_rebuilds(); + // Only use build_data if the environment variable isn't set + // The environment variable is always set when working via Nix + if std::env::var(GIT_COMMIT).is_err() { + build_data::set_GIT_COMMIT(); + build_data::set_GIT_DIRTY(); + build_data::no_debug_rebuilds(); + } } diff --git a/crates/nargo_cli/src/cli/codegen_verifier_cmd.rs b/crates/nargo_cli/src/cli/codegen_verifier_cmd.rs index 4e713ff4a4..319a572270 100644 --- a/crates/nargo_cli/src/cli/codegen_verifier_cmd.rs +++ b/crates/nargo_cli/src/cli/codegen_verifier_cmd.rs @@ -1,14 +1,19 @@ -use super::fs::{create_named_dir, write_to_file}; +use super::fs::{create_named_dir, program::read_program_from_file, write_to_file}; use super::NargoConfig; -use crate::{cli::compile_cmd::compile_circuit, constants::CONTRACT_DIR, errors::CliError}; -use acvm::SmartContract; +use crate::{ + cli::compile_cmd::compile_circuit, constants::CONTRACT_DIR, constants::TARGET_DIR, + errors::CliError, +}; use clap::Args; -use nargo::ops::preprocess_program; +use nargo::ops::{codegen_verifier, preprocess_program}; use noirc_driver::CompileOptions; /// Generates a Solidity verifier smart contract for the program #[derive(Debug, Clone, Args)] pub(crate) struct CodegenVerifierCommand { + /// The name of the circuit build files (ACIR, proving and verification keys) + circuit_name: Option, + #[clap(flatten)] compile_options: CompileOptions, } @@ -16,11 +21,21 @@ pub(crate) struct CodegenVerifierCommand { pub(crate) fn run(args: CodegenVerifierCommand, config: NargoConfig) -> Result<(), CliError> { let backend = crate::backends::ConcreteBackend; - let compiled_program = compile_circuit(&backend, &config.program_dir, &args.compile_options)?; - let preprocessed_program = preprocess_program(&backend, compiled_program)?; + // TODO(#1201): Should this be a utility function? + let circuit_build_path = args + .circuit_name + .map(|circuit_name| config.program_dir.join(TARGET_DIR).join(circuit_name)); + + let preprocessed_program = match circuit_build_path { + Some(circuit_build_path) => read_program_from_file(circuit_build_path)?, + None => { + let compiled_program = + compile_circuit(&backend, config.program_dir.as_ref(), &args.compile_options)?; + preprocess_program(&backend, compiled_program)? + } + }; - #[allow(deprecated)] - let smart_contract_string = backend.eth_contract_from_cs(preprocessed_program.bytecode); + let smart_contract_string = codegen_verifier(&backend, &preprocessed_program.verification_key)?; let contract_dir = config.program_dir.join(CONTRACT_DIR); create_named_dir(&contract_dir, "contract"); diff --git a/crates/nargo_cli/src/lib.rs b/crates/nargo_cli/src/lib.rs index a943e58063..da4dee6fd5 100644 --- a/crates/nargo_cli/src/lib.rs +++ b/crates/nargo_cli/src/lib.rs @@ -1,5 +1,5 @@ #![forbid(unsafe_code)] -#![warn(unused_crate_dependencies, unused_extern_crates)] +#![warn(unused_extern_crates)] #![warn(unreachable_pub)] #![warn(clippy::semicolon_if_nothing_returned)] diff --git a/crates/nargo_cli/tests/prove_and_verify.rs b/crates/nargo_cli/tests/prove_and_verify.rs index 15e860bf05..070db6d8ce 100644 --- a/crates/nargo_cli/tests/prove_and_verify.rs +++ b/crates/nargo_cli/tests/prove_and_verify.rs @@ -3,12 +3,8 @@ use tempdir::TempDir; use std::collections::BTreeMap; use std::fs; -const TEST_DIR: &str = "tests"; -const TEST_DATA_DIR: &str = "test_data"; -const CONFIG_FILE: &str = "config.toml"; - mod tests { - use std::path::Path; + use std::path::{Path, PathBuf}; use super::*; @@ -51,13 +47,17 @@ mod tests { #[test] fn noir_integration() { - let current_dir = std::env::current_dir().unwrap(); - - let test_data_dir = current_dir.join(TEST_DIR).join(TEST_DATA_DIR); + // Try to find the directory that Cargo sets when it is running; otherwise fallback to assuming the CWD + // is the root of the repository and append the crate path + let manifest_dir = match std::env::var("CARGO_MANIFEST_DIR") { + Ok(dir) => PathBuf::from(dir), + Err(_) => std::env::current_dir().unwrap().join("crates").join("nargo_cli"), + }; + let test_data_dir = manifest_dir.join("tests").join("test_data"); + let config_path = test_data_dir.join("config.toml"); - // Load config.toml file from test_data directory - let config_file_path = test_data_dir.join(CONFIG_FILE); - let config_data: BTreeMap> = load_conf(&config_file_path); + // Load config.toml file from `test_data` directory + let config_data: BTreeMap> = load_conf(&config_path); // Copy all the test cases into a temp dir so we don't leave artifacts around. let tmp_dir = TempDir::new("p_and_v_tests").unwrap(); diff --git a/crates/nargo_cli/tests/test_data/merkle_insert/Prover.toml b/crates/nargo_cli/tests/test_data/merkle_insert/Prover.toml index 909fc1bbf6..691ea27abd 100644 --- a/crates/nargo_cli/tests/test_data/merkle_insert/Prover.toml +++ b/crates/nargo_cli/tests/test_data/merkle_insert/Prover.toml @@ -1,11 +1,11 @@ -old_root = "0x083b35b32ba24436c1614e4262cb4ad8f98f99cdb5fb0da8932ab2a290034867" -old_leaf = "0x0b81829b478114d28b964bd382ebff8be0216741aa72ff2896909110aef1704a" +old_root = "0x04ccfbbb859b8605546e03dcaf41393476642859ff7f99446c054b841f0e05c8" +old_leaf = "0x1cdcf02431ba623767fe389337d011df1048dcc24b98ed81cec97627bab454a0" old_hash_path = [ - "0x0000000000000000000000000000000000000000000000000000000000000000", - "0x0e4223f3925f98934393c74975142bd73079ab0621f4ee133cee050a3c194f1a", - "0x2fd7bb412155bf8693a3bd2a3e7581a679c95c68a052f835dddca85fa1569a40" + "0x1cdcf02431ba623767fe389337d011df1048dcc24b98ed81cec97627bab454a0", + "0x0b5e9666e7323ce925c28201a97ddf4144ac9d148448ed6f49f9008719c1b85b", + "0x22ec636f8ad30ef78c42b7fe2be4a4cacf5a445cfb5948224539f59a11d70775", ] -new_root = "0x256c13d7694e2f900f55756246aa1104169efd9fb9e7c6be54c15794795d476f" -leaf = "0x2e5ba44f3c5329aeb915c703e39b33c5872f1542500cbb22f12b71640aba502f" +new_root = "0x293ee9de893904164a8f8b248b6941217d16a4c9042ec54b71896dddbbce7a3b" +leaf = "0x085ca53be9c9d95b57e6e5fc91c5d531ad9e63e85dd71af7e35562991774b435" index = "0" mimc_input = [12,45,78,41] diff --git a/crates/nargo_cli/tests/test_data/pedersen_check/Prover.toml b/crates/nargo_cli/tests/test_data/pedersen_check/Prover.toml index c5eaec281c..4459711e6e 100644 --- a/crates/nargo_cli/tests/test_data/pedersen_check/Prover.toml +++ b/crates/nargo_cli/tests/test_data/pedersen_check/Prover.toml @@ -2,5 +2,5 @@ x = "0" y = "1" salt = "42" -out_x = "0x229fb88be21cec523e9223a21324f2e305aea8bff9cdbcb3d0c6bba384666ea1" -out_y = "0x296b4b4605e586a91caa3202baad557628a8c56d0a1d6dff1a7ca35aed3029d5" +out_x = "0x11831f49876c313f2a9ec6d8d521c7ce0b6311c852117e340bfe27fd1ac096ef" +out_y = "0x0ecf9d98be4597a88c46a7e0fa8836b57a7dcb41ee30f8d8787b11cc259c83fa" diff --git a/crates/nargo_cli/tests/test_data/simple_shield/Prover.toml b/crates/nargo_cli/tests/test_data/simple_shield/Prover.toml index 67e825f633..554ae9900a 100644 --- a/crates/nargo_cli/tests/test_data/simple_shield/Prover.toml +++ b/crates/nargo_cli/tests/test_data/simple_shield/Prover.toml @@ -1,11 +1,11 @@ # Random test key priv_key = "0x000000000000000000000000000000000000000000000000000000616c696365" -note_root = "0x165cdc3cbcf4a6bf7b69514807409a3ed2e701556821f396ca04781a6434d96a" +note_root = "0x293ee9de893904164a8f8b248b6941217d16a4c9042ec54b71896dddbbce7a3b" index = "0" note_hash_path = [ - "0x0000000000000000000000000000000000000000000000000000000000000000", - "0x0e4223f3925f98934393c74975142bd73079ab0621f4ee133cee050a3c194f1a", - "0x2fd7bb412155bf8693a3bd2a3e7581a679c95c68a052f835dddca85fa1569a40", + "0x1cdcf02431ba623767fe389337d011df1048dcc24b98ed81cec97627bab454a0", + "0x0b5e9666e7323ce925c28201a97ddf4144ac9d148448ed6f49f9008719c1b85b", + "0x22ec636f8ad30ef78c42b7fe2be4a4cacf5a445cfb5948224539f59a11d70775", ] to_pubkey_x = "0x0000000000000000000000000000000000000000000000000000000000000001" to_pubkey_y = "0x0000000000000002cf135e7506a45d632d270d45f1181294833fc48d823f272c" diff --git a/crates/nargo_cli/tests/test_data/xor/Nargo.toml b/crates/nargo_cli/tests/test_data/xor/Nargo.toml new file mode 100644 index 0000000000..e0b467ce5d --- /dev/null +++ b/crates/nargo_cli/tests/test_data/xor/Nargo.toml @@ -0,0 +1,5 @@ +[package] +authors = [""] +compiler_version = "0.1" + +[dependencies] \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data/xor/Prover.toml b/crates/nargo_cli/tests/test_data/xor/Prover.toml new file mode 100644 index 0000000000..f28f2f8cc4 --- /dev/null +++ b/crates/nargo_cli/tests/test_data/xor/Prover.toml @@ -0,0 +1,2 @@ +x = "5" +y = "10" diff --git a/crates/nargo_cli/tests/test_data/xor/src/main.nr b/crates/nargo_cli/tests/test_data/xor/src/main.nr new file mode 100644 index 0000000000..cc7caf17fa --- /dev/null +++ b/crates/nargo_cli/tests/test_data/xor/src/main.nr @@ -0,0 +1,5 @@ +fn main(x : u32, y : pub u32) { + let m = x ^ y; + + constrain m != 10; +} \ No newline at end of file diff --git a/crates/noirc_evaluator/src/ssa/acir_gen/operations/sort.rs b/crates/noirc_evaluator/src/ssa/acir_gen/operations/sort.rs index fe7c39de7c..04524959fb 100644 --- a/crates/noirc_evaluator/src/ssa/acir_gen/operations/sort.rs +++ b/crates/noirc_evaluator/src/ssa/acir_gen/operations/sort.rs @@ -130,6 +130,7 @@ mod test { struct MockBackend {} impl PartialWitnessGenerator for MockBackend { fn solve_black_box_function_call( + &self, _initial_witness: &mut BTreeMap, _func_call: &BlackBoxFuncCall, ) -> Result { diff --git a/crates/wasm/build.rs b/crates/wasm/build.rs index 9c91effe74..3b96be74ef 100644 --- a/crates/wasm/build.rs +++ b/crates/wasm/build.rs @@ -1,6 +1,14 @@ +const GIT_COMMIT: &&str = &"GIT_COMMIT"; + fn main() { - build_data::set_GIT_COMMIT(); - build_data::set_GIT_DIRTY(); + // Only use build_data if the environment variable isn't set + // The environment variable is always set when working via Nix + if std::env::var(GIT_COMMIT).is_err() { + build_data::set_GIT_COMMIT(); + build_data::set_GIT_DIRTY(); + build_data::no_debug_rebuilds(); + } + build_data::set_SOURCE_TIMESTAMP(); build_data::no_debug_rebuilds(); } diff --git a/crates/wasm/src/circuit.rs b/crates/wasm/src/circuit.rs index 6168fe77bb..97f9ef9cf1 100644 --- a/crates/wasm/src/circuit.rs +++ b/crates/wasm/src/circuit.rs @@ -2,29 +2,6 @@ use acvm::acir::circuit::Circuit; use gloo_utils::format::JsValueSerdeExt; use wasm_bindgen::prelude::*; -// Deserializes bytes into ACIR structure -#[deprecated( - note = "we have moved away from this serialization strategy. Call `acir_read_bytes` instead" -)] -#[allow(deprecated)] -#[wasm_bindgen] -pub fn acir_from_bytes(bytes: Vec) -> JsValue { - console_error_panic_hook::set_once(); - let circuit = Circuit::from_bytes(&bytes); - ::from_serde(&circuit).unwrap() -} - -#[deprecated( - note = "we have moved away from this serialization strategy. Call `acir_write_bytes` instead" -)] -#[allow(deprecated)] -#[wasm_bindgen] -pub fn acir_to_bytes(acir: JsValue) -> Vec { - console_error_panic_hook::set_once(); - let circuit: Circuit = JsValueSerdeExt::into_serde(&acir).unwrap(); - circuit.to_bytes() -} - // Deserializes bytes into ACIR structure #[wasm_bindgen] pub fn acir_read_bytes(bytes: Vec) -> JsValue { diff --git a/crates/wasm/src/lib.rs b/crates/wasm/src/lib.rs index 56995c21df..2a659b9496 100644 --- a/crates/wasm/src/lib.rs +++ b/crates/wasm/src/lib.rs @@ -12,8 +12,7 @@ use wasm_bindgen::prelude::*; mod circuit; mod compile; -#[allow(deprecated)] -pub use circuit::{acir_from_bytes, acir_read_bytes, acir_to_bytes, acir_write_bytes}; +pub use circuit::{acir_read_bytes, acir_write_bytes}; pub use compile::{compile, WASMCompileOptions}; #[derive(Serialize, Deserialize)] diff --git a/cspell.json b/cspell.json index 37667e96ba..f0031d724b 100644 --- a/cspell.json +++ b/cspell.json @@ -52,6 +52,10 @@ "uninstantiated", "urem", "vecmap", + "direnv", + "nixpkgs", + "envrc", + "subshell", // Dependencies // "acir", diff --git a/default.nix b/default.nix index 0bb5c39e06..9e230590a6 100644 --- a/default.nix +++ b/default.nix @@ -1,11 +1,13 @@ -{ pkgs ? import {} }: - -pkgs.mkShell { - buildInputs = [ - pkgs.openssl - pkgs.pkg-config - pkgs.cmake - pkgs.llvmPackages.openmp - pkgs.rustup - ]; -} \ No newline at end of file +let + lock = builtins.fromJSON (builtins.readFile ./flake.lock); + flakeCompatRev = lock.nodes.flake-compat.locked.rev; + flakeCompatHash = lock.nodes.flake-compat.locked.narHash; + flakeCompat = fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/${flakeCompatRev}.tar.gz"; + sha256 = flakeCompatHash; + }; + compat = import flakeCompat { + src = ./.; + }; +in +compat.defaultNix diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000000..04dbc188a5 --- /dev/null +++ b/flake.lock @@ -0,0 +1,156 @@ +{ + "nodes": { + "barretenberg": { + "inputs": { + "flake-utils": [ + "flake-utils" + ], + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1682019675, + "narHash": "sha256-KZ/VL/u81z2sFTdwfxvUFR+ftqf3+2AA0gR9kkgKxe4=", + "owner": "AztecProtocol", + "repo": "barretenberg", + "rev": "a38e3611590e085e5f25c322757871fb048aa3d7", + "type": "github" + }, + "original": { + "owner": "AztecProtocol", + "repo": "barretenberg", + "type": "github" + } + }, + "crane": { + "inputs": { + "flake-compat": [ + "flake-compat" + ], + "flake-utils": [ + "flake-utils" + ], + "nixpkgs": [ + "nixpkgs" + ], + "rust-overlay": [ + "rust-overlay" + ] + }, + "locked": { + "lastModified": 1681680516, + "narHash": "sha256-EB8Adaeg4zgcYDJn9sR6UMjN/OHdIiMMK19+3LmmXQY=", + "owner": "ipetkov", + "repo": "crane", + "rev": "54b63c8eae4c50172cb50b612946ff1d2bc1c75c", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1673956053, + "narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1681202837, + "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "cfacdce06f30d2b68473a46042957675eebb3401", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1681932375, + "narHash": "sha256-tSXbYmpnKSSWpzOrs27ie8X3I0yqKA6AuCzCYNtwbCU=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "3d302c67ab8647327dba84fbdb443cdbf0e82744", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-22.11", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "barretenberg": "barretenberg", + "crane": "crane", + "flake-compat": "flake-compat", + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "flake-utils": [ + "flake-utils" + ], + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1681957132, + "narHash": "sha256-52GaHyeLyyiT0u4OL3uGbo0vsUMKm33Z3zLkPyK/ZRY=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "4771640d46c214d702512a8ece591f582ae507fa", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000000..ab5012160d --- /dev/null +++ b/flake.nix @@ -0,0 +1,191 @@ +{ + description = "Build the Noir programming language"; + + inputs = { + nixpkgs = { + url = "github:NixOS/nixpkgs/nixos-22.11"; + }; + + flake-utils = { + url = "github:numtide/flake-utils"; + }; + + flake-compat = { + url = "github:edolstra/flake-compat"; + 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 + 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, ... }: + 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" ]; + }; + + craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain; + + environment = { + # rust-bindgen needs to know the location of libclang + LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib"; + + # 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"; + + # We set the environment variable because barretenberg must be compiled in a special way for wasm + BARRETENBERG_BIN_DIR = "${pkgs.barretenberg-wasm}/bin"; + + # We provide `barretenberg-transcript00` from the overlay to the build. + # This is necessary because the Nix sandbox disables the $HOME so downloading during tests would fail + BARRETENBERG_TRANSCRIPT = pkgs.barretenberg-transcript00; + }; + + # 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"; + + # 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; + + # Combine the environment and other configuration needed for crane to build our Rust packages + commonArgs = environment // { + pname = "noir"; + # x-release-please-start-version + version = "0.3.2"; + # x-release-please-end + + # Use our custom stdenv to build and test our Rust project + inherit stdenv; + + src = ./.; + + # Running checks don't do much more than compiling itself and increase + # the build time by a lot, so we disable them throughout all our flakes + doCheck = false; + + 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 + ]; + + buildInputs = [ + pkgs.llvmPackages.openmp + pkgs.barretenberg + ] ++ 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 + ]; + + inherit GIT_COMMIT; + inherit GIT_DIRTY; + }; + + # Build *just* the cargo dependencies, so we can reuse all of that work between runs + cargoArtifacts = craneLib.buildDepsOnly commonArgs; + + noir = craneLib.buildPackage (commonArgs // { + inherit cargoArtifacts; + }); + in + rec { + checks = { + cargo-clippy = craneLib.cargoClippy (commonArgs // { + inherit cargoArtifacts; + + # TODO(#1198): It'd be nice to include these flags when running `cargo clippy` in a devShell. + cargoClippyExtraArgs = "--all-targets -- -D warnings"; + + doCheck = true; + }); + + cargo-test = craneLib.cargoTest (commonArgs // { + inherit cargoArtifacts; + + # TODO(#1198): It'd be nice to include this flag when running `cargo test` in a devShell. + cargoTestExtraArgs = "--workspace"; + + doCheck = true; + }); + }; + + packages.default = noir; + + # 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, + # and extra tooling via `nativeBuildInputs` + devShells.default = pkgs.mkShell.override { inherit stdenv; } (environment // { + inputsFrom = builtins.attrValues checks; + + nativeBuildInputs = with pkgs; [ + which + starship + git + nil + nixpkgs-fmt + llvmPackages.lldb # This ensures the right lldb is in the environment for running rust-lldb + ]; + + shellHook = '' + eval "$(starship init bash)" + ''; + }); + }); +} diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000000..b72d4a4697 --- /dev/null +++ b/shell.nix @@ -0,0 +1,13 @@ +let + lock = builtins.fromJSON (builtins.readFile ./flake.lock); + flakeCompatRev = lock.nodes.flake-compat.locked.rev; + flakeCompatHash = lock.nodes.flake-compat.locked.narHash; + flakeCompat = fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/${flakeCompatRev}.tar.gz"; + sha256 = flakeCompatHash; + }; + compat = import flakeCompat { + src = ./.; + }; +in +compat.shellNix