Skip to content

Commit 5091fcb

Browse files
committed
Implement extraSources for Rust
1 parent 1b07117 commit 5091fcb

File tree

7 files changed

+53
-28
lines changed

7 files changed

+53
-28
lines changed

extras/flake-rust.nix

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,70 @@
11
pkgs:
22

3-
{ crane, src, system, crateName, localDeps, dataDeps }:
3+
{ crane, src, system, crateName, extraSources ? [ ], extraSourcesDir ? ".extras", data ? [ ], dataDir ? "data" }:
44
let
55
rustWithTools = pkgs.rust-bin.stable.latest.default.override {
66
extensions = [ "rustfmt" "rust-analyzer" "clippy" ];
77
};
88
craneLib = crane.lib.${system}.overrideToolchain rustWithTools;
99

10-
buildEnv = pkgs.stdenv.mkDerivation {
11-
inherit src;
12-
name = "lbf-rust-workspace";
13-
unpackPhase = builtins.concatStringsSep "\n"
14-
([
15-
"mkdir -p $out"
16-
"cp -r $src $out/${crateName}"
17-
]
18-
++ (map ({ name, path }: "cp -r ${path} $out/${name}") localDeps)
19-
++ (map ({ name, path }: "cp -r ${path} $out/${name}") dataDeps)
20-
);
10+
# 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+
'';
2121
};
2222
commonArgs = {
23-
src = buildEnv;
23+
src = fullSrc;
2424
pname = crateName;
2525
strictDeps = true;
26-
cargoLock = "${src}/Cargo.lock";
27-
cargoToml = "${src}/Cargo.toml";
28-
postUnpack = ''
29-
cd $sourceRoot/${crateName}
30-
sourceRoot="."
31-
'';
3226
};
3327
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
3428

29+
# Extra sources
30+
extra-sources = pkgs.linkFarm "extra-sources" extraSources;
31+
hasExtraSources = builtins.length extraSources > 0;
32+
linkExtraSources = pkgs.lib.optionalString hasExtraSources ''
33+
echo "Linking extra sources"
34+
if [ -e ./${extraSourcesDir} ]; then rm ./${extraSourcesDir}; fi
35+
ln -s ${extra-sources} ./${extraSourcesDir}
36+
'';
37+
copyExtraSources = pkgs.lib.optionalString hasExtraSources ''
38+
echo "Copying extra sources"
39+
cp -Lr ${extra-sources} ./${extraSourcesDir}
40+
'';
41+
42+
# Data
43+
data-drv = pkgs.linkFarm "data" data;
44+
hasData = builtins.length data > 0;
45+
linkData = pkgs.lib.optionalString hasData ''
46+
echo "Linking data"
47+
if [ -e ./${dataDir} ]; then rm ./${dataDir}; fi
48+
ln -s ${data-drv} ./${dataDir}
49+
'';
50+
copyData = pkgs.lib.optionalString hasData ''
51+
echo "Copying data"
52+
cp -Lr ${data-drv} ./${dataDir}
53+
'';
3554
in
3655
{
3756
devShells."dev-${crateName}-rust" = craneLib.devShell {
38-
# checks = self'.checks;
57+
shellHook = ''
58+
${linkExtraSources}
59+
${linkData}
60+
'';
3961
};
4062

41-
packages."${crateName}-rust" = craneLib.buildPackage commonArgs // {
63+
packages."${crateName}-rust" = craneLib.buildPackage (commonArgs // {
4264
inherit cargoArtifacts;
4365
doCheck = false;
44-
};
66+
doInstallCargoArtifacts = true;
67+
});
4568

4669
checks."${crateName}-rust-test" = craneLib.cargoNextest (commonArgs // {
4770
inherit cargoArtifacts;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/target
22
/proptest-regressions
3+
/.extras
4+
/data

runtimes/rust/lbr-prelude-derive/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ syn = { version = "2.0.38", features = ["extra-traits"] }
1313
trybuild = { version = "1.0.49", features = ["diff"] }
1414

1515
[dev-dependencies]
16-
lbr-prelude = { version = "0.1.0", path = "../lbr-prelude", default-features = false }
16+
lbr-prelude = { version = "0.1.0", path = ".extras/lbr-prelude", default-features = false }
1717
num-bigint = "0.4.4"
1818
serde_json = "1.0.107"

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
src = ./.;
88
crane = inputs.crane;
99
crateName = "lbr-prelude-derive";
10-
localDeps = [
10+
extraSources = [
1111
{
1212
name = "lbr-prelude";
1313
path = ../lbr-prelude;
1414
}
1515
];
16-
dataDeps = [ ];
1716
};
1817
in
1918
{

runtimes/rust/lbr-prelude/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/target
22
/proptest-regressions
3+
/.extras
4+
/data

runtimes/rust/lbr-prelude/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ proptest = "1.3.1"
1212
serde = "1.0.188"
1313
serde_json = { version = "1.0.107", features = ["arbitrary_precision"] }
1414
thiserror = "1.0.49"
15-
lbr-prelude-derive = { version = "0.1.0", path = "../lbr-prelude-derive", optional = true }
15+
lbr-prelude-derive = { version = "0.1.0", path = ".extras/lbr-prelude-derive", optional = true }
1616

1717
[features]
1818
default = ["derive"]

runtimes/rust/lbr-prelude/build.nix

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
src = ./.;
88
crane = inputs.crane;
99
crateName = "lbr-prelude";
10-
localDeps = [
10+
extraSources = [
1111
{
1212
name = "lbr-prelude-derive";
1313
path = ../lbr-prelude-derive;
1414
}
1515
];
16-
dataDeps = [ ];
1716
};
1817
in
1918
{

0 commit comments

Comments
 (0)