node2nix icon indicating copy to clipboard operation
node2nix copied to clipboard

Proposal for override-able sources

Open Fresheyeball opened this issue 5 years ago • 2 comments

I was going to write this and submit a PR, but time constraints are getting in the way. I propose the following changes to the generated code:

node-packages.nix

Top of file change

{nodeEnv, fetchurl, fetchgit, globalBuildInputs ? [], stdenv}: # add stdenv

let
  rawSources = { # change symbol name to rawSources

Middle of file

args = {sources, extraArgs}: { # change args a function with 2 fields

Bottom of file

  } // extraArgs; # union with extraArgs at the bottom of the expression

  f = x: # add function to apply an override to the args function
    {
      tarball = nodeEnv.buildNodeSourceDist (args x);
      package = nodeEnv.buildNodePackage (args x);
      shell = nodeEnv.buildNodeShell (args x);
    };
in
  stdenv.lib.makeOverridable f { sources = rawSources; extraArgs = {}; } # allow for override

default.nix

import ./node-packages.nix {
  inherit (pkgs) fetchurl fetchgit stdenv; # add stdenv to inherit list
  inherit nodeEnv globalBuildInputs;
}

consumption

Usage in override.nix looks something like this

  node2nix = (import ../default.nix { inherit pkgs ; }).override (old:
    { sources = old.sources // {
        "my/local/package" = old.sources."my/local/package" //
            { src = "${(import ../my/local/package { inherit gitignore; }).tarball}/tarballs/mypack.tgz"; };
      };
      extraArgs = old.extraArgs // {
        src = gitignore ignorance ../.;
      };
    });

I have made this change locally, and find it generally useful to override things. Right now every time I run node2nix, I have to re-implement this change, but it would be better as a PR to this project or fork I think.

Fresheyeball avatar Jul 08 '20 17:07 Fresheyeball

Ah I see the approach you're taking. So yes indeed, with your proposal, we introduce an extra argument that we can use an override mechanism.

I think it will be quite challenging to implement this in node-env.nix as it currently is, although it is not undoable.

One of my main goals for node2nix at the moment is to simplify this file -- it was already too big and too complicated, but with all these crazy tricks that I have added over the years (e.g. package lock generation, NPM cache bypassing etc) it has grown so complicated that even I have difficulties grasping it. :)

I'm working on a companion tool to do all necessary node_modules/ tree modifications. Combining node2nix with this companion tool, allows me to simplify the functions in node-env.nix a lot, and as a result, we can also more easily implement the same features that conventional Nix build functions also have, including overrides.

This new approach will also use data exchange file that contains the dependency tree. It should be quite easy to override, modify and augment this file with function parameters. You can also make the same kind of modifications described in your example expressions.

Perhaps it's better to aim for this solution. It will take a bit of time, but then most of the complex things we're facing now (such as overriding packages), will become considerably easier.

svanderburg avatar Jul 08 '20 21:07 svanderburg

@svanderburg If I submit a PR with the solution I proposed. Would you merge it? I just want to know before I invest the effort. Also how soon to do you think this will be addressed broadly. Because if it's going to be a while, I might need to do the work anyway, and just maintain a fork.

Fresheyeball avatar Jul 27 '20 20:07 Fresheyeball