{ nixpkgsPath ? <nixpkgs> }:

let
  defaultNixpkgsPath = nixpkgsPath;
in

self: super: {
  selfExpr = let
    config = builtins.removeAttrs self.config [ "_undeclared" ];
    configJson = builtins.toJSON config;

    getSelfExpr = {
      useConfig ? true,
      nixpkgsPath ? defaultNixpkgsPath,
      ...
    }: let
      configText = ''
          config = (builtins.fromJSON '''
            ${configJson}
          ''') // args.config or {};
      '';

      removedAttrNames = self.lib.optional useConfig "config";
      removedAttrNamesText = builtins.toJSON removedAttrNames;
    in ''
      { ... } @ args:

      import ${nixpkgsPath} (
        {
          ${self.lib.optionalString useConfig configText}
        } // builtins.removeAttrs args (builtins.fromJSON '''
          ${removedAttrNamesText}
        ''')
      )
    '';

    mkNixpkgsChannel = args: self.writeTextFile {
      name = args.name or "nixpkgs-self";
      destination = "/default.nix";
      text = getSelfExpr args;
    } // {
      __functor = _: args: mkNixpkgsChannel args;
    };
  in mkNixpkgsChannel {};
}