summaryrefslogtreecommitdiff
path: root/pkgs/development/interpreters/python/passthrufun.nix
blob: 8b930de5f39451946795cc3274dabc1a298e8de3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
{
  lib,
  stdenv,
  callPackage,
  pythonPackagesExtensions,
  config,
  makeScopeWithSplicing',
  ...
}:

{
  implementation,
  libPrefix,
  executable,
  sourceVersion,
  pythonVersion,
  packageOverrides,
  sitePackages,
  hasDistutilsCxxPatch,
  pythonOnBuildForBuild,
  pythonOnBuildForHost,
  pythonOnBuildForTarget,
  pythonOnHostForHost,
  pythonOnTargetForTarget,
  pythonAttr ? null,
  pythonABITags ? [ "none" ],
  self, # is pythonOnHostForTarget
}:
let
  pythonPackages =
    let
      ensurePythonModules =
        items:
        let
          exceptions = [
            stdenv
          ];
          providesSetupHook = lib.attrByPath [ "provides" "setupHook" ] false;
          valid =
            value: pythonPackages.hasPythonModule value || providesSetupHook value || lib.elem value exceptions;
          func =
            name: value:
            if lib.isDerivation value then
              lib.extendDerivation (
                valid value
                || throw "${name} should use `buildPythonPackage` or `toPythonModule` if it is to be part of the Python packages set."
              ) { } value
            else
              value;
        in
        lib.mapAttrs func items;
    in
    ensurePythonModules (
      callPackage
        # Function that when called
        # - imports python-packages.nix
        # - adds spliced package sets to the package set
        # - applies overrides from `packageOverrides` and `pythonPackagesOverlays`.
        (
          {
            pkgs,
            stdenv,
            python,
            overrides,
          }:
          let
            pythonPackagesFun = import ./python-packages-base.nix {
              inherit stdenv pkgs lib;
              python = self;
            };
            otherSplices = {
              selfBuildBuild = pythonOnBuildForBuild.pkgs;
              selfBuildHost = pythonOnBuildForHost.pkgs;
              selfBuildTarget = pythonOnBuildForTarget.pkgs;
              selfHostHost = pythonOnHostForHost.pkgs;
              selfTargetTarget = pythonOnTargetForTarget.pkgs or { }; # There is no Python TargetTarget.
            };
            hooks = import ./hooks/default.nix;
            keep = self: hooks self { };
            optionalExtensions = cond: as: lib.optionals cond as;
            pythonExtension = import ../../../top-level/python-packages.nix;
            python2Extension = import ../../../top-level/python2-packages.nix;
            extensions = lib.composeManyExtensions (
              [
                hooks
                pythonExtension
              ]
              ++ (optionalExtensions (!self.isPy3k) [
                python2Extension
              ])
              ++ pythonPackagesExtensions
              ++ [
                overrides
              ]
            );
            aliases =
              self: super:
              lib.optionalAttrs config.allowAliases (import ../../../top-level/python-aliases.nix lib self super);
          in
          makeScopeWithSplicing' {
            inherit otherSplices keep;
            f = lib.extends (lib.composeExtensions aliases extensions) pythonPackagesFun;
          }
        )
        {
          overrides = packageOverrides;
          python = self;
        }
    );
in
rec {
  isPy27 = pythonVersion == "2.7";
  isPy37 = pythonVersion == "3.7";
  isPy38 = pythonVersion == "3.8";
  isPy39 = pythonVersion == "3.9";
  isPy310 = pythonVersion == "3.10";
  isPy311 = pythonVersion == "3.11";
  isPy312 = pythonVersion == "3.12";
  isPy313 = pythonVersion == "3.13";
  isPy314 = pythonVersion == "3.14";
  isPy2 = lib.strings.substring 0 1 pythonVersion == "2";
  isPy3 = lib.strings.substring 0 1 pythonVersion == "3";
  isPy3k = isPy3;
  isPyPy = lib.hasInfix "pypy" interpreter;

  buildEnv = callPackage ./wrapper.nix {
    python = self;
    inherit (pythonPackages) requiredPythonModules;
  };
  withPackages = import ./with-packages.nix { inherit buildEnv pythonPackages; };
  pkgs = pythonPackages;
  interpreter = "${self}/bin/${executable}";
  inherit
    executable
    implementation
    libPrefix
    pythonVersion
    sitePackages
    ;
  inherit sourceVersion;
  pythonAtLeast = lib.versionAtLeast pythonVersion;
  pythonOlder = lib.versionOlder pythonVersion;
  inherit hasDistutilsCxxPatch;
  inherit
    pythonOnBuildForBuild
    pythonOnBuildForHost
    pythonOnBuildForTarget
    pythonOnHostForHost
    pythonOnTargetForTarget
    ;
  inherit pythonABITags;

  tests = callPackage ./tests.nix {
    python = self;
  };

  inherit pythonAttr;
}