Unverified Commit 81f2a116 authored by Philip Taron's avatar Philip Taron Committed by GitHub
Browse files

fetch{url,zip,git}: restructure with `lib.extendMkDerivation` (#455994)

parents 86ef9403 1ec0227c
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -193,6 +193,10 @@ cffc27daf06c77c0d76bc35d24b929cb9d68c3c9
# nixos/kanidm: inherit lib, nixfmt
8f18393d380079904d072007fb19dc64baef0a3a

# fetchgit, fetchurl, fetchzip:
# format after refactoring with lib.extendMkDerivation (#455994)
aeddd850c6d3485fc1af2edfb111e58141d18dc1

# fetchhg: format after refactoring with lib.extendMkDerivation and make overridable (#423539)
34a5b1eb23129f8fb62c677e3760903f6d43228f

+192 −173
Original line number Diff line number Diff line
@@ -23,6 +23,21 @@ let
in

lib.makeOverridable (
  lib.extendMkDerivation {
    constructDrv = stdenvNoCC.mkDerivation;

    excludeDrvArgNames = [
      # Passed via `passthru`
      "tag"

      # Hashes, handled by `lib.fetchers.withNormalizedHash`
      # whose outputs contain outputHash* attributes.
      "hash"
      "sha256"
    ];

    extendDrvArgs =
      finalAttrs:
      lib.fetchers.withNormalizedHash { } (
        # NOTE Please document parameter additions or changes in
        #   ../../../doc/build-helpers/fetchers.chapter.md
@@ -120,7 +135,7 @@ lib.makeOverridable (
          throw
            "Please provide directories/patterns for sparse checkout as a list of strings. Passing a (multi-line) string is not supported any more."
        else
      stdenvNoCC.mkDerivation {
          {
            inherit name;

            builder = ./builder.sh;
@@ -199,5 +214,9 @@ lib.makeOverridable (
            }
            // passthru;
          }
  )
      );

    # No ellipsis.
    inheritFunctionArgs = false;
  }
)
+244 −223
Original line number Diff line number Diff line
@@ -53,6 +53,22 @@ let

in

lib.extendMkDerivation {
  constructDrv = stdenvNoCC.mkDerivation;

  excludeDrvArgNames = [
    # Passed via passthru
    "url"

    # Hash attributes will be map to the corresponding outputHash*
    "hash"
    "sha1"
    "sha256"
    "sha512"
  ];

  extendDrvArgs =
    finalAttrs:
    {
      # URL to fetch.
      url ? "",
@@ -69,13 +85,13 @@ in
      # Additional curl options needed for the download to succeed.
      curlOptsList ? [ ],

  # Name of the file.  If empty, use the basename of `url' (or of the
  # first element of `urls').
  name ? "",
      # Name of the file when pname + version is unspecified.
      # Default to the basename of `url' (or of the first element of `urls').
      name ? null,

      # for versioned downloads optionally take pname + version.
  pname ? "",
  version ? "",
      pname ? null,
      version ? null,

      # SRI hash.
      hash ? "",
@@ -212,33 +228,17 @@ let
          "${lib.head mirrorList}${lib.elemAt mirrorSplit 1}";
    in

assert
  (lib.isList curlOpts)
  -> lib.warn ''
    fetchurl for ${toString (builtins.head urls_)}: curlOpts is a list (${
      lib.generators.toPretty { multiline = false; } curlOpts
    }), which is not supported anymore.
    - If you wish to get the same effect as before, for elements with spaces (even if escaped) to expand to multiple curl arguments, use a string argument instead:
      curlOpts = ${lib.strings.escapeNixString (toString curlOpts)};
    - If you wish for each list element to be passed as a separate curl argument, allowing arguments to contain spaces, use curlOptsList instead:
      curlOptsList = [ ${lib.concatMapStringsSep " " lib.strings.escapeNixString curlOpts} ];'' true;

stdenvNoCC.mkDerivation (
  (
    if (pname != "" && version != "") then
      { inherit pname version; }
    else
    {
      name =
          if showURLs then
        if pname != null && version != null then
          "${finalAttrs.pname}-${finalAttrs.version}"
        else if showURLs then
          "urls"
          else if name != "" then
        else if name != null then
          name
        else
            baseNameOf (toString (builtins.head urls_));
      }
  )
  // {
          baseNameOf (toString (lib.head urls_));

      builder = ./builder.sh;

      nativeBuildInputs = [ curl ] ++ nativeBuildInputs;
@@ -270,8 +270,26 @@ stdenvNoCC.mkDerivation (

      outputHashMode = if (recursiveHash || executable) then "recursive" else "flat";

    inherit curlOpts;
      curlOpts = lib.warnIf (lib.isList curlOpts) (
        let
          url = toString (builtins.head urls_);
          curlOptsRepresentation = lib.generators.toPretty { multiline = false; } curlOpts;
          curlOptsAsStringRepresentation = lib.strings.escapeNixString (toString curlOpts);
          curlOptsListElementsRepresentation =
            lib.concatMapStringsSep " " lib.strings.escapeNixString
              curlOpts;
        in
        ''
          fetchurl for ${url}: curlOpts is a list (${curlOptsRepresentation}), which is not supported anymore.
          - If you wish to get the same effect as before, for elements with spaces (even if escaped) to expand to multiple curl arguments, use a string argument instead:
            curlOpts = ${curlOptsAsStringRepresentation};
          - If you wish for each list element to be passed as a separate curl argument, allowing arguments to contain spaces, use curlOptsList instead:
            curlOptsList = [ ${curlOptsListElementsRepresentation} ];
        ''
      ) curlOpts;

      curlOptsList = lib.escapeShellArgs curlOptsList;

      inherit
        showURLs
        mirrorsFile
@@ -300,5 +318,8 @@ stdenvNoCC.mkDerivation (
        inherit url resolvedUrl;
      }
      // passthru;
    };

  # No ellipsis
  inheritFunctionArgs = false;
}
)
+88 −84
Original line number Diff line number Diff line
@@ -14,6 +14,19 @@
  glibcLocalesUtf8,
}:

lib.extendMkDerivation {
  constructDrv = fetchurl;

  excludeDrvArgNames = [
    "extraPostFetch"

    # TODO(@ShamrockLee): Move these arguments to derivationArgs when available.
    "extension"
    "stripRoot"
  ];

  extendDrvArgs =
    finalAttrs:
    {
      url ? "",
      urls ? [ ],
@@ -33,10 +46,6 @@
      ...
    }@args:

assert
  (extraPostFetch != "")
  -> lib.warn "use 'postFetch' instead of 'extraPostFetch' with 'fetchzip' and 'fetchFromGitHub' or 'fetchFromGitLab'." true;

    let
      tmpFilename =
        if extension != null then
@@ -45,7 +54,6 @@ let
          baseNameOf (if url != "" then url else builtins.head urls);
    in

fetchurl (
    {
      inherit name;
      recursiveHash = true;
@@ -93,17 +101,13 @@ fetchurl (
      )
      + ''
        ${postFetch}
      ${extraPostFetch}
        ${lib.warnIf (extraPostFetch != "")
          "use 'postFetch' instead of 'extraPostFetch' with 'fetchzip' and 'fetchFromGitHub' or 'fetchFromGitLab'."
          extraPostFetch
        }
        chmod 755 "$out"
      '';
      # ^ Remove non-owner write permissions
      # Fixes https://github.com/NixOS/nixpkgs/issues/38649
    };
}
  // removeAttrs args [
    "stripRoot"
    "extraPostFetch"
    "postFetch"
    "extension"
    "nativeBuildInputs"
  ]
)