query
On this page

callPackageWith

lib.customisation.callPackageWith

Docs pulled from | This Revision | 12 minutes ago


Nixpkgs manual

Call the package function in the file fn with the required arguments automatically. The function is called with the arguments args, but any missing arguments are obtained from autoArgs. This function is intended to be partially parameterised, e.g.,

callPackage = callPackageWith pkgs;
pkgs = {
  libfoo = callPackage ./foo.nix { };
  libbar = callPackage ./bar.nix { };
};

If the libbar function expects an argument named libfoo, it is automatically passed as an argument. Overrides or missing arguments can be supplied in args, e.g.

libbar = callPackage ./bar.nix {
  libfoo = null;
  enableX11 = true;
};

Inputs

autoArgs

1. Function argument

fn

2. Function argument

args

3. Function argument

Type

callPackageWith :: AttrSet -> ((AttrSet -> a) | Path) -> AttrSet -> a

Noogle detected

Aliases

Implementation

The following is the current implementation of this function.

autoArgs: fn: args:
    let
      f = if isFunction fn then fn else import fn;
      fargs = functionArgs f;

      # All arguments that will be passed to the function
      # This includes automatic ones and ones passed explicitly
      allArgs = intersectAttrs fargs autoArgs // args;

      # arguments that weren't passed automatically to the function
      unpassedArgs = removeAttrs fargs (attrNames allArgs);

    in
    # if nonempty, check if the function has defaults for those other args
    if unpassedArgs == { } || all (value: value) (attrValues unpassedArgs) then
      makeOverridable f allArgs
    else
      # Only show the error for the first missing argument
      # This needs to be an abort so it can't be caught with `builtins.tryEval`,
      # which is used by nix-env and ofborg to filter out packages that don't evaluate.
      # This way we're forced to fix such errors in Nixpkgs,
      # which is especially relevant with allowAliases = false
      abort (makeErrorMessage autoArgs fn args fargs unpassedArgs)