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

git-lfs support #10153

Open
wants to merge 61 commits into
base: master
Choose a base branch
from
Open

git-lfs support #10153

wants to merge 61 commits into from

Conversation

b-camacho
Copy link

@b-camacho b-camacho commented Mar 4, 2024

Motivation

nix fetches git repos using libgit2, which does not run filters by default. This means LFS-enabled repos can be fetched, but LFS pointer files are not smudged.

This change adds a lfs attribute to fetcher URLs. With lfs=1, when fetching LFS-enabled repos, nix will smudge all the files.

Context

See #10079.
Git Large File Storage lets you track large files directly in git, using git filters. A clean filter runs on your LFS-enrolled files before push, replacing large files with small "pointer files". Upon checkout, a "smudge" filter replaces pointer files with full file contents. When this works correctly, it is not visible to users, which is nice.

Changes

  • builtins.fetchGit has new bool lfs attr
  • when lfs=true, GitSourceAccessor will smudge any pointer files with the lfs filter attribute
  • as verified by new test in tests/nixos/fetchgit (this is why lfs is now enabled on the test gitea instance)

Priorities and Process

Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@github-actions github-actions bot added the fetching Networking with the outside (non-Nix) world, input locking label Mar 4, 2024
@b-camacho
Copy link
Author

Small complication:
it seems that nix flake lock calls fetch which in turn calls Input::fetch -> InputScheme::fetch -> fetchToStore.

In other words, I don't currently see a way to:

  • materialize LFS files when fetching -source store paths, but
  • don't materialize LFS files during nix flake lock.

bew added a commit to bew/nixed-os-configs that referenced this pull request Mar 8, 2024
Using git-lfs, when the flake copies the repo to the store (for purity)
the 'virtual file' stored in git is copied (with oid/size info of the
object in LFS) instead of the actual (large) file :/
ref: NixOS/nix#10153

I think it was working before because the file was in git temporarily at
some point, then I moved it to LFS, but after the system was built..
(or something like that 🤷)
bew added a commit to bew/nixed-os-configs that referenced this pull request Mar 8, 2024
Using git-lfs, when the flake copies the repo to the store (for purity)
the 'virtual file' stored in git is copied (with oid/size info of the
object in LFS) instead of the actual (large) file :/
ref: NixOS/nix#10153

I think it was working before because the file was in git temporarily at
some point, then I moved it to LFS, but after the system was built..
(or something like that 🤷)
bew added a commit to bew/nixed-os-configs that referenced this pull request Mar 8, 2024
Using git-lfs, when the flake copies the repo to the store (for purity)
the 'virtual file' stored in git is copied (with oid/size info of the
object in LFS) instead of the actual (large) file :/
ref: NixOS/nix#10153

I think it was working before because the file was in git temporarily at
some point, then I moved it to LFS, but after the system was built..
(or something like that 🤷)
@L-as
Copy link
Member

L-as commented Mar 9, 2024

What use case do you have in mind? Isn't LFS typically for large files, that wouldn't usually affect evaluation anyway?

@b-camacho
Copy link
Author

What use case do you have in mind? Isn't LFS typically for large files, that wouldn't usually affect evaluation anyway?

builtins.fetchGit populates the nix store with a <hash>-store path. This path is used as the source when building a derivation. Currently, the builder will see the unsmudged LFS pointer files, but I'd like the builder to optionally see smudged files. I agree that smudged files are usually not needed at eval time, but I don't see a good alternative of making them available at build time, besides a fixed-output derivation (but fixed-output derivations have their own problems)

@L-as
Copy link
Member

L-as commented Mar 14, 2024

A FOD seems optimal here, in general you shouldn't use builtins.fetchGit if you're only going to use it at build time.

@b-camacho
Copy link
Author

In general I agree, but (afaik) other fetchers can't use git credentials.

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/2024-03-11-nix-team-meeting-132/42960/1

@khoitd1997
Copy link

@roberth have you had a chance to take a look at this issue? We have been staying at older versions of Nix as a workaround but newer versions now have fixes for critical issues so sticking to old ones would no longer be optimal.

@roberth
Copy link
Member

roberth commented Jun 22, 2024

Hi @b-camacho, thanks for the ping and sorry for the delay. This PR was assigned to me, but I hadn't prioritized it because it was a draft. Wrong assumption on my end, because I do think this is valuable, and I have some things to say :)

when fetching LFS-enabled repos, nix will smudge all the files.

That's a good start, but we need to make sure that the smudging happens in a controlled manner; otherwise we risk adding impurities.

Specifically, we should parse the attribute to check that they're supposed to be unsmudged by lfs; if not, ignore the smudge rule. It seems you were already investigating how this could be implemented.

Furthermore, we should validate the sha256 so that we don't increase the potential for silent errors by a whole external program. The hash should be easy to parse from the pointer file, and while reading other programs' inputs is a little ad hoc, I don't expect any serious issues from this, as we won't cause users to accidentally rely on a bug this way.


the LFS-tracked files are materialized during nix flake lock - this is bad

This won't happen unnecessarily either of these are implemented

If we need to backtrack on the removal of narHashes (#6530), we can also avoid re-locking transitive inputs whose lock has already been computed by the dependency's lock.

So yes, this isn't efficient yet, but it will be.

A FOD seems optimal here, in general you shouldn't use builtins.fetchGit if you're only going to use it at build time.

A fixed output derivation works best when all you're using it for is as an input to another derivation (and it's publicly available, as mentioned).
However, if the "fetched" source is a flake (e.g. you have a flake.nix in a repo with LFS files), then you also need to evaluate files from the fetched source, which would constitute import from derivation, which is not optimal. Furthermore you'd need to produce fixed-output hashes for your local repo files, which is such horrible UX we don't need to consider it as a solution.


To summarize, this is worth implementing, I see no blocking issues, design or otherwise, and the following needs to be done:

  • lfs attribute with default false, LGTM
  • figure out which files are LFS
  • invoke the Git LFS filter from $PATH; no need for a rigid dependency or makeWrapper
  • check the sha256
  • add a test, perhaps extending tests/nixos/fetch-git
  • documentation for the lfs attribute (currently under fetchGit's entry in doc/manual/src/language/builtins.md); mention the runtime dependency on the LFS package.

@kip93
Copy link

kip93 commented Jul 23, 2024

What's the state on this PR? Seems to unfortunately be a bit stale given the delayed review. This issue has been plaguing us for a while, so I'm willing to pick up the torch here and try to get this out the door (was actually starting to see how to fix this myself back in March when I saw this PR and decided to see what came out of this).

@roberth
Copy link
Member

roberth commented Jul 23, 2024

@kip93 I think your question was directed towards @b-camacho, but I'd like to add that we would welcome and support anyone who'd like to work on this.

Feel free to ask questions here or in the meetings if you can make them. We generally have some agenda, but we also like to make time for contributors during or after, when we often hang out while we get some things done. Link to the video conference is in the scratchpad linked there. We also have a matrix room, although personally I'm guilty of neglecting that one sometimes.

@b-camacho
Copy link
Author

Thanks for the thorough writeup @roberth !
I owe you all an update. To avoid shelling out and implement some features we need to merge, I need a subset of a git-lft C/C++ client. I reimplemented one from Python into C++ here https://github.com/b-camacho/git-lfs-fetch-cpp.

Once I add some tests and integrate git-lfs-fetch-cpp here, we should be ready for another review!

I'm still on vacation with not-great internet, but back in 6 days and will update you all on 7/31 regardless.

Thanks for the feedback and sorry for the wait!

@roberth
Copy link
Member

roberth commented Jul 24, 2024

Oh, I don't think shelling out was such a big deal because we can verify the correctness of the result, kind of like how fixed output derivations are allowed to do "grossly impure" things because we can verify the output.

I guess a library implementation of it is still nice for a consistent UX with a small closure size though.

@L-as

This comment was marked as off-topic.

@roberth

This comment was marked as off-topic.

@L-as

This comment was marked as off-topic.

@kip93
Copy link

kip93 commented Oct 10, 2024

Hey! It's me again! I just want to ask if there's anything I can help with here. Maybe I can try and doing some testing, or do a smaller version of this that uses the git-lfs CLI tools while the full implementation gets done?

We have a lot of repos with LFS files that would greatly benefit from this, so I'm willing to do whatever work is needed, but also don't want to add extra work for others where it's not wanted.

@kip93
Copy link

kip93 commented Dec 18, 2024

Ok I've reintroduced the git_attr_get_ext and have it working. I'm currently testing that it does not import files into the store too early like it used to be the case the first time around.

Also rewrote the tests to use subtests as suggested by the code review.

@kip93
Copy link

kip93 commented Dec 19, 2024

With my quick-ish tests (with several large lfs repos which take a long time to clone) it all looks ok? I think it's ready for another review.

Given that holidays are around the corner, I won't have any more time to work on this 'till after new years (of course, I'm also not expecting you to skip xmas to review this, I'm fine if the review is delayed as well). If anything needs fixing I can take this back up again probably 6-ish of january.

src/libfetchers/git-utils.cc Outdated Show resolved Hide resolved
src/libfetchers/git-utils.cc Outdated Show resolved Hide resolved
src/libfetchers/git-utils.cc Outdated Show resolved Hide resolved
src/libfetchers/git-utils.cc Outdated Show resolved Hide resolved
src/libfetchers/git-lfs-fetch.hh Outdated Show resolved Hide resolved
src/libfetchers/git-lfs-fetch.hh Outdated Show resolved Hide resolved
src/libfetchers/git-lfs-fetch.hh Outdated Show resolved Hide resolved
src/libfetchers/git-lfs-fetch.hh Outdated Show resolved Hide resolved
src/libfetchers/git-lfs-fetch.hh Outdated Show resolved Hide resolved
@kip93
Copy link

kip93 commented Jan 10, 2025

Hey! Been back at it for a couple of days now, trying to implement the code reviews. Currently, only the reworks of Fetch::fetchUrls is left, but I might or might not be down a rabbit hole since the FileTransfer::download (nor the FileTransfer::upload) quite do what I need it to. Will let you guys know when it's all done.

kip93 added 3 commits January 10, 2025 16:11
Plus, switched CURLOPT_PROGRESSFUNCTION to CURLOPT_XFERINFOFUNCTION since docs say it's deprecated
@kip93 kip93 requested a review from Ericson2314 as a code owner January 10, 2025 18:19
@kip93
Copy link

kip93 commented Jan 13, 2025

Been testing it, looks like it's working for me. I'd say this is ready for another review.

src/libfetchers/git-lfs-fetch.hh Outdated Show resolved Hide resolved
src/libfetchers/git-lfs-fetch.hh Outdated Show resolved Hide resolved
src/libfetchers/git-lfs-fetch.hh Outdated Show resolved Hide resolved
Comment on lines 232 to 234
// example resp here:
// {"objects":[{"oid":"f5e02aa71e67f41d79023a128ca35bad86cf7b6656967bfe0884b3a3c4325eaf","size":10000000,"actions":{"download":{"href":"https://gitlab.com/b-camacho/test-lfs.git/gitlab-lfs/objects/f5e02aa71e67f41d79023a128ca35bad86cf7b6656967bfe0884b3a3c4325eaf","header":{"Authorization":"Basic
// Yi1jYW1hY2hvOmV5SjBlWEFpT2lKS1YxUWlMQ0poYkdjaU9pSklVekkxTmlKOS5leUprWVhSaElqcDdJbUZqZEc5eUlqb2lZaTFqWVcxaFkyaHZJbjBzSW1wMGFTSTZJbUptTURZNFpXVTFMVEprWmpVdE5HWm1ZUzFpWWpRMExUSXpNVEV3WVRReU1qWmtaaUlzSW1saGRDSTZNVGN4TkRZeE16ZzBOU3dpYm1KbUlqb3hOekUwTmpFek9EUXdMQ0psZUhBaU9qRTNNVFEyTWpFd05EVjkuZk9yMDNkYjBWSTFXQzFZaTBKRmJUNnJTTHJPZlBwVW9lYllkT0NQZlJ4QQ=="}}},"authenticated":true}]}
Copy link
Member

Choose a reason for hiding this comment

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

This example may be suitable for a unit test.

Copy link

Choose a reason for hiding this comment

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

To test this function would require at least an http server and for full test also an ssh server, tried to find if there's some other test I can reference from but none that I could find.

src/libfetchers/git-lfs-fetch.hh Outdated Show resolved Hide resolved
src/libfetchers/git-lfs-fetch.hh Outdated Show resolved Hide resolved
const auto md = parseLfsMetadata(std::string(content), std::string(pointerFilePath));
if (md == std::nullopt) {
debug("Skip git-lfs, invalid pointer file");
warn("Encountered a file that should have been a pointer, but wasn't: %s", pointerFilePath);
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer for this and the above error to be an error, so that a potential "outdated" version of Nix won't succeed with the wrong result.

Copy link

Choose a reason for hiding this comment

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

This is the one thing I did not do (yet?) since the current implementation replicates the behaviour of the git-lfs package. If we make this an error there will be a disparity that might confuse people.

Now, the ideal thing would be for people to get their shit together and not have half-baked repos with weird lfs but not really lfs files. But I've seen this in the wild (actually run into this when looking for example repos to test this implementation, so it's not as rare as one might like to think). And I think it's common with nix to package other people's code, so we might not have the possibility of fixing the root cause.

So we either also throw a warning (risky for compatibility with multiple nix versions) or we diverge and fail in these cases (risky due to incompatibilities with some existing repos), but then we need to document that malformed repos will not work with our implementation.

I'm fine with either, and I AM somewhat inclined to make this a hard fail, but I don't think it's a choice that should be made lightly.

src/libfetchers/git-lfs-fetch.hh Outdated Show resolved Hide resolved
Eventually this should probably become a struct of options.
Copy link
Member

@edolstra edolstra left a comment

Choose a reason for hiding this comment

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

Did some code review/cleanup, otherwise looks great! Thanks!

@kip93
Copy link

kip93 commented Feb 10, 2025

Nice, thanks for the support! Looks like with my subpar c++ skills I missed some details (:

Now, from what I see there's 2 final talking points.

  1. The TODO that you've added. We're using the StringSink just to validate the download was not tampered with. Probably not NECESSARY, just a nice to have, so we could drop this in favour of a generic sink.
  2. The talk about warn vs fail on invalid lfs files. The warn behaviour matches the default git-lfs implementation, but the hard fail is probably best for reproducibility. I think changing to a fail might be best, even if it differs from the default behaviour, we don't need to support broken setups, and these are simple to fix on their side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation fetching Networking with the outside (non-Nix) world, input locking
Projects
Status: 🏁 Review
Development

Successfully merging this pull request may close these issues.

10 participants