From 2af26fcfa1795543d20ffddd18d6a6b84ea38fe4 Mon Sep 17 00:00:00 2001 From: Marien Zwart Date: Sun, 26 May 2024 22:42:51 +1000 Subject: [PATCH] Use fetchTree / github fetcher for CI This seems to be much more space-efficient: ~/.cache/nix/tarball-cache is about 700MiB uncompressed, under 300MiB as tzst (using tar's defaults, CI uses zstdmt but I assume will be in the same ballpark). The gitv3 cache is multiple GiB. CI will still build doom-example using fetchGit. I intend to shrink the number of modules enabled in the example to keep gitv3 cache size under control. --- checks.nix | 1 + default.nix | 55 ++++++++++++++++++++++++++++++++++------------------- flake.nix | 1 + 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/checks.nix b/checks.nix index b0cc7a8..f2695af 100644 --- a/checks.nix +++ b/checks.nix @@ -31,6 +31,7 @@ let # TODO: drop after NixOS 24.05 release. emacs = emacs29; doomLocalDir = "~/.local/share/nix-doom-unstraightened"; + experimentalFetchTree = true; }; mkDoom = args: (makeDoomPackages (common // args)).doomEmacs; mkDoomDir = args: writeTextDir "init.el" (toInit args); diff --git a/default.nix b/default.nix index 24f4558..506d4ff 100644 --- a/default.nix +++ b/default.nix @@ -39,6 +39,8 @@ profileName ? "nix", /* Disable profile early in startup, so "normal" cache/state dirs are used. */ noProfileHack ? false, + /* Use fetchTree instead of fetchGit for package fetches. */ + experimentalFetchTree ? false, callPackages, git, @@ -233,27 +235,40 @@ let else (throw "${name}: cannot derive url from recipe ${p.recipe or ""}")); # Use builtins.fetchGit instead of nixpkgs's fetchFromGitHub because # fetchGit allows fetching a specific git commit without a hash. - # TODO: port to fetchTree once (mostly) stable - # (in particular the github fetcher may be noticably more efficient) - src = builtins.fetchGit ( - { - inherit url; - rev = pin; - allRefs = true; - # Skip submodules by default because they seem to be hitting - # https://github.com/NixOS/nix/issues/10773 (or a similar caching issue) and for - # parity between fetchTree's github fetcher and fetchGit (Github's exports don't - # seem to contain submodules). - submodules = !(p.recipe.nonrecursive or true); - # TODO: pull ref from derivation.src when not pulling it from p.recipe? - # Note Doom does have packages with pin + branch (or nonrecursive) set, - # expecting to inherit the rest of the recipe from Straight. + fetchGitArgs = { + inherit url; + rev = pin; + allRefs = true; + # Skip submodules by default because they seem to be hitting + # https://github.com/NixOS/nix/issues/10773 (or a similar caching issue) and for + # parity between fetchTree's github fetcher and fetchGit (Github's exports don't + # seem to contain submodules). + submodules = !(p.recipe.nonrecursive or true); + # TODO: pull ref from derivation.src when not pulling it from p.recipe? + # Note Doom does have packages with pin + branch (or nonrecursive) set, + # expecting to inherit the rest of the recipe from Straight. - # Always specify a ref to work around https://github.com/NixOS/nix/issues/10773 - ref = p.recipe.branch or "HEAD"; - } - // optionalAttrs (p ? recipe.depth) { shallow = p.recipe.depth == 1; } - ); + # Always specify a ref to work around https://github.com/NixOS/nix/issues/10773 + ref = p.recipe.branch or "HEAD"; + } + // optionalAttrs (p ? recipe.depth) { shallow = p.recipe.depth == 1; }; + src = + if experimentalFetchTree + then builtins.fetchTree ( + if lib.hasPrefix "https://github.com/" url + then let + tail = lib.removePrefix "https://github.com/" url; + split = lib.splitString "/" tail; + owner = lib.head split; + repo = lib.removeSuffix ".git" (lib.elemAt split 1); + in { + type = "github"; + inherit owner repo; + rev = pin; + } else ({ + type = "git"; + } // fetchGitArgs)) + else builtins.fetchGit fetchGitArgs; # Run locally to avoid a network roundtrip. reqfile = runCommandLocal "${name}-deps" { } '' ${lib.getExe emacs} -Q --batch --script \ diff --git a/flake.nix b/flake.nix index c015a1f..712c321 100644 --- a/flake.nix +++ b/flake.nix @@ -101,6 +101,7 @@ doomDir = ./doomdirs/minimal; doomLocalDir = "~/.local/share/nix-doom-unstraightened"; full = true; + experimentalFetchTree = true; }).doomEmacs.emacsWithPackages.deps; }; });