Skip to content

Commit

Permalink
builtins.fetchGit: use same name as nix-prefetch-git
Browse files Browse the repository at this point in the history
When using npins with (generic, i.e. non-github) git repositories, we
currently have the issue of having to download these twice: once while
doing `npins add git ...`, and then a second time when evaluating the
source attribute in Nix.

The source of this is that nix-prefetch-git, which we use to get the
hash, sets as name for the store path it produces a name derived from
the url; this behaviour is not configurable [1]. In contrast,
builtins.fetchGit sets the name as "source" by default.

The nixpkgs fetcher uses a nix implementation of the same logic to
derive the desired name in nix [2].

This commit adds a function which does the same, derived from the one in
Nixpkgs, but tweaked to only use builtin functions, and uses it to set
an appropriate name for the git fetcher.

[1] https://github.com/NixOS/nixpkgs/blob/c215fb18dc1c4ef927c773078d2365fc559365c5/pkgs/build-support/fetchgit/nix-prefetch-git#L142
[2] https://github.com/NixOS/nixpkgs/blob/2116a1123122d639b7c69e47284e4b35198c3fa6/pkgs/build-support/fetchgit/default.nix#L2
  • Loading branch information
stuebinm committed May 6, 2024
1 parent f5559eb commit cee7023
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions npins/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ let
revision,
url ? null,
hash,
branch ? null,
...
}:
assert repository ? type;
Expand All @@ -39,9 +40,24 @@ let
})
else
assert repository.type == "Git";
let
urlToName =
url: rev:
let
matched = builtins.match "^.*/([^/]*)(\\.git)?$" repository.url;

short = builtins.substring 0 7 rev;

appendShort =
if (branch == null && (builtins.match "[a-f0-9]*" rev) != null) then "-${short}" else "";
in
"${if matched == null then "source" else builtins.head matched}${appendShort}";
name = urlToName repository.url revision;
in
builtins.fetchGit {
url = repository.url;
rev = revision;
inherit name;
# hash = hash;
};

Expand Down

0 comments on commit cee7023

Please sign in to comment.