git.iko.soy/typeable/nixpkgs
on master help sign in
fetchipfs: init

Fixes #18296

commitd71833ee36c707717821b7f833587817168b4684 authorknupfer <fknupfer@gmail.com> dateSat 4 Nov 2017, 23:01 (9 years ago) parent59c995c
4 files changed+148−4
pkgs/applications/misc/hello/default.nix+5−4 pkgs/build-support/fetchipfs/builder.sh+87 pkgs/build-support/fetchipfs/default.nix+52 pkgs/top-level/all-packages.nix+4
pkgs/applications/misc/hello/default.nix+5−4
@@ -1,11 +1,12 @@
1−{ stdenv, fetchurl }:
1+{ stdenv, fetchipfs }:
22
33stdenv.mkDerivation rec {
44 name = "hello-2.10";
55
6− src = fetchurl {
7− url = "mirror://gnu/hello/${name}.tar.gz";
8− sha256 = "0ssi1wpaf7plaswqqjwigppsg5fyh99vdlb9kzl7c9lng89ndq1i";
6+ src = fetchipfs {
7+ url = "https://ftp.gnu.org/gnu/hello/hello-2.10.tar.gz";
8+ ipfs = "QmWyj65ak3wd8kG2EvPCXKd6Tij15m4SwJz6g2yG2rQ7w8";
9+ sha256 = "1im1gglfm4k10bh4mdaqzmx3lm3kivnsmxrvl6vyvmfqqzljq75l";
910 };
1011
1112 doCheck = true;
pkgs/build-support/fetchipfs/builder.shnew file+87
@@ -0,0 +1,87 @@
1+source $stdenv/setup
2+
3+# Curl flags to handle redirects, not use EPSV, handle cookies for
4+# servers to need them during redirects, and work on SSL without a
5+# certificate (this isn't a security problem because we check the
6+# cryptographic hash of the output anyway).
7+
8+set -o noglob
9+
10+curl="curl \
11+ --location \
12+ --max-redirs 20 \
13+ --retry 2 \
14+ --disable-epsv \
15+ --cookie-jar cookies \
16+ --insecure \
17+ --speed-time 5 \
18+ -# \
19+ --fail \
20+ $curlOpts \
21+ $NIX_CURL_FLAGS"
22+
23+finish() {
24+ runHook postFetch
25+ set +o noglob
26+ exit 0
27+}
28+
29+ipfs_add() {
30+ if curl --retry 0 --head --silent "localhost:5001" > /dev/null; then
31+ echo "=IPFS= add $ipfs"
32+ tar --owner=root --group=root -cWf "source.tar" $(echo *)
33+ res=$(curl -# -F "file=@source.tar" "localhost:5001/api/v0/tar/add" | sed 's/.*"Hash":"\(.*\)".*/\1/')
34+ if [ $ipfs != $res ]; then
35+ echo "\`ipfs tar add' results in $res when $ipfs is expected"
36+ exit 1
37+ fi
38+ rm "source.tar"
39+ fi
40+}
41+
42+echo
43+
44+mkdir download
45+cd download
46+
47+if curl --retry 0 --head --silent "localhost:5001" > /dev/null; then
48+ curlexit=18;
49+ echo "=IPFS= get $ipfs"
50+ # if we get error code 18, resume partial download
51+ while [ $curlexit -eq 18 ]; do
52+ # keep this inside an if statement, since on failure it doesn't abort the script
53+ if $curl -C - "http://localhost:5001/api/v0/tar/cat?arg=$ipfs" --output "$ipfs.tar"; then
54+ unpackFile "$ipfs.tar"
55+ rm "$ipfs.tar"
56+ set +o noglob
57+ mv $(echo *) "$out"
58+ finish
59+ else
60+ curlexit=$?;
61+ fi
62+ done
63+fi
64+
65+if test -n "$url"; then
66+ curlexit=18;
67+ echo "Downloading $url"
68+ while [ $curlexit -eq 18 ]; do
69+ # keep this inside an if statement, since on failure it doesn't abort the script
70+ if $curl "$url" -O; then
71+ set +o noglob
72+ tmpfile=$(echo *)
73+ unpackFile $tmpfile
74+ rm $tmpfile
75+ ipfs_add
76+ mv $(echo *) "$out"
77+ finish
78+ else
79+ curlexit=$?;
80+ fi
81+ done
82+fi
83+
84+echo "error: cannot download $ipfs from ipfs or the given url"
85+echo
86+set +o noglob
87+exit 1
pkgs/build-support/fetchipfs/default.nixnew file+52
@@ -0,0 +1,52 @@
1+{ stdenv
2+, curl
3+}:
4+
5+{ ipfs
6+, url ? ""
7+, curlOpts ? ""
8+, outputHash ? ""
9+, outputHashAlgo ? ""
10+, md5 ? ""
11+, sha1 ? ""
12+, sha256 ? ""
13+, sha512 ? ""
14+, meta ? {}
15+, port ? "8080"
16+, postFetch ? ""
17+}:
18+
19+assert sha512 != "" -> builtins.compareVersions "1.11" builtins.nixVersion <= 0;
20+
21+let
22+
23+ hasHash = (outputHash != "" && outputHashAlgo != "")
24+ || md5 != "" || sha1 != "" || sha256 != "" || sha512 != "";
25+
26+in
27+
28+if (!hasHash) then throw "Specify sha for fetchipfs fixed-output derivation" else stdenv.mkDerivation {
29+ name = ipfs;
30+ builder = ./builder.sh;
31+ buildInputs = [ curl ];
32+
33+ # New-style output content requirements.
34+ outputHashAlgo = if outputHashAlgo != "" then outputHashAlgo else
35+ if sha512 != "" then "sha512" else if sha256 != "" then "sha256" else if sha1 != "" then "sha1" else "md5";
36+ outputHash = if outputHash != "" then outputHash else
37+ if sha512 != "" then sha512 else if sha256 != "" then sha256 else if sha1 != "" then sha1 else md5;
38+
39+ outputHashMode = "recursive";
40+
41+ inherit curlOpts
42+ postFetch
43+ ipfs
44+ url
45+ port;
46+
47+ # Doing the download on a remote machine just duplicates network
48+ # traffic, so don't do that.
49+ preferLocalBuild = true;
50+
51+ inherit meta;
52+}
pkgs/top-level/all-packages.nix+4
@@ -177,6 +177,10 @@ with pkgs;
177177
178178 fetchRepoProject = callPackage ../build-support/fetchrepoproject { };
179179
180+ fetchipfs = import ../build-support/fetchipfs {
181+ inherit curl stdenv;
182+ };
183+
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