Fetchurl and curl cannot work, wget can work

I try do download a deb:

  src = fetchurl {
    url = "https://d1.music.126.net/dmusic/netease-cloud-music_1.2.1_amd64_ubuntu_20190428.deb";
    sha256 = "";
  };

I got

error: builder for '/nix/store/2zby3zhdq04cr1d75vqp92wf49vxyi93-netease-cloud-music_1.2.1_amd64_ubuntu_20190428.deb.drv' failed with exit code 1;
       last 7 log lines:
       >
       > trying https://d1.music.126.net/dmusic/netease-cloud-music_1.2.1_amd64_ubuntu_20190428.deb
       >   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
       >                                  Dload  Upload   Total   Spent    Left  Speed
       >   0   146    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
       > curl: (22) The requested URL returned error: 403
       > error: cannot download netease-cloud-music_1.2.1_amd64_ubuntu_20190428.deb from any mirror
       For full logs, run 'nix log /nix/store/2zby3zhdq04cr1d75vqp92wf49vxyi93-netease-cloud-music_1.2.1_amd64_ubuntu_20190428.deb.drv'.

I try curl https://d1.music.126.net/dmusic/netease-cloud-music_1.2.1_amd64_ubuntu_20190428.deb and get same error.

However, I try wget https://d1.music.126.net/dmusic/netease-cloud-music_1.2.1_amd64_ubuntu_20190428.deb and it can work. How can I let fetchurl use wget not curl?

So the differrence between wget and curl is just a couple of different headers in the request. That site is doing something funky, 403 means “forbidden” which probably means it checks user agents and only allows wget or something.

Luckily, you can change your curl request to be exactly the same.

Try wget -d and curl --verbose (piping the response into /dev/null or something). Both conmands will show you what requests the respective command actually ends up sending.

Then modify the curl request with cli args until you find out what property that website is looking for, and pass the same arguments to fetchurl’s curlOptsList.

Right, the following code can work:

  src = fetchurl {
    url = "https://d1.music.126.net/dmusic/netease-cloud-music_1.2.1_amd64_ubuntu_20190428.deb";
    sha256 = "sha256-HunwKELmwsjHnEiy6TIHT5whOo60I45eY/IEOFYv7Ls=";
    curlOptsList = ["-HUser-Agent: Wget/1.21.4"];
  };
1 Like

It will never cease to amaze me what people abuse the user-agent for…