Difficulty with getting a python poetry environment up and running

I’m trying to get a local python environment built as a template for future python projects that I might want. Since there is a poetry2nix (and it seems well supported), I thought I would start by trying to get poetry into my environment, and since I’m just learning nix, and flakes are the future, I thought I would try with a flake:

{
  description = "Application packaged using poetry2nix";

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

  outputs = { self, nixpkgs, flake-utils, poetry2nix }:
    {
      # Nixpkgs overlay providing the application
      overlay = nixpkgs.lib.composeManyExtensions [
        poetry2nix.overlay
        (final: prev: {
          # The application
          myapp = prev.poetry2nix.mkPoetryApplication {
            projectDir = ./.;
          };
        })
      ];
    } // (flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = import nixpkgs {
          inherit system;
          overlays = [ self.overlay ];
        };
      in
      {
        apps = {
          myapp = pkgs.myapp;
        };

        defaultApp = pkgs.myapp;

        devShell = pkgs.mkShell {
          buildInputs = with pkgs; [
            (python39.withPackages (ps: with ps; [ poetry ]))
          ];
        };
      }));
}

It is fairly simple, python 3.9 with poetry (and poetry2nix) from a template I found somewhere, however it fails to build:

❯ nix develop .                                                                                                                                ~proj/t/local_python_2
error: Package ‘python3.9-pyopenssl-22.0.0’ in /nix/store/di36mqc6y19ivaa4qjrb2l82c6dqg7m3-source/pkgs/development/python-modules/pyopenssl/default.nix:73 is marked as broken, refusing to evaluate.

       a) To temporarily allow broken packages, you can use an environment variable
          for a single invocation of the nix tools.

            $ export NIXPKGS_ALLOW_BROKEN=1

        Note: For `nix shell`, `nix build`, `nix develop` or any other Nix 2.4+
        (Flake) command, `--impure` must be passed in order to read this
        environment variable.

       b) For `nixos-rebuild` you can set
         { nixpkgs.config.allowBroken = true; }
       in configuration.nix to override this.

       c) For `nix-env`, `nix-build`, `nix-shell` or any other Nix command you can add
         { allowBroken = true; }
       to ~/.config/nixpkgs/config.nix.
(use '--show-trace' to show detailed location information)

Ok, maybe it is just because 22.05 has some problems, lets replace 22.05 with 21.11 and see if that changes things:

warning: updating lock file '/Users/spott/projects/templates/local_python_nodirenv/flake.lock':
• Updated input 'nixpkgs':
    'github:NixOS/nixpkgs/ce6aa13369b667ac2542593170993504932eb836' (2022-05-30)
  → 'github:NixOS/nixpkgs/a7ecde854aee5c4c7cd6177f54a99d2c1ff28a31' (2021-11-30)
error: builder for '/nix/store/s83b0apc094z6qjs249vqb246nd64ksh-python3.9-cheroot-8.5.2.drv' failed with exit code 1;
       last 10 log lines:
       >     warnings.warn(pytest.PytestUnhandledThreadExceptionWarning(msg))
       >
       > -- Docs: https://docs.pytest.org/en/stable/warnings.html
       > =========================== short test summary info ============================
       > FAILED cheroot/test/test_ssl.py::test_ssl_env[VerifyMode.CERT_NONE-False-pyopenssl]
       > FAILED cheroot/test/test_ssl.py::test_ssl_env[VerifyMode.CERT_NONE-True-pyopenssl]
       > FAILED cheroot/test/test_ssl.py::test_ssl_env[VerifyMode.CERT_OPTIONAL-False-pyopenssl]
       > FAILED cheroot/test/test_ssl.py::test_ssl_env[VerifyMode.CERT_OPTIONAL-True-pyopenssl]
       > FAILED cheroot/test/test_ssl.py::test_ssl_env[VerifyMode.CERT_REQUIRED-True-pyopenssl]
       > ===== 5 failed, 103 passed, 41 deselected, 3 xfailed, 4 warnings in 40.43s =====
       For full logs, run 'nix log /nix/store/s83b0apc094z6qjs249vqb246nd64ksh-python3.9-cheroot-8.5.2.drv'.
error: 1 dependencies of derivation '/nix/store/bcq4n3bfpgwffdg5kf70xj8hszqzc938-python3.9-cherrypy-18.6.0.drv' failed to build
error: 1 dependencies of derivation '/nix/store/mpf5iv3y5vc7mvqha6x6f708xykixg5v-python3.9-cachecontrol-0.12.10.drv' failed to build
error: 1 dependencies of derivation '/nix/store/q1gghrjl3a00yj3r007x856bwn00ffpw-python3-3.9.6-env.drv' failed to build
error: 1 dependencies of derivation '/nix/store/hs4d78xqmxn982w9ni95xwhnmrpq28vw-nix-shell-env.drv' failed to build

Ok, that didn’t work… lets try a different template:

{
  description = "My Python application";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = nixpkgs.legacyPackages.${system};

        customOverrides = self: super: {
          # Overrides go here
        };

        app = pkgs.poetry2nix.mkPoetryApplication {
          projectDir = ./.;
          overrides =
            [ pkgs.poetry2nix.defaultPoetryOverrides customOverrides ];
        };

        packageName = "test";
      in {
        packages.${packageName} = app;

        #defaultPackage = self.packages.${system}.${packageName};

        devShell = pkgs.mkShell {
          buildInputs = with pkgs; [ poetry ];
          inputsFrom = builtins.attrValues self.packages.${system};
        };
      });
}

but this just gives:

error: getting status of '/nix/store/nga7hra037ia2mwsglr5dlzyrziz4nhx-source/poetry.lock': No such file or directory

But I don’t have a poetry.lock because I haven’t installed poetry yet!

even a nix-shell -p python39Packages.poetry spits out the can’t find a matching version problem.

But pypi has matching versions that fit the requirment (Links for xattr), and nixpkgs does too: Nix Package Versions (though it seems this requires using a completely different nixpkgs version…)

I finally got this working by doing:
nix-shell -p python39Packages.xattr -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/ee01de29d2f58d56b1be4ae24c24bd91c5380cea.tar.gz python39Packages.poetry
though this uses an older version of poetry (1.1.14).

How am I supposed to be doing this? Is Poetry just broken in nixpkgs-unstable (and 22.05? and 21.11?). does nixpkgs only have 1 version of python packages?

ps: I’ll be honest, I’m running this on an M1 MacBook on Ventura (which came out before 22.05), so maybe it is my machine… though theoretically I can run x86 code, and the system environment shouldn’t matter in nix… that is the whole point, right?

Nixpkgs generally has 1 version of a python package at a time. You can mix different versions of nixpkgs to resolve different python package versions. Nix Package Versions is a tool that can help you find specific revisions (see the blogpost here for how to use it Searching and installing old versions of Nix packages – Marcelo Lazaroni – Developing for the Interwebs)

GitHub - DavHau/mach-nix: Create highly reproducible python environments can resolve and patch packages from pypi ; which allows one to (more easily) install specific python package versions via requirements string.

Poetry2Nix isn’t for installing Poetry, it’s for installing Python packages listed in an existing Poetry lockfile.

You can install Poetry with the poetry package and then run the poetry initialization like you would without Nix. Then setup Poetry2Nix to read your lockfile and build your Python project/devshell.

Edit: I’m not sure if the poetry package is broken, but it should be available on a stable release (but you don’t need one inside python3Packages).

poetry is not broken , but locked on version 1.4.2.
because poetry package depend on a lot, upgrade it to new version need lots of compile time