Skip to content

Commit

Permalink
Merge pull request #184 from mlabs-haskell/szg251/simplified-rust-flake
Browse files Browse the repository at this point in the history
Use simplified rustFlake + handle lb package versions
  • Loading branch information
szg251 authored Feb 5, 2024
2 parents 7dee7ea + dfca33b commit 063055a
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 232 deletions.
3 changes: 1 addition & 2 deletions extras/build.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

# Makes a per system `lbf-nix` option.
perSystem = flake-parts-lib.mkPerSystemOption
({ pkgs, config, pkgsForCtl, pkgsForHaskellNix, pkgsForRust, ... }: {
({ pkgs, config, pkgsForCtl, pkgsForHaskellNix, ... }: {

options.lbf-nix = lib.mkOption {
type = lib.types.anything;
Expand All @@ -43,7 +43,6 @@

purescriptFlake = import ./flake-purescript.nix pkgsForCtl;

rustFlake = import ./flake-rust.nix pkgsForRust;
haskellData = import ./haskell-data.nix pkgs;
haskellFlake = import ./flake-haskell.nix pkgsForHaskellNix;
haskellPlutusFlake = import ./flake-haskell-plutus.nix inputs.cardano-haskell-packages pkgsForHaskellNix;
Expand Down
109 changes: 0 additions & 109 deletions extras/flake-rust.nix

This file was deleted.

54 changes: 31 additions & 23 deletions extras/lbf-nix/lbf-rust.nix
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ let
, # Version of the package and also the version of the Cargo crate.
# Examples: version = "0.1.0.0"
version ? "0.1.0"
}: { inherit src imports files classes dependencies configs name version; };
, # Version of dependencies
# A package will only be added to Cargo.toml, if the generated code directly depends on it
# Defaults to version 0.1.0 for all packages
extraVersions ? { }
}: { inherit src imports files classes dependencies configs name version extraVersions; };

lbf-build = import ./lbf-build.nix pkgs lbf;

Expand Down Expand Up @@ -66,29 +70,27 @@ let
};

cargoTemplate = opts: with (lbfRustOpts opts);
pkgs.writeTextFile {
name = "lambda-buffers-cargo-template";
text = ''
[package]
name = "${name}"
version = "${version}"
edition = "2021"
[dependencies]
'';
};
pkgs.writers.writeJSON "lambda-buffers-cargo-template"
{
package = {
inherit name version;
edition = "2021";
};
};

# This is a lookup table of default crate versions used by lamba-buffers modules
# Based on the contents of `build.json` a subset of these will be attached to the
# Cargo.toml file
crateVersions = pkgs.writeTextFile {
name = "lambda-buffers-crate-versions";
text = ''
num-bigint = "0.4.4"
serde_json = { version = "1.0.107", features = ["arbitrary_precision"] }
plutus-ledger-api = { git = "https://github.com/mlabs-haskell/plutus-ledger-api-rust", features = [ "lbf", ], rev = "d66d39c949f59e543c91ee36377c93422d8e9d75" }
'';
};
# Note: lbr-prelude and and plutus prelude versions are pinned here, but can be overridden with `extraVersions`
versionTable =
{
num-bigint = "~0.4";
serde_json = { version = "^1.0"; features = [ "arbitrary_precision" ]; };
plutus-ledger-api = { git = "https://github.com/mlabs-haskell/plutus-ledger-api-rust"; features = [ "lbf" ]; rev = "d66d39c949f59e543c91ee36377c93422d8e9d75"; };
};

crateVersions = opts: with (lbfRustOpts opts);
pkgs.writers.writeJSON "lambda-buffers-crate-versions" (versionTable // extraVersions);

build = opts: with (lbfRustOpts opts);
let
Expand All @@ -100,6 +102,7 @@ let
outputs = [ "out" "buildjson" ];
buildInputs = [
pkgs.jq
pkgs.yj
];
buildPhase = ''
ln -s ${lbfBuilt} autogen;
Expand All @@ -109,22 +112,27 @@ let
# Generating Cargo manifest file
DEPS=$(echo ${builtins.concatStringsSep " " dependencies} $(cat build.json | jq -r ".[]" | sort -u));
echo "Gathered Cargo deps $DEPS";
cat ${cargoTemplate opts} > Cargo.toml;
cat ${cargoTemplate opts} > Cargo.json;
# Using the lookup table `crateVersions`, filling in the library version.
# If no version is found, we default to a local path dependency, pointing to
# a sibling directory (directory in extra-sources or .extras)
# e.g.: for `lbr-prelude` we print `lbr-prelude = { path = "../lbr-prelude" }
for DEP in $DEPS; do
if [ $DEP != "std" ]; then
echo "$(cat ${crateVersions} | grep "$DEP" || echo "$DEP = { path = \"../$DEP\" }")" >> Cargo.toml
VER=$(cat ${crateVersions opts} | jq ".\"$DEP\"" -c);
if [ $VER == "null" ]; then
VER="{\"path\": \"../$DEP-0.1.0\"}"
fi
cat Cargo.json | jq ".dependencies+={\"$DEP\":$VER}" > tmp.json;
mv tmp.json Cargo.json
fi
done
cat Cargo.json | yj -jt > Cargo.toml;
'';

installPhase = ''
cp build.json $buildjson;
echo "Dependencies collected"
cat $buildjson;
mkdir -p $out/src;
cp -r autogen/* $out/src
Expand Down
14 changes: 7 additions & 7 deletions runtimes/rust/lbr-prelude-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ repository = "https://github.com/mlabs-haskell/lambda-buffers"
proc-macro = true

[dependencies]
proc-macro2 = { version = "1.0.66", default-features = false }
quote = "1.0.33"
syn = { version = "2.0.38", features = ["extra-traits"] }
trybuild = { version = "1.0.49", features = ["diff"] }
proc-macro2 = { version = "^1.0.66", default-features = false }
quote = "^1.0.33"
syn = { version = "^2.0.38", features = ["extra-traits"] }
trybuild = { version = "^1.0.49", features = ["diff"] }

[dev-dependencies]
lbr-prelude = { path = ".extras/lbr-prelude", default-features = false }
num-bigint = "0.4.4"
serde_json = "1.0.107"
lbr-prelude = { path = ".extras/lbr-prelude-0.1.0", default-features = false }
num-bigint = "~0.4"
serde_json = "^1.0"
9 changes: 3 additions & 6 deletions runtimes/rust/lbr-prelude-derive/build.nix
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
{ inputs, ... }: {
perSystem = { config, ... }:
perSystem = { config, system, ... }:
let
rustFlake =
config.lbf-nix.rustFlake {
inputs.flake-lang.lib.${system}.rustFlake {
src = ./.;
inherit (inputs) crane;
crateName = "lbr-prelude-derive";
extraSources = [
{
name = "lbr-prelude";
path = config.packages.lbr-prelude-rust-src;
}
config.packages.lbr-prelude-rust-src
];
devShellHook = config.settings.shell.hook;
};
Expand Down
14 changes: 7 additions & 7 deletions runtimes/rust/lbr-prelude/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ description = "LambdaBuffers runtime library for the Prelude schema."
repository = "https://github.com/mlabs-haskell/lambda-buffers"

[dependencies]
data-encoding = "2.4.0"
num-bigint = "0.4.4"
proptest = "1.3.1"
serde = "1.0.188"
serde_json = { version = "1.0.107", features = ["arbitrary_precision"] }
thiserror = "1.0.49"
lbr-prelude-derive = { version = "0.1.0", path = ".extras/lbr-prelude-derive", optional = true }
data-encoding = "^2.4.0"
num-bigint = "~0.4"
proptest = "^1.3.1"
serde = "^1.0.188"
serde_json = { version = "^1.0", features = ["arbitrary_precision"] }
thiserror = "^1.0.49"
lbr-prelude-derive = { version = "0.1.0", path = ".extras/lbr-prelude-derive-0.1.0", optional = true }

[features]
default = ["derive"]
Expand Down
9 changes: 3 additions & 6 deletions runtimes/rust/lbr-prelude/build.nix
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
{ inputs, ... }: {
perSystem = { config, ... }:
perSystem = { config, system, ... }:
let
rustFlake =
config.lbf-nix.rustFlake {
inputs.flake-lang.lib.${system}.rustFlake {
src = ./.;
inherit (inputs) crane;
crateName = "lbr-prelude";
extraSources = [
{
name = "lbr-prelude-derive";
path = config.packages.lbr-prelude-derive-rust-src;
}
config.packages.lbr-prelude-derive-rust-src
];
devShellHook = config.settings.shell.hook;
};
Expand Down
6 changes: 3 additions & 3 deletions testsuites/lbt-plutus/lbt-plutus-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ edition = "2021"
plutus-ledger-api = { git = "https://github.com/mlabs-haskell/plutus-ledger-api-rust", features = [
"lbf",
], rev = "d66d39c949f59e543c91ee36377c93422d8e9d75" }
lbf-plutus-rust-golden-api = { path = ".extras/lbf-plutus-rust-golden-api" }
lbf-prelude = { path = ".extras/lbf-prelude" }
lbr-prelude = { path = ".extras/lbr-prelude" }
lbf-plutus-rust-golden-api = { path = ".extras/lbf-plutus-rust-golden-api-0.1.0" }
lbf-prelude = { path = ".extras/lbf-prelude-0.1.0" }
lbr-prelude = { path = ".extras/lbr-prelude-0.1.0" }
serde_json = "1.0.108"
num-bigint = "0.4.4"
56 changes: 21 additions & 35 deletions testsuites/lbt-plutus/lbt-plutus-rust/build.nix
Original file line number Diff line number Diff line change
@@ -1,43 +1,29 @@
{ inputs, ... }: {
perSystem = { config, ... }:
perSystem = { config, system, ... }:

let
rustFlake = config.lbf-nix.rustFlake {
src = ./.;
inherit (inputs) crane;
crateName = "lbt-plutus";
rustFlake =
inputs.flake-lang.lib.${system}.rustFlake {
src = ./.;
inherit (inputs) crane;
crateName = "lbt-plutus";

extraSources = [
{
name = "lbf-plutus-rust-golden-api";
path = config.packages.lbf-plutus-golden-api-rust;
}
{
name = "lbf-prelude";
path = config.packages.lbf-prelude-rust;
}
{
name = "lbf-plutus";
path = config.packages.lbf-plutus-rust;
}
{
name = "lbr-prelude";
path = config.packages.lbr-prelude-rust-src;
}
{
name = "lbr-prelude-derive";
path = config.packages.lbr-prelude-derive-rust-src;
}
];
data = [
{
name = "lbt-plutus-golden-data";
path = config.packages.lbt-plutus-golden-rust;
}
];
devShellHook = config.settings.shell.hook;
extraSources = [
config.packages.lbf-plutus-golden-api-rust
config.packages.lbf-prelude-rust
config.packages.lbf-plutus-rust
config.packages.lbr-prelude-rust-src
config.packages.lbr-prelude-derive-rust-src
];
data = [
{
name = "lbt-plutus-golden-data";
path = config.packages.lbt-plutus-golden-rust;
}
];
devShellHook = config.settings.shell.hook;

};
};
in
{

Expand Down
6 changes: 3 additions & 3 deletions testsuites/lbt-prelude/lbt-prelude-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ version = "0.1.0"
edition = "2021"

[dependencies]
lbf-prelude-golden-api = { path = ".extras/lbf-prelude-golden-api" }
lbf-prelude = { path = ".extras/lbf-prelude" }
lbr-prelude = { path = ".extras/lbr-prelude" }
lbf-prelude-golden-api = { path = ".extras/lbf-prelude-golden-api-0.1.0" }
lbf-prelude = { path = ".extras/lbf-prelude-0.1.0" }
lbr-prelude = { path = ".extras/lbr-prelude-0.1.0" }
serde_json = "1.0.108"
num-bigint = "0.4.4"
Loading

0 comments on commit 063055a

Please sign in to comment.