Nix-shell: poetry2nix ModuleNotFoundError: No module named 'hatchling'

I am not able to solve this problem with the poetry2nix.

This is my simple derivation

let
  # Import nixpkgs
  pkgs = import <nixpkgs> { };

  # Import poetry2nix and its functions
  poetry2nix = pkgs.poetry2nix;

  # Create a Python environment using poetry2nix
  baseEnv = poetry2nix.mkPoetryEnv {
    python = pkgs.python310;
    projectDir = ./.;
    poetrylock = ./poetry.lock; # Path to your poetry.lock file
  };
  finalEnv = baseEnv.overrideAttrs (oldAttrs: {
    buildInputs = oldAttrs.buildInputs ++ [ pkgs.python310Packages.hatchling ];
  });
in
with pkgs;
stdenv.mkDerivation {
  name = "git-dev-nix-env";

  buildInputs = [
    gcc
    sqlite
    autoconf
    git
    clang
    libtool
    sqlite
    autoconf
    autogen
    automake
    gnumake
    pkg-config
    gmp
    zlib
    gettext
    libsodium

    # Python dep
    poetry
    python310
    python310Packages.pip
    python310Packages.pytest
    baseEnv # Add the poetry environment here

    # optional dev libraries
    ccache

    # debugs libraries
    valgrind

    # Indirected dependencies for running tests
    bitcoind
  ];
}

But my nix-shell fails with

error: builder for '/nix/store/pq31ss6pdxdxppy7p2w4v8gdcibvhvn7-python3.10-attrs-23.1.0.drv' failed with exit code 2;
       last 10 log lines:
       >   File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
       >   File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
       >   File "<frozen importlib._bootstrap>", line 992, in _find_and_load_unlocked
       >   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
       >   File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
       >   File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
       >   File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
       > ModuleNotFoundError: No module named 'hatchling'
       >
       >
       For full logs, run 'nix log /nix/store/pq31ss6pdxdxppy7p2w4v8gdcibvhvn7-python3.10-attrs-23.1.0.drv'.
error (ignored): error: cannot unlink '/tmp/nix-build-python3.10-base58-2.1.1.drv-1/base58-2.1.1': Directory not empty
error: 1 dependencies of derivation '/nix/store/xbaxapcgg0hgzjx5mbj3lk5vaxkl3x95-python3-3.10.12-env.drv' failed to build

Any idea on how to solve this?

It seems that you’re not actually using the finalEnv where you added hatchling.
Or is finalEnv just a leftover from another attempt?

Ah my bad, the following diff produce the same error

diff --git a/contrib/nix/default.nix b/contrib/nix/default.nix
index 36f12da41..9c2e68511 100644
--- a/contrib/nix/default.nix
+++ b/contrib/nix/default.nix
@@ -41,7 +41,7 @@ stdenv.mkDerivation {
     python310
     python310Packages.pip
     python310Packages.pytest
-    baseEnv # Add the poetry environment here
+    finalEnv # Add the poetry environment here

     # optional dev libraries
     ccache

The failing derivation is not your env, but the attrs package. that derivation has to be overriden to depend on hatchling. You should be able to do this by adding

overrides = poetry2nix.overrides.withDefaults (self: super: {
  attrs = super.attrs.overridePythonAttrs (old: {
    buildInputs = old.buildInputs or [] ++ [super.hatchling];
  });
});

to your mkPoetryEnv invocation.

1 Like