Skip to content

Commit

Permalink
dummy-contract: factor out nix, build with recommended toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
marijanp committed Sep 6, 2024
1 parent 5645404 commit b7d2292
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 32 deletions.
51 changes: 51 additions & 0 deletions dummy-contract/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{ inputs, ... }:
{
perSystem = { self', inputs', pkgs, lib, ... }:
let
# nightly-2023-03-25: https://github.com/casper-network/casper-node/blob/release-2.0.0-rc4/smart_contracts/rust-toolchain
toolchainAttrs = { channel = "nightly"; date = "2023-03-25"; sha256 = "sha256-vWMW7tpbU/KseRztVYQ6CukrQWJgPgtlFuB6OPoZ/v8="; };
rustToolchain = with inputs'.fenix.packages; combine [
(toolchainOf toolchainAttrs).toolchain
(targets.wasm32-unknown-unknown.toolchainOf toolchainAttrs).rust-std
];
craneLib = (inputs.crane.mkLib pkgs).overrideToolchain rustToolchain;

contractAttrs = {
pname = "dummy-contract";
src = lib.cleanSourceWith {
src = lib.fileset.toSource {
root = ./.;
fileset = lib.fileset.unions [
./Cargo.toml
./Cargo.lock
./src
];
};
};
cargoExtraArgs = "--target wasm32-unknown-unknown";
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_LINKER = "lld";
nativeBuildInputs = [ pkgs.binaryen pkgs.llvmPackages_16.bintools ];
doCheck = false;
# optimize wasm
postInstall = ''
directory="$out/bin/"
for file in "$directory"*.wasm; do
if [ -e "$file" ]; then
wasm-opt -Oz --strip-debug --signext-lowering "$file"
fi
done
'';
};
in
{
devShells.contract = pkgs.mkShell {
inputsFrom = [ self'.packages.dummy-contract ];
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_LINKER = "lld";
};
packages = {
# Used for testing purposes
dummy-contract = craneLib.buildPackage contractAttrs;
};
};
flake = { };
}
34 changes: 2 additions & 32 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@
imports = [
treefmt-nix.flakeModule
./nixos
./dummy-contract
];
perSystem = { self', inputs', pkgs, lib, ... }:
let
rustToolchain = with inputs'.fenix.packages; combine [
stable.toolchain
targets.wasm32-unknown-unknown.stable.rust-std
];
rustToolchain = inputs'.fenix.packages.stable.toolchain;
craneLib = (inputs.crane.mkLib pkgs).overrideToolchain rustToolchain;

cctl = inputs'.cctl.packages.cctl;
Expand Down Expand Up @@ -80,31 +78,6 @@
cctl
];
};

contractAttrs = {
pname = "dummy-contract";
src = lib.cleanSourceWith {
src = lib.fileset.toSource {
root = ./dummy-contract;
fileset = lib.fileset.unions [
./dummy-contract
];
};
};
cargoExtraArgs = "--target wasm32-unknown-unknown";
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_LINKER = "lld";
nativeBuildInputs = [ pkgs.binaryen pkgs.lld ];
doCheck = false;
# optimize wasm
postInstall = ''
directory="$out/bin/"
for file in "$directory"*.wasm; do
if [ -e "$file" ]; then
wasm-opt -Oz --strip-debug --signext-lowering "$file"
fi
done
'';
};
in
{
devShells.default = pkgs.mkShell {
Expand All @@ -113,9 +86,6 @@
};

packages = {
# Used for testing purposes
dummy-contract = craneLib.buildPackage contractAttrs;

cctl-rs-deps = craneLib.buildDepsOnly cctlAttrs;

cctl-rs-docs = craneLib.cargoDoc (cctlAttrs // {
Expand Down
Binary file modified test-resources/dummy-contract.wasm
Binary file not shown.

0 comments on commit b7d2292

Please sign in to comment.