Editable python environment with poetry2nix?

Hi,

I’m trying to make an editable python environment using a nix flake. I want to install my project in editable mode, and its dependencies.

My understanding is that the poetry2nix.mkPoetryEnv helper was created for this purpose, however I have had trouble getting it to work as I hoped.

I have the following flake, that does what I need, but has some shortfalls:

{
  inputs = {
    flake-utils.url = "github:numtide/flake-utils";
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
    poetry2nix = {
      url = "github:nix-community/poetry2nix";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = { self, nixpkgs, flake-utils, poetry2nix }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
        _poetry2nix = poetry2nix.lib.mkPoetry2Nix { inherit pkgs; };
        myEnv = _poetry2nix.mkPoetryEnv {
          projectDir = ./.;
          editablePackageSources = {
            myProject = "${builtins.getEnv "PWD"}/src";
          };
        };
      in {
        devShells.default = pkgs.mkShell {
          inputsFrom = [ myEnv.env ];
          packages = [ pkgs.poetry ];
        };
      });
}

It’s issues are:

  • It relies on $PWD being the local root of the project. If I do ./., it points to the nix store and doesn’t see the edits I make.
  • I need to use --impure when I run nix develop, presumably because when I make edits these are propagated to the nix store?

I have three questions:

  1. Is a shell.nix file a better application for this? I’m not sure of best-practices around poetry2nix.
  2. Is there a more elegant way of setting editablePackageSources, that doesn’t rely on $PWD being the project root?
  3. What is myEnv, and what is myEnv.env (python is not found, if I just pass my_env to mkShell.fromInputs)?

Many thanks for any help!

1 Like

Just wanted to say thanks for posting your flake, it got me up an running with an editable environment with flakes :grinning:

Also, if you hadn’t seen it, this discussion partly discusses your questions I think. Basically, I don’t think there’s any way to avoid impure when using flakes and trying to have an editable codebase. In which case, something like a shell.nix would probably work. Although for my case, having an “impure” dev environment seems fine