···602#### Using pytestCheckHook {#using-pytestcheckhook}
603604`pytestCheckHook` is a convenient hook which will substitute the setuptools
605-`test` command for a checkPhase which runs `pytest`. This is also beneficial
606when a package may need many items disabled to run the test suite.
607608-Using the example above, the analagous pytestCheckHook usage would be:
609610```
611 checkInputs = [ pytestCheckHook ];
···624 ];
625```
626627-This is expecially useful when tests need to be conditionallydisabled,
628for example:
629630```
···640 "socket"
641 ];
642```
643-Trying to concatenate the related strings to disable tests in a regular checkPhase
644-would be much harder to read. This also enables us to comment on why specific tests
645-are disabled.
0646647#### Using pythonImportsCheck {#using-pythonimportscheck}
648649-Although unit tests are highly prefered to validate correctness of a package, not
650-all packages have test suites that can be ran easily, and some have none at all.
651To help ensure the package still works, `pythonImportsCheck` can attempt to import
652the listed modules.
653654```
655 pythonImportsCheck = [ "requests" "urllib" ];
656```
0657roughly translates to:
0658```
659 postCheck = ''
660 PYTHONPATH=$out/${python.sitePackages}:$PYTHONPATH
661 python -c "import requests; import urllib"
662 '';
663```
664-However, this is done in it's own phase, and not dependent on whether `doCheck = true;`
0665666This can also be useful in verifying that the package doesn't assume commonly
667-present packages (e.g. `setuptools`)
668669#### Using pythonRelaxDepsHook {#using-pythonrelaxdepshook}
670···719```
720721In general you should always use `pythonRelaxDeps`, because `pythonRemoveDeps`
722-will convert build errors in runtime errors. However `pythonRemoveDeps` may
723still be useful in exceptional cases, and also to remove dependencies wrongly
724declared by upstream (for example, declaring `black` as a runtime dependency
725instead of a dev dependency).
···738without having to reinstall after each and every change you make. Development
739mode is also available. Let's see how you can use it.
740741-In the previous Nix expression the source was fetched from an url. We can also
742refer to a local source instead using `src = ./path/to/source/tree;`
743744If we create a `shell.nix` file which calls `buildPythonPackage`, and if `src`
745is a local source, and if the local source has a `setup.py`, then development
746mode is activated.
747748-In the following example we create a simple environment that has a Python 3.9
749version of our package in it, as well as its dependencies and other packages we
750like to have in the environment, all specified with `propagatedBuildInputs`.
751Indeed, we can just add any package we like to have in our environment to
···862863### Optimizations {#optimizations}
864865-The Python interpreters are by default not build with optimizations enabled, because
866the builds are in that case not reproducible. To enable optimizations, override the
867interpreter of interest, e.g using
868···913#### `buildPythonPackage` function {#buildpythonpackage-function}
914915The `buildPythonPackage` function is implemented in
916-`pkgs/development/interpreters/python/mk-python-derivation`
917using setup hooks.
918919The following is an example:
···954* In the `postFixup` phase, the `wrapPythonPrograms` bash function is called to
955 wrap all programs in the `$out/bin/*` directory to include `$PATH`
956 environment variable and add dependent libraries to script's `sys.path`.
957-* In the `installCheck` phase, `${python.interpreter} setup.py test` is ran.
958959By default tests are run because `doCheck = true`. Test dependencies, like
960e.g. the test runner, should be added to `checkInputs`.
···969970* `catchConflicts ? true`: If `true`, abort package build if a package name
971 appears more than once in dependency tree. Default is `true`.
972-* `disabled` ? false: If `true`, package is not built for the particular Python
973 interpreter version.
974* `dontWrapPythonPrograms ? false`: Skip wrapping of Python programs.
975* `permitUserSite ? false`: Skip setting the `PYTHONNOUSERSITE` environment
···14211422### `python setup.py bdist_wheel` cannot create .whl {#python-setup.py-bdist_wheel-cannot-create-.whl}
14231424-Executing `python setup.py bdist_wheel` in a `nix-shell `fails with
01425```
1426ValueError: ZIP does not support timestamps before 1980
1427```
···1513 # the environment.
1514 pythonPackages.python
15151516- # This execute some shell code to initialize a venv in $venvDir before
1517 # dropping into the shell
1518 pythonPackages.venvShellHook
1519
···602#### Using pytestCheckHook {#using-pytestcheckhook}
603604`pytestCheckHook` is a convenient hook which will substitute the setuptools
605+`test` command for a `checkPhase` which runs `pytest`. This is also beneficial
606when a package may need many items disabled to run the test suite.
607608+Using the example above, the analagous `pytestCheckHook` usage would be:
609610```
611 checkInputs = [ pytestCheckHook ];
···624 ];
625```
626627+This is expecially useful when tests need to be conditionally disabled,
628for example:
629630```
···640 "socket"
641 ];
642```
643+644+Trying to concatenate the related strings to disable tests in a regular
645+`checkPhase` would be much harder to read. This also enables us to comment on
646+why specific tests are disabled.
647648#### Using pythonImportsCheck {#using-pythonimportscheck}
649650+Although unit tests are highly preferred to validate correctness of a package, not
651+all packages have test suites that can be run easily, and some have none at all.
652To help ensure the package still works, `pythonImportsCheck` can attempt to import
653the listed modules.
654655```
656 pythonImportsCheck = [ "requests" "urllib" ];
657```
658+659roughly translates to:
660+661```
662 postCheck = ''
663 PYTHONPATH=$out/${python.sitePackages}:$PYTHONPATH
664 python -c "import requests; import urllib"
665 '';
666```
667+668+However, this is done in its own phase, and not dependent on whether `doCheck = true;`.
669670This can also be useful in verifying that the package doesn't assume commonly
671+present packages (e.g. `setuptools`).
672673#### Using pythonRelaxDepsHook {#using-pythonrelaxdepshook}
674···723```
724725In general you should always use `pythonRelaxDeps`, because `pythonRemoveDeps`
726+will convert build errors into runtime errors. However `pythonRemoveDeps` may
727still be useful in exceptional cases, and also to remove dependencies wrongly
728declared by upstream (for example, declaring `black` as a runtime dependency
729instead of a dev dependency).
···742without having to reinstall after each and every change you make. Development
743mode is also available. Let's see how you can use it.
744745+In the previous Nix expression the source was fetched from a url. We can also
746refer to a local source instead using `src = ./path/to/source/tree;`
747748If we create a `shell.nix` file which calls `buildPythonPackage`, and if `src`
749is a local source, and if the local source has a `setup.py`, then development
750mode is activated.
751752+In the following example, we create a simple environment that has a Python 3.9
753version of our package in it, as well as its dependencies and other packages we
754like to have in the environment, all specified with `propagatedBuildInputs`.
755Indeed, we can just add any package we like to have in our environment to
···866867### Optimizations {#optimizations}
868869+The Python interpreters are by default not built with optimizations enabled, because
870the builds are in that case not reproducible. To enable optimizations, override the
871interpreter of interest, e.g using
872···917#### `buildPythonPackage` function {#buildpythonpackage-function}
918919The `buildPythonPackage` function is implemented in
920+`pkgs/development/interpreters/python/mk-python-derivation.nix`
921using setup hooks.
922923The following is an example:
···958* In the `postFixup` phase, the `wrapPythonPrograms` bash function is called to
959 wrap all programs in the `$out/bin/*` directory to include `$PATH`
960 environment variable and add dependent libraries to script's `sys.path`.
961+* In the `installCheck` phase, `${python.interpreter} setup.py test` is run.
962963By default tests are run because `doCheck = true`. Test dependencies, like
964e.g. the test runner, should be added to `checkInputs`.
···973974* `catchConflicts ? true`: If `true`, abort package build if a package name
975 appears more than once in dependency tree. Default is `true`.
976+* `disabled ? false`: If `true`, package is not built for the particular Python
977 interpreter version.
978* `dontWrapPythonPrograms ? false`: Skip wrapping of Python programs.
979* `permitUserSite ? false`: Skip setting the `PYTHONNOUSERSITE` environment
···14251426### `python setup.py bdist_wheel` cannot create .whl {#python-setup.py-bdist_wheel-cannot-create-.whl}
14271428+Executing `python setup.py bdist_wheel` in a `nix-shell`fails with
1429+1430```
1431ValueError: ZIP does not support timestamps before 1980
1432```
···1518 # the environment.
1519 pythonPackages.python
15201521+ # This executes some shell code to initialize a venv in $venvDir before
1522 # dropping into the shell
1523 pythonPackages.venvShellHook
1524