From 86c902d67374a83d54f0bfabab67951e8e312d69 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Fri, 18 Mar 2022 20:39:11 +0100 Subject: [PATCH 1/3] 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(-) diff --git a/pkgs/build-support/fetchurl/builder.sh b/pkgs/build-support/fetchurl/builder.sh index 5b04a702aff4..5ca09b6fc77d 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 776089dab999..2e5fa9162926 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; From b404704911f2e3d078d5d0287006bbe263167202 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Sat, 19 Mar 2022 01:02:15 +0100 Subject: [PATCH 2/3] factorio: Use curlOptsList to fix warning --- pkgs/games/factorio/default.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/games/factorio/default.nix b/pkgs/games/factorio/default.nix index 4cf335a61378..db77d1fc99e4 100644 --- a/pkgs/games/factorio/default.nix +++ b/pkgs/games/factorio/default.nix @@ -85,7 +85,7 @@ let (lib.overrideDerivation (fetchurl { inherit name url sha256; - curlOpts = [ + curlOptsList = [ "--get" "--data-urlencode" "username@username" "--data-urlencode" "token@token" From 588439e1311c41a5779877d4d8ef603410b79cd4 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Thu, 30 Jun 2022 19:49:54 +0200 Subject: [PATCH 3/3] 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 diff --git a/pkgs/build-support/fetchurl/tests.nix b/pkgs/build-support/fetchurl/tests.nix new file mode 100644 index 000000000000..fc7fb25e158f --- /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 4110327946d9..0e8d5ee0f23b 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 { };