From a20f305265ee61ba8b1cc10957e29777aa92826a Mon Sep 17 00:00:00 2001 From: Yusuf Bera Ertan Date: Wed, 31 Mar 2021 16:26:11 +0300 Subject: [PATCH 1/7] feat: use builtin fetchGit instead of fetchgit --- crate2nix/src/config.rs | 4 +- crate2nix/src/resolve.rs | 2 +- crate2nix/src/sources.rs | 2 +- crate2nix/templates/Cargo.nix.tera | 6 +-- .../templates/crate2nix-sources.nix.tera | 4 +- tests.nix | 4 +- tools.nix | 45 +++++++++++++++---- 7 files changed, 47 insertions(+), 20 deletions(-) diff --git a/crate2nix/src/config.rs b/crate2nix/src/config.rs index dc37acda..dee96b3f 100644 --- a/crate2nix/src/config.rs +++ b/crate2nix/src/config.rs @@ -105,7 +105,7 @@ pub enum Source { /// The revision hash. rev: String, /// The sha256 of the fetched result. - sha256: String, + sha256: Option, }, /// Get the source from a nix expression. Nix { @@ -181,7 +181,7 @@ impl Display for Source { version, sha256, } => write!(f, "{} {} from crates.io: {}", name, version, sha256), - Source::Git { url, rev, sha256 } => write!(f, "{}#{} via git: {}", url, rev, sha256), + Source::Git { url, rev, sha256 } => write!(f, "{}#{} via git: {:?}", url, rev, sha256), Source::Nix { file, attr: None } => write!(f, "{}", file), Source::Nix { file, diff --git a/crate2nix/src/resolve.rs b/crate2nix/src/resolve.rs index e8ff2c41..4fafdde8 100644 --- a/crate2nix/src/resolve.rs +++ b/crate2nix/src/resolve.rs @@ -370,7 +370,7 @@ impl From for ResolvedSource { url, rev, r#ref: None, - sha256: Some(sha256), + sha256, }), crate::config::Source::CratesIo { name, diff --git a/crate2nix/src/sources.rs b/crate2nix/src/sources.rs index 243a365d..db6a40bd 100644 --- a/crate2nix/src/sources.rs +++ b/crate2nix/src/sources.rs @@ -46,7 +46,7 @@ pub fn git_io_source(url: Url, rev: String) -> Result { let sha256 = prefetchable.prefetch()?; eprintln!("done."); - Ok(config::Source::Git { url, rev, sha256 }) + Ok(config::Source::Git { url, rev, sha256: Some(sha256) }) } /// Operations on assmebling out-of-tree sources via nix. diff --git a/crate2nix/templates/Cargo.nix.tera b/crate2nix/templates/Cargo.nix.tera index 6a1e7c96..fcacf36a 100644 --- a/crate2nix/templates/Cargo.nix.tera +++ b/crate2nix/templates/Cargo.nix.tera @@ -130,12 +130,10 @@ rec { else {{crate.source.LocalDirectory.path | safe}}; {%- elif crate.source.Git %} workspace_member = null; - src = pkgs.fetchgit { + src = builtins.fetchGit { url = {{crate.source.Git.url}}; rev = {{crate.source.Git.rev}}; - {%- if crate.source.Git.sha256 %} - sha256 = {{ crate.source.Git.sha256 }}; - {%- endif %} + allRefs = true; }; {%- else %} src = builtins.throw ''ERROR: Could not resolve source: {{crate.source | json_encode() | safe}}''; diff --git a/crate2nix/templates/crate2nix-sources.nix.tera b/crate2nix/templates/crate2nix-sources.nix.tera index 613d103c..6f29adb5 100644 --- a/crate2nix/templates/crate2nix-sources.nix.tera +++ b/crate2nix/templates/crate2nix-sources.nix.tera @@ -40,10 +40,10 @@ rec { assert builtins.isString type; if type == "Git" - then pkgs.fetchgit { + then builtins.fetchGit { url = source.url; rev = source.rev; - sha256 = source.sha256; + allRefs = true; } else if type == "CratesIo" then downloadFromCratesIo source diff --git a/tests.nix b/tests.nix index 14f3a498..481c4462 100644 --- a/tests.nix +++ b/tests.nix @@ -759,10 +759,10 @@ in # # It is to have them directly as attributes for testing. - registryGit = pkgs.fetchgit { + registryGit = builtins.fetchGit { url = "https://github.com/rust-lang/crates.io-index"; rev = "18e3f063f594fc08a078f0de2bb3f94beed16ae2"; - sha256 = "0rpv12ifgnni55phlkb5ppmala7y3zrsc9dl8l99pbsjpqx95vmj"; + allRefs = true; }; registry = pkgs.linkFarm "crates.io-index" [ diff --git a/tools.nix b/tools.nix index 5fc8d0d6..8de70957 100644 --- a/tools.nix +++ b/tools.nix @@ -170,10 +170,16 @@ rec { withoutGitPlus = lib.removePrefix "git+" source; splitHash = lib.splitString "#" withoutGitPlus; splitQuestion = lib.concatMap (lib.splitString "?") splitHash; + maybeBranchSplited = lib.splitString "branch=" withoutGitPlus; in { url = builtins.head splitQuestion; rev = lib.last splitQuestion; + branch = if (lib.length maybeBranchSplited) == 1 then + "master" + else ( + lib.head (lib.splitString "#" (lib.last maybeBranchSplited)) + ); }; vendorSupport = { crateDir ? ./., ... }: @@ -235,7 +241,7 @@ rec { src = builtins.fetchGit { submodules = true; inherit (parsed) url rev; - ref = attrs.branch or "master"; + ref = parsed.branch or null; }; hash = pkgs.runCommand "hash-of-${attrs.name}" { nativeBuildInputs = [ pkgs.nix ]; } '' echo -n "$(nix-hash --type sha256 ${src})" > $out @@ -305,13 +311,14 @@ rec { [source."${parsed.url}"] git = "${parsed.url}" rev = "${parsed.rev}" - ${lib.optionalString (isNull (builtins.match ".*\\?rev=[0-9a-z]{40}.*" source)) ''branch = "${attrs.branch or "master"}"''} + ${lib.optionalString (isNull (builtins.match ".*\\?rev=[0-9a-z]{40}.*" source)) ''branch = "${attrs.branch or parsed.branch or "master"}"''} replace-with = "vendored-sources" ''; gitSources = packagesByType."git" or [ ]; gitSourcesUnique = lib.unique gitSources; gitSourceConfigs = builtins.map gitSourceConfig gitSourcesUnique; - gitSourceConfigsString = lib.concatStrings gitSourceConfigs; + gitSourceConfigsUnique = lib.unique gitSourceConfigs; + gitSourceConfigsString = lib.concatStrings gitSourceConfigsUnique; in pkgs.writeText "vendor-config" @@ -355,16 +362,38 @@ rec { or extraHashes.${packageId} or (builtins.throw "Checksum for ${packageId} not found in crate-hashes.json"); parsed = parseGitSource source; - src = pkgs.fetchgit { - name = "${name}-${version}"; - inherit sha256; + src = builtins.fetchGit { inherit (parsed) url rev; + allRefs = true; }; + srcName = "${name}-${version}"; + + rootCargo = builtins.fromTOML (builtins.readFile "${src}/Cargo.toml"); + isWorkspace = rootCargo ? "workspace"; + isPackage = rootCargo ? "package"; + containedCrates = rootCargo.workspace.members ++ (if isPackage then [ "." ] else [ ]); + + getCrateNameFromPath = path: + let + cargoTomlCrate = builtins.fromTOML (builtins.readFile "${src}/${path}/Cargo.toml"); + in + cargoTomlCrate.package.name; + + pathToExtract = + if isWorkspace then + builtins.head + (builtins.filter + (to_filter: + (getCrateNameFromPath to_filter) == name + ) + containedCrates) + else + "."; in - pkgs.runCommand (lib.removeSuffix ".tar.gz" src.name) { } + pkgs.runCommand (lib.removeSuffix ".tar.gz" srcName) { } '' mkdir -p $out - cp -apR ${src}/* $out + cp -apR ${src}/${pathToExtract}/* $out echo '{"package":null,"files":{}}' > $out/.cargo-checksum.json ''; From f0ff64e36a1836a508e8d15afdf391305e0fc4b0 Mon Sep 17 00:00:00 2001 From: Yusuf Bera Ertan Date: Mon, 26 Apr 2021 21:45:26 +0300 Subject: [PATCH 2/7] fix: more submodule fetching --- crate2nix/templates/Cargo.nix.tera | 1 + crate2nix/templates/crate2nix-sources.nix.tera | 3 ++- tools.nix | 13 ++++++++----- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/crate2nix/templates/Cargo.nix.tera b/crate2nix/templates/Cargo.nix.tera index fcacf36a..232288ac 100644 --- a/crate2nix/templates/Cargo.nix.tera +++ b/crate2nix/templates/Cargo.nix.tera @@ -134,6 +134,7 @@ rec { url = {{crate.source.Git.url}}; rev = {{crate.source.Git.rev}}; allRefs = true; + submodules = true; }; {%- else %} src = builtins.throw ''ERROR: Could not resolve source: {{crate.source | json_encode() | safe}}''; diff --git a/crate2nix/templates/crate2nix-sources.nix.tera b/crate2nix/templates/crate2nix-sources.nix.tera index 6f29adb5..794b8685 100644 --- a/crate2nix/templates/crate2nix-sources.nix.tera +++ b/crate2nix/templates/crate2nix-sources.nix.tera @@ -44,6 +44,7 @@ rec { url = source.url; rev = source.rev; allRefs = true; + submodules = true; } else if type == "CratesIo" then downloadFromCratesIo source @@ -108,4 +109,4 @@ rec { tar -xzf ${archive} --strip-components=1 -C $out ''; }; -} \ No newline at end of file +} diff --git a/tools.nix b/tools.nix index 8de70957..3c81aa03 100644 --- a/tools.nix +++ b/tools.nix @@ -175,11 +175,13 @@ rec { { url = builtins.head splitQuestion; rev = lib.last splitQuestion; - branch = if (lib.length maybeBranchSplited) == 1 then - "master" - else ( - lib.head (lib.splitString "#" (lib.last maybeBranchSplited)) - ); + branch = + if (lib.length maybeBranchSplited) == 1 then + "master" + else + ( + lib.head (lib.splitString "#" (lib.last maybeBranchSplited)) + ); }; vendorSupport = { crateDir ? ./., ... }: @@ -365,6 +367,7 @@ rec { src = builtins.fetchGit { inherit (parsed) url rev; allRefs = true; + submodules = true; }; srcName = "${name}-${version}"; From ae3c51e82970574d98bc0d548d5e2647b019fa2b Mon Sep 17 00:00:00 2001 From: Yusuf Bera Ertan Date: Sun, 13 Jun 2021 18:20:21 +0300 Subject: [PATCH 3/7] fix: better support git refs, use allRefs if no ref is passed --- crate2nix/src/config.rs | 4 +- crate2nix/src/main.rs | 7 +- crate2nix/src/resolve.rs | 10 +-- crate2nix/src/sources.rs | 6 +- crate2nix/templates/Cargo.nix.tera | 9 ++- .../templates/crate2nix-sources.nix.tera | 8 +- tools.nix | 76 ++++++++++++------- 7 files changed, 74 insertions(+), 46 deletions(-) diff --git a/crate2nix/src/config.rs b/crate2nix/src/config.rs index dee96b3f..b0bb3978 100644 --- a/crate2nix/src/config.rs +++ b/crate2nix/src/config.rs @@ -104,6 +104,8 @@ pub enum Source { url: url::Url, /// The revision hash. rev: String, + /// The reference (branch or tag). + r#ref: Option, /// The sha256 of the fetched result. sha256: Option, }, @@ -181,7 +183,7 @@ impl Display for Source { version, sha256, } => write!(f, "{} {} from crates.io: {}", name, version, sha256), - Source::Git { url, rev, sha256 } => write!(f, "{}#{} via git: {:?}", url, rev, sha256), + Source::Git { url, rev, r#ref, sha256 } => write!(f, "{}#{} on {:?} via git: {:?}", url, rev, r#ref, sha256), Source::Nix { file, attr: None } => write!(f, "{}", file), Source::Nix { file, diff --git a/crate2nix/src/main.rs b/crate2nix/src/main.rs index a360beed..9059d6b0 100644 --- a/crate2nix/src/main.rs +++ b/crate2nix/src/main.rs @@ -267,6 +267,9 @@ pub enum SourceAddingCommands { #[structopt(long = "rev", parse(from_str), help = "The git revision hash.")] rev: String, + + #[structopt(long = "ref", parse(from_str), help = "The git reference (branch or tag).")] + r#ref: Option, }, #[structopt( @@ -319,8 +322,8 @@ impl SourceAddingCommands { let source = crate2nix::sources::crates_io_source(crate_name, crate_version)?; (name, source) } - SourceAddingCommands::Git { name, url, rev } => { - let source = crate2nix::sources::git_io_source(url, rev)?; + SourceAddingCommands::Git { name, url, rev, r#ref } => { + let source = crate2nix::sources::git_io_source(url, rev, r#ref)?; (name, source) } SourceAddingCommands::Nix { diff --git a/crate2nix/src/resolve.rs b/crate2nix/src/resolve.rs index 4fafdde8..858abb3f 100644 --- a/crate2nix/src/resolve.rs +++ b/crate2nix/src/resolve.rs @@ -366,10 +366,10 @@ pub enum ResolvedSource { impl From for ResolvedSource { fn from(source: crate::config::Source) -> Self { match source { - crate::config::Source::Git { url, rev, sha256 } => ResolvedSource::Git(GitSource { + crate::config::Source::Git { url, rev, r#ref, sha256 } => ResolvedSource::Git(GitSource { url, rev, - r#ref: None, + r#ref, sha256, }), crate::config::Source::CratesIo { @@ -459,8 +459,8 @@ impl ResolvedSource { let mut url = url::Url::parse(&source_string[GIT_SOURCE_PREFIX.len()..])?; let mut query_pairs = url.query_pairs(); - let branch = query_pairs - .find(|(k, _)| k == "branch") + let r#ref = query_pairs + .find(|(k, _)| k == "branch" || k == "tag") .map(|(_, v)| v.to_string()); let rev = if let Some((_, rev)) = query_pairs.find(|(k, _)| k == "rev") { rev.to_string() @@ -479,7 +479,7 @@ impl ResolvedSource { Ok(ResolvedSource::Git(GitSource { url, rev, - r#ref: branch, + r#ref, sha256: None, })) } diff --git a/crate2nix/src/sources.rs b/crate2nix/src/sources.rs index db6a40bd..727fc696 100644 --- a/crate2nix/src/sources.rs +++ b/crate2nix/src/sources.rs @@ -34,11 +34,11 @@ pub fn crates_io_source(name: String, version: Version) -> Result Result { +pub fn git_io_source(url: Url, rev: String, r#ref: Option) -> Result { let prefetchable = GitSource { url: url.clone(), rev: rev.clone(), - r#ref: None, + r#ref: r#ref.clone(), sha256: None, }; @@ -46,7 +46,7 @@ pub fn git_io_source(url: Url, rev: String) -> Result { let sha256 = prefetchable.prefetch()?; eprintln!("done."); - Ok(config::Source::Git { url, rev, sha256: Some(sha256) }) + Ok(config::Source::Git { url, rev, r#ref, sha256: Some(sha256) }) } /// Operations on assmebling out-of-tree sources via nix. diff --git a/crate2nix/templates/Cargo.nix.tera b/crate2nix/templates/Cargo.nix.tera index 232288ac..6c442d14 100644 --- a/crate2nix/templates/Cargo.nix.tera +++ b/crate2nix/templates/Cargo.nix.tera @@ -130,12 +130,15 @@ rec { else {{crate.source.LocalDirectory.path | safe}}; {%- elif crate.source.Git %} workspace_member = null; - src = builtins.fetchGit { + src = builtins.fetchGit ({ url = {{crate.source.Git.url}}; rev = {{crate.source.Git.rev}}; - allRefs = true; submodules = true; - }; + } // (if isNull {% if crate.source.Git.ref %} {{crate.source.Git.ref}} {% else %} null {% endif %} then { + allRefs = true; + } else { + ref = {% if crate.source.Git.ref %} {{crate.source.Git.ref}} {% else %} null {% endif %}; + })); {%- else %} src = builtins.throw ''ERROR: Could not resolve source: {{crate.source | json_encode() | safe}}''; {%- endif -%} diff --git a/crate2nix/templates/crate2nix-sources.nix.tera b/crate2nix/templates/crate2nix-sources.nix.tera index 794b8685..33044390 100644 --- a/crate2nix/templates/crate2nix-sources.nix.tera +++ b/crate2nix/templates/crate2nix-sources.nix.tera @@ -40,12 +40,14 @@ rec { assert builtins.isString type; if type == "Git" - then builtins.fetchGit { + then builtins.fetchGit ({ url = source.url; rev = source.rev; - allRefs = true; submodules = true; - } + } // (if isNull source.ref + then { allRefs = true; } + else { inherit (source) ref; } + )) else if type == "CratesIo" then downloadFromCratesIo source else if type == "Nix" diff --git a/tools.nix b/tools.nix index 3c81aa03..12382288 100644 --- a/tools.nix +++ b/tools.nix @@ -162,26 +162,32 @@ rec { builtins.throw "unknown source type: ${source}"; # Extracts URL and rev from a git source URL. - # - # Crude, should be more robust :( parseGitSource = source: assert builtins.isString source; let - withoutGitPlus = lib.removePrefix "git+" source; - splitHash = lib.splitString "#" withoutGitPlus; - splitQuestion = lib.concatMap (lib.splitString "?") splitHash; - maybeBranchSplited = lib.splitString "branch=" withoutGitPlus; + extractRevision = source: lib.last (lib.splitString "#" source); + extractPart = part: source: if lib.hasInfix part source then lib.last (lib.splitString part (lib.head (lib.splitString "#" source))) else null; + extractRepoUrl = source: + let + splitted = lib.head (lib.splitString "?" source); + split = lib.substring 4 (lib.stringLength splitted) splitted; + in + lib.head (lib.splitString "#" split); + + revision = extractRevision source; + rev = extractPart "?rev=" source; + tag = extractPart "?tag=" source; + branch = extractPart "?branch=" source; + url = extractRepoUrl source; in { - url = builtins.head splitQuestion; - rev = lib.last splitQuestion; - branch = - if (lib.length maybeBranchSplited) == 1 then - "master" - else - ( - lib.head (lib.splitString "#" (lib.last maybeBranchSplited)) - ); + inherit revision url; + } // lib.optionalAttrs (! isNull rev) { + inherit rev; + } // lib.optionalAttrs (! isNull tag) { + inherit tag; + } // lib.optionalAttrs (! isNull branch) { + inherit branch; }; vendorSupport = { crateDir ? ./., ... }: @@ -240,11 +246,16 @@ rec { mkGitHash = { source, ... }@attrs: let parsed = parseGitSource source; - src = builtins.fetchGit { + ref = parsed.branch or parsed.tag or null; + src = builtins.fetchGit ({ + inherit (parsed) url; + rev = parsed.rev or parsed.revision; submodules = true; - inherit (parsed) url rev; - ref = parsed.branch or null; - }; + } // (if isNull ref then { + allRefs = true; + } else { + inherit ref; + })); hash = pkgs.runCommand "hash-of-${attrs.name}" { nativeBuildInputs = [ pkgs.nix ]; } '' echo -n "$(nix-hash --type sha256 ${src})" > $out ''; @@ -307,13 +318,19 @@ rec { assert builtins.isString source; let parsed = parseGitSource source; + has = name: attrs: ! isNull (attrs.${name} or null); + hasTag = has "tag"; + hasRev = has "rev"; + putTag = (hasTag attrs) || (hasTag parsed); + putRev = (hasRev attrs) || (hasRev parsed); in '' [source."${parsed.url}"] git = "${parsed.url}" - rev = "${parsed.rev}" - ${lib.optionalString (isNull (builtins.match ".*\\?rev=[0-9a-z]{40}.*" source)) ''branch = "${attrs.branch or parsed.branch or "master"}"''} + rev = "${parsed.rev or parsed.revision}" + ${lib.optionalString ((! putRev) && (! putTag)) ''branch = "${attrs.branch or parsed.branch or "master"}"''} + ${lib.optionalString ((! putRev) && putTag) ''tag = "${attrs.tag or parsed.tag}"''} replace-with = "vendored-sources" ''; gitSources = packagesByType."git" or [ ]; @@ -359,16 +376,17 @@ rec { assert (sourceType package) == "git"; let packageId = toPackageId package; - sha256 = - hashes.${packageId} - or extraHashes.${packageId} - or (builtins.throw "Checksum for ${packageId} not found in crate-hashes.json"); parsed = parseGitSource source; - src = builtins.fetchGit { - inherit (parsed) url rev; - allRefs = true; + ref = parsed.branch or parsed.tag or null; + src = builtins.fetchGit ({ + inherit (parsed) url; + rev = parsed.rev or parsed.revision; submodules = true; - }; + } // (if isNull ref then { + allRefs = true; + } else { + inherit ref; + })); srcName = "${name}-${version}"; rootCargo = builtins.fromTOML (builtins.readFile "${src}/Cargo.toml"); From a3251d295d4c45db0a54e8781a2068f531e3358e Mon Sep 17 00:00:00 2001 From: Yusuf Bera Ertan Date: Fri, 18 Jun 2021 18:01:05 +0300 Subject: [PATCH 4/7] fix: handle new cargo git vendoring case --- tools.nix | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/tools.nix b/tools.nix index 12382288..db75536b 100644 --- a/tools.nix +++ b/tools.nix @@ -249,7 +249,7 @@ rec { ref = parsed.branch or parsed.tag or null; src = builtins.fetchGit ({ inherit (parsed) url; - rev = parsed.rev or parsed.revision; + rev = parsed.revision; submodules = true; } // (if isNull ref then { allRefs = true; @@ -321,15 +321,26 @@ rec { has = name: attrs: ! isNull (attrs.${name} or null); hasTag = has "tag"; hasRev = has "rev"; + hasBranch = has "branch"; + putBranch = (hasBranch attrs) || (hasBranch parsed); putTag = (hasTag attrs) || (hasTag parsed); putRev = (hasRev attrs) || (hasRev parsed); + isNewerCargo = builtins.compareVersions pkgs.cargo.version "1.53.0" > (-1); in '' [source."${parsed.url}"] git = "${parsed.url}" - rev = "${parsed.rev or parsed.revision}" - ${lib.optionalString ((! putRev) && (! putTag)) ''branch = "${attrs.branch or parsed.branch or "master"}"''} + ${ + if isNewerCargo + then lib.optionalString putRev ''rev = "${attrs.rev or parsed.rev or parsed.revision}"'' + else ''rev = "${attrs.rev or parsed.rev or parsed.revision}"'' + } + ${ + if isNewerCargo + then lib.optionalString ((! putRev) && putBranch) ''branch = "${attrs.branch or parsed.branch}"'' + else lib.optionalString ((! putRev) && (! putTag)) ''branch = "${attrs.branch or parsed.branch or "master"}"'' + } ${lib.optionalString ((! putRev) && putTag) ''tag = "${attrs.tag or parsed.tag}"''} replace-with = "vendored-sources" ''; @@ -380,7 +391,7 @@ rec { ref = parsed.branch or parsed.tag or null; src = builtins.fetchGit ({ inherit (parsed) url; - rev = parsed.rev or parsed.revision; + rev = parsed.revision; submodules = true; } // (if isNull ref then { allRefs = true; From 9845606de6308aed815d59c04e6cc1dbc0b4a9ae Mon Sep 17 00:00:00 2001 From: Yusuf Bera Ertan Date: Sat, 19 Jun 2021 21:29:33 +0300 Subject: [PATCH 5/7] fix: tag refs handling --- tools.nix | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/tools.nix b/tools.nix index db75536b..2eb95a10 100644 --- a/tools.nix +++ b/tools.nix @@ -190,6 +190,23 @@ rec { inherit branch; }; + getSrcFromGitSource = source: + let + parsed = parseGitSource source; + ref = parsed.branch or parsed.tag or null; + in builtins.fetchGit ({ + inherit (parsed) url; + rev = parsed.revision; + submodules = true; + } // (if isNull ref then { + allRefs = true; + } else { + ref = + if ref == (parsed.tag or null) + then "refs/tags/${ref}" + else ref; + })); + vendorSupport = { crateDir ? ./., ... }: rec { toPackageId = { name, version, source, ... }: @@ -245,17 +262,7 @@ rec { mkGitHash = { source, ... }@attrs: let - parsed = parseGitSource source; - ref = parsed.branch or parsed.tag or null; - src = builtins.fetchGit ({ - inherit (parsed) url; - rev = parsed.revision; - submodules = true; - } // (if isNull ref then { - allRefs = true; - } else { - inherit ref; - })); + src = getSrcFromGitSource source; hash = pkgs.runCommand "hash-of-${attrs.name}" { nativeBuildInputs = [ pkgs.nix ]; } '' echo -n "$(nix-hash --type sha256 ${src})" > $out ''; @@ -387,17 +394,7 @@ rec { assert (sourceType package) == "git"; let packageId = toPackageId package; - parsed = parseGitSource source; - ref = parsed.branch or parsed.tag or null; - src = builtins.fetchGit ({ - inherit (parsed) url; - rev = parsed.revision; - submodules = true; - } // (if isNull ref then { - allRefs = true; - } else { - inherit ref; - })); + src = getSrcFromGitSource source; srcName = "${name}-${version}"; rootCargo = builtins.fromTOML (builtins.readFile "${src}/Cargo.toml"); From bbdcd4a33b99e26db40f43a10110e87cdf2afc8b Mon Sep 17 00:00:00 2001 From: Yusuf Bera Ertan Date: Mon, 12 Jul 2021 20:11:37 +0300 Subject: [PATCH 6/7] fix: weird sources not declaring their crates in workspace members --- tools.nix | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tools.nix b/tools.nix index 2eb95a10..964d4690 100644 --- a/tools.nix +++ b/tools.nix @@ -408,14 +408,19 @@ rec { in cargoTomlCrate.package.name; + filteredPaths = + builtins.filter + (to_filter: getCrateNameFromPath to_filter == name) + containedCrates; + pathToExtract = if isWorkspace then - builtins.head - (builtins.filter - (to_filter: - (getCrateNameFromPath to_filter) == name - ) - containedCrates) + # Workaround for sources that have isWorkspace as true, but don't + # declare all their members in `workspace.members` + if builtins.length filteredPaths > 0 + then builtins.head filteredPaths + # This does not cover all possible cases, only is a last ditch effort + else name else "."; in From 9f6d5f15f8f8dbe5cf615660097dfb37f76823db Mon Sep 17 00:00:00 2001 From: Yusuf Bera Ertan Date: Mon, 2 Aug 2021 23:51:35 +0300 Subject: [PATCH 7/7] fix: make sure to add a key to the git source replacements so they are distinguished --- tools.nix | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tools.nix b/tools.nix index 964d4690..328cad3a 100644 --- a/tools.nix +++ b/tools.nix @@ -333,10 +333,14 @@ rec { putTag = (hasTag attrs) || (hasTag parsed); putRev = (hasRev attrs) || (hasRev parsed); isNewerCargo = builtins.compareVersions pkgs.cargo.version "1.53.0" > (-1); + key = if putRev then "?rev=${attrs.rev or parsed.rev}" else + if putTag then "?tag=${attrs.tag or parsed.tag}" else + if putBranch then "?branch=${attrs.branch or parsed.branch}" else + ""; in '' - [source."${parsed.url}"] + [source."${parsed.url}${key}"] git = "${parsed.url}" ${ if isNewerCargo