Search code examples
pythonnix

Python module not found in Nix-generated Python environment


I need my user environment configured with Python and the beancount module available. The way I'm currently doing this is to add this package to home.packages (I am using home-manager):

(python310.withPackages (_: [ python310Packages.beancount ]))

This gives me Python 3.10, but upon trying import beancount, I get a ModuleNotFoundError.

The reason I'm invoking withPackages in this way, and not with (python-packages: with python-packages; [ beancount ]) is because I am overriding the beancount module. I am on an Apple Silicon system and am having issues building beancount on aarch64-darwin, so I simply use the x86_64-darwin version, which works fine. This is done with the following two overlays:

(final: prev: (optionalAttrs (prev.stdenv.system == "aarch64-darwin") {
  pkgs-x86 = import nixpkgs-unstable {
    system = "x86_64-darwin";
      inherit (nixpkgsConfig) config;
    };
}))

(final: prev: (optionalAttrs (prev.stdenv.system == "aarch64-darwin") {
  python310Packages.beancount = final.pkgs-x86.python310Packages.beancount;
}))

My suspicion is that the way I'm invoking withPackages is the reason the package is not added to Python's environment, but I'm not sure if this is the case. Either way, how would I go about fixing the issue?

I read somewhere that withPackages fetches packages from PyPi. Is there a way to force x86_64-darwin to be used here? Alternatively, is there a way to manually add modules to Nix's Python environment?


Solution

  • Please take a look at https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/python.section.md#overriding-python-packages-overriding-python-packages on how to overwrite python packages.

    Example overlay from my that I used a while back:

    final: prev: {
      python3 = prev.python3.override {
        packageOverrides = final: prev: {
          python-swiftclient = prev.python-swiftclient.overrideAttrs (oldAttrs: rec {
            postInstall = ''
              install -Dm644 tools/swift.bash_completion $out/share/bash_completion.d/swift
            '';
          });
        };
      };
    }