How download file from an URL with special characters in it?

I have this piece of a code in a package (note the %2F& in the URL):

  src = fetchurl {
    # This does not work; why?
    url = "https://sdrive.cnrs.fr/s/BmXHoY3EgbY6Yre/download?path=%2F&files=imager-dec24.tar.gz";
 };

When I build the package, I get the following error:

nix-build --arg pkgs 'import <nixpkgs> {}' -A imager
warning: found empty hash, assuming 'sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA='
these 2 derivations will be built:
  /nix/store/g18472sawslpg3r6qp0gx9nmdgrb5d55-download?path=-2F-files=imager-dec24.tar.gz.drv
  /nix/store/dycqzbv0sd4vigmbicmvnn2n6xij8xzm-imager-4.4-01.drv
building '/nix/store/g18472sawslpg3r6qp0gx9nmdgrb5d55-download?path=-2F-files=imager-dec24.tar.gz.drv'...

trying https://sdrive.cnrs.fr/s/BmXHoY3EgbY6Yre/download?path=%2F&files=imager-dec24.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (22) The requested URL returned error: 403
error: cannot download download?path=-2F-files=imager-dec24.tar.gz from any mirror

Note that %2F& has been replaced by -2F-. How can I escape these characters in the URL?

The -2F- is just the name of the resulting derivation. It still tries to download the URL you specified. The actual issue is that the web server returns HTTP error 403 Forbidden, which you can also see when you run curl yourself:

$ curl "https://sdrive.cnrs.fr/s/BmXHoY3EgbY6Yre/download?path=%2F&files=imager-dec24.tar.gz"
403 Forbidden

Maybe the server needs some authentication or other headers. You can pass other options to curl passing curlOptsList attribute to fetchurl.

Also you can change the name of the derivation by passing the name attribute.

Thanks for your answer. Indeed when I open this URL in my browser:

https://sdrive.cnrs.fr/s/BmXHoY3EgbY6Yre

I get a message to acknowledge the term of use for website. Can this be done with a curl option?

You have to figure out what calls are made to said site to constitute acceptance, then figure out what curl options would allow you to perform said calls. Or the other option is to download it manually and throw it in the store via nix-prefetch-url.

1 Like