3030 , # Version of the package and also the version of the Cargo crate.
3131 # Examples: version = "0.1.0.0"
3232 version ? "0.1.0"
33- } : { inherit src imports files classes dependencies configs name version ; } ;
33+ , # Version of dependencies
34+ # A package will only be added to Cargo.toml, if the generated code directly depends on it
35+ # Defaults to version 0.1.0 for all packages
36+ extraVersions ? { }
37+ } : { inherit src imports files classes dependencies configs name version extraVersions ; } ;
3438
3539 lbf-build = import ./lbf-build.nix pkgs lbf ;
3640
6670 } ;
6771
6872 cargoTemplate = opts : with ( lbfRustOpts opts ) ;
69- pkgs . writeTextFile {
70- name = "lambda-buffers-cargo-template" ;
71- text = ''
72- [package]
73- name = "${ name } "
74- version = "${ version } "
75- edition = "2021"
76-
77- [dependencies]
78- '' ;
79- } ;
73+ pkgs . writers . writeJSON "lambda-buffers-cargo-template"
74+ {
75+ package = {
76+ inherit name version ;
77+ edition = "2021" ;
78+ } ;
79+ } ;
8080
8181 # This is a lookup table of default crate versions used by lamba-buffers modules
8282 # Based on the contents of `build.json` a subset of these will be attached to the
8383 # Cargo.toml file
84- crateVersions = pkgs . writeTextFile {
85- name = "lambda-buffers-crate-versions" ;
86- text = ''
87- num-bigint = "0.4.4"
88- serde_json = { version = "1.0.107", features = ["arbitrary_precision"] }
89- plutus-ledger-api = { git = "https://github.com/mlabs-haskell/plutus-ledger-api-rust", features = [ "lbf", ], rev = "d66d39c949f59e543c91ee36377c93422d8e9d75" }
90- '' ;
91- } ;
84+ # Note: lbr-prelude and and plutus prelude versions are pinned here, but can be overridden with `extraVersions`
85+ versionTable =
86+ {
87+ num-bigint = "~0.4" ;
88+ serde_json = { version = "^1.0" ; features = [ "arbitrary_precision" ] ; } ;
89+ plutus-ledger-api = { git = "https://github.com/mlabs-haskell/plutus-ledger-api-rust" ; features = [ "lbf" ] ; rev = "d66d39c949f59e543c91ee36377c93422d8e9d75" ; } ;
90+ } ;
91+
92+ crateVersions = opts : with ( lbfRustOpts opts ) ;
93+ pkgs . writers . writeJSON "lambda-buffers-crate-versions" ( versionTable // extraVersions ) ;
9294
9395 build = opts : with ( lbfRustOpts opts ) ;
9496 let
100102 outputs = [ "out" "buildjson" ] ;
101103 buildInputs = [
102104 pkgs . jq
105+ pkgs . yj
103106 ] ;
104107 buildPhase = ''
105108 ln -s ${ lbfBuilt } autogen;
@@ -109,22 +112,27 @@ let
109112 # Generating Cargo manifest file
110113 DEPS=$(echo ${ builtins . concatStringsSep " " dependencies } $(cat build.json | jq -r ".[]" | sort -u));
111114 echo "Gathered Cargo deps $DEPS";
112- cat ${ cargoTemplate opts } > Cargo.toml ;
115+ cat ${ cargoTemplate opts } > Cargo.json ;
113116 # Using the lookup table `crateVersions`, filling in the library version.
114117 # If no version is found, we default to a local path dependency, pointing to
115118 # a sibling directory (directory in extra-sources or .extras)
116119 # e.g.: for `lbr-prelude` we print `lbr-prelude = { path = "../lbr-prelude" }
117120 for DEP in $DEPS; do
118121 if [ $DEP != "std" ]; then
119- echo "$(cat ${ crateVersions } | grep "$DEP" || echo "$DEP = { path = \"../$DEP\" }")" >> Cargo.toml
122+ VER=$(cat ${ crateVersions opts } | jq ".\"$DEP\"" -c);
123+ if [ $VER == "null" ]; then
124+ VER="{\"path\": \"../$DEP-0.1.0\"}"
125+ fi
126+ cat Cargo.json | jq ".dependencies+={\"$DEP\":$VER}" > tmp.json;
127+ mv tmp.json Cargo.json
120128 fi
121129 done
130+ cat Cargo.json | yj -jt > Cargo.toml;
122131 '' ;
123132
124133 installPhase = ''
125134 cp build.json $buildjson;
126135 echo "Dependencies collected"
127- cat $buildjson;
128136
129137 mkdir -p $out/src;
130138 cp -r autogen/* $out/src
0 commit comments