Unverified Commit 0e21d4f7 authored by Yueh-Shun Li's avatar Yueh-Shun Li Committed by GitHub
Browse files

fetchurl, fetchgit: converge hash attributes and reference from `finalAttrs.hash` (#463871)

parents 84384eb5 4e37d03b
Loading
Loading
Loading
Loading
+20 −2
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@ lib.makeOverridable (

      # Hashes, handled by `lib.fetchers.withNormalizedHash`
      # whose outputs contain outputHash* attributes.
      "hash"
      # Use `hash` when overriding with `<pkg>.overrideAttrs`.
      "sha256"
    ];

@@ -131,6 +131,11 @@ lib.makeOverridable (
          server admins start using the new version?
        */

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

        derivationArgs
        // {
          inherit name;
@@ -145,7 +150,20 @@ lib.makeOverridable (
          ++ lib.optionals fetchLFS [ git-lfs ]
          ++ nativeBuildInputs;

          inherit outputHash outputHashAlgo;
          hash =
            if outputHashAlgo == null || outputHash == "" || lib.hasPrefix outputHashAlgo outputHash then
              outputHash
            else
              "${outputHashAlgo}:${outputHash}";

          outputHash =
            if finalAttrs.hash == "" then
              lib.fakeHash
            else if finalHashHasColon then
              lib.elemAt finalHashColonMatch 1
            else
              finalAttrs.hash;
          outputHashAlgo = if finalHashHasColon then lib.head finalHashColonMatch else null;
          outputHashMode = "recursive";

          sparseCheckout =
+22 −4
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"
@@ -214,12 +213,15 @@ lib.extendMkDerivation {
          }
        else if cacert != null then
          {
            outputHashAlgo = "sha256";
            outputHash = "";
            outputHashAlgo = null;
            outputHash = lib.fakeHash;
          }
        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
+99 −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,98 @@ let
      };
    };

  tests-fetchgit =
    let
      src-with-sha256 = pkgs.fetchgit {
        url = "https://example.com/source.git";
        sha256 = lib.fakeSha256;
      };
    in
    {
      test-fetchgit-hash-compat = {
        expr = {
          inherit (src-with-sha256)
            outputHash
            outputHashAlgo
            ;
        };
        expected = {
          outputHash = lib.fakeSha256;
          outputHashAlgo = "sha256";
        };
      };
      test-fetchgit-overrideAttrs-hash = {
        expr = {
          inherit (src-with-sha256.overrideAttrs { hash = pkgs.nix.src.hash; })
            outputHash
            outputHashAlgo
            ;
        };
        expected = {
          outputHash = pkgs.nix.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-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=";