Skip to content

Commit

Permalink
Merge pull request #1358 from nix-community/pyproject-nix
Browse files Browse the repository at this point in the history
Use pyproject.nix's implementations of pep508 & name normalization
  • Loading branch information
adisbladis authored Oct 25, 2023
2 parents 60559cd + 3a448dd commit 6249f97
Show file tree
Hide file tree
Showing 23 changed files with 1,869 additions and 279 deletions.
30 changes: 20 additions & 10 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
, poetryLib ? import ./lib.nix { inherit lib pkgs; inherit (pkgs) stdenv; }
}:
let
inherit (poetryLib) isCompatible readTOML normalizePackageName normalizePackageSet;
inherit (poetryLib) isCompatible readTOML;

pyproject-nix = import ./vendor/pyproject.nix { inherit lib; };

# Name normalization
inherit (pyproject-nix.pypa) normalizePackageName;
normalizePackageSet = lib.attrsets.mapAttrs' (name: value: lib.attrsets.nameValuePair (normalizePackageName name) value);

# Map SPDX identifiers to license names
spdxLicenses = lib.listToAttrs (lib.filter (pair: pair.name != null) (builtins.map (v: { name = if lib.hasAttr "spdxId" v then v.spdxId else null; value = v; }) (lib.attrValues lib.licenses)));
Expand Down Expand Up @@ -104,7 +110,8 @@ lib.makeScope pkgs.newScope (self: {
}:
assert editablePackageSources != { };
import ./editable.nix {
inherit pyProject python pkgs lib poetryLib editablePackageSources;
inherit pyProject python pkgs lib editablePackageSources;
inherit pyproject-nix;
};

/* Returns a package containing scripts defined in tool.poetry.scripts.
Expand Down Expand Up @@ -141,11 +148,6 @@ lib.makeScope pkgs.newScope (self: {
, extras ? [ "*" ]
}:
let
/* The default list of poetry2nix override overlays */
mkEvalPep508 = import ./pep508.nix {
inherit lib poetryLib;
inherit (python) stdenv;
};
getFunctorFn = fn: if builtins.typeOf fn == "set" then fn.__functor else fn;

scripts = pyProject.tool.poetry.scripts or { };
Expand All @@ -170,12 +172,19 @@ lib.makeScope pkgs.newScope (self: {
in
lib.listToAttrs (lib.mapAttrsToList (n: v: { name = normalizePackageName n; value = v; }) lockfiles);

evalPep508 = mkEvalPep508 python;
pep508Env = pyproject-nix.pep508.mkEnviron python;

# Filter packages by their PEP508 markers & pyproject interpreter version
partitions =
let
supportsPythonVersion = pkgMeta: if pkgMeta ? marker then (evalPep508 pkgMeta.marker) else true && isCompatible (poetryLib.getPythonVersion python) pkgMeta.python-versions;
supportsPythonVersion = pkgMeta:
if pkgMeta ? marker then
(
let
marker = pyproject-nix.pep508.parseMarkers pkgMeta.marker;
in
pyproject-nix.pep508.evalMarkers pep508Env marker
) else true && isCompatible (poetryLib.getPythonVersion python) pkgMeta.python-versions;
in
lib.partition supportsPythonVersion poetryLock.package;
compatible = partitions.right;
Expand Down Expand Up @@ -242,7 +251,8 @@ lib.makeScope pkgs.newScope (self: {
self: super:
{
mkPoetryDep = self.callPackage ./mk-poetry-dep.nix {
inherit lib python poetryLib evalPep508;
inherit lib python poetryLib pep508Env;
inherit pyproject-nix;
};

__toPluginAble = toPluginAble self;
Expand Down
4 changes: 2 additions & 2 deletions editable.nix
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{ pkgs
, lib
, poetryLib
, pyProject
, python
, editablePackageSources
, pyproject-nix
}:
let
name = poetryLib.normalizePackageName pyProject.tool.poetry.name;
name = pyproject-nix.pypa.normalizePackageName pyProject.tool.poetry.name;

# Just enough standard PKG-INFO fields for an editable installation
pkgInfoFields = {
Expand Down
13 changes: 0 additions & 13 deletions lib.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@ let
genList (i: if i == idx then value else (builtins.elemAt list i)) (length list)
);

# Normalize package names as per PEP 503
normalizePackageName = name:
let
parts = builtins.split "[-_.]+" name;
partsWithoutSeparator = builtins.filter (x: builtins.typeOf x == "string") parts;
in
lib.strings.toLower (lib.strings.concatStringsSep "-" partsWithoutSeparator);

# Normalize an entire attrset of packages
normalizePackageSet = lib.attrsets.mapAttrs' (name: value: lib.attrsets.nameValuePair (normalizePackageName name) value);

# Get a full semver pythonVersion from a python derivation
getPythonVersion = python:
let
Expand Down Expand Up @@ -242,8 +231,6 @@ in
getBuildSystemPkgs
satisfiesSemver
cleanPythonSources
normalizePackageName
normalizePackageSet
getPythonVersion
getTargetMachine
;
Expand Down
18 changes: 15 additions & 3 deletions mk-poetry-dep.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
, python
, buildPythonPackage
, poetryLib
, evalPep508
, pep508Env
, pyproject-nix
}:
{ name
, version
, pos ? __curPos
, extras ? [ ]
, files
, source
, dependencies ? { }
Expand All @@ -27,7 +29,8 @@ pythonPackages.callPackage
}@args:
let
inherit (python) stdenv;
inherit (poetryLib) isCompatible getManyLinuxDeps fetchFromLegacy fetchFromPypi normalizePackageName;
inherit (pyproject-nix.pypa) normalizePackageName;
inherit (poetryLib) isCompatible getManyLinuxDeps fetchFromLegacy fetchFromPypi;

inherit (import ./pep425.nix {
inherit lib poetryLib python stdenv;
Expand Down Expand Up @@ -141,7 +144,16 @@ pythonPackages.callPackage
constraints = v.python or "";
pep508Markers = v.markers or "";
in
compat constraints && evalPep508 pep508Markers
compat constraints && (if pep508Markers == "" then true else
(pyproject-nix.pep508.evalMarkers
(pep508Env // {
extra = {
# All extras are always enabled
type = "extra";
value = lib.attrNames extras;
};
})
(pyproject-nix.pep508.parseMarkers pep508Markers)))
)
dependencies
);
Expand Down
Loading

0 comments on commit 6249f97

Please sign in to comment.