Recomended way of packaiging python packages via the flake

I’m working on flake for my python project. I would like to keep it consistent with something, but can not really find good guidelines.

I see that nixpkgs uses the pkgs.pythonXXXPackages structure, and I wonder if similar structure recreated in flake packages output would be possible and reasonable.
At lest for flake parts, which I’m using, such structure fails. perSystem.packages.* has to be of type package and therefore not allowing nesting.

As a side-note, based on flake-parts perSystem i created following abomination to handle python packages defined as: perPython = {python, ...}: {pythonPackages.foo = {...};}.
I doubt it is the right way. Would be grateful for some advice on it.

{ lib, config, ... }:
let
  inherit (lib)
    mkOption
    evalModules
    mergeAttrsList
    ;
  inherit (lib.types)
    deferredModuleWith
    lazyAttrsOf
    attrs
    package
    ;
in
{
  options = {
    perPython = mkOption {
      type = deferredModuleWith {
        staticModules = [
          {
            options.pythonPackages = mkOption {
              type = attrs;
            };
          }
        ];
      };
    };
  };
  config = {
    perSystem =
      args@{ pkgs, ... }:
      mergeAttrsList (
        map
          (v: {
            packages."python${v}Packages" =
              (evalModules {
                modules = [ config.perPython ];
                specialArgs = {
                  inherit args;
                  python = args.pkgs."python${v}";
                };
              }).config.pythonPackages;
          })
          [
            "3"
            "315"
            "314"
          ]
      );
  };
}

I also noticed that when using the nix shell .#foo nixpkgs#python3, the module is visible but fails on import due to missing dependencies. The exposed foo binnary works.

Somehow it is unclear what you want or what you are questioning. I cannot help you with flake-parts, but with plain nixpkgs it is quite simple. See here for an example: https://git.sr.ht/~bwolf/diktat/tree/master/item/flake.nix. You can easily find other examples, using the GitHub advanced search. Probably also usages of flake-parts.

1 Like