@@ -31,10 +31,9 @@ let
3131
3232 callPackage = pkgs.newScope self;
3333
34− bootstrapped-pip = callPackage ../development/python-modules/bootstrapped-pip { };
34+ namePrefix = python.libPrefix + "-";
3535
36− mkPythonDerivation = makeOverridable( callPackage ../development/interpreters/python/mk-python-derivation.nix {
37− });
36+ bootstrapped-pip = callPackage ../development/python-modules/bootstrapped-pip { };
3837
3938 # Derivations built with `buildPythonPackage` can already be overriden with `override`, `overrideAttrs`, and `overrideDerivation`.
4039 # This function introduces `overridePythonAttrs` and it overrides the call to `buildPythonPackage`.
@@ -52,13 +51,20 @@ let
5251 }
5352 else ff;
5453
55− buildPythonPackage = makeOverridablePythonPackage (callPackage ../development/interpreters/python/build-python-package.nix {
56− inherit mkPythonDerivation;
54+ buildPythonPackage = makeOverridablePythonPackage ( makeOverridable (callPackage ../development/interpreters/python/build-python-package.nix {
5755 inherit bootstrapped-pip;
5856 flit = self.flit;
59− });
57+ # We want Python libraries to be named like e.g. "python3.6-${name}"
58+ inherit namePrefix;
59+ pythonModule = python;
60+ }));
6061
61− buildPythonApplication = args: buildPythonPackage ({namePrefix="";} // args );
62+ buildPythonApplication = makeOverridablePythonPackage ( makeOverridable (callPackage ../development/interpreters/python/build-python-package.nix {
63+ inherit bootstrapped-pip;
64+ flit = self.flit;
65+ namePrefix = "";
66+ pythonModule = false;
67+ }));
6268
6369 graphiteVersion = "1.0.2";
6470
@@ -80,10 +86,40 @@ let
8086 else throw "Unsupported kind ${kind}");
8187 in fetcher (builtins.removeAttrs attrs ["format"]) );
8288
89+ # Check whether a derivation provides a Python module.
90+ hasPythonModule = drv: (hasAttr "pythonModule" drv) && ( (getAttr "pythonModule" drv) == python);
91+
92+ # Get list of required Python modules given a list of derivations.
93+ requiredPythonModules = drvs: let
94+ filterNull = list: filter (x: !isNull x) list;
95+ conditionalGetRecurse = attr: condition: drv: let f = conditionalGetRecurse attr condition; in
96+ (if (condition drv) then unique [drv]++(concatMap f (filterNull(getAttr attr drv))) else []);
97+ _required = drv: conditionalGetRecurse "propagatedBuildInputs" hasPythonModule drv;
98+ in [python] ++ (unique (concatMap _required (filterNull drvs)));
99+
100+ # Create a PYTHONPATH from a list of derivations. This function recurses into the items to find derivations
101+ # providing Python modules.
102+ makePythonPath = drvs: stdenv.lib.makeSearchPath python.sitePackages (requiredPythonModules drvs);
103+
104+ # Convert derivation to a Python module.
105+ toPythonModule = drv:
106+ drv.overrideAttrs( oldAttrs: {
107+ # Use passthru in order to prevent rebuilds when possible.
108+ passthru = (oldAttrs.passthru or {})// {
109+ name = namePrefix + oldAttrs.name;
110+ pythonModule = python;
111+ pythonPath = [ ]; # Deprecated, for compatibility.
112+ };
113+ });
114+
115+ disabledIf = x: drv:
116+ if x then throw "${removePrefix namePrefix (drv.pname or drv.name)} not supported for interpreter ${python.executable}" else drv;
117+
83118in {
84119
85− inherit python bootstrapped-pip pythonAtLeast pythonOlder isPy26 isPy27 isPy33 isPy34 isPy35 isPy36 isPyPy isPy3k mkPythonDerivation buildPythonPackage buildPythonApplication;
120+ inherit python bootstrapped-pip pythonAtLeast pythonOlder isPy26 isPy27 isPy33 isPy34 isPy35 isPy36 isPyPy isPy3k buildPythonPackage buildPythonApplication;
86121 inherit fetchPypi callPackage;
122+ inherit hasPythonModule requiredPythonModules makePythonPath disabledIf;
87123
88124 # helpers
89125
@@ -154,9 +190,7 @@ in {
154190
155191 breathe = callPackage ../development/python-modules/breathe { };
156192
157− browsermob-proxy = if ! isPy3k then
158− callPackage ../development/python-modules/browsermob-proxy {}
159− else throw "browsermob-proxy is not supported for ${python.executable}";
193+ browsermob-proxy = disabledIf isPy3k (callPackage ../development/python-modules/browsermob-proxy {});
160194
161195 bugseverywhere = callPackage ../applications/version-management/bugseverywhere {};
162196
@@ -222,11 +256,11 @@ in {
222256
223257 pyamf = callPackage ../development/python-modules/pyamf { };
224258
225− pyatspi = if isPy3k then callPackage ../development/python-modules/pyatspi { } else throw "pyatspi not supported for interpreter ${python.executable}";
259+ pyatspi = disabledIf (!isPy3k) (callPackage ../development/python-modules/pyatspi { });
226260
227261 pycairo = callPackage ../development/python-modules/pycairo { };
228262
229− pycangjie = if isPy3k then callPackage ../development/python-modules/pycangjie { } else throw "pycangjie not supported for interpreter ${python.executable}";
263+ pycangjie = disabledIf (!isPy3k) (callPackage ../development/python-modules/pycangjie { });
230264
231265 pycrypto = callPackage ../development/python-modules/pycrypto { };
232266
@@ -236,7 +270,7 @@ in {
236270
237271 PyChromecast = callPackage ../development/python-modules/pychromecast { };
238272
239− pyexiv2 = if (!isPy3k) then callPackage ../development/python-modules/pyexiv2 {} else throw "pyexiv2 not supported for interpreter ${python.executable}";
273+ pyexiv2 = disabledIf isPy3k (callPackage ../development/python-modules/pyexiv2 {});
240274
241275 py3exiv2 = callPackage ../development/python-modules/py3exiv2 { };
242276
@@ -298,13 +332,13 @@ in {
298332
299333 PyWebDAV = callPackage ../development/python-modules/pywebdav { };
300334
301− pyxml = if !isPy3k then callPackage ../development/python-modules/pyxml{ } else throw "pyxml not supported for interpreter ${python.executable}";
335+ pyxml = disabledIf isPy3k (callPackage ../development/python-modules/pyxml{ });
302336
303337 relatorio = callPackage ../development/python-modules/relatorio { };
304338
305339 pyzufall = callPackage ../development/python-modules/pyzufall { };
306340
307− rhpl = if !isPy3k then callPackage ../development/python-modules/rhpl {} else throw "rhpl not supported for interpreter ${python.executable}";
341+ rhpl = disabledIf isPy3k (callPackage ../development/python-modules/rhpl {});
308342
309343 simpleeval = callPackage ../development/python-modules/simpleeval { };
310344
@@ -1336,10 +1370,10 @@ in {
13361370
13371371 # Build boost for this specific Python version
13381372 # TODO: use separate output for libboost_python.so
1339− boost = pkgs.boost.override {
1373+ boost = toPythonModule (pkgs.boost.override {
13401374 inherit (self) python numpy;
13411375 enablePython = true;
1342− };
1376+ });
13431377
13441378 buttersink = buildPythonPackage rec {
13451379 name = "buttersink-0.6.8";
@@ -3200,15 +3234,15 @@ in {
32003234 propagatedBuildInputs = with self; [ pyusb ];
32013235 };
32023236
3203− opencv = pkgs.opencv.override {
3237+ opencv = toPythonModule (pkgs.opencv.override {
32043238 enablePython = true;
32053239 pythonPackages = self;
3206− };
3240+ });
32073241
3208− opencv3 = pkgs.opencv3.override {
3242+ opencv3 = toPythonModule (pkgs.opencv3.override {
32093243 enablePython = true;
32103244 pythonPackages = self;
3211− };
3245+ });
32123246
32133247 openidc-client = callPackage ../development/python-modules/openidc-client/default.nix {};
32143248
@@ -4735,13 +4769,11 @@ in {
47354769
47364770 easy-thumbnails = callPackage ../development/python-modules/easy-thumbnails { };
47374771
4738− eccodes = if (isPy27) then
4739− (pkgs.eccodes.overrideAttrs (oldattrs: {
4740− name = "${python.libPrefix}-" + oldattrs.name;
4741− })).override {
4742− enablePython = true;
4743− pythonPackages = self;
4744− } else throw "eccodes not supported for interpreter ${python.executable}";
4772+ eccodes = disabledIf (!isPy27)
4773+ (toPythonModule (pkgs.eccodes.override {
4774+ enablePython = true;
4775+ pythonPackages = self;
4776+ }));
47454777
47464778 EditorConfig = buildPythonPackage rec {
47474779 name = "EditorConfig-${version}";
@@ -8712,10 +8744,10 @@ in {
87128744
87138745 folium = callPackage ../development/python-modules/folium { };
87148746
8715− fontforge = pkgs.fontforge.override {
8747+ fontforge = toPythonModule (pkgs.fontforge.override {
87168748 withPython = true;
87178749 inherit python;
8718− };
8750+ });
87198751
87208752 fonttools = callPackage ../development/python-modules/fonttools { };
87218753
@@ -8889,11 +8921,9 @@ in {
88898921 };
88908922 };
88918923
8892− gdal = (pkgs.gdal.overrideDerivation (oldattrs: {
8893− name = "${python.libPrefix}-" + oldattrs.name;
8894− })).override {
8924+ gdal = toPythonModule (pkgs.gdal.override {
88958925 pythonPackages = self;
8896− };
8926+ });
88978927
88988928 gdrivefs = buildPythonPackage rec {
88998929 version = "0.14.8";
@@ -9265,13 +9295,11 @@ in {
92659295 };
92669296 };
92679297
9268− grib-api = if (isPy27) then
9269− (pkgs.grib-api.overrideAttrs (oldattrs: {
9270− name = "${python.libPrefix}-" + oldattrs.name;
9271− })).override {
9272− enablePython = true;
9273− pythonPackages = self;
9274− } else throw "grib-api not supported for interpreter ${python.executable}";
9298+ grib-api = disabledIf (!isPy27) (toPythonModule
9299+ (pkgs.grib-api.override {
9300+ enablePython = true;
9301+ pythonPackages = self;
9302+ }));
92759303
92769304 gspread = buildPythonPackage rec {
92779305 version = "0.2.3";
@@ -10373,14 +10401,14 @@ in {
1037310401 inherit (pkgs) libsodium;
1037410402 };
1037510403
10376− libplist = if isPy3k then throw "libplist not supported for interpreter ${python.executable}" else
10377− (pkgs.libplist.override{python2Packages=self; }).py;
10404+ libplist = disabledIf isPy3k
10405+ (toPythonModule (pkgs.libplist.override{python2Packages=self; })).py;
1037810406
10379− libxml2 = if isPy3k then throw "libxml2 not supported for interpreter ${python.executable}" else
10380− (pkgs.libxml2.override{pythonSupport=true; python2=python;}).py;
10407+ libxml2 = disabledIf isPy3k
10408+ (toPythonModule (pkgs.libxml2.override{pythonSupport=true; python2=python;})).py;
1038110409
10382− libxslt = if isPy3k then throw "libxslt not supported for interpreter ${python.executable}" else
10383− (pkgs.libxslt.override{pythonSupport=true; python2=python; inherit (self) libxml2;}).py;
10410+ libxslt = disabledIf isPy3k
10411+ (toPythonModule (pkgs.libxslt.override{pythonSupport=true; python2=python; inherit (self) libxml2;})).py;
1038410412
1038510413 limnoria = buildPythonPackage rec {
1038610414 name = "limnoria-${version}";
@@ -16994,9 +17022,8 @@ in {
1699417022 };
1699517023 };
1699617024
16997− qscintilla = if isPy3k || isPyPy
16998− then throw "qscintilla-${pkgs.qscintilla.version} not supported for interpreter ${python.executable}"
16999− else buildPythonPackage rec {
17025+ qscintilla = disabledIf (isPy3k || isPyPy)
17026+ (buildPythonPackage rec {
1700017027 # TODO: Qt5 support
1700117028 name = "qscintilla-${version}";
1700217029 version = pkgs.qscintilla.version;
@@ -17025,7 +17052,7 @@ in {
1702517052 maintainers = with maintainers; [ danbst ];
1702617053 platforms = platforms.linux;
1702717054 };
17028− };
17055+ });
1702917056
1703017057
1703117058 qserve = buildPythonPackage rec {
@@ -17173,6 +17200,10 @@ in {
1717317200
1717417201 readme_renderer = callPackage ../development/python-modules/readme_renderer { };
1717517202
17203+ rivet = disabledIf isPy3k (toPythonModule (pkgs.rivet.override {
17204+ python2 = python;
17205+ }));
17206+
1717617207 rjsmin = callPackage ../development/python-modules/rjsmin { };
1717717208
1717817209 pysolr = buildPythonPackage rec {
@@ -17444,7 +17475,7 @@ in {
1744417475
1744517476 rply = callPackage ../development/python-modules/rply/default.nix {};
1744617477
17447− rpm = (pkgs.rpm.override{inherit python;});
17478+ rpm = toPythonModule (pkgs.rpm.override{inherit python;});
1744817479
1744917480 rpmfluff = callPackage ../development/python-modules/rpmfluff {};
1745017481
@@ -22091,7 +22122,7 @@ EOF
2209122122 };
2209222123
2209322124 # For backwards compatibility. Please use nixpkgs.udiskie instead.
22094− udiskie = pkgs.udiskie.override { pythonPackages = self; };
22125+ udiskie = toPythonModule (pkgs.udiskie.override { pythonPackages = self; });
2209522126
2209622127 # Should be bumped along with EFL!
2209722128 pythonefl = buildPythonPackage rec {
@@ -24353,9 +24384,8 @@ EOF
2435324384 ROPGadget = callPackage ../development/python-modules/ROPGadget { };
2435424385
2435524386 # We need "normal" libxml2 and not the python package by the same name.
24356− pywbem = if !(isPy36)
24357− then callPackage ../development/python-modules/pywbem { libxml2 = pkgs.libxml2; }
24358− else throw "pywbem not supported for interpreter ${python.executable}";
24387+ pywbem = disabledIf isPy36
24388+ (callPackage ../development/python-modules/pywbem { libxml2 = pkgs.libxml2; });
2435924389
2436024390 unicorn = callPackage ../development/python-modules/unicorn { };
2436124391
@@ -24427,8 +24457,8 @@ EOF
2442724457
2442824458 zeep = callPackage ../development/python-modules/zeep { };
2442924459
24430− zeitgeist = if isPy3k then throw "zeitgeist not supported for interpreter ${python.executable}" else
24431− (pkgs.zeitgeist.override{python2Packages=self;}).py;
24460+ zeitgeist = disabledIf isPy3k
24461+ (toPythonModule (pkgs.zeitgeist.override{python2Packages=self;})).py;
2443224462
2443324463 zeroconf = callPackage ../development/python-modules/zeroconf { };
2443424464