summaryrefslogtreecommitdiff
path: root/pkgs/development/python-modules/auto-lazy-imports/default.nix
blob: 4ff6cf8b3fa5df7497bb666c69903fbf76ebe37b (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
{
  lib,
  python,
  buildPythonPackage,
  fetchFromGitHub,
  hatchling,
  hatch-autorun,
  pytestCheckHook,
  runCommand,
}:

buildPythonPackage rec {
  pname = "auto-lazy-imports"; # matches pypi
  version = "0.4.2";
  pyproject = true;

  src = fetchFromGitHub {
    owner = "hmiladhia";
    repo = "lazyimports";
    tag = "v${version}";
    hash = "sha256-DPk/fupBuYmm7Xy5+qFkqeRoglflECuX8A0C2ncARhI=";
  };

  build-system = [
    hatchling
    hatch-autorun
  ];

  pythonImportsCheck = [
    "lazyimports"
  ];

  nativeCheckInputs = [
    pytestCheckHook
  ];

  preInstallCheck = ''
    if [[ -f "$out/${python.sitePackages}"/hatch_autorun_auto_lazy_imports.pth ]]; then
      echo >&2 "Found hatch_autorun_auto_lazy_imports.pth in site-packages"
    else
      echo >&2 "ERROR: no hatch_autorun_auto_lazy_imports.pth file found in site-packages, please re-check derivation assumptions"
      false
    fi
  '';

  preCheck = ''
    # Makes python load the .pth file in site-packages
    export NIX_PYTHONPATH="$out/${python.sitePackages}''${NIX_PYTHONPATH:+:"$NIX_PYTHONPATH"}"
  '';

  # check if NIX_PYTHONPATH is properly set in downstream environments
  passthru.tests = {
    check-autorun-pyenv =
      runCommand "${pname}-check-autorun-pyenv"
        {
          nativeBuildInputs = [
            (python.withPackages (ps: [
              ps.auto-lazy-imports
              ps.pytest
            ]))
          ];
        }
        ''
          pytest ${src}/tests && touch $out
        '';

    # TODO: this requires user to set NIX_PYTHONPATH
    # check-autorun-env =
    #   runCommand "${pname}-check-autorun-env"
    #     {
    #       nativeBuildInputs = [
    #         python
    #         python.pkgs.auto-lazy-imports
    #         python.pkgs.pytest
    #       ];
    #     }
    #     ''
    #       pytest ${src}/tests && touch $out
    #     '';
  };

  meta = {
    description = "Enable lazy imports using native python syntax";
    homepage = "https://github.com/hmiladhia/lazyimports";
    license = lib.licenses.mit;
    maintainers = with lib.maintainers; [ pbsds ];
  };
}