Commit ae800ea5 authored by Yueh-Shun Li's avatar Yueh-Shun Li
Browse files

fetchurl: reference outputHash from finalAttrs.hash

parent cf998632
Loading
Loading
Loading
Loading
+20 −2
Original line number Diff line number Diff line
@@ -64,7 +64,6 @@ lib.extendMkDerivation {
    "derivationArgs"

    # Hash attributes will be map to the corresponding outputHash*
    "hash"
    "sha1"
    "sha256"
    "sha512"
@@ -220,6 +219,9 @@ lib.extendMkDerivation {
        else
          throw "fetchurl requires a hash for fixed-output derivation: ${lib.generators.toPretty { } urls_}";

      finalHashHasColon = lib.hasInfix ":" finalAttrs.hash;
      finalHashColonMatch = lib.match "([^:]+)[:](.*)" finalAttrs.hash;

      resolvedUrl =
        let
          mirrorSplit = lib.match "mirror://([[:alpha:]]+)/(.+)" url;
@@ -259,7 +261,23 @@ lib.extendMkDerivation {
      preferHashedMirrors = false;

      # New-style output content requirements.
      inherit (hash_) outputHashAlgo outputHash;
      hash =
        if
          hash_.outputHashAlgo == null
          || hash_.outputHash == ""
          || lib.hasPrefix hash_.outputHashAlgo hash_.outputHash
        then
          hash_.outputHash
        else
          "${hash_.outputHashAlgo}:${hash_.outputHash}";
      outputHashAlgo = if finalHashHasColon then lib.head finalHashColonMatch else null;
      outputHash =
        if finalAttrs.hash == "" then
          lib.fakeHash
        else if finalHashHasColon then
          lib.elemAt finalHashColonMatch 1
        else
          finalAttrs.hash;

      # Disable TLS verification only when we know the hash and no credentials are
      # needed to access the resource
+53 −1
Original line number Diff line number Diff line
@@ -5,7 +5,13 @@
}:

let
  tests = tests-stdenv // test-extendMkDerivation // tests-fetchhg // tests-go // tests-python;
  tests =
    tests-stdenv
    // test-extendMkDerivation
    // tests-fetchhg
    // tests-fetchurl
    // tests-go
    // tests-python;

  tests-stdenv =
    let
@@ -131,6 +137,52 @@ let
      };
    };

  tests-fetchurl =
    let
      src-with-sha256 = pkgs.fetchurl {
        url = "https://example.com/source.tar.gz";
        sha256 = lib.fakeSha256;
      };
    in
    {
      test-fetchurl-hash-compat = {
        expr = {
          inherit (src-with-sha256)
            outputHash
            outputHashAlgo
            ;
        };
        expected = {
          outputHash = lib.fakeSha256;
          outputHashAlgo = "sha256";
        };
      };
      test-fetchurl-overrideAttrs-hash = {
        expr = {
          inherit (src-with-sha256.overrideAttrs { hash = pkgs.hello.src.hash; })
            outputHash
            outputHashAlgo
            ;
        };
        expected = {
          outputHash = pkgs.hello.src.hash;
          outputHashAlgo = null;
        };
      };
      test-fetchurl-overrideAttrs-hash-empty = {
        expr = {
          inherit (src-with-sha256.overrideAttrs { hash = ""; })
            outputHash
            outputHashAlgo
            ;
        };
        expected = {
          outputHash = lib.fakeHash;
          outputHashAlgo = null;
        };
      };
    };

  tests-fetchhg =
    let
      ruamel_0_18_14-hash = "sha256-HDkPPp1xI3uoGYlS9mwPp1ZjG2gKvx6vog0Blj6tBuI=";