···11+source $stdenv/setup
22+33+# Curl flags to handle redirects, not use EPSV, handle cookies for
44+# servers to need them during redirects, and work on SSL without a
55+# certificate (this isn't a security problem because we check the
66+# cryptographic hash of the output anyway).
77+88+set -o noglob
99+1010+curl="curl \
1111+ --location \
1212+ --max-redirs 20 \
1313+ --retry 2 \
1414+ --disable-epsv \
1515+ --cookie-jar cookies \
1616+ --insecure \
1717+ --speed-time 5 \
1818+ -# \
1919+ --fail \
2020+ $curlOpts \
2121+ $NIX_CURL_FLAGS"
2222+2323+finish() {
2424+ runHook postFetch
2525+ set +o noglob
2626+ exit 0
2727+}
2828+2929+ipfs_add() {
3030+ if curl --retry 0 --head --silent "localhost:5001" > /dev/null; then
3131+ echo "[0m[01;36m=IPFS=[0m add $ipfs"
3232+ tar --owner=root --group=root -cWf "source.tar" $(echo *)
3333+ res=$(curl -# -F "file=@source.tar" "localhost:5001/api/v0/tar/add" | sed 's/.*"Hash":"\(.*\)".*/\1/')
3434+ if [ $ipfs != $res ]; then
3535+ echo "\`ipfs tar add' results in $res when $ipfs is expected"
3636+ exit 1
3737+ fi
3838+ rm "source.tar"
3939+ fi
4040+}
4141+4242+echo
4343+4444+mkdir download
4545+cd download
4646+4747+if curl --retry 0 --head --silent "localhost:5001" > /dev/null; then
4848+ curlexit=18;
4949+ echo "[0m[01;36m=IPFS=[0m get $ipfs"
5050+ # if we get error code 18, resume partial download
5151+ while [ $curlexit -eq 18 ]; do
5252+ # keep this inside an if statement, since on failure it doesn't abort the script
5353+ if $curl -C - "http://localhost:5001/api/v0/tar/cat?arg=$ipfs" --output "$ipfs.tar"; then
5454+ unpackFile "$ipfs.tar"
5555+ rm "$ipfs.tar"
5656+ set +o noglob
5757+ mv $(echo *) "$out"
5858+ finish
5959+ else
6060+ curlexit=$?;
6161+ fi
6262+ done
6363+fi
6464+6565+if test -n "$url"; then
6666+ curlexit=18;
6767+ echo "Downloading $url"
6868+ while [ $curlexit -eq 18 ]; do
6969+ # keep this inside an if statement, since on failure it doesn't abort the script
7070+ if $curl "$url" -O; then
7171+ set +o noglob
7272+ tmpfile=$(echo *)
7373+ unpackFile $tmpfile
7474+ rm $tmpfile
7575+ ipfs_add
7676+ mv $(echo *) "$out"
7777+ finish
7878+ else
7979+ curlexit=$?;
8080+ fi
8181+ done
8282+fi
8383+8484+echo "[01;31merror:[0m cannot download $ipfs from ipfs or the given url"
8585+echo
8686+set +o noglob
8787+exit 1
+52
pkgs/build-support/fetchipfs/default.nix
···11+{ stdenv
22+, curl
33+}:
44+55+{ ipfs
66+, url ? ""
77+, curlOpts ? ""
88+, outputHash ? ""
99+, outputHashAlgo ? ""
1010+, md5 ? ""
1111+, sha1 ? ""
1212+, sha256 ? ""
1313+, sha512 ? ""
1414+, meta ? {}
1515+, port ? "8080"
1616+, postFetch ? ""
1717+}:
1818+1919+assert sha512 != "" -> builtins.compareVersions "1.11" builtins.nixVersion <= 0;
2020+2121+let
2222+2323+ hasHash = (outputHash != "" && outputHashAlgo != "")
2424+ || md5 != "" || sha1 != "" || sha256 != "" || sha512 != "";
2525+2626+in
2727+2828+if (!hasHash) then throw "Specify sha for fetchipfs fixed-output derivation" else stdenv.mkDerivation {
2929+ name = ipfs;
3030+ builder = ./builder.sh;
3131+ buildInputs = [ curl ];
3232+3333+ # New-style output content requirements.
3434+ outputHashAlgo = if outputHashAlgo != "" then outputHashAlgo else
3535+ if sha512 != "" then "sha512" else if sha256 != "" then "sha256" else if sha1 != "" then "sha1" else "md5";
3636+ outputHash = if outputHash != "" then outputHash else
3737+ if sha512 != "" then sha512 else if sha256 != "" then sha256 else if sha1 != "" then sha1 else md5;
3838+3939+ outputHashMode = "recursive";
4040+4141+ inherit curlOpts
4242+ postFetch
4343+ ipfs
4444+ url
4545+ port;
4646+4747+ # Doing the download on a remote machine just duplicates network
4848+ # traffic, so don't do that.
4949+ preferLocalBuild = true;
5050+5151+ inherit meta;
5252+}
+4
pkgs/top-level/all-packages.nix
···177177178178 fetchRepoProject = callPackage ../build-support/fetchrepoproject { };
179179180180+ fetchipfs = import ../build-support/fetchipfs {
181181+ inherit curl stdenv;
182182+ };
183183+180184 # fetchurlBoot is used for curl and its dependencies in order to
181185 # prevent a cyclic dependency (curl depends on curl.tar.bz2,
182186 # curl.tar.bz2 depends on fetchurl, fetchurl depends on curl). It