How to import a derivation with import?

I am trying to understand how IFD works, and I am trying to learn by writing a mkYarnModules

With pkgs.yarn2nix, I can get a derivation like this(I used plugin-xml as an example). Run the following command inside the cloned dir:

nix-shell -p yarn2nix
yarn2nix --lockfile=./yarn.lock > yarn.nix
nix repl "<nixpkgs>"
# Inside repl
(pkgs.callPackage ./yarn.nix {}).offline_cache

with that I will get a derivation like this(I have cropped out the rest as it is too long):

Derive([("out","/nix/store/p9pilgm7gaql5h0iqg7y3szp3lnlc49b-offline","","")],[("/nix/store/02ywn247n1i6fzdk3lb3g1k4yvsy28dv-path_exists___path_exists_4.0.0.tgz.drv",["out"]))

How can I make use of that? I tried importing this with drv = (import pathTodrv), but it doesn’t work.

And I see this is being used like this in mkYarnModule:

Does yarn understand the content of this file???

If you’re running yarn2nix manually, you’re not going to use IFD. (pkgs.callPackage ./yarn.nix {}).offline_cache is the nix expression you’re after; there’s nothing to import. You might use another derivation to generate yarn.nix rather than generating it manually, and then importing the build output of that derivation would be using IFD.

EDIT: The reason you’re seeing Derive([... is because the nix expression (pkgs.callPackage ./yarn.nix {}).offline_cache results in a derivation that will build the actual thing you’re after, so nix repl prints it as its .drv file path, which is basically the file Nix reads to know how to do the build. It’s a nix internal file format.

2 Likes

Yes I am able to solve it at the end like this, and it is actually importable with callPackage

  mkYarnModule = { src }:
    let
      yarnDrv = pkgs.runCommand "yarn2nix" { } ''
        ${yarn2nix}/bin/yarn2nix --lockfile="${src}/yarn.lock" --no-patch --builtin-fetchgit > "$out"
      '';

      offlineCache = (pkgs.callPackage yarnDrv { }).offline_cache;

      defaultYarnFlags = [
        "--offline"
        "--frozen-lockfile"
        "--ignore-engines"
        "--ignore-scripts"
      ];
    in (stdenv.mkDerivation {
      name = "node_modules";
      #NOTE Need to have nodejs here, or else patchShebangs won't work correctly
      #REF https://discourse.nixos.org/t/what-is-the-patchshebangs-command-in-nix-build-expressions/12656
      buildInputs = with pkgs; [ nodejs yarn ];
      dontUnpack = true;
      buildPhase = ''
        source $stdenv/setup
        export HOME=$(pwd)
        yarn config set yarn-offline-mirror ${offlineCache}
        cp ${src + /package.json} ./package.json
        cp ${src + /yarn.lock} ./yarn.lock
        chmod +wx ./yarn.lock
        ${pkgs.fixup_yarn_lock}/bin/fixup_yarn_lock ./yarn.lock
        yarn install ${lib.escapeShellArgs defaultYarnFlags}
      '';

      installPhase = ''
        mkdir -p "$out"
        if test -d node_modules; then
        mv node_modules "$out"/
        if test -d "$out"/node_modules/.bin; then
        patchShebangs "$out"/node_modules/.bin/tsc
        ln -s "$out"/node_modules/.bin "$out"/bin
        fi
        fi 
      '';
    });