Skip to content

fetchgit: Support sparse checkout#135881

Merged
roberth merged 4 commits intoNixOS:masterfrom
azuwis:master
Jan 25, 2022
Merged

fetchgit: Support sparse checkout#135881
roberth merged 4 commits intoNixOS:masterfrom
azuwis:master

Conversation

@azuwis
Copy link
Contributor

@azuwis azuwis commented Aug 27, 2021

Motivation for this change

This allow git checkout small parts of a large repo, and avoid fetching
unnecessary blobs from server.

For example, the nerd-fonts git repo, even with --depth=1, need network transfer > 2G. Using sparse checkout and partialclonefilter "blob:none", we can fetch a subdir for < 20M:

{ lib, stdenvNoCC, fetchgit }:

stdenvNoCC.mkDerivation rec {
  name = "jetbrains-mono-nerdfonts";

  src = fetchgit {
    url = "https://github.com/ryanoasis/nerd-fonts.git";
    rev = "bc4416e176d4ac2092345efd7bcb4abef9d6411e";
    sparseCheckout = ''
      patched-fonts/JetBrainsMono/Ligatures
    '';
    sha256 = "04ag7p8jvs6628qfhkyh7w5584xc2d27iskwl7sq8anzjv5xmrlg";
  };

  installPhase = ''
    mkdir -p $out/share/fonts/truetype
    cp patched-fonts/JetBrainsMono/Ligatures/*/complete/*Complete.ttf $out/share/fonts/truetype
  '';

  meta = with lib; {
    description = "Iconic font aggregator, collection, & patcher. 3,600+ icons, 50+ patched fonts";
    homepage = "https://nerdfonts.com/";
    license = licenses.mit;
  };
}
Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • 21.11 Release Notes (or backporting 21.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

@github-actions github-actions bot added the 6.topic: fetch Fetchers (e.g. fetchgit, fetchsvn, ...) label Aug 27, 2021
@ofborg ofborg bot added 10.rebuild-darwin: 11-100 This PR causes between 11 and 100 packages to rebuild on Darwin. 10.rebuild-linux: 11-100 This PR causes between 11 and 100 packages to rebuild on Linux. labels Aug 27, 2021
@r-rmcgibbo
Copy link

r-rmcgibbo commented Aug 27, 2021

Result of nixpkgs-review pr 135881 at f48630c1 run on x86_64-linux 1

15 packages built successfully:
  • bundix
  • cabal2nix
  • crate2nix
  • crystal2nix
  • dep2nix
  • go2nix
  • haskellPackages.cabal2nix-unstable
  • haskellPackages.nvfetcher
  • haskellPackages.update-nix-fetchgit
  • nix-prefetch-git
  • nix-prefetch-scripts
  • nix-update-source
  • nvfetcher
  • update-nix-fetchgit
  • vgo2nix

Result of nixpkgs-review pr 135881 at bfebcde8 run on aarch64-linux 1

14 packages built successfully:
  • bundix
  • cabal2nix
  • crate2nix
  • dep2nix
  • go2nix
  • haskellPackages.cabal2nix-unstable
  • haskellPackages.nvfetcher
  • haskellPackages.update-nix-fetchgit
  • nix-prefetch-git
  • nix-prefetch-scripts
  • nix-update-source
  • nvfetcher
  • update-nix-fetchgit
  • vgo2nix

@roberth roberth mentioned this pull request Aug 28, 2021
12 tasks
@Artturin
Copy link
Member

Artturin commented Sep 8, 2021

Looks useful, could you add documentation for it too? https://nixos.org/manual/nixpkgs/stable/#fetchgit
doc/builders/fetchers.chapter.md doc/builders/fetchers.chapter.md

Should something be added to fetchFromGitHub to make it possible to use this with it?

@github-actions github-actions bot added the 8.has: documentation This PR adds or changes documentation label Sep 8, 2021
@roberth
Copy link
Member

roberth commented Oct 2, 2021

Could you add a test case in pkgs/build-support/fetchgit/tests.nix?

@azuwis
Copy link
Contributor Author

azuwis commented Oct 7, 2021

Could you please give some tips about how to write and run tests?

I read https://nixos.org/manual/nixpkgs/stable/#sec-package-tests, but can not figure out.

❯ nix-build -A hello.tests   
these derivations will be built:
  /nix/store/k7xrpk94bz0ny7ql321v0z2lwf4mhfyb-test-version.drv
these paths will be fetched (5.57 MiB download, 29.61 MiB unpacked):
  /nix/store/2yccw7959qx77a5yw612xd54mclmnxag-gmp-6.2.1
  /nix/store/3z7w87pg7xcghgdmw450qc25ws99wpfj-libiconv-50
  ...
building '/nix/store/k7xrpk94bz0ny7ql321v0z2lwf4mhfyb-test-version.drv'...
hello (GNU Hello) 2.10
/nix/store/a29vrm7h17cyxpcpaqa0m2wymidw1d59-test-version

❯ nix-build -A fetchgit.tests
error: cannot auto-call a function that has an argument without a default value ('url')

@Artturin
Copy link
Member

Artturin commented Oct 7, 2021

@roberth
Copy link
Member

roberth commented Oct 7, 2021

Originally I introduced an extra attribute for the purpose of running the tests, but in the end I cut it because it shouldn't be necessary.

You can run them from nix repl .

nix-repl> :b fetchgit.tests.simple

this derivation produced the following outputs:
  out -> /nix/store/j9yzk4f0z66va8qy31wc46a9k0wvsh7r-nix-source-salted-gy6zpkzq0gmx

or nix build .#fetchgit.tests.simple.

Neither of these run all tests though, so maybe packageTests should be reintroduced. See 8651143

@azuwis
Copy link
Contributor Author

azuwis commented Nov 8, 2021

Test case added, anything else need to be done?

Copy link
Member

@roberth roberth left a comment

Choose a reason for hiding this comment

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

Just a question and a comment. Otherwise, LGTM.

Copy link
Member

Choose a reason for hiding this comment

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

Did you consider using git sparse-checkout set --stdin?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem necessary. If you want to make it "correct" you could use printf %q, but the real solution is to replace this code by something simple, like a loop that pops arguments using shift.
I'll ignore it for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is necessary because --sparse-checkout arg may contain "\n", without this change, the following test fails.

~/src/nixpkgs master* ⇣⇡
❯ git diff
diff --git a/pkgs/build-support/fetchgit/nix-prefetch-git b/pkgs/build-support/fetchgit/nix-prefetch-git
index f940d10b2ae..be595e59b0c 100755
--- a/pkgs/build-support/fetchgit/nix-prefetch-git
+++ b/pkgs/build-support/fetchgit/nix-prefetch-git
@@ -98,7 +98,7 @@ for arg; do
         case $argfun in
             set_*)
                 var=${argfun#set_}
-                eval $var="\"$arg\""
+                eval $var=$arg
                 ;;
         esac
         argfun=""

~/src/nixpkgs master* ⇣⇡
❯ nix repl .
Welcome to Nix version 2.5pre20211007_844dd90. Type :? for help.

Loading '.'...
Added 15361 variables.

nix-repl> :b fetchgit.tests.sparseCheckout                    
error: builder for '/nix/store/cnh4q2g2vmxl6l4cjq28677hqdxrwxbx-nix-source-salted-bydqcp0cva5n.drv' failed with exit code 127;
       last 2 log lines:
       > exporting https://github.com/NixOS/nix (rev 9d9dbe6ed05854e03811c361a3380e09183f4f4a) into /nix/store/yw6ycpfg088q7jswqjgjj19724drl3ji-nix-source-salted-bydqcp0cva5n
       > /nix/store/k4wdi6yqznx8qww12v715sz46vd4p1jf-nix-prefetch-git: line 101: tests: command not found
       For full logs, run 'nix log /nix/store/cnh4q2g2vmxl6l4cjq28677hqdxrwxbx-nix-source-salted-bydqcp0cva5n.drv'.

The tests comes from:

    sparseCheckout = ''
      src
      tests
    '';

eval without quote leads to code injection.

Copy link
Member

Choose a reason for hiding this comment

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

How about something like eval "$var=$(printf %q "$arg")"

It could also append to the previous value, so it can be used multiple times on the command line. I guess that'd be append_* although I think the argument parsing should be turned into a simple loop that consumes one or more arguments per iteration. Much simpler than this weird state machine business.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

Choose a reason for hiding this comment

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

declare "$var=$arg" ...

@azuwis
Copy link
Contributor Author

azuwis commented Nov 12, 2021

Please review the new changes.

Copy link
Member

@roberth roberth left a comment

Choose a reason for hiding this comment

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

I'll ignore the preexisting problem of the overcomplicated argument parsing and consider this PR an incremental improvement.

Copy link
Member

@SuperSandro2000 SuperSandro2000 left a comment

Choose a reason for hiding this comment

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

Can we forward that option to the various fetchFrom*?

@azuwis
Copy link
Contributor Author

azuwis commented Jan 24, 2022

Rebase to master, add fetchFromGitHub.

@roberth
Copy link
Member

roberth commented Jan 25, 2022

@ofborg build tests.fetchgit tests.fetchFromGitHub

@nixos-discourse
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: fetch Fetchers (e.g. fetchgit, fetchsvn, ...) 8.has: documentation This PR adds or changes documentation 10.rebuild-darwin: 11-100 This PR causes between 11 and 100 packages to rebuild on Darwin. 10.rebuild-linux: 11-100 This PR causes between 11 and 100 packages to rebuild on Linux. 12.first-time contribution This PR is the author's first one; please be gentle!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants