Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(rust): deduplicate vendor and utils code #1054

Merged
merged 5 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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