Using a Python package not in Nixpkgs from a flake

Hi all,

I’ve recently started my Nix journey and am looking for some advice. I’ve managed to prepare a flake that provides a Python development environment with a Python package not in Nixpkgs, matplotlib-set-diagrams:

{
  description = "A Python development environment for Linux";

  inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-24.11";

  outputs = { nixpkgs, ... }: let
    system = "x86_64-linux";
    pkgs = import nixpkgs { inherit system; };
    python = pkgs.python312.withPackages (ps: with ps; [
      buildPythonPackage rec {
        pname = "matplotlib_set_diagrams";
        version = "0.0.2";
        src = fetchPypi {
          inherit pname version;
          sha256 = "sha256-CdkFSoVThCO1k7ZpjAVoBjxpbWvvT3etAajC4OGjVA0=";
        };
        doCheck = false;
        format = "pyproject";
        nativeBuildInputs = [ setuptools ];
        propagatedBuildInputs = [
          numpy
          scipy
          matplotlib
          shapely
          wordcloud
        ];
      }
    ]);
  in {
    devShells."${system}".default = pkgs.mkShell {
      packages = [
        pkgs.black
        pkgs.python312Packages.matplotlib
        pkgs.python312Packages.numpy
        python
      ];
    };
  };
}

However, it feels like a clunky way to do it. I’d rather define matplotlib-set-diagrams separately from the Python package itself and list it as a package similarly to how the Nixpkg ones work (eg numpy), but I can’t figure out how to do that.

Can anyone recommend a better way to write this flake, either along the lines I’m thinking, or a different approach that you have?

Well, that embarrassing - this flake doesn’t actually work. A previous version was functional but it seems my attempts to edit it have left the current version broken and I’m struggling to fix it. The original questions stands, now with the additional request of it, in fact, working. :sweat_smile:

Here is one solution:

{
  description = "A Python development environment for Linux";
  inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-24.11";
  outputs =
    { nixpkgs, ... }:
    let
      system = "x86_64-linux";
      pkgs = import nixpkgs { inherit system; };
    in
    rec {
      packages.${system}.matplotlib-set-diagram =
        with pkgs.python3Packages;
        buildPythonPackage rec {
          pname = "matplotlib_set_diagrams";
          version = "0.0.2";
          src = fetchPypi {
            inherit pname version;
            sha256 = "sha256-CdkFSoVThCO1k7ZpjAVoBjxpbWvvT3etAajC4OGjVA0=";
          };
          doCheck = false;
          format = "pyproject";
          nativeBuildInputs = [ setuptools ];
          propagatedBuildInputs = [
            numpy
            scipy
            matplotlib
            shapely
            wordcloud
          ];
        };
      devShells."${system}".default = pkgs.mkShell {
        packages = [
          (pkgs.python3.withPackages (
            ps: with ps; [
              black
              matplotlib
              numpy
              packages.${system}.matplotlib-set-diagram
            ]
          ))
        ];
      };
    };
}

That’s very helpful, thank you @nim65s :slight_smile: