git.iko.soy/typeable/nixpkgs
on master help sign in
poetry2nix: 1.4.0 -> 1.5.0
commite90bf5782ebb7554e27f8190933dd990f1473afa authoradisbladis <adisbladis@gmail.com> dateMon 24 Feb 2020, 13:36 (7 years ago) parentda84ff3
8 files changed+118−64
pkgs/development/tools/poetry2nix/poetry2nix/default.nix+45−38 pkgs/development/tools/poetry2nix/poetry2nix/hooks/default.nix+25 pkgs/development/tools/poetry2nix/poetry2nix/pyproject-without-path.py → pkgs/development/tools/poetry2nix/poetry2nix/hooks/pyproject-without-path.py pkgs/development/tools/poetry2nix/poetry2nix/hooks/remove-path-dependencies.sh+8 pkgs/development/tools/poetry2nix/poetry2nix/lib.nix+26−22 pkgs/development/tools/poetry2nix/poetry2nix/mk-poetry-dep.nix+12−2 pkgs/development/tools/poetry2nix/poetry2nix/overrides.nix+1−1 pkgs/development/tools/poetry2nix/update+1−1
pkgs/development/tools/poetry2nix/poetry2nix/default.nix+45−38
@@ -25,19 +25,21 @@ let
2525 # Get license by id falling back to input string
2626 getLicenseBySpdxId = spdxId: spdxLicenses.${spdxId} or spdxId;
2727
28− #
29− # Returns an attrset { python, poetryPackages } for the given lockfile
30− #
31− mkPoetryPython =
32− { poetrylock
28+ /*
29+ Returns an attrset { python, poetryPackages, pyProject, poetryLock } for the given pyproject/lockfile.
30+ */
31+ mkPoetryPackages =
32+ { pyproject
33+ , poetrylock
3334 , poetryPkg
3435 , overrides ? [ defaultPoetryOverrides ]
3536 , meta ? {}
3637 , python ? pkgs.python3
3738 , pwd ? null
3839 }@attrs: let
39− lockData = readTOML poetrylock;
40− lockFiles = lib.getAttrFromPath [ "metadata" "files" ] lockData;
40+ pyProject = readTOML pyproject;
41+ poetryLock = readTOML poetrylock;
42+ lockFiles = lib.getAttrFromPath [ "metadata" "files" ] poetryLock;
4143
4244 specialAttrs = [
4345 "overrides"
@@ -48,11 +50,18 @@ let
4850
4951 evalPep508 = mkEvalPep508 python;
5052
51− # Filter packages by their PEP508 markers
53+ # Filter packages by their PEP508 markers & pyproject interpreter version
5254 partitions = let
53− supportsPythonVersion = pkgMeta: if pkgMeta ? marker then (evalPep508 pkgMeta.marker) else true;
55+ supportsPythonVersion = pkgMeta: let
56+ pep508Result = if pkgMeta ? marker then (evalPep508 pkgMeta.marker) else true;
57+
58+ flatDeps = (pyProject.tool.poetry.dependencies or {}) // (pyProject.tool.poetry.dev-dependencies or {});
59+ constraints = flatDeps.${pkgMeta.name}.python or "";
60+ pyprojectResult = isCompatible python.pythonVersion constraints;
61+ in
62+ pyprojectResult && pep508Result;
5463 in
55− lib.partition supportsPythonVersion lockData.package;
64+ lib.partition supportsPythonVersion poetryLock.package;
5665
5766 compatible = partitions.right;
5867 incompatible = partitions.wrong;
@@ -82,18 +91,22 @@ let
8291 );
8392 in
8493 lockPkgs;
85−
8694 overlays = builtins.map getFunctorFn (
8795 [
8896 (
89− self: super: {
90− mkPoetryDep = self.callPackage ./mk-poetry-dep.nix {
91− inherit pkgs lib python poetryLib;
92− };
93− poetry = poetryPkg;
94− # The canonical name is setuptools-scm
95− setuptools-scm = super.setuptools_scm;
96− }
97+ self: super: let
98+ hooks = self.callPackage ./hooks {};
99+ in
100+ {
101+ mkPoetryDep = self.callPackage ./mk-poetry-dep.nix {
102+ inherit pkgs lib python poetryLib;
103+ };
104+ poetry = poetryPkg;
105+ # The canonical name is setuptools-scm
106+ setuptools-scm = super.setuptools_scm;
107+
108+ inherit (hooks) removePathDependenciesHook;
109+ }
97110 )
98111 # Null out any filtered packages, we don't want python.pkgs from nixpkgs
99112 (self: super: builtins.listToAttrs (builtins.map (x: { name = x.name; value = null; }) incompatible))
@@ -110,6 +123,8 @@ let
110123 {
111124 python = py;
112125 poetryPackages = map (pkg: py.pkgs.${pkg.name}) compatible;
126+ poetryLock = poetryLock;
127+ inherit pyProject;
113128 };
114129
115130 /* Returns a package with a python interpreter and all packages specified in the poetry.lock lock file.
@@ -118,7 +133,8 @@ let
118133 poetry2nix.mkPoetryEnv { poetrylock = ./poetry.lock; python = python3; }
119134 */
120135 mkPoetryEnv =
121− { poetrylock
136+ { pyproject
137+ , poetrylock
122138 , overrides ? [ defaultPoetryOverrides ]
123139 , meta ? {}
124140 , pwd ? null
@@ -126,9 +142,9 @@ let
126142 }:
127143 let
128144 poetryPkg = poetry.override { inherit python; };
129− py = mkPoetryPython (
145+ py = mkPoetryPackages (
130146 {
131− inherit poetryPkg poetrylock overrides meta python pwd;
147+ inherit poetryPkg pyproject poetrylock overrides meta python pwd;
132148 }
133149 );
134150 in
@@ -147,13 +163,12 @@ let
147163 }@attrs: let
148164 poetryPkg = poetry.override { inherit python; };
149165
150− py = (
151− mkPoetryPython {
152− inherit poetryPkg poetrylock overrides meta python pwd;
153− }
154− ).python;
166+ poetryPython = mkPoetryPackages {
167+ inherit poetryPkg pyproject poetrylock overrides meta python pwd;
168+ };
169+ py = poetryPython.python;
155170
156− pyProject = readTOML pyproject;
171+ inherit (poetryPython) pyProject;
157172
158173 specialAttrs = [
159174 "overrides"
@@ -187,21 +202,13 @@ let
187202
188203 buildInputs = mkInput "buildInputs" buildSystemPkgs;
189204 propagatedBuildInputs = mkInput "propagatedBuildInputs" (getDeps "dependencies") ++ ([ py.pkgs.setuptools ]);
190− nativeBuildInputs = mkInput "nativeBuildInputs" [ pkgs.yj ];
205+ nativeBuildInputs = mkInput "nativeBuildInputs" [ pkgs.yj py.pkgs.removePathDependenciesHook ];
191206 checkInputs = mkInput "checkInputs" (getDeps "dev-dependencies");
192207
193208 passthru = {
194209 python = py;
195210 };
196211
197− postPatch = (passedAttrs.postPatch or "") + ''
198− # Tell poetry not to resolve the path dependencies. Any version is
199− # fine !
200− yj -tj < pyproject.toml | ${python.interpreter} ${./pyproject-without-path.py} > pyproject.json
201− yj -jt < pyproject.json > pyproject.toml
202− rm pyproject.json
203− '';
204−
205212 meta = meta // {
206213 inherit (pyProject.tool.poetry) description homepage;
207214 license = getLicenseBySpdxId (pyProject.tool.poetry.license or "unknown");
@@ -240,7 +247,7 @@ let
240247
241248in
242249{
243− inherit mkPoetryEnv mkPoetryApplication cli doc;
250+ inherit mkPoetryEnv mkPoetryApplication mkPoetryPackages cli doc;
244251
245252 /*
246253 The default list of poetry2nix override overlays
pkgs/development/tools/poetry2nix/poetry2nix/hooks/default.nixnew file+25
@@ -0,0 +1,25 @@
1+{ python
2+, callPackage
3+, makeSetupHook
4+, yj
5+}:
6+
7+let
8+ pythonInterpreter = python.pythonForBuild.interpreter;
9+in
10+{
11+
12+ removePathDependenciesHook = callPackage (
13+ {}:
14+ makeSetupHook {
15+ name = "remove-path-dependencies.sh";
16+ deps = [];
17+ substitutions = {
18+ inherit pythonInterpreter;
19+ yj = "${yj}/bin/yj";
20+ pyprojectPatchScript = "${./pyproject-without-path.py}";
21+ };
22+ } ./remove-path-dependencies.sh
23+ ) {};
24+
25+}
pkgs/development/tools/poetry2nix/poetry2nix/pyproject-without-path.py → pkgs/development/tools/poetry2nix/poetry2nix/hooks/pyproject-without-path.pyrenamed

The content is the same.

pkgs/development/tools/poetry2nix/poetry2nix/hooks/remove-path-dependencies.shnew file+8
@@ -0,0 +1,8 @@
1+remove-path-dependencies-hook() {
2+ # Tell poetry not to resolve the path dependencies. Any version is fine!
3+ @yj@ -tj < pyproject.toml | @pythonInterpreter@ @pyprojectPatchScript@ > pyproject.json
4+ @yj@ -jt < pyproject.json > pyproject.toml
5+ rm pyproject.json
6+}
7+
8+postPatchHooks+=(remove-path-dependencies-hook)
pkgs/development/tools/poetry2nix/poetry2nix/lib.nix+26−22
@@ -8,27 +8,30 @@ let
88 genList (i: if i == idx then value else (builtins.elemAt list i)) (length list)
99 );
1010
11− # Returns true if pythonVersion matches with the expression in pythonVersions
12− isCompatible = pythonVersion: pythonVersions:
13− let
14− operators = {
15− "||" = cond1: cond2: cond1 || cond2;
16− "," = cond1: cond2: cond1 && cond2; # , means &&
17− };
18− # split string at "," and "||"
19− tokens = builtins.filter (x: x != "") (builtins.split "(,|\\|\\|)" pythonVersions);
20− combine = acc: v:
21− let
22− isOperator = builtins.typeOf v == "list";
23− operator = if isOperator then (builtins.elemAt v 0) else acc.operator;
24− in
25− if isOperator then (acc // { inherit operator; }) else {
26− inherit operator;
27− state = operators."${operator}" acc.state (satisfiesSemver pythonVersion v);
28− };
29− initial = { operator = ","; state = true; };
30− in
31− (builtins.foldl' combine initial tokens).state;
11+ # Compare a semver expression with a version
12+ isCompatible = version: let
13+ operators = {
14+ "||" = cond1: cond2: cond1 || cond2;
15+ "," = cond1: cond2: cond1 && cond2; # , means &&
16+ "&&" = cond1: cond2: cond1 && cond2;
17+ };
18+ splitRe = "(" + (builtins.concatStringsSep "|" (builtins.map (x: lib.replaceStrings [ "|" ] [ "\\|" ] x) (lib.attrNames operators))) + ")";
19+ in
20+ expr:
21+ let
22+ tokens = builtins.filter (x: x != "") (builtins.split splitRe expr);
23+ combine = acc: v:
24+ let
25+ isOperator = builtins.typeOf v == "list";
26+ operator = if isOperator then (builtins.elemAt v 0) else acc.operator;
27+ in
28+ if isOperator then (acc // { inherit operator; }) else {
29+ inherit operator;
30+ state = operators."${operator}" acc.state (satisfiesSemver version v);
31+ };
32+ initial = { operator = "&&"; state = true; };
33+ in
34+ if expr == "" then true else (builtins.foldl' combine initial tokens).state;
3235
3336 fromTOML = builtins.fromTOML or
3437 (
@@ -65,7 +68,7 @@ let
6568 else { pkg = []; str = null; };
6669
6770 # Fetch the artifacts from the PyPI index. Since we get all
68− # info we need from the lock file we don't use nixpkgs' fetchPypi
71+ # info we need from the lock file we don't use nixpkgs' fetchPyPi
6972 # as it modifies casing while not providing anything we don't already
7073 # have.
7174 #
@@ -101,5 +104,6 @@ in
101104 isCompatible
102105 readTOML
103106 getBuildSystemPkgs
107+ satisfiesSemver
104108 ;
105109}
pkgs/development/tools/poetry2nix/poetry2nix/mk-poetry-dep.nix+12−2
@@ -83,7 +83,13 @@ pythonPackages.callPackage (
8383 else (builtins.elemAt (lib.strings.splitString "-" name) 2);
8484 };
8585
86− baseBuildInputs = lib.optional (name != "setuptools_scm" && name != "setuptools-scm") pythonPackages.setuptools-scm;
86+ # Prevent infinite recursion
87+ skipSetupToolsSCM = [
88+ "setuptools_scm"
89+ "setuptools-scm"
90+ "toml" # Toml is an extra for setuptools-scm
91+ ];
92+ baseBuildInputs = lib.optional (! lib.elem name skipSetupToolsSCM) pythonPackages.setuptools-scm;
8793
8894 format = if isLocal then "pyproject" else if isGit then "setuptools" else fileInfo.format;
8995
@@ -100,7 +106,11 @@ pythonPackages.callPackage (
100106 # Stripping pre-built wheels lead to `ELF load command address/offset not properly aligned`
101107 dontStrip = format == "wheel";
102108
103− nativeBuildInputs = if (!isSource && (getManyLinuxDeps fileInfo.name).str != null) then [ autoPatchelfHook ] else [];
109+ nativeBuildInputs = (if (!isSource && (getManyLinuxDeps fileInfo.name).str != null) then [ autoPatchelfHook ] else [])
110+ ++ lib.optional (isLocal) pkgs.yj
111+ ++ lib.optional (format == "pyproject") pythonPackages.removePathDependenciesHook
112+ ;
113+
104114 buildInputs = (
105115 baseBuildInputs
106116 ++ lib.optional (!isSource) (getManyLinuxDeps fileInfo.name).pkg
pkgs/development/tools/poetry2nix/poetry2nix/overrides.nix+1−1
@@ -103,7 +103,7 @@ self: super:
103103 );
104104
105105 # importlib-metadata has an incomplete dependency specification
106− importlib-metadata = super.importlib-metadata.overrideAttrs (
106+ importlib-metadata = if super.importlib-metadata == null then null else super.importlib-metadata.overrideAttrs (
107107 old: {
108108 propagatedBuildInputs = old.propagatedBuildInputs ++ lib.optional self.python.isPy2 self.pathlib2;
109109 }
pkgs/development/tools/poetry2nix/update+1−1
@@ -15,7 +15,7 @@ mv poetry2nix-master/* .
1515
1616mkdir build
1717cp *.nix *.json *.py build/
18−cp -r bin build/
18+cp -r hooks bin build/
1919rm build/shell.nix build/generate.py build/overlay.nix build/flake.nix
2020
2121cat > build/README.md << EOF