mkYarnPackage curl: (7) Couldn't connect to server

Hello, I am trying to package a basic Node application as follows:

package.json

{
  "name": "foo",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "cowsay": "^1.5.0"
  }
}

flake.nix

{
  outputs = { self, nixpkgs }: {
    packages.x86_64-linux.default = nixpkgs.legacyPackages.x86_64-linux.mkYarnPackage {
      name = "foo";
      src = ./.;
    };
  };
}

Upon executing nix build, I always get errors similar to this one:

error: builder for '/nix/store/cq0pilh0b0ji8aynsj98wwfw75yzxrzw-cowsay___cowsay_1.5.0.tgz.drv' failed with exit code 1;
       last 6 log lines:
       >
       > trying https://registry.yarnpkg.com/cowsay/-/cowsay-1.5.0.tgz
       >   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
       >                                  Dload  Upload   Total   Spent    Left  Speed
       >   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Couldn't connect to server
       > error: cannot download cowsay___cowsay_1.5.0.tgz from any mirror
       For full logs, run 'nix log /nix/store/cq0pilh0b0ji8aynsj98wwfw75yzxrzw-cowsay___cowsay_1.5.0.tgz.drv'.
error: 1 dependencies of derivation '/nix/store/50wxcf4ywb0q4212sjp1ddmn6c80k0hv-offline.drv' failed to build
error: 1 dependencies of derivation '/nix/store/yxz23fs72mmb82pcni13jb2d5b7rzg3f-foo-modules-1.0.0.drv' failed to build
error: 1 dependencies of derivation '/nix/store/clr5cy3nr34q7ymsm7yya8jn8wph7hsh-foo.drv' failed to build

I say “similar” because the specific package it fails to download varies between runs, but it always fails.

I’m not sure what to make of this error, because it would seem to indicate a problem in my network configuration, but I can successfully connect to registry.yarnpkg.com either in a browser or with curl just fine. I even hunted down what I think is the exact curl command nix is using, but that also consistently succeeds.

Does anyone have any ideas what could be causing this?

You don’t have access to the network inside of builders.

You need to make impure derivations or explicitly provide the output hash before building.

“Yarn plugin Nixify” is worth checking out.

Sorry, I don’t really understand. Why does this network call happen if it is doomed to fail? Is mkYarnPackage not intended to be used this way? I honestly can’t tell, because it has very limited documentation.

Do you have a yarn.lock?

Without it mkYarnPackage can’t do it’s magic.

Yes, there is a yarn.lock. If anyone would like to try it for themselves, I’ve created a repo here: GitHub - darichey/nix-mkYarnPackage-fail. Just clone and run nix build.

Unfortunately, the behavior seems to be wildly inconsistent. On a whim, I tried just repeatedly running nix build over and over, and eventually it worked! And now, if I have just cowsay in my dependency list, it always works. But if I try something like yarn add @commitlint/cli, then nix build fails in similar ways:

$ nix build
warning: Git tree '/home/david/test/nix-mkYarnPackage-fail' is dirty
error: builder for '/nix/store/564cazqnhfyisbzhgbg7p4j16iybvplr-_commitlint_format___format_17.0.0.tgz.drv' failed with exit code 1;
       last 8 log lines:
       > error checking the existence of https://tarballs.nixos.org/sha512/MZzJv7rBp/r6ZQJDEodoZvdRM0vXu1PfQvMTNWFb8jFraxnISMTnPBWMMjr2G/puoMashwaNM//fl7j8gGV5lA==:
       > curl: (7) Couldn't connect to server
       >
       > trying https://registry.yarnpkg.com/@commitlint/format/-/format-17.0.0.tgz
       >   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
       >                                  Dload  Upload   Total   Spent    Left  Speed
       >   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Couldn't connect to server
       > error: cannot download _commitlint_format___format_17.0.0.tgz from any mirror
       For full logs, run 'nix log /nix/store/564cazqnhfyisbzhgbg7p4j16iybvplr-_commitlint_format___format_17.0.0.tgz.drv'.
error: 1 dependencies of derivation '/nix/store/plfkf83l4665q86nv72x719sbh7gwpjv-offline.drv' failed to build
error: 1 dependencies of derivation '/nix/store/khwn2n2vdla80l8ksk9wdg5ajzrx306d-foo-modules-1.0.0.drv' failed to build
error: 1 dependencies of derivation '/nix/store/h10dq4ri57hixvlii0px93x8sxj7qrhj-foo-1.0.0.drv' failed to build

$ nix build
warning: Git tree '/home/david/test/nix-mkYarnPackage-fail' is dirty
error: builder for '/nix/store/63r81qfvbfvahaxjq5pz87fk4qf1dj5d-_commitlint_lint___lint_17.0.0.tgz.drv' failed with exit code 1;
       last 8 log lines:
       > error checking the existence of https://tarballs.nixos.org/sha512/5FL7VLvGJQby24q0pd4UdM8FNFcL+ER1T/UBf8A9KRL5+QXV1Rkl6Zhcl7+SGpGlVo6Yo0pm6aLW716LVKWLGg==:
       > curl: (7) Couldn't connect to server
       >
       > trying https://registry.yarnpkg.com/@commitlint/lint/-/lint-17.0.0.tgz
       >   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
       >                                  Dload  Upload   Total   Spent    Left  Speed
       >   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Couldn't connect to server
       > error: cannot download _commitlint_lint___lint_17.0.0.tgz from any mirror
       For full logs, run 'nix log /nix/store/63r81qfvbfvahaxjq5pz87fk4qf1dj5d-_commitlint_lint___lint_17.0.0.tgz.drv'.
error: 1 dependencies of derivation '/nix/store/plfkf83l4665q86nv72x719sbh7gwpjv-offline.drv' failed to build
error: 1 dependencies of derivation '/nix/store/khwn2n2vdla80l8ksk9wdg5ajzrx306d-foo-modules-1.0.0.drv' failed to build
error: 1 dependencies of derivation '/nix/store/h10dq4ri57hixvlii0px93x8sxj7qrhj-foo-1.0.0.drv' failed to build

$ nix build
warning: Git tree '/home/david/test/nix-mkYarnPackage-fail' is dirty
error: builder for '/nix/store/l29vi8559l7jfbfjjdnbqc4dl8kg7x1r-_commitlint_message___message_17.0.0.tgz.drv' failed with exit code 1;
       last 8 log lines:
       > error checking the existence of https://tarballs.nixos.org/sha512/LpcwYtN+lBlfZijHUdVr8aNFTVpHjuHI52BnfoV01TF7iSLnia0jttzpLkrLmI8HNQz6Vhr9UrxDWtKZiMGsBw==:
       > curl: (7) Couldn't connect to server
       >
       > trying https://registry.yarnpkg.com/@commitlint/message/-/message-17.0.0.tgz
       >   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
       >                                  Dload  Upload   Total   Spent    Left  Speed
       >   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Couldn't connect to server
       > error: cannot download _commitlint_message___message_17.0.0.tgz from any mirror
       For full logs, run 'nix log /nix/store/l29vi8559l7jfbfjjdnbqc4dl8kg7x1r-_commitlint_message___message_17.0.0.tgz.drv'.
error: 1 dependencies of derivation '/nix/store/plfkf83l4665q86nv72x719sbh7gwpjv-offline.drv' failed to build
error: 1 dependencies of derivation '/nix/store/khwn2n2vdla80l8ksk9wdg5ajzrx306d-foo-modules-1.0.0.drv' failed to build
error: 1 dependencies of derivation '/nix/store/h10dq4ri57hixvlii0px93x8sxj7qrhj-foo-1.0.0.drv' failed to build
type or paste code here

Again, note that the particular dependency that fails to be downloaded changes between runs.

I think there is some kind of caching going on that results in cowsay working after succeeding once. If I change the version to 1.4.0, it fails again.