Skip to content

Commit

Permalink
Add huggingface fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaynard committed Aug 20, 2024
1 parent 569f3fb commit a8c22c1
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 10 deletions.
32 changes: 22 additions & 10 deletions utils/fetchers/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,28 @@ in
{
overlays.fetchers = final: prev:
let
fetchFromHuggingFace = prev.callPackage ./fetchFromHuggingFace.nix {
inherit (final) huggingface-model-downloader;
};
extras = { };
in
lib.foldFor pnames (pname: {
${pname} = prev.callPackage
(./. + "/${pname}.nix")
(extras."${pname}" or { });
});
lib.foldFor pnames
(pname: {
${pname} = prev.callPackage
(./. + "/${pname}.nix")
(extras."${pname}" or { });
}) // {
inherit fetchFromHuggingFace;
};
} //
lib.foldFor lib.platforms.all (system: {
packages.${system} = self.overlays.fetchers
(nixpkgs.legacyPackages.${system} // self.packages.${system})
nixpkgs.legacyPackages.${system};
})
lib.foldFor lib.platforms.all (system:
let
pkgs = nixpkgs.legacyPackages.${system};
in
{
packages.${system} =
lib.filterAttrs (_: lib.isDerivation) self.legacyPackages.${system};
legacyPackages.${system} = self.overlays.fetchers
(pkgs // self.packages.${system})
pkgs;
})
53 changes: 53 additions & 0 deletions utils/fetchers/fetchFromHuggingFace.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{ lib
, huggingface-model-downloader
, stdenvNoCC
}:

{ model
, version
, hash ? null
, requireToken ? false
, extraArgs ? [ ]
}:

stdenvNoCC.mkDerivation {
pname = "hf-${lib.replaceStrings ["/"] ["-"] model}";
inherit version;

dontUnpack = true;
dontFixup = true;

impureEnvVars = [ "HUGGING_FACE_HUB_TOKEN" ];

nativeBuildInputs = [
huggingface-model-downloader
];

preBuild = lib.optionalString requireToken ''
if [ -z ''${HUGGING_FACE_HUB_TOKEN} ]; then
echo "Private: fetchFromHuggingFace requires the nix building process (nix-daemon in multi user mode) to have the HUGGING_FACE_HUB_TOKEN env var set." >&2
exit 1
fi
'';

EXTRA_ARGS = extraArgs;

buildPhase = ''
runHook preBuild
${lib.getExe huggingface-model-downloader} -m ${model} "''${EXTRA_ARGS[@]}"
runHook postBuild
'';

installPhase = ''
runHook preInstall
cp -r --reflink=auto . "$out"
runHook postInstall
'';

outputHash = hash;
outputHashMode = "recursive";
}

0 comments on commit a8c22c1

Please sign in to comment.