Skip to content

Commit

Permalink
refactor(rust): deduplicate vendor and utils code (nix-community#1054)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
yusdacra authored Nov 12, 2024
1 parent 432bdc6 commit 44d4141
Show file tree
Hide file tree
Showing 17 changed files with 417 additions and 788 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
}: {
imports = [
dream2nix.modules.dream2nix.rust-cargo-lock
dream2nix.modules.dream2nix.rust-cargo-vendor
dream2nix.modules.dream2nix.rust-crane
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
}: {
imports = [
dream2nix.modules.dream2nix.rust-cargo-lock
dream2nix.modules.dream2nix.rust-cargo-vendor
dream2nix.modules.dream2nix.buildRustPackage
];

Expand Down
1 change: 1 addition & 0 deletions examples/packages/languages/rust-packaging/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
}: {
imports = [
dream2nix.modules.dream2nix.rust-cargo-lock
dream2nix.modules.dream2nix.rust-cargo-vendor
dream2nix.modules.dream2nix.rust-crane
];

Expand Down
124 changes: 16 additions & 108 deletions modules/dream2nix/buildRustPackage/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,133 +3,52 @@
lib,
dream2nix,
...
} @ topArgs: let
}: let
l = lib // builtins;

dreamLock = config.rust-cargo-lock.dreamLock;

sourceRoot = config.mkDerivation.src;

fetchDreamLockSources =
import ../../../lib/internal/fetchDreamLockSources.nix
{inherit lib;};
getDreamLockSource = import ../../../lib/internal/getDreamLockSource.nix {inherit lib;};
readDreamLock = import ../../../lib/internal/readDreamLock.nix {inherit lib;};
hashPath = import ../../../lib/internal/hashPath.nix {
inherit lib;
inherit (config.deps) runCommandLocal nix;
};
hashFile = import ../../../lib/internal/hashFile.nix {
inherit lib;
inherit (config.deps) runCommandLocal nix;
};

# fetchers
fetchers = {
git = import ../../../lib/internal/fetchers/git {
inherit hashPath;
inherit (config.deps) fetchgit;
};
http = import ../../../lib/internal/fetchers/http {
inherit hashFile lib;
inherit (config.deps.stdenv) mkDerivation;
inherit (config.deps) fetchurl;
};
crates-io = import ../../../lib/internal/fetchers/crates-io {
inherit hashFile;
inherit (config.deps) fetchurl runCommandLocal;
};
};

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

fetchedSources' = fetchDreamLockSources {
inherit (dreamLockInterface) defaultPackageName defaultPackageVersion;
inherit (dreamLockLoaded.lock) sources;
inherit fetchers;
};

fetchedSources =
fetchedSources'
// {
${defaultPackageName}.${defaultPackageVersion} = sourceRoot;
};

# name: version: -> store-path
getSource = getDreamLockSource fetchedSources;

inherit
(dreamLockInterface)
getDependencies # name: version: -> [ {name=; version=; } ]
# Attributes

subsystemAttrs # attrset
packageVersions
defaultPackageName
defaultPackageVersion
;

toTOML = import ../../../lib/internal/toTOML.nix {inherit lib;};

utils = import ./utils.nix {
inherit dreamLock getSource lib toTOML sourceRoot;
inherit
(dreamLockInterface)
getSourceSpec
getRoot
subsystemAttrs
packages
;
inherit
(config.deps)
writeText
;
};

vendoring = import ./vendor.nix {
inherit dreamLock getSource lib;
inherit
(dreamLockInterface)
getSourceSpec
subsystemAttrs
;
inherit
(config.deps)
cargo
jq
moreutils
python3Packages
runCommandLocal
writePython3
;
};
meta = let
meta = subsystemAttrs.meta.${pname}.${version};
in
meta
// {
license = l.map (name: l.licenses.${name}) meta.license;
};

pname = config.name;
version = config.version;

src = utils.getRootSource pname version;
src = config.rust-cargo-vendor.getRootSource pname version;
replacePaths =
utils.replaceRelativePathsWithAbsolute
config.rust-cargo-vendor.replaceRelativePathsWithAbsolute
subsystemAttrs.relPathReplacements.${pname}.${version};
writeGitVendorEntries = vendoring.writeGitVendorEntries "vendored-sources";
writeGitVendorEntries = config.rust-cargo-vendor.writeGitVendorEntries "vendored-sources";

cargoBuildFlags = "--package ${pname}";
buildArgs = {
inherit pname version;
src = lib.mkForce src;

meta = utils.getMeta pname version;
inherit meta;

cargoBuildFlags = cargoBuildFlags;
cargoTestFlags = cargoBuildFlags;

cargoVendorDir = "../nix-vendor";
dream2nixVendorDir = vendoring.vendoredDependencies;
dream2nixVendorDir = config.rust-cargo-vendor.vendoredSources;

postUnpack = ''
${vendoring.copyVendorDir "$dream2nixVendorDir" "./nix-vendor"}
${config.rust-cargo-vendor.copyVendorDir "$dream2nixVendorDir" "./nix-vendor"}
export CARGO_HOME=$(pwd)/.cargo_home
'';

Expand All @@ -140,7 +59,7 @@
fi
${writeGitVendorEntries}
${replacePaths}
${utils.writeCargoLock}
${config.rust-cargo-lock.writeCargoLock}
'';
};
in {
Expand All @@ -154,24 +73,13 @@ in {
package-func.args = buildArgs;

public = {
meta = utils.getMeta pname version;
inherit meta;
};

deps = {nixpkgs, ...}: {
inherit
(nixpkgs)
cargo
fetchurl
jq
moreutils
python3Packages
runCommandLocal
rustPlatform
writeText
;
inherit
(nixpkgs.writers)
writePython3
;
};
}
191 changes: 0 additions & 191 deletions modules/dream2nix/buildRustPackage/utils.nix

This file was deleted.

Loading

0 comments on commit 44d4141

Please sign in to comment.