at 24.11-pre 77 lines 2.5 kB view raw
1# This function downloads and unpacks an archive file, such as a zip 2# or tar file. This is primarily useful for dynamically generated 3# archives, such as GitHub's /archive URLs, where the unpacked content 4# of the zip file doesn't change, but the zip file itself may 5# (e.g. due to minor changes in the compression algorithm, or changes 6# in timestamps). 7 8{ lib, fetchurl, withUnzip ? true, unzip, glibcLocalesUtf8 }: 9 10{ name ? "source" 11, url ? "" 12, urls ? [] 13, nativeBuildInputs ? [] 14, postFetch ? "" 15, extraPostFetch ? "" 16 17# Optionally move the contents of the unpacked tree up one level. 18, stripRoot ? true 19# Allows to set the extension for the intermediate downloaded 20# file. This can be used as a hint for the unpackCmdHooks to select 21# an appropriate unpacking tool. 22, extension ? null 23 24# the rest are given to fetchurl as is 25, ... } @ args: 26 27assert (extraPostFetch != "") -> lib.warn "use 'postFetch' instead of 'extraPostFetch' with 'fetchzip' and 'fetchFromGitHub' or 'fetchFromGitLab'." true; 28 29let 30 tmpFilename = 31 if extension != null 32 then "download.${extension}" 33 else baseNameOf (if url != "" then url else builtins.head urls); 34in 35 36fetchurl ({ 37 inherit name; 38 recursiveHash = true; 39 40 downloadToTemp = true; 41 42 # Have to pull in glibcLocalesUtf8 for unzip in setup-hook.sh to handle 43 # UTF-8 aware locale: 44 # https://github.com/NixOS/nixpkgs/issues/176225#issuecomment-1146617263 45 nativeBuildInputs = lib.optionals withUnzip [ unzip glibcLocalesUtf8 ] ++ nativeBuildInputs; 46 47 postFetch = 48 '' 49 unpackDir="$TMPDIR/unpack" 50 mkdir "$unpackDir" 51 cd "$unpackDir" 52 53 renamed="$TMPDIR/${tmpFilename}" 54 mv "$downloadedFile" "$renamed" 55 unpackFile "$renamed" 56 chmod -R +w "$unpackDir" 57 '' + (if stripRoot then '' 58 if [ $(ls -A "$unpackDir" | wc -l) != 1 ]; then 59 echo "error: zip file must contain a single file or directory." 60 echo "hint: Pass stripRoot=false; to fetchzip to assume flat list of files." 61 exit 1 62 fi 63 fn=$(cd "$unpackDir" && ls -A) 64 if [ -f "$unpackDir/$fn" ]; then 65 mkdir $out 66 fi 67 mv "$unpackDir/$fn" "$out" 68 '' else '' 69 mv "$unpackDir" "$out" 70 '') + '' 71 ${postFetch} 72 ${extraPostFetch} 73 chmod 755 "$out" 74 ''; 75 # ^ Remove non-owner write permissions 76 # Fixes https://github.com/NixOS/nixpkgs/issues/38649 77} // removeAttrs args [ "stripRoot" "extraPostFetch" "postFetch" "extension" "nativeBuildInputs" ])