How to use node2nix in nix derivation correctly?

I am trying to learn how to write a derivation properly for Nodejs. I do aware yarn2nix and mkYarnModules exist, but I want to do it once by myself.

I am trying to use node2nix to generate the nix file for package.json in a derivation like this:

{ pkgs, lib, stdenv, ... }:

  mkNpmModules = { pname, version, src }:
    let cwd = pkgs.fetchFromGitHub src;
    in (pkgs.stdenv.mkDerivation {
      inherit pname version;

      src = cwd;

      buildInputs = with pkgs; [ nodejs nodePackages.node2nix git ];

      buildPhase = let
        nodeDependencies = pkgs.runCommand "node.nix" { } ''
          ${pkgs.nodePackages.node2nix}/bin/node2nix -i ${cwd}/package.json
      in ''
        ls -a ${nodeDependencies}

Unfortunately this doesn’t work. I am getting info retry will retry, error on last attempt: Error: getaddrinfo ENOTFOUND, which I believe is because of the sandbox nature of derivation, which network doesn’t exist there?

But I have seen similar being done in mkYarnModules like this. Why is that? Is the derivation from yarn2nix generated before entering the derivation?

I remember node2nix with --input flag querying the npm registry to create a nix file of the locked packages. This won’t work within the sandbox. You can try --lock flag to specify “package-lock.json” and it might create the nix file using the lock file without needing to query the npm registry.

1 Like

I see so I should prevent the query from running in the first place. Thanks for the help.