Add Python packages to python3Packages through overlay?

Hi folks, I need some help to navigate this issue.
I’m trying to add a new Python package using overlays.

I have written a buildPythonPackage derivation for the package following some examples on nixpkgs like so:

# derivations/idasen_ha.nix
{ lib
, buildPythonPackage
, pythonOlder
, fetchFromGitHub
, idasen
,
}:

buildPythonPackage rec {
  pname = "idasen-ha";
  version = "2.6.2";
  format = "pyproject";

  disabled = pythonOlder "3.9";

  src = fetchFromGitHub {
    owner = "abmantis";
    repo = "idasen-ha";
    rev = "refs/tags/v${version}";
    hash = lib.fakeSha256;
  };

  propagatedBuildInputs = [
    idasen
  ];
}

Later, I have tried to add this derivation to the python3Packages by using the overlay, following this post: Add Python package via overlay - #3 by jacg

# overlays.nix
{ ... }:

{
  nixpkgs.overlays = [
    (final: prev: {
      pythonPackagesOverlays = (prev.pythonPackagesOverlays or [ ]) ++ [
        (python-final: python-prev: {
          idasen_ha = python-final.callPackage ./derivations/idasen-ha.nix { };
        })
      ];

      python3 =
        let
          self = prev.python3.override {
            inherit self;
            packageOverrides = prev.lib.composeManyExtensions final.pythonPackagesOverlays;
          };
        in
        self;

      python3Packages = final.python3.pkgs;
    })
  ];
}

And then tried to add those in services.home-assistant:

services.home-assistant = {
    enable = true;
    extraPackages = ps: with ps; [
      idasen_ha  # here!
    ];
    config = {
      default_config = { };
      http = {
        use_x_forwarded_for = true;
        trusted_proxies = [ "127.0.0.1" "::1" ];
      };
    };
  };

However, during nixos-rebuild switch it fails with this error:

       error: undefined variable 'idasen_ha'
       at /nix/store/91wvhfs10j2pl22b05p1h4rfqhl4fdl1-source/hosts/momonoke/home-assistant.nix:11:7:
           10|     extraPackages = ps: with ps; [
           11|       idasen_ha
             |       ^
           12|     ];

which I think means two things:

  1. The overlay approach to add this Python package to python3Packages is not correct,
  2. The python3Packages reference given to services.home-assistant is not the “final” one.

Any ideas?
Thanks!

Modified your derivation a little bit by adding the hash and setuptools:

{
  lib,
  buildPythonPackage,
  pythonOlder,
  fetchFromGitHub,
  setuptools,
  idasen,
}:

buildPythonPackage rec {
  pname = "idasen-ha";
  version = "2.6.2";
  format = "pyproject";

  disabled = pythonOlder "3.9";

  src = fetchFromGitHub {
    owner = "abmantis";
    repo = "idasen-ha";
    rev = "refs/tags/v${version}";
    hash = "sha256-lqqSx4jxQVq2pjVv9lvaX6nNK6OqtMjPqOtLMLpVMUU=";
  };

  nativeBuildInputs = [ setuptools ];

  propagatedBuildInputs = [ idasen ];
}

Don’t know about home-assistant, but using your overlay with environment.systemPackages = [ pkgs.python3Packages.idasen_ha ]; builds the package successfully:

Added packages:
...
[A.]  #4  python3.12-idasen         0.12.0
[A+]  #5  python3.12-idasen-ha      2.6.2

Edit: Not exactly sure if this is the correct way, but this seems to work:

    extraPackages = ps: with ps; [ pkgs.python3Packages.idasen_ha ];
1 Like

That is indeed the case. Home-assistant does some internal overriding of Python packages, which overrides the ones you do for the global set.
Instead override the home-assistant attribute:

final: prev: {
  home-assistant = prev.override { packageOverrides = python-final: python-prev: {}; };
}
1 Like

This doesn’t work, seems like prev.override does not exist?

       error: attribute 'override' missing
       at /nix/store/4cnbvnrdp87f9fl44ch9bx8lcrs0x3hd-source/derivations/overlay.nix:28:24:
           27|
           28|       home-assistant = prev.override {
             |                        ^
           29|         packageOverrides = python-final: python-prev: {
       Did you mean one of overrideCC or overskride?

It should be prev.home-assistant.override, but I’m testing that as well and it still doesn’t work, I guess.

Thanks for the fixes! I have applied those, and nixos-rebuild switch doesn’t complain.

However I am still getting the error that got me here in the first place, so it seems like the module is not made available to Home Assistant (though I guess that’s a different issue):

Aug 02 12:49:30 momonoke hass[4086394]: 2024-08-02 12:49:30.557 ERROR (MainThread) [homeassistant.config_entries] Error occurred loading flow for integration idasen_desk: No module named 'idasen_ha'
1 Like

Maybe it lacks proper support or configuration? You can perhaps open a GitHub issue, ping the Nix home-assistant team and ask them about this.