How to use/build a Python package from a local nixpkgs repo?

I wanted to use the latest langchain version and found out that the nixpkgs release 23.05 has langchain version 0.0.170. I verified this as following

$ nix repl
> :l <nixpkgs>
> pkgs.python3Packages.langchain.version
"0.0.170"

Looking at nixpkgs repo on GitHub, I found out that langchain version 0.0.268 is available in master branch.

In an attempt to see how I could use the latest available version in master, I found this on the wiki: User:Raboof - NixOS Wiki (I came across channels as well but this approach looked simpler)

I tried following this approach by setting up a worktree named nixpkgs-langchain and using a simple Nix expression but it doesn’t seem to work. Here’s the expression:

# default.nix
{ pkgs ? import <nixpkgs> {} }:

let
  customPkgs = import ~/Source/nixpkgs-langchain {
    config.allowUnfree = true;
  };
  langchain = customPkgs.python3Packages.langchain;
  # langchain = pkgs.python311Packages.buildPythonPackage customPkgs.python311Packages.langchain; # I tried this as well, didn't make any difference
  my-python-packages = ps: with ps; [
    langchain
  ];
  my-python = pkgs.python3.withPackages my-python-packages;
in my-python.env

When I run nix-shell, enter the Python repl and try to import langchain, I get ModuleNotFoundError: No module named 'langchain'.

If I use nix repl, I can see that the path is indeed correct:

$ nix repl
> let x = import ~/Source/nixpkgs-langchain {}; in x.python3Packages.langchain.version
"0.0.268"

I would appreciate any guidance as to what I’m doing wrong. Thanks.

try this:

    python3 = pkgs.python3.override {
      packageOverrides = self: super: rec {
        langchain = customPkgs.python3Packages.langchain;
      };
    };
    my-python-packages = ps: with ps; [
      langchain
    ];
    python-with-packages = python3.withPackages my-python-packages;

Thanks for responding. I followed your suggestion and tried this Nix expression:

{ pkgs ? import <nixpkgs> {} }:

let
  customPkgs = import ~/Source/nixpkgs-langchain {
    config.allowUnfree = true;
  };

  python3 = pkgs.python311.override {
    packageOverrides = self: super: rec {
      langchain = customPkgs.python311Packages.langchain;
    };
  };
  my-python-packages = ps: with ps; [
    langchain
  ];
  my-python = python3.withPackages my-python-packages;
in my-python.env

However, the behavior is same, I get an import error when trying to run import langchain inside Python REPL. If this works on your system, can you please share the full Nix expression which I can compare with mine to see if there are any differences?

Thank you for your help.

I found a better way to do this by simply using the worktree as pkgs instead of using both pkgs and customPkgs.

# default.nix
{ }:

let
  pkgs = import ~/Source/nixpkgs-langchain {
    config.allowUnfree = true;
  };
  my-python-packages = ps: [
    ps.langchain
  ];
  my-python = pkgs.python3.withPackages my-python-packages;
in my-python.env