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

builtins.fetchGit: use same name as nix-prefetch-git #74

Merged
merged 2 commits into from
May 11, 2024

Conversation

stuebinm
Copy link
Contributor

@stuebinm stuebinm commented May 6, 2024

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. 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.

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. With it, a the name exposed by npins to nix code changes to the one that nix-prefetch-git also used, thus we avoid this doubled download after adding a new source.

This logic is somewhat delicate. I've added a safe guard so that if it ever fails to extract a name, we fall back to using "source" as attribute name.

Note that while this is a similar issue to #57, it is (afaict, anyways) not related to builtins.fetchGit not using the fetcher cache. That's a separate issue which also causes doubled downloads.

@stuebinm stuebinm force-pushed the nix-prefetch-git-names branch from 138887e to cee7023 Compare May 6, 2024 22:13
Copy link
Owner

@andir andir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! This sounds really useful! I'm currently curious how to keep this from regressing going forward. Could you come up with one or two tests that assure that the output names are as expected?

stuebinm added 2 commits May 7, 2024 16:14
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
These three tests should (afaict) cover all cases that can occur. In all
of them, no new path should be added to the store after `npins add' has
been run. If a new path does get added, we've got an avoidable cache
miss, which is a bug.
@stuebinm stuebinm force-pushed the nix-prefetch-git-names branch from cee7023 to 24ec372 Compare May 7, 2024 14:17
@stuebinm
Copy link
Contributor Author

stuebinm commented May 7, 2024

I've added some tests, which should hopefully cover all uses of npins add git which actually occur. Incidentally, they already helped me find a bug in my initial commit, so seems like having them is definitely worthwhile :)

@andir andir merged commit d640c29 into andir:master May 11, 2024
1 check passed
@stuebinm stuebinm deleted the nix-prefetch-git-names branch June 4, 2024 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants