Skip to content

Commit 19e4e9a

Browse files
committed
Rust lbf-nix scaffolding
1 parent a868760 commit 19e4e9a

File tree

19 files changed

+967
-14
lines changed

19 files changed

+967
-14
lines changed

extras/flake-rust.nix

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,36 @@ let
77
};
88
craneLib = crane.lib.${pkgs.system}.overrideToolchain rustWithTools;
99

10+
cleanSrc = craneLib.cleanCargoSource (craneLib.path src);
11+
1012
# Library source code with extra dependencies attached
11-
fullSrc = pkgs.stdenv.mkDerivation {
12-
src = craneLib.cleanCargoSource (craneLib.path src);
13-
name = "lbf-rust-build-env";
14-
unpackPhase = ''
15-
mkdir $out
16-
cp -r $src/* $out
17-
cd $out
18-
${copyExtraSources}
19-
${copyData}
20-
'';
21-
};
13+
fullSrc =
14+
pkgs.stdenv.mkDerivation
15+
{
16+
src = cleanSrc;
17+
name = "${crateName}-build-env";
18+
unpackPhase = ''
19+
mkdir $out
20+
cp -r $src/* $out
21+
cd $out
22+
${copyExtraSources}
23+
${copyData}
24+
'';
25+
};
26+
27+
vendoredSrc =
28+
pkgs.stdenv.mkDerivation
29+
{
30+
src = cleanSrc;
31+
name = "${crateName}-vendored-src";
32+
unpackPhase = ''
33+
mkdir $out
34+
cp -r $src/* $out
35+
cd $out
36+
sed -i 's/.extras/../g' Cargo.toml
37+
'';
38+
};
39+
2240
commonArgs = {
2341
src = fullSrc;
2442
pname = crateName;
@@ -28,6 +46,7 @@ let
2846

2947
# Extra sources
3048
extra-sources = pkgs.linkFarm "extra-sources" extraSources;
49+
3150
hasExtraSources = builtins.length extraSources > 0;
3251
linkExtraSources = pkgs.lib.optionalString hasExtraSources ''
3352
echo "Linking extra sources"
@@ -68,6 +87,8 @@ in
6887
doInstallCargoArtifacts = true;
6988
});
7089

90+
packages."${crateName}-rust-src" = vendoredSrc;
91+
7192
checks."${crateName}-rust-test" = craneLib.cargoNextest (commonArgs // {
7293
inherit cargoArtifacts;
7394
nativeBuildInputs = testTools;

extras/lbf-nix/build.nix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ _: {
44
lbg-haskell = "${config.packages.lbg-haskell}/bin/lbg-haskell";
55
lbg-plutarch = "${config.packages.lbg-plutarch}/bin/lbg-plutarch";
66
lbg-purescript = "${config.packages.lbg-purescript}/bin/lbg-purescript";
7+
lbg-rust = "${config.packages.lbg-rust}/bin/lbg-rust";
78

89
in
910
{
@@ -18,6 +19,9 @@ _: {
1819
lbfPurescript = import ./lbf-purescript.nix pkgs config.packages.lbf lbg-purescript;
1920
lbfPreludePurescript = import ./lbf-prelude-purescript.nix pkgs config.packages.lbf lbg-purescript;
2021
lbfPlutusPurescript = import ./lbf-plutus-purescript.nix pkgs config.packages.lbf lbg-purescript;
22+
lbfRust = import ./lbf-rust.nix pkgs config.packages.lbf lbg-rust;
23+
lbfPreludeRust = import ./lbf-prelude-rust.nix pkgs config.packages.lbf lbg-rust;
24+
lbfPlutusRust = import ./lbf-plutus-rust.nix pkgs config.packages.lbf lbg-rust;
2125
};
2226

2327
};

extras/lbf-nix/lbf-plutus-rust.nix

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Build .lbf schemas that use LB Plutus (and by extension LB Prelude) package and targets Rust's plutus-ledger-api library.
2+
pkgs: lbf: lbg-rust: lbfRustOpts:
3+
let
4+
utils = import ./utils.nix pkgs;
5+
6+
lbfRust = import ./lbf-rust.nix pkgs lbf lbg-rust;
7+
lbfRustOptsForPlutus = utils.overrideAttrs
8+
{
9+
imports = {
10+
default = [ ];
11+
override = libs: libs ++ [ ../../libs/lbf-prelude ../../libs/lbf-plutus ];
12+
};
13+
dependencies = {
14+
default = [ ];
15+
override = deps: deps ++ [ "lbf-prelude-rust" "lbf-plutus-rust" ];
16+
};
17+
classes = {
18+
default = [ ];
19+
override = cls: cls ++ [ "Prelude.Eq" "Plutus.V1.PlutusData" ];
20+
};
21+
configs = {
22+
default = [ ];
23+
override = _: [ ../../lambda-buffers-codegen/data/rust-prelude.json ../../lambda-buffers-codegen/data/rust-plutus.json ];
24+
};
25+
}
26+
lbfRustOpts;
27+
in
28+
lbfRust lbfRustOptsForPlutus
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Build .lbf schemas that use LB Prelude package and targets Rust's std (and friends) library.
2+
pkgs: lbf: lbg-rust: lbfRustOpts:
3+
let
4+
utils = import ./utils.nix pkgs;
5+
6+
lbfRs = import ./lbf-rust.nix pkgs lbf lbg-rust;
7+
lbfRustOptsForPrelude = utils.overrideAttrs
8+
{
9+
imports = {
10+
default = [ ];
11+
override = libs: libs ++ [ ../../libs/lbf-prelude ];
12+
};
13+
dependencies = {
14+
default = [ ];
15+
override = deps: deps ++ [ "lbf-prelude" ];
16+
};
17+
classes = {
18+
default = [ ];
19+
override = cls: cls ++ [ "Prelude.Eq" "Prelude.Json" ];
20+
};
21+
configs = {
22+
default = [ ];
23+
override = cfgs: cfgs ++ [ ../../lambda-buffers-codegen/data/rust-prelude-base.json ];
24+
};
25+
}
26+
lbfRustOpts;
27+
28+
in
29+
lbfRs lbfRustOptsForPrelude

extras/lbf-nix/lbf-rust.nix

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Base API for constructing Rust packages given .lbf schemas
2+
3+
# Nixpkgs
4+
pkgs:
5+
# LambdaBuffers Frontend
6+
lbf:
7+
# LambdaBuffers Rust Codegen
8+
lbg-rust:
9+
let
10+
lbfRustOpts =
11+
{
12+
# Source that is passed to `lbf` as the `--import-path` flag and used to find `files`.
13+
# Examples: src = ./api
14+
src
15+
, # Additional sources that are passed to `lbf` as the `--import-path` flag.
16+
# Examples: imports = [ lbf-prelude ]
17+
imports ? [ ]
18+
, # .lbf files in `src` to compile and codegen.
19+
# Examples: files = [ "Foo.lbf" "Foo/Bar.lbf" ]
20+
files
21+
# Classes for which to generate implementations for (default lbf-prelude classes).
22+
, classes ? [ ]
23+
, # Dependencies to include in the Cabal's `build-depends` stanza.
24+
# examples: dependencies = [ "lbf-prelude" ]
25+
dependencies ? [ ]
26+
, configs ? [ ]
27+
, # Name of the package and also the name of the Cabal package.
28+
# Examples: name = "lbf-myproject"
29+
name
30+
, # Version of the package and also the version of the Cabal package.
31+
# Examples: version = "0.1.0.0"
32+
version ? "0.1.0"
33+
}: { inherit src imports files classes dependencies configs name version; };
34+
35+
lbf-build = import ./lbf-build.nix pkgs lbf;
36+
37+
lbfBuild = opts: with (lbfRustOpts opts);
38+
lbf-build.build
39+
{
40+
inherit src;
41+
opts = {
42+
inherit files;
43+
import-paths = imports;
44+
gen = lbg-rust;
45+
gen-classes = classes;
46+
gen-dir = "autogen";
47+
gen-opts = builtins.map (c: "--config=${c}") configs; # WARN(bladyjoker): If I put quotes here everything breaks.
48+
work-dir = ".work";
49+
};
50+
};
51+
52+
cargoTemplate = opts: with (lbfRustOpts opts);
53+
pkgs.writeTextFile {
54+
name = "lambda-buffers-cabal-template";
55+
text = ''
56+
[package]
57+
name = "${name}"
58+
version = "${version}"
59+
edition = "2021"
60+
61+
[dependencies]
62+
'';
63+
};
64+
65+
crateVersions = pkgs.writeTextFile {
66+
name = "lambda-buffers-crate-versions";
67+
text = ''
68+
num-bigint = "0.4.4"
69+
serde_json = { version = "1.0.107", features = ["arbitrary_precision"] }
70+
plutus-ledger-api = { github = "https://github.com/mlabs-haskell/plutus-ledger-api-rust", features = ["lbf"] }
71+
lbr-prelude = { path = "../lbr-prelude" }
72+
lbr-prelude-derive = { path = "../lbr-prelude-derive" }
73+
'';
74+
};
75+
76+
build = opts: with (lbfRustOpts opts);
77+
let
78+
lbfBuilt = lbfBuild opts;
79+
in
80+
pkgs.stdenv.mkDerivation {
81+
inherit src version;
82+
pname = name;
83+
outputs = [ "out" "buildjson" ];
84+
buildInputs = [
85+
pkgs.jq
86+
];
87+
buildPhase = ''
88+
ln -s ${lbfBuilt} autogen;
89+
ln -s ${lbfBuilt.workdir} .work-dir;
90+
ln -s ${lbfBuilt.buildjson} build.json;
91+
92+
# Generating Cargo manifest file
93+
DEPS=$(echo ${builtins.concatStringsSep " " dependencies} $(cat build.json | jq -r ".[]" | sort -u));
94+
echo "Gathered Cargo deps $DEPS";
95+
cat ${cargoTemplate opts} > Cargo.toml;
96+
for DEP in $DEPS; do
97+
if [ $DEP != "std" ]; then
98+
echo "$(cat ${crateVersions} | grep "$DEP" || echo "$DEP = { path = \"../$DEP\" }")" >> Cargo.toml
99+
fi
100+
done
101+
'';
102+
103+
installPhase = ''
104+
cp build.json $buildjson;
105+
echo "Dependencies collected"
106+
cat $buildjson;
107+
108+
mkdir -p $out/src;
109+
cp -r autogen/* $out/src
110+
cp Cargo.toml $out/Cargo.toml;
111+
112+
# Generating module files
113+
chmod -R u+w $out/src
114+
pushd $out/src
115+
116+
MODS=$(find . -type f -name "*.rs")
117+
MODS+=" "
118+
MODS+=$(find . -type d)
119+
120+
for MOD in $MODS; do
121+
if [ "$MOD" != "." ]; then
122+
if [ $(dirname $MOD) = "." ];
123+
then MODFILE="lib.rs";
124+
else MODFILE=$(dirname $MOD).rs;
125+
fi
126+
DOC="pub mod $(basename $MOD .rs);"
127+
128+
if [ ! $(grep "$DOC" $MODFILE) ]; then
129+
echo $DOC >> $MODFILE;
130+
fi
131+
fi
132+
done
133+
134+
echo "Files generated"
135+
find $out/;
136+
'';
137+
};
138+
in
139+
build

flake.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
./testsuites/lbt-prelude/golden/build.nix
5050
./testsuites/lbt-prelude/lbt-prelude-haskell/build.nix
5151
./testsuites/lbt-prelude/lbt-prelude-purescript/build.nix
52+
./testsuites/lbt-prelude/lbt-prelude-rust/build.nix
5253
./testsuites/lbt-plutus/api/build.nix
5354
./testsuites/lbt-plutus/golden/build.nix
5455
./testsuites/lbt-plutus/lbt-plutus-haskell/build.nix

lambda-buffers-codegen/data/rust-prelude-base.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"char"
4040
],
4141
"Prelude.Integer": [
42-
"num_bigint",
42+
"num-bigint",
4343
"",
4444
"BigInt"
4545
],

libs/build.nix

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@
3636
configs = [ "${config.packages.codegen-configs}/plutarch-prelude.json" ];
3737
};
3838

39+
lbf-prelude-rust = config.lbf-nix.lbfRust {
40+
name = "lbf-prelude";
41+
src = ./lbf-prelude;
42+
files = [ "Prelude.lbf" ];
43+
classes = [ "Prelude.Eq" "Prelude.Json" ];
44+
configs = [ "${config.packages.codegen-configs}/rust-prelude-base.json" ];
45+
};
46+
3947
lbf-plutus = pkgs.stdenv.mkDerivation {
4048
name = "lbf-plutus";
4149
src = ./lbf-plutus;

runtimes/rust/lbr-prelude-derive/build.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
extraSources = [
1010
{
1111
name = "lbr-prelude";
12-
path = ../lbr-prelude;
12+
path = config.packages.lbr-prelude-rust-src;
1313
}
1414
];
1515
devShellHook = config.settings.shell.hook;

runtimes/rust/lbr-prelude/build.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
extraSources = [
1010
{
1111
name = "lbr-prelude-derive";
12-
path = ../lbr-prelude-derive;
12+
path = config.packages.lbr-prelude-derive-rust-src;
1313
}
1414
];
1515
devShellHook = config.settings.shell.hook;

0 commit comments

Comments
 (0)