From 86c902d67374a83d54f0bfabab67951e8e312d69 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Fri, 18 Mar 2022 20:39:11 +0100 Subject: fetchurl: Introduce curlOptsList as an improvement over curlOpts It's impossible to pass arguments with spaces with curlOpts. curlOptsList supports that. Passing a list to curlOpts has been deprecated. This commit is fully backwards compatible. --- pkgs/build-support/fetchurl/builder.sh | 2 ++ pkgs/build-support/fetchurl/default.nix | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) (limited to 'pkgs/build-support/fetchurl') diff --git a/pkgs/build-support/fetchurl/builder.sh b/pkgs/build-support/fetchurl/builder.sh index 5b04a702aff..5ca09b6fc77 100644 --- a/pkgs/build-support/fetchurl/builder.sh +++ b/pkgs/build-support/fetchurl/builder.sh @@ -22,6 +22,8 @@ if ! [ -f "$SSL_CERT_FILE" ]; then curl+=(--insecure) fi +eval "curl+=($curlOptsList)" + curl+=( $curlOpts $NIX_CURL_FLAGS diff --git a/pkgs/build-support/fetchurl/default.nix b/pkgs/build-support/fetchurl/default.nix index 776089dab99..2e5fa916292 100644 --- a/pkgs/build-support/fetchurl/default.nix +++ b/pkgs/build-support/fetchurl/default.nix @@ -45,8 +45,13 @@ in urls ? [] , # Additional curl options needed for the download to succeed. + # Warning: Each space (no matter the escaping) will start a new argument. + # If you wish to pass arguments with spaces, use `curlOptsList` curlOpts ? "" +, # 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 ? "" @@ -148,7 +153,14 @@ stdenvNoCC.mkDerivation { outputHashMode = if (recursiveHash || executable) then "recursive" else "flat"; - inherit curlOpts showURLs mirrorsFile postFetch downloadToTemp executable; + curlOpts = lib.warnIf (lib.isList curlOpts) '' + 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} ];'' curlOpts; + curlOptsList = lib.escapeShellArgs curlOptsList; + inherit showURLs mirrorsFile postFetch downloadToTemp executable; impureEnvVars = impureEnvVars ++ netrcImpureEnvVars; -- cgit 1.4.1 From 588439e1311c41a5779877d4d8ef603410b79cd4 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Thu, 30 Jun 2022 19:49:54 +0200 Subject: fetchurl: Add curlOptsList test --- pkgs/build-support/fetchurl/tests.nix | 13 +++++++++++++ pkgs/test/default.nix | 1 + 2 files changed, 14 insertions(+) create mode 100644 pkgs/build-support/fetchurl/tests.nix (limited to 'pkgs/build-support/fetchurl') diff --git a/pkgs/build-support/fetchurl/tests.nix b/pkgs/build-support/fetchurl/tests.nix new file mode 100644 index 00000000000..fc7fb25e158 --- /dev/null +++ b/pkgs/build-support/fetchurl/tests.nix @@ -0,0 +1,13 @@ +{ invalidateFetcherByDrvHash, fetchurl, jq, moreutils, ... }: { + # Tests that we can send custom headers with spaces in them + header = + let headerValue = "Test '\" <- These are some quotes"; + in invalidateFetcherByDrvHash fetchurl { + url = "https://httpbin.org/headers"; + sha256 = builtins.hashString "sha256" (headerValue + "\n"); + curlOptsList = [ "-H" "Hello: ${headerValue}" ]; + postFetch = '' + ${jq}/bin/jq -r '.headers.Hello' $out | ${moreutils}/bin/sponge $out + ''; + }; +} diff --git a/pkgs/test/default.nix b/pkgs/test/default.nix index 4110327946d..0e8d5ee0f23 100644 --- a/pkgs/test/default.nix +++ b/pkgs/test/default.nix @@ -27,6 +27,7 @@ with pkgs; cc-multilib-gcc = callPackage ./cc-wrapper/multilib.nix { stdenv = gccMultiStdenv; }; cc-multilib-clang = callPackage ./cc-wrapper/multilib.nix { stdenv = clangMultiStdenv; }; + fetchurl = callPackages ../build-support/fetchurl/tests.nix { }; fetchpatch = callPackages ../build-support/fetchpatch/tests.nix { }; fetchgit = callPackages ../build-support/fetchgit/tests.nix { }; fetchFirefoxAddon = callPackages ../build-support/fetchfirefoxaddon/tests.nix { }; -- cgit 1.4.1