From e99c059cbcb687737412d57f891bf383073545a0 Mon Sep 17 00:00:00 2001 From: Mifom Date: Sun, 19 Dec 2021 01:08:31 +0300 Subject: [PATCH 1/5] Add registry independence --- flake.nix | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/flake.nix b/flake.nix index d749eb1..e8607cc 100644 --- a/flake.nix +++ b/flake.nix @@ -14,14 +14,19 @@ let isGit = builtins.match ''git\+(.*)\?rev=([0-9a-f]+)(#.*)?'' pkg.source; - registry = "registry+https://github.com/rust-lang/crates.io-index"; + isRegistry = builtins.match ''registry\+(.*)'' pkg.source; in - if pkg.source == registry then + if isRegistry != null then let - sha256 = pkg.checksum or lockFile'.metadata."checksum ${pkg.name} ${pkg.version} (${registry})"; - tarball = import { - url = "https://crates.io/api/v1/crates/${pkg.name}/${pkg.version}/download"; + regIndex = builtins.fetchGit { + url = builtins.elemAt isRegistry 0; + }; + regConfig = builtins.readFile (regIndex + /config.json); + regUrl = (builtins.fromJSON regConfig).dl; + sha256 = pkg.checksum or lockFile'.metadata."checksum ${pkg.name} ${pkg.version} (${pkg.source})"; + tarball = builtins.fetchurl { + url = "${regUrl}/${pkg.name}/${pkg.version}/download"; inherit sha256; }; in pkgs.runCommand "${pkg.name}-${pkg.version}" {} From 6e48b5405d4005104dfd6d5a67836e892b3e11a5 Mon Sep 17 00:00:00 2001 From: Mifom Date: Sun, 19 Dec 2021 01:08:42 +0300 Subject: [PATCH 2/5] Get registries by attr --- flake.nix | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/flake.nix b/flake.nix index e8607cc..45a110d 100644 --- a/flake.nix +++ b/flake.nix @@ -4,9 +4,13 @@ outputs = { self }: rec { builders.importCargo = - { lockFile, pkgs }: - let lockFile' = builtins.fromTOML (builtins.readFile lockFile); in - rec { + { lockFile, pkgs, registries ? {} }: + let + lockFile' = builtins.fromTOML (builtins.readFile lockFile); + registriesUrl = { + "https://github.com/rust-lang/crates.io-index" = "https://crates.io/api/v1/crates/"; + } // registries; + in rec { # Fetch and unpack the crates specified in the lock file. unpackedCrates = map @@ -19,11 +23,8 @@ if isRegistry != null then let - regIndex = builtins.fetchGit { - url = builtins.elemAt isRegistry 0; - }; - regConfig = builtins.readFile (regIndex + /config.json); - regUrl = (builtins.fromJSON regConfig).dl; + registry = builtins.elemAt isRegistry 0; + regUrl = registriesUrl."${registry}" or (throw ''Unsupported registry: ${registry}. Add {"${registry}" = ; ... } to registries''); sha256 = pkg.checksum or lockFile'.metadata."checksum ${pkg.name} ${pkg.version} (${pkg.source})"; tarball = builtins.fetchurl { url = "${regUrl}/${pkg.name}/${pkg.version}/download"; @@ -82,6 +83,16 @@ cat > $out/vendor/config < Date: Sun, 19 Dec 2021 01:08:48 +0300 Subject: [PATCH 3/5] Add allRefs to git deps --- flake.nix | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 45a110d..202c9e8 100644 --- a/flake.nix +++ b/flake.nix @@ -44,7 +44,10 @@ let rev = builtins.elemAt isGit 1; url = builtins.elemAt isGit 0; - tree = builtins.fetchGit { inherit url rev; }; + tree = builtins.fetchGit { + inherit url rev; + allRefs = true; + }; in pkgs.runCommand "${pkg.name}-${pkg.version}" {} '' tree=${tree} From eccab1a17098cdd7b04783d39d0da737a2561cba Mon Sep 17 00:00:00 2001 From: Mifom Date: Thu, 31 Mar 2022 22:00:45 +0300 Subject: [PATCH 4/5] Improve git deps parsing --- flake.nix | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/flake.nix b/flake.nix index 202c9e8..a0dbfd1 100644 --- a/flake.nix +++ b/flake.nix @@ -17,7 +17,17 @@ (pkg: let - isGit = builtins.match ''git\+(.*)\?rev=([0-9a-f]+)(#.*)?'' pkg.source; + gitRegexp = x: builtins.match "git\+(.*)\?((branch=(.+)#([0-9a-f]+)(#.*)?)|(rev=([0-9a-f]+)(#.*)?))" x; + isGit = let gitArr = gitRegexp pkg.source; in + if isNull gitArr then + null + else + let url_ = builtins.elemAt gitArr 0; + url = builtins.substring 1 (builtins.stringLength url_ - 2) url_; + rev4 = builtins.elemAt gitArr 3; + rev5 = builtins.elemAt gitArr 4; + rev8 = builtins.elemAt gitArr 7; + in if isNull rev4 then [url null rev8] else [url rev4 rev5]; isRegistry = builtins.match ''registry\+(.*)'' pkg.source; in @@ -42,7 +52,8 @@ else if isGit != null then let - rev = builtins.elemAt isGit 1; + rev = builtins.elemAt isGit 2; + branch = builtins.elemAt isGit 1; url = builtins.elemAt isGit 0; tree = builtins.fetchGit { inherit url rev; @@ -65,8 +76,9 @@ printf '{"files":{},"package":null}' > "$out/.cargo-checksum.json" cat > $out/.cargo-config < Date: Mon, 4 Apr 2022 23:34:19 +0300 Subject: [PATCH 5/5] Inner crates in git repos parsing --- flake.nix | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index a0dbfd1..9352e7a 100644 --- a/flake.nix +++ b/flake.nix @@ -59,11 +59,12 @@ inherit url rev; allRefs = true; }; + isInner = ((fromTOML (builtins.readFile (tree + /Cargo.toml))).package or {}).name != pkg.name; in pkgs.runCommand "${pkg.name}-${pkg.version}" {} '' tree=${tree} - if grep --quiet '\[workspace\]' $tree/Cargo.toml; then + if ${if isInner then "true" else "false"} || grep --quiet 'name = \[workspace\]' $tree/Cargo.toml; then if [[ -e $tree/${pkg.name} ]]; then tree=$tree/${pkg.name} fi