Proper setup for python development with Nix and VS Code

Thank you for the response! I tried the configurations you suggested and several others, and finally, I ended up with something like this (code below). It’s using autoPatchelf to change paths in all executables in venv, so I’ll end up with a “standalone” environment. So far, everything seems to work correctly, with the disadvantage that I need to rerun autoPatchelf with every requirements update.

shell.nix:

{ sources ? import ./nix/sources.nix }:

with import sources.nixpkgs {};

mkShell {
  name = "example-env";
  buildInputs = [
    python39
    python39Packages.venvShellHook
    autoPatchelfHook
  ];
  propagatedBuildInputs = [
    stdenv.cc.cc.lib
  ];

  venvDir = "./venv";
  postVenvCreation = ''
    unset SOURCE_DATE_EPOCH
    pip install -U pip setuptools wheel
    pip install -r requirements.txt -r requirements-dev.txt
    pip install -e .
    autoPatchelf ./venv
  '';

  # LD_LIBRARY_PATH = "${pkgs.stdenv.cc.cc.lib}/lib:$LD_LIBRARY_PATH";

  postShellHook = ''
  # set SOURCE_DATE_EPOCH so that we can use python wheels
  export SOURCE_DATE_EPOCH=315532800
  unset LD_LIBRARY_PATH
  '';
  preferLocalBuild = true;
}

.envrc:

use nix

VIRTUAL_ENV="$PWD/venv"
export VIRTUAL_ENV
PATH_add "$VIRTUAL_ENV/bin"

Sources: