numtide / devshell

Per project developer environments
https://numtide.github.io/devshell/
MIT License
1.23k stars 88 forks source link

For python, how to use venvShellHook with `numtide / devshell` #309

Open geekodour opened 5 months ago

geekodour commented 5 months ago

We can use venvShellHook with plain shell.nix with something like

let
  pkgs = import <nixpkgs> { };
  pyPackages = pkgs.python311Packages;
in pkgs.mkShell {
  name = "py";
  venvDir = "./.venv";
  buildInputs = [
    pyPackages.pip
    pyPackages.venvShellHook
    pkgs.ruff # linter
    pkgs.python311
  ];

  postShellHook = ''
    pip install poetry
    unset SOURCE_DATE_EPOCH
  '';
}

And with vanilla flake with something like:

 devShells.default = pkgs.mkShell {
        nativeBuildInputs = with pkgs; [pkgs.python311Packages.pip pkgs.python311Packages.venvShellHook];
        venvDir = "./.venv";
 };

But with numtide / devshell it seems like I cannot use venvShellHook and hence no way to have the sweet virtual env created for me as I cd into the directory.

yajo commented 2 months ago

Did you find a solution?

geekodour commented 2 months ago

@yajo my solution was not using numtide devshell but using flake devshell with flake as described here: https://fasterthanli.me/series/building-a-rust-service-with-nix/part-10

Here's a flake i use for one of my projects:

Hope it helps!