Skip to content

Commit 98d8c03

Browse files
committed
Improve rust-nix documentation
1 parent ae5bacd commit 98d8c03

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

extras/flake-rust.nix

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ let
99

1010
cleanSrc = craneLib.cleanCargoSource (craneLib.path src);
1111

12-
# Library source code with extra dependencies attached
13-
fullSrc =
12+
# Library source code with extra dependencies copied
13+
buildEnv =
1414
pkgs.stdenv.mkDerivation
1515
{
1616
src = cleanSrc;
@@ -24,6 +24,9 @@ let
2424
'';
2525
};
2626

27+
# Library source code, intended to be in extra-sources (inside the `.extras` directory)
28+
# The main difference is that dependencies are not copied, to `.extras`
29+
# but they are referenced from the parent directory (`../`).
2730
vendoredSrc =
2831
pkgs.stdenv.mkDerivation
2932
{
@@ -38,7 +41,7 @@ let
3841
};
3942

4043
commonArgs = {
41-
src = fullSrc;
44+
src = buildEnv;
4245
pname = crateName;
4346
strictDeps = true;
4447
};
@@ -89,6 +92,8 @@ in
8992

9093
packages."${crateName}-rust-src" = vendoredSrc;
9194

95+
packages."${crateName}-rust-build-env" = buildEnv;
96+
9297
checks."${crateName}-rust-test" = craneLib.cargoNextest (commonArgs // {
9398
inherit cargoArtifacts;
9499
nativeBuildInputs = testTools;

extras/lbf-nix/lbf-rust.nix

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ let
2020
files
2121
# Classes for which to generate implementations for (default lbf-prelude classes).
2222
, classes ? [ ]
23-
, # Dependencies to include in the Cabal's `build-depends` stanza.
23+
, # Dependencies to include in the Cargo's `dependencies` section.
2424
# examples: dependencies = [ "lbf-prelude" ]
2525
dependencies ? [ ]
2626
, configs ? [ ]
27-
, # Name of the package and also the name of the Cabal package.
27+
, # Name of the package and also the name of the Cargo crate.
2828
# Examples: name = "lbf-myproject"
2929
name
30-
, # Version of the package and also the version of the Cabal package.
30+
, # Version of the package and also the version of the Cargo crate.
3131
# Examples: version = "0.1.0.0"
3232
version ? "0.1.0"
3333
}: { inherit src imports files classes dependencies configs name version; };
@@ -67,7 +67,7 @@ let
6767

6868
cargoTemplate = opts: with (lbfRustOpts opts);
6969
pkgs.writeTextFile {
70-
name = "lambda-buffers-cabal-template";
70+
name = "lambda-buffers-cargo-template";
7171
text = ''
7272
[package]
7373
name = "${name}"
@@ -78,7 +78,9 @@ let
7878
'';
7979
};
8080

81-
#
81+
# This is a lookup table of default crate versions used by lamba-buffers modules
82+
# Based on the contents of `build.json` a subset of these will be attached to the
83+
# Cargo.toml file
8284
crateVersions = pkgs.writeTextFile {
8385
name = "lambda-buffers-crate-versions";
8486
text = ''
@@ -108,6 +110,10 @@ let
108110
DEPS=$(echo ${builtins.concatStringsSep " " dependencies} $(cat build.json | jq -r ".[]" | sort -u));
109111
echo "Gathered Cargo deps $DEPS";
110112
cat ${cargoTemplate opts} > Cargo.toml;
113+
# Using the lookup table `crateVersions`, filling in the library version.
114+
# If no version is found, we default to a local path dependency, pointing to
115+
# a sibling directory (directory in extra-sources or .extras)
116+
# e.g.: for `lbr-prelude` we print `lbr-prelude = { path = "../lbr-prelude" }
111117
for DEP in $DEPS; do
112118
if [ $DEP != "std" ]; then
113119
echo "$(cat ${crateVersions} | grep "$DEP" || echo "$DEP = { path = \"../$DEP\" }")" >> Cargo.toml
@@ -128,6 +134,10 @@ let
128134
chmod -R u+w $out/src
129135
pushd $out/src
130136
137+
# Collecting modules of the library and attaching a module declaration
138+
# to parent modules. Any directory in the path must also
139+
# be considered as a module (e.g. for `foo/bar/baz.rs` we have to create
140+
# `lib.rs`, `foo.rs`and `foo/bar.rs`)
131141
MODS=$(find . -type f -name "*.rs")
132142
MODS+=" "
133143
MODS+=$(find . -type d)

0 commit comments

Comments
 (0)