diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 92efc9a..460f5b0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,39 +10,38 @@ env: REPO: ${{ github.event.repository.name }} jobs: - check: - environment: check + ci: runs-on: ubuntu-22.04 steps: - name: git checkout uses: actions/checkout@v3 - - uses: DeterminateSystems/nix-installer-action@main + - name: Check Nixpkgs input in flake + uses: DeterminateSystems/flake-checker-action@v5 with: - extra-conf: | - binary-caches = https://cache.nixos.org https://${{ secrets.CACHIX_CACHE }}.cachix.org - trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= ${{ secrets.CACHIX_TRUSTED_PUBLIC_KEY }} - trusted-substituters = https://cache.nixos.org https://${{ secrets.CACHIX_CACHE }}.cachix.org - trusted-users = root runner - - uses: DeterminateSystems/magic-nix-cache-action@main - - name: Set up Nix environment + fail-mode: true + + - name: Install Nix + uses: DeterminateSystems/nix-installer-action@v10 + + - name: Set up Magic Nix Cache + uses: DeterminateSystems/magic-nix-cache-action@v4 + + - name: nix flake check run: | - nix develop --command direnv allow - eval "$(nix develop --command direnv export bash)" - echo "$PATH" >> $GITHUB_PATH - - name: Binary cache setup + nix flake check --all-systems + + - name: Display package contents run: | - cachix authtoken ${{ secrets.CACHIX_AUTH_TOKEN }} - cachix use ${{ secrets.CACHIX_CACHE }} - - name: Build all - run: nix build -L - - name: Run fully test suite - run: run-test-suite - - name: Cache development environment + nix build ".#hello-wasm-pkg" + nix develop --command tree ./result + + - name: Build all packages run: | - nix develop --profile ${{ env.REPO }} - cachix push ${{ secrets.CACHIX_CACHE }} ${{ env.REPO }} - - name: Cache build artifacts + for pkg in hello-wasm-pkg hello-wasmedge-exec hello-wasmtime-exec; do + nix build ".#${pkg}" + done + + - name: Run scripts run: | - nix flake archive --json \ - | jq -r '.path,(.inputs|to_entries[].value.path)' \ - | cachix push "${{ secrets.CACHIX_CACHE }}" + nix run ".#hello-wasmedge-exec" + nix run ".#hello-wasmtime-exec" diff --git a/.github/workflows/flakehub-cache.yml b/.github/workflows/flakehub-cache.yml new file mode 100644 index 0000000..bec88a0 --- /dev/null +++ b/.github/workflows/flakehub-cache.yml @@ -0,0 +1,31 @@ +name: Push dev shell to FlakeHub Cache + +on: + push: + branches: [main] + +jobs: + push-dev-shell-to-flakehub-cache: + env: + ACTIONS_STEP_DEBUG: true + runs-on: ${{ matrix.systems.runner }} + permissions: + id-token: "write" + contents: "read" + strategy: + matrix: + systems: + - nix-system: "aarch64-darwin" + runner: "macos-latest-xlarge" + - nix-system: "x86_64-darwin" + runner: "macos-12" + - nix-system: "x86_64-linux" + runner: "ubuntu-22.04" + steps: + - uses: actions/checkout@v3 + - uses: DeterminateSystems/nix-installer-action@main + - uses: DeterminateSystems/magic-nix-cache-action@main + with: + use-flakehub: true + - name: Build dev shell for ${{ matrix.systems.nix-system }} on ${{ matrix.systems.runner }} + run: nix build .#devShells.${{ matrix.systems.nix-system }}.default diff --git a/.github/workflows/flakehub-publish-rolling.yaml b/.github/workflows/flakehub-publish-rolling.yaml new file mode 100644 index 0000000..6a7a8d8 --- /dev/null +++ b/.github/workflows/flakehub-publish-rolling.yaml @@ -0,0 +1,21 @@ +name: "Publish every Git push to main to FlakeHub" + +on: + push: + branches: + - "main" + +jobs: + flakehub-publish: + runs-on: "ubuntu-22.04" + permissions: + id-token: "write" + contents: "read" + steps: + - uses: "actions/checkout@v4" + - uses: "DeterminateSystems/nix-installer-action@v10" + - uses: "DeterminateSystems/flakehub-push@v3" + with: + name: "DeterminateSystems/nix-wasm-example" + rolling: true + visibility: "public" diff --git a/Cargo.lock b/Cargo.lock index 6fea0ef..83949fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,335 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "bitflags" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" - -[[package]] -name = "bitflags" -version = "2.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "487f1e0fcbe47deb8b0574e646def1c903389d95241dd1bbcc6ce4a715dfc0c1" - -[[package]] -name = "cc" -version = "1.0.79" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" - -[[package]] -name = "clap" -version = "4.1.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42dfd32784433290c51d92c438bb72ea5063797fc3cc9a21a8c4346bebbb2098" -dependencies = [ - "bitflags 2.0.2", - "clap_derive", - "clap_lex", - "is-terminal", - "once_cell", - "strsim", - "termcolor", -] - -[[package]] -name = "clap_derive" -version = "4.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fddf67631444a3a3e3e5ac51c36a5e01335302de677bd78759eaa90ab1f46644" -dependencies = [ - "heck", - "proc-macro-error", - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "clap_lex" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "033f6b7a4acb1f358c742aaca805c939ee73b4c6209ae4318ec7aca81c42e646" -dependencies = [ - "os_str_bytes", -] - -[[package]] -name = "errno" -version = "0.2.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1" -dependencies = [ - "errno-dragonfly", - "libc", - "winapi", -] - -[[package]] -name = "errno-dragonfly" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" -dependencies = [ - "cc", - "libc", -] - -[[package]] -name = "heck" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" - [[package]] name = "hello-wasm" version = "0.1.0" -dependencies = [ - "clap", -] - -[[package]] -name = "hermit-abi" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" - -[[package]] -name = "io-lifetimes" -version = "1.0.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09270fd4fa1111bc614ed2246c7ef56239a3063d5be0d1ec3b589c505d400aeb" -dependencies = [ - "hermit-abi", - "libc", - "windows-sys", -] - -[[package]] -name = "is-terminal" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8687c819457e979cc940d09cb16e42a1bf70aa6b60a549de6d3a62a0ee90c69e" -dependencies = [ - "hermit-abi", - "io-lifetimes", - "rustix", - "windows-sys", -] - -[[package]] -name = "libc" -version = "0.2.140" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c" - -[[package]] -name = "linux-raw-sys" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" - -[[package]] -name = "once_cell" -version = "1.17.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" - -[[package]] -name = "os_str_bytes" -version = "6.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ceedf44fb00f2d1984b0bc98102627ce622e083e49a5bacdb3e514fa4238e267" - -[[package]] -name = "proc-macro-error" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" -dependencies = [ - "proc-macro-error-attr", - "proc-macro2", - "quote", - "syn", - "version_check", -] - -[[package]] -name = "proc-macro-error-attr" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" -dependencies = [ - "proc-macro2", - "quote", - "version_check", -] - -[[package]] -name = "proc-macro2" -version = "1.0.53" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba466839c78239c09faf015484e5cc04860f88242cff4d03eb038f04b4699b73" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "rustix" -version = "0.36.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db4165c9963ab29e422d6c26fbc1d37f15bace6b2810221f9d925023480fcf0e" -dependencies = [ - "bitflags 1.3.2", - "errno", - "io-lifetimes", - "libc", - "linux-raw-sys", - "windows-sys", -] - -[[package]] -name = "strsim" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" - -[[package]] -name = "syn" -version = "1.0.109" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "termcolor" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" -dependencies = [ - "winapi-util", -] - -[[package]] -name = "unicode-ident" -version = "1.0.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" - -[[package]] -name = "version_check" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" - -[[package]] -name = "winapi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - -[[package]] -name = "winapi-util" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" -dependencies = [ - "winapi", -] - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - -[[package]] -name = "windows-sys" -version = "0.45.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" -dependencies = [ - "windows-targets", -] - -[[package]] -name = "windows-targets" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" -dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", -] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" - -[[package]] -name = "windows_i686_gnu" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" - -[[package]] -name = "windows_i686_msvc" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" diff --git a/Cargo.toml b/Cargo.toml index aef2bfe..2343528 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,3 @@ name = "hello-wasm" version = "0.1.0" edition = "2021" - -[dependencies] -clap = { version = "4.1.11", features = ["derive"] } diff --git a/README.md b/README.md index a8fa00d..b8e87fb 100644 --- a/README.md +++ b/README.md @@ -19,17 +19,19 @@ nix develop > **Note**: This should happen automatically if you have [direnv] installed and run `direnv allow`. -## Actions +## Packages + +Below are some packages you can build in this project. ### Build Wasm binary To build a Wasm executable from the [Rust sources](./src/main.rs): ```shell -nix build ".#wasm" +nix build ".#wasm-rust" ``` -This generates a Wasm binary at `result/bin/nix-wasm-example.wasm`. +This generates a Wasm binary at `result/bin/hello-wasm.wasm`. ### Build a [stripped] binary @@ -37,15 +39,15 @@ This generates a Wasm binary at `result/bin/nix-wasm-example.wasm`. nix build ".#stripped" ``` -This generates a [stripped] Wasm binary at `result/bin/nix-wasm-example-stripped.wasm`. +This generates a [stripped] Wasm binary at `result/bin/hello-wasm-stripped.wasm`. -### Generate [opcode] usage +### Generate [stats] ```shell -nix build ".#opcode" +nix build ".#stats" ``` -This generates a `.dist` file at `result/share/nix-wasm-example.dist`. +This generates a `.dist` file at `result/share/hello-wasm.dist`. ### Build a WebAssembly text format ([WAT]) file @@ -53,29 +55,33 @@ This generates a `.dist` file at `result/share/nix-wasm-example.dist`. nix build ".#wat" ``` -This generates a human-readable `.wat` file at `result/share/nix-wasm-example.wat`. +This generates a human-readable `.wat` file at `result/share/hello-wasm.wat`. + +### Generate an [object dump][objdump] + +```shell +nix build ".#objdump" +``` + +This generates `.txt` file at `result/share/hello-wasm-dump-txt`. ### Build everything ```shell nix build -# shorthand for: -nix build ".#all" +# shorthand for both of these: +nix build ".#default" +nix build ".#wasm-all" ``` This generates several files in `result`[^1]: -* `bin/nix-wasm-example.wasm` (the raw binary) -* `bin/nix-wasm-example-stripped.wasm` (the [stripped] version of the binary) -* `share/nix-wasm-example.dist` (the [opcode] file) -* `share/nix-wasm-example.wat` (the human-readable [WAT] file) - -### Test - -```shell -run-test-suite -``` +* `bin/hello-wasm.wasm` (the raw binary) +* `bin/hello-wasm-stripped.wasm` (the [stripped] version of the binary) +* `share/hello-wasm-dump.txt` (the ) +* `share/hello-wasm.dist` (the [stats] file) +* `share/hello-wasm.wat` (the human-readable [WAT] file) ### Run @@ -103,8 +109,9 @@ validate-wasm [dni]: https://github.com/DeterminateSystems/nix-installer [flakes]: https://zero-to-nix.com/concepts/flakes [nix]: https://zero-to-nix.com -[opcode]: https://pengowray.github.io/wasm-ops +[objdump]: https://webassembly.github.io/wabt/doc/wasm-objdump.1.html [rust]: https://rust-lang.org +[stats]: https://pengowray.github.io/wasm-ops [store]: https://zero-to-nix.com/concepts/nix-store [stripped]: https://webassembly.github.io/wabt/doc/wasm-strip.1.html [wasm]: https://webassembly.org diff --git a/flake.lock b/flake.lock index 3f4b295..48b06ce 100644 --- a/flake.lock +++ b/flake.lock @@ -1,58 +1,109 @@ { "nodes": { - "flake-utils": { + "fenix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ], + "rust-analyzer-src": "rust-analyzer-src" + }, "locked": { - "lastModified": 1659877975, - "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0", - "type": "github" + "lastModified": 1711952616, + "narHash": "sha256-WJvDdOph001fA1Ap3AyaQtz/afJAe7meSG5uJAdSE+A=", + "rev": "209048d7c545905c470f6f8c05c5061f391031a8", + "revCount": 1822, + "type": "tarball", + "url": "https://api.flakehub.com/f/pinned/nix-community/fenix/0.1.1822%2Brev-209048d7c545905c470f6f8c05c5061f391031a8/018e98ba-d842-7dad-9d6a-0d0ee173b6b1/source.tar.gz" }, "original": { - "owner": "numtide", - "repo": "flake-utils", - "type": "github" + "type": "tarball", + "url": "https://flakehub.com/f/nix-community/fenix/0.1.%2A.tar.gz" } }, - "nixpkgs": { + "flake-compat": { "locked": { - "lastModified": 1679410443, - "narHash": "sha256-xDHO/jixWD+y5pmW5+2q4Z4O/I/nA4MAa30svnZKK+M=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "c9ece0059f42e0ab53ac870104ca4049df41b133", - "type": "github" + "lastModified": 1696426674, + "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", + "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", + "revCount": 57, + "type": "tarball", + "url": "https://api.flakehub.com/f/pinned/edolstra/flake-compat/1.0.1/018afb31-abd1-7bff-a5e4-cff7e18efb7a/source.tar.gz" }, "original": { - "id": "nixpkgs", - "type": "indirect" + "type": "tarball", + "url": "https://flakehub.com/f/edolstra/flake-compat/%2A.tar.gz" } }, - "root": { - "inputs": { - "nixpkgs": "nixpkgs", - "rust-overlay": "rust-overlay" + "flake-schemas": { + "locked": { + "lastModified": 1697467827, + "narHash": "sha256-j8SR19V1SRysyJwpOBF4TLuAvAjF5t+gMiboN4gYQDU=", + "rev": "764932025c817d4e500a8d2a4d8c565563923d29", + "revCount": 29, + "type": "tarball", + "url": "https://api.flakehub.com/f/pinned/DeterminateSystems/flake-schemas/0.1.2/018b3da8-4cc3-7fbb-8ff7-1588413c53e2/source.tar.gz" + }, + "original": { + "type": "tarball", + "url": "https://flakehub.com/f/DeterminateSystems/flake-schemas/%2A.tar.gz" } }, - "rust-overlay": { + "naersk": { "inputs": { - "flake-utils": "flake-utils", "nixpkgs": [ "nixpkgs" ] }, "locked": { - "lastModified": 1679451618, - "narHash": "sha256-gWFYRgmeT+8xDYHK4HSuCY9Pi7mSxC+2illHrmDkG7A=", - "owner": "oxalica", - "repo": "rust-overlay", - "rev": "a89d328ca7d106c3fdbbd072b6c7088ab5b798a3", + "lastModified": 1713520724, + "narHash": "sha256-CO8MmVDmqZX2FovL75pu5BvwhW+Vugc7Q6ze7Hj8heI=", + "rev": "c5037590290c6c7dae2e42e7da1e247e54ed2d49", + "revCount": 335, + "type": "tarball", + "url": "https://api.flakehub.com/f/pinned/nix-community/naersk/0.1.335%2Brev-c5037590290c6c7dae2e42e7da1e247e54ed2d49/018ef76d-0fd8-71be-8bed-58aa46d5dc3a/source.tar.gz" + }, + "original": { + "type": "tarball", + "url": "https://flakehub.com/f/nix-community/naersk/0.1.%2A.tar.gz" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1714253743, + "narHash": "sha256-mdTQw2XlariysyScCv2tTE45QSU9v/ezLcHJ22f0Nxc=", + "rev": "58a1abdbae3217ca6b702f03d3b35125d88a2994", + "revCount": 617818, + "type": "tarball", + "url": "https://api.flakehub.com/f/pinned/NixOS/nixpkgs/0.1.617818%2Brev-58a1abdbae3217ca6b702f03d3b35125d88a2994/018f25c6-e917-75c9-967b-ebda3b3dc4ae/source.tar.gz" + }, + "original": { + "type": "tarball", + "url": "https://flakehub.com/f/NixOS/nixpkgs/0.1.%2A.tar.gz" + } + }, + "root": { + "inputs": { + "fenix": "fenix", + "flake-compat": "flake-compat", + "flake-schemas": "flake-schemas", + "naersk": "naersk", + "nixpkgs": "nixpkgs" + } + }, + "rust-analyzer-src": { + "flake": false, + "locked": { + "lastModified": 1711885694, + "narHash": "sha256-dyezzeSbWMpflma+E9USmvSxuLgGcNGcGw3cOnX36ko=", + "owner": "rust-lang", + "repo": "rust-analyzer", + "rev": "e4a405f877efd820bef9c0e77a02494e47c17512", "type": "github" }, "original": { - "owner": "oxalica", - "repo": "rust-overlay", + "owner": "rust-lang", + "ref": "nightly", + "repo": "rust-analyzer", "type": "github" } } diff --git a/flake.nix b/flake.nix index ab34752..8fc6110 100644 --- a/flake.nix +++ b/flake.nix @@ -2,193 +2,179 @@ description = "Nix + WebAssembly example project"; inputs = { - nixpkgs.url = "nixpkgs"; - rust-overlay = { - url = "github:oxalica/rust-overlay"; + nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.1.*.tar.gz"; + fenix = { + url = "https://flakehub.com/f/nix-community/fenix/0.1.*.tar.gz"; inputs.nixpkgs.follows = "nixpkgs"; }; + naersk = { + url = "https://flakehub.com/f/nix-community/naersk/0.1.*.tar.gz"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + flake-compat.url = "https://flakehub.com/f/edolstra/flake-compat/*.tar.gz"; + flake-schemas.url = "https://flakehub.com/f/DeterminateSystems/flake-schemas/*.tar.gz"; }; - outputs = - { self - , nixpkgs - , rust-overlay - }: + outputs = { self, ... }@inputs: let pkgName = (self.lib.fromToml ./Cargo.toml).package.name; - - overlays = [ - # Provides a `rust-bin` attribute I can use to build a custom Rust toolchain - rust-overlay.overlays.default - (final: prev: rec { - # Builds a Rust toolchain from rust-toolchain.toml - rustToolchain = prev.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; - # Uses the Rust toolchain above to construct a special build function - buildRustWasiWasm = self.lib.buildRustWasiWasm final; - }) - ]; - supportedSystems = [ - "aarch64-darwin" - "aarch64-linux" - "x86_64-darwin" - "x86_64-linux" - ]; - forAllSystems = f: nixpkgs.lib.genAttrs supportedSystems (system: f { - pkgs = import nixpkgs { inherit overlays system; }; + supportedSystems = [ "aarch64-darwin" "aarch64-linux" "x86_64-darwin" "x86_64-linux" ]; + forAllSystems = f: inputs.nixpkgs.lib.genAttrs supportedSystems (system: f { + pkgs = import inputs.nixpkgs { inherit system; overlays = [ self.overlays.default ]; }; inherit system; }); + rustWasmTarget = "wasm32-wasi"; in { + overlays.default = final: prev: rec { + system = final.stdenv.hostPlatform.system; + + # Builds a Rust toolchain from rust-toolchain.toml + rustToolchain = with inputs.fenix.packages.${system}; + combine [ + latest.rustc + latest.cargo + targets.${rustWasmTarget}.latest.rust-std + ]; + + buildRustWasiWasm = self.lib.buildRustWasiWasm final; + buildRustWasmPackage = self.lib.buildRustWasmPackage final; + buildRustWasmScript = self.lib.buildRustWasmScript final; + buildRustWasmEdgeExec = self.lib.buildRustWasmEdgeExec final; + buildRustWasmtimeExec = self.lib.buildRustWasmtimeExec final; + }; + + # Development environments devShells = forAllSystems ({ pkgs, system }: { default = let - checks = import ./nix/checks.nix { - inherit pkgName pkgs; - inherit (self.packages.${system}) stripped wasm; - }; helpers = with pkgs; [ direnv jq ]; in pkgs.mkShell { - packages = helpers ++ checks ++ (with pkgs; [ + packages = helpers ++ (with pkgs; [ rustToolchain # cargo, etc. - cachix # Binary caching wabt # WebAssembly Binary Toolkit wasmedge # Wasm runtime wasmtime # Wasm runtime cargo-edit # cargo add, cargo rm, etc. + tree # for visualizing results ]); }; }); - packages = forAllSystems ({ pkgs, system }: - let - wasmPkgs = self.packages.${system}; - in - rec { - default = all; - - all = - pkgs.stdenv.mkDerivation { - name = "wasm-all"; - src = ./.; - installPhase = '' - mkdir -p $out/lib $out/share - cp ${wasmPkgs.wasm}/lib/${pkgName}.wasm $out/lib - cp ${wasmPkgs.stripped}/lib/${pkgName}-stripped.wasm $out/lib - cp ${wasmPkgs.wat}/share/${pkgName}.wat $out/share - cp ${wasmPkgs.opcode}/share/${pkgName}.dist $out/share - cp ${wasmPkgs.objdump}/share/${pkgName}-dump.txt $out/share - ''; - }; - - hello-wasm = pkgs.stdenv.mkDerivation rec { - name = "hello-wasm"; - nativeBuildInputs = with pkgs; [ makeWrapper ]; - src = ./.; - installPhase = '' - mkdir -p $out/bin $out/lib - cp ${wasmPkgs.wasm}/lib/${pkgName}.wasm $out/lib - makeWrapper ${pkgs.wasmtime}/bin/wasmtime $out/bin/${name} \ - --add-flags "$out/lib/${pkgName}.wasm" \ - --add-flags "--" - ''; - }; - - hello-wasm-edge = pkgs.stdenv.mkDerivation rec { - name = "hello-wasm-edge"; - nativeBuildInputs = with pkgs; [ makeWrapper ]; - src = ./.; - installPhase = '' - mkdir -p $out/bin $out/lib - cp ${wasmPkgs.wasm}/lib/${pkgName}.wasm $out/lib - makeWrapper ${pkgs.wasmedge}/bin/wasmedge $out/bin/${name} \ - --add-flags "$out/lib/${pkgName}.wasm" - ''; - }; + packages = forAllSystems ({ pkgs, system }: rec { + default = hello-wasm-pkg; + hello-wasm-pkg = pkgs.buildRustWasmPackage { }; + hello-wasmtime-exec = pkgs.buildRustWasmtimeExec { }; + hello-wasmedge-exec = pkgs.buildRustWasmEdgeExec { }; + }); - # Generate Wasm binary using Rust - wasm = pkgs.buildRustWasiWasm { - name = pkgName; - src = ./.; - cargoLock.lockFile = ./Cargo.lock; - }; + lib = { + # Helper function for reading TOML files + fromToml = file: builtins.fromTOML (builtins.readFile file); - # Generate WAT file (WebAssembly Text Format) - objdump = pkgs.stdenv.mkDerivation { - name = "wasm-into-objdump"; - src = ./.; - buildInputs = with pkgs; [ wabt ]; - buildPhase = '' - wasm-objdump \ - --details ${wasmPkgs.wasm}/lib/${pkgName}.wasm > ${pkgName}-dump.txt - ''; - installPhase = '' - mkdir -p $out/share - cp ${pkgName}-dump.txt $out/share - ''; + handleArgs = + { name ? null + , src ? self + , cargoToml ? ./Cargo.toml + }: + let + meta = (self.lib.fromToml ./Cargo.toml).package; + pkgName = if name == null then meta.name else name; + pkgSrc = builtins.path { path = src; name = "${pkgName}-source"; }; + in + { + inherit (meta) name; + inherit pkgName; + src = pkgSrc; + inherit cargoToml; }; - wat = pkgs.stdenv.mkDerivation { - name = "wasm-into-wat"; - src = ./.; + buildRustWasiWasm = pkgs: { name, src }: + let + naerskLib = pkgs.callPackage inputs.naersk { + cargo = pkgs.rustToolchain; + rustc = pkgs.rustToolchain; + }; + in + naerskLib.buildPackage { + inherit name src; + CARGO_BUILD_TARGET = rustWasmTarget; buildInputs = with pkgs; [ wabt ]; - buildPhase = '' - wasm2wat ${wasmPkgs.wasm}/lib/${pkgName}.wasm > ${pkgName}.wat - ''; - installPhase = '' - mkdir -p $out/share - cp ${pkgName}.wat $out/share + postInstall = '' + wasm-validate $out/bin/${name}.wasm ''; }; - opcode = pkgs.stdenv.mkDerivation { - name = "wasm-into-opcode-count"; - src = ./.; - buildInputs = with pkgs; [ wabt ]; - buildPhase = '' - wasm-opcodecnt ${wasmPkgs.wasm}/lib/${pkgName}.wasm -o ${pkgName}.dist - ''; + buildRustWasmtimeExec = pkgs: args: + let + finalArgs = self.lib.handleArgs args; + wasmPkg = self.lib.buildRustWasiWasm pkgs { + inherit (finalArgs) name src; + }; + in + pkgs.stdenv.mkDerivation rec { + name = finalArgs.name; + src = finalArgs.src; + nativeBuildInputs = with pkgs; [ makeWrapper ]; + # TODO: bring in accordance with the new Wasmtime interface (WASMTIME_NEW_CLI=1) installPhase = '' - mkdir -p $out/share - cp ${pkgName}.dist $out/share + mkdir -p $out/lib + cp ${wasmPkg}/bin/${finalArgs.name}.wasm $out/lib/${finalArgs.pkgName}.wasm + makeWrapper ${pkgs.wasmtime}/bin/wasmtime $out/bin/${finalArgs.pkgName} \ + --set WASMTIME_NEW_CLI 0 \ + --add-flags "$out/lib/${finalArgs.pkgName}.wasm" \ + --add-flags "--" ''; }; - stripped = pkgs.stdenv.mkDerivation { - name = "wasm-stripped"; - src = ./.; - buildInputs = with pkgs; [ wabt ]; - buildPhase = '' - wasm-strip ${wasmPkgs.wasm}/lib/${pkgName}.wasm -o ${pkgName}-stripped.wasm - ''; + buildRustWasmEdgeExec = pkgs: args: + let + finalArgs = self.lib.handleArgs args; + wasmPkg = self.lib.buildRustWasiWasm pkgs { + inherit (finalArgs) name src; + }; + in + pkgs.stdenv.mkDerivation rec { + name = finalArgs.name; + src = finalArgs.src; + nativeBuildInputs = with pkgs; [ makeWrapper ]; installPhase = '' mkdir -p $out/lib - cp ${pkgName}-stripped.wasm $out/lib + cp ${wasmPkg}/bin/${finalArgs.name}.wasm $out/lib/${finalArgs.pkgName}.wasm + makeWrapper ${pkgs.wasmedge}/bin/wasmedge $out/bin/${finalArgs.pkgName} \ + --add-flags "$out/lib/${finalArgs.pkgName}.wasm" ''; }; - }); - - lib = { - # Helper function for reading TOML files - fromToml = file: builtins.fromTOML (builtins.readFile file); - buildRustWasiWasm = pkgs: { name, src, cargoLock }: + buildRustWasmPackage = pkgs: args: let - rustPlatform = pkgs.makeRustPlatform { - cargo = pkgs.rustToolchain; - rustc = pkgs.rustToolchain; + finalArgs = self.lib.handleArgs args; + wasmPkg = self.lib.buildRustWasiWasm pkgs { + inherit (finalArgs) name src; }; - target = "wasm32-wasi"; in - rustPlatform.buildRustPackage { - inherit cargoLock name src; + pkgs.stdenv.mkDerivation { + name = finalArgs.name; + src = finalArgs.src; + buildInputs = with pkgs; [ + # includes wasm-strip, wasm2wat, wasm-stats, wasm-objdump, and wasm-validate + wabt + ]; buildPhase = '' - cargo build --release --target ${target} + mkdir -p $out/{lib,share} + cp ${wasmPkg}/bin/${finalArgs.name}.wasm $out/lib/${finalArgs.pkgName}.wasm + wasm-strip $out/lib/${finalArgs.pkgName}.wasm -o $out/lib/${finalArgs.pkgName}-stripped.wasm + wasm2wat $out/lib/${finalArgs.pkgName}.wasm > $out/share/${finalArgs.pkgName}.wat + wasm-stats $out/lib/${finalArgs.pkgName}.wasm -o $out/share/${finalArgs.pkgName}.dist + wasm-objdump \ + --details $out/lib/${finalArgs.pkgName}.wasm > $out/share/${finalArgs.pkgName}-dump.txt ''; - installPhase = '' - mkdir -p $out/lib - cp target/${target}/release/${pkgName}.wasm $out/lib + checkPhase = '' + wasm-validate $out/lib/${finalArgs.pkgName}.wasm + wasm-validate $out/lib/${finalArgs.pkgName}-stripped.wasm ''; + doCheck = true; }; }; }; diff --git a/nix/checks.nix b/nix/checks.nix deleted file mode 100644 index 366bade..0000000 --- a/nix/checks.nix +++ /dev/null @@ -1,37 +0,0 @@ -{ pkgName -, pkgs -, stripped -, wasm -}: - -let - wasmValidate = "${pkgs.wabt}/bin/wasm-validate"; - wasmtime = "${pkgs.wasmtime}/bin/wasmtime"; - wasmFile = "${wasm}/lib/${pkgName}.wasm"; - strippedWasmFile = "${stripped}/lib/${pkgName}-stripped.wasm"; - - # Make basic script - mkBin = name: text: pkgs.writeShellApplication { inherit name text; }; - - # Make CLI tool (pass in CLI args) - mkCli = name: text: pkgs.writeShellApplication { inherit name; text = ''${text} "''${@}"''; }; -in -[ - # Ensure that the binary can be run - (mkCli "run-wasm" "${wasmtime} ${wasmFile}") - - # Ensure that the stripped version of the binary can be run - (mkCli "run-wasm-stripped" "${wasmtime} ${strippedWasmFile}") - - # Ensure that the binary is valid - (mkBin "validate-wasm" "${wasmValidate} ${wasmFile}") - - (mkBin "validate-stripped-wasm" "${wasmValidate} ${strippedWasmFile}") - - (mkBin "run-test-suite" '' - validate-wasm - validate-stripped-wasm - run-wasm -- --name "Nix enthusiast" - run-wasm-stripped -- --name "Nix enthusiast" - '') -] diff --git a/rust-toolchain.toml b/rust-toolchain.toml deleted file mode 100644 index 0f49977..0000000 --- a/rust-toolchain.toml +++ /dev/null @@ -1,4 +0,0 @@ -[toolchain] -channel = "nightly-2023-03-06" -targets = ["wasm32-wasi"] -profile = "minimal" diff --git a/src/main.rs b/src/main.rs index ccd218b..cf1e5a2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,3 @@ -use clap::Parser; - -#[derive(Parser, Debug)] -#[command(author, version, about, long_about = None)] -struct Args { - #[arg(short, long)] - name: String, -} - fn main() { - let args = Args::parse(); - let name = args.name; - println!("Hello there, {name}!"); + println!("Hello there, Nix enthusiast!"); }