Skip to content

Commit 44d4141

Browse files
authored
refactor(rust): deduplicate vendor and utils code (#1054)
* refactor(rust): consolidate the separated vendoring code into rust-cargo-vendor * refactor(rust): move the utility code in utils.nix to rust-cargo-lock and rust-cargo-vendor * fix(rust-crane): add the necessary dependencies to config.deps for devshell * refactor(rust-cargo-vendor): remove unnecessary mkMerge * fix(rust-crane): override cargo in config.deps with our toolchain so that rust-cargo-vendor uses it too
1 parent 432bdc6 commit 44d4141

File tree

17 files changed

+417
-788
lines changed

17 files changed

+417
-788
lines changed

examples/packages/languages/rust-local-development-workspace/default.nix

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
}: {
77
imports = [
88
dream2nix.modules.dream2nix.rust-cargo-lock
9+
dream2nix.modules.dream2nix.rust-cargo-vendor
910
dream2nix.modules.dream2nix.rust-crane
1011
];
1112

examples/packages/languages/rust-packaging-buildRustPackage/default.nix

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
}: {
77
imports = [
88
dream2nix.modules.dream2nix.rust-cargo-lock
9+
dream2nix.modules.dream2nix.rust-cargo-vendor
910
dream2nix.modules.dream2nix.buildRustPackage
1011
];
1112

examples/packages/languages/rust-packaging/default.nix

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
}: {
77
imports = [
88
dream2nix.modules.dream2nix.rust-cargo-lock
9+
dream2nix.modules.dream2nix.rust-cargo-vendor
910
dream2nix.modules.dream2nix.rust-crane
1011
];
1112

modules/dream2nix/buildRustPackage/default.nix

+16-108
Original file line numberDiff line numberDiff line change
@@ -3,133 +3,52 @@
33
lib,
44
dream2nix,
55
...
6-
} @ topArgs: let
6+
}: let
77
l = lib // builtins;
88

9-
dreamLock = config.rust-cargo-lock.dreamLock;
10-
11-
sourceRoot = config.mkDerivation.src;
12-
13-
fetchDreamLockSources =
14-
import ../../../lib/internal/fetchDreamLockSources.nix
15-
{inherit lib;};
16-
getDreamLockSource = import ../../../lib/internal/getDreamLockSource.nix {inherit lib;};
179
readDreamLock = import ../../../lib/internal/readDreamLock.nix {inherit lib;};
18-
hashPath = import ../../../lib/internal/hashPath.nix {
19-
inherit lib;
20-
inherit (config.deps) runCommandLocal nix;
21-
};
22-
hashFile = import ../../../lib/internal/hashFile.nix {
23-
inherit lib;
24-
inherit (config.deps) runCommandLocal nix;
25-
};
26-
27-
# fetchers
28-
fetchers = {
29-
git = import ../../../lib/internal/fetchers/git {
30-
inherit hashPath;
31-
inherit (config.deps) fetchgit;
32-
};
33-
http = import ../../../lib/internal/fetchers/http {
34-
inherit hashFile lib;
35-
inherit (config.deps.stdenv) mkDerivation;
36-
inherit (config.deps) fetchurl;
37-
};
38-
crates-io = import ../../../lib/internal/fetchers/crates-io {
39-
inherit hashFile;
40-
inherit (config.deps) fetchurl runCommandLocal;
41-
};
42-
};
4310

4411
dreamLockLoaded =
4512
readDreamLock {inherit (config.rust-cargo-lock) dreamLock;};
4613
dreamLockInterface = dreamLockLoaded.interface;
4714

48-
fetchedSources' = fetchDreamLockSources {
49-
inherit (dreamLockInterface) defaultPackageName defaultPackageVersion;
50-
inherit (dreamLockLoaded.lock) sources;
51-
inherit fetchers;
52-
};
53-
54-
fetchedSources =
55-
fetchedSources'
56-
// {
57-
${defaultPackageName}.${defaultPackageVersion} = sourceRoot;
58-
};
59-
60-
# name: version: -> store-path
61-
getSource = getDreamLockSource fetchedSources;
62-
6315
inherit
6416
(dreamLockInterface)
65-
getDependencies # name: version: -> [ {name=; version=; } ]
66-
# Attributes
67-
6817
subsystemAttrs # attrset
69-
packageVersions
70-
defaultPackageName
71-
defaultPackageVersion
7218
;
7319

74-
toTOML = import ../../../lib/internal/toTOML.nix {inherit lib;};
75-
76-
utils = import ./utils.nix {
77-
inherit dreamLock getSource lib toTOML sourceRoot;
78-
inherit
79-
(dreamLockInterface)
80-
getSourceSpec
81-
getRoot
82-
subsystemAttrs
83-
packages
84-
;
85-
inherit
86-
(config.deps)
87-
writeText
88-
;
89-
};
90-
91-
vendoring = import ./vendor.nix {
92-
inherit dreamLock getSource lib;
93-
inherit
94-
(dreamLockInterface)
95-
getSourceSpec
96-
subsystemAttrs
97-
;
98-
inherit
99-
(config.deps)
100-
cargo
101-
jq
102-
moreutils
103-
python3Packages
104-
runCommandLocal
105-
writePython3
106-
;
107-
};
20+
meta = let
21+
meta = subsystemAttrs.meta.${pname}.${version};
22+
in
23+
meta
24+
// {
25+
license = l.map (name: l.licenses.${name}) meta.license;
26+
};
10827

10928
pname = config.name;
11029
version = config.version;
11130

112-
src = utils.getRootSource pname version;
31+
src = config.rust-cargo-vendor.getRootSource pname version;
11332
replacePaths =
114-
utils.replaceRelativePathsWithAbsolute
33+
config.rust-cargo-vendor.replaceRelativePathsWithAbsolute
11534
subsystemAttrs.relPathReplacements.${pname}.${version};
116-
writeGitVendorEntries = vendoring.writeGitVendorEntries "vendored-sources";
35+
writeGitVendorEntries = config.rust-cargo-vendor.writeGitVendorEntries "vendored-sources";
11736

11837
cargoBuildFlags = "--package ${pname}";
11938
buildArgs = {
12039
inherit pname version;
12140
src = lib.mkForce src;
12241

123-
meta = utils.getMeta pname version;
42+
inherit meta;
12443

12544
cargoBuildFlags = cargoBuildFlags;
12645
cargoTestFlags = cargoBuildFlags;
12746

12847
cargoVendorDir = "../nix-vendor";
129-
dream2nixVendorDir = vendoring.vendoredDependencies;
48+
dream2nixVendorDir = config.rust-cargo-vendor.vendoredSources;
13049

13150
postUnpack = ''
132-
${vendoring.copyVendorDir "$dream2nixVendorDir" "./nix-vendor"}
51+
${config.rust-cargo-vendor.copyVendorDir "$dream2nixVendorDir" "./nix-vendor"}
13352
export CARGO_HOME=$(pwd)/.cargo_home
13453
'';
13554

@@ -140,7 +59,7 @@
14059
fi
14160
${writeGitVendorEntries}
14261
${replacePaths}
143-
${utils.writeCargoLock}
62+
${config.rust-cargo-lock.writeCargoLock}
14463
'';
14564
};
14665
in {
@@ -154,24 +73,13 @@ in {
15473
package-func.args = buildArgs;
15574

15675
public = {
157-
meta = utils.getMeta pname version;
76+
inherit meta;
15877
};
15978

16079
deps = {nixpkgs, ...}: {
16180
inherit
16281
(nixpkgs)
163-
cargo
164-
fetchurl
165-
jq
166-
moreutils
167-
python3Packages
168-
runCommandLocal
16982
rustPlatform
170-
writeText
171-
;
172-
inherit
173-
(nixpkgs.writers)
174-
writePython3
17583
;
17684
};
17785
}

modules/dream2nix/buildRustPackage/utils.nix

-191
This file was deleted.

0 commit comments

Comments
 (0)