Adding extra arguments to callPackage within packagesFromDirectoryRecursive

I have a flake with a subdirectory of packages, which are all scripts for the devshell of the flake. Some of them may need access to one or more of the flake’s inputs, but I can’t figure out how to get that to happen.

I tried it like this (terranix being a flake input here):

lib.packagesFromDirectoryRecursive {
  inherit (pkgs) newScope;
  callPackage = path: attrs:
    lib.callPackageWith pkgs path (
      attrs
      // { inherit terranix;}
    );
  directory = ./pkgs;

with the package using terranix looking like this:

{
  pkgs,
  lib,
  terranix,
  ...
}:
terranix.lib.terranixConfiguration {
  extraArgs = {
    inherit lib;
  };
  pkgs = pkgs;
  modules = map lib.custom.relativeToRoot ["terranix"];
}

I cannot figure out why this isn’t working.
when inspecting this using the repl, It seems not to find any of the custom inputs that I added. It says it couldn’t find them (Function called without required argument "terranix"). It does work when I manually use callPackage without packagesFromDirectoryRecursive and import every single package on its own, giving the right args.

I have banged my head against this problem for about a day now and found (as far as i can tell) nothing that would point me in the right direction.

Is there any way i can give extra args to these packages in some way?

the callPackage definition looks somewhat wonky, have you tried something simpler like

callPackage = lib.callPackageWith (pkgs // { inherit terranix; });

one other option would be to force terranix into pkgs via an overlay

The following would suffice:

lib.packagesFromDirectoryRecursive {
  inherit (pkgs) newScope;
  callPackage = pkgs.newScope { inherit terranix; };
  directory = ./pkgs;
};

Don’t use callPackageWith, you’ll miss everything from pkgs.xorg for one.

That’s a much better answer than mine, but is passing the scope itself enough? I thought you’d need to reach into its callPackage

Not sure what you meant, but this is how callPackage is defined by default:

I tried this, but it gives the exact same problem.

My mistake then, I didn’t check the nixpkgs lib code. If you pass newScope it develops the callPackage based off it, evidently:

So I presume you’d have to modify newScope itself rather than callPackage (again, untested, I’m not at my machine presently), which starts to look a bit more inelegant :grimacing:

newScope = extra: (pkgs.newScope { inherit terranix; } // extra);

If that still doesn’t work, we’ll need an example repo or such for us to replicate whatever you’re seeing, to be sure the error isn’t from some other code.

Hah I’ve been staring at that code for so long, I didn’t see it.
Thanks for the help, It works now.