Python
makePythonEnvironment
Create a source-able Python virtual environment using makePythonPoetryEnvironment.
Pre-requisites:
Having both pyproject.toml and poetry.lock.
Types:
- makePythonEnvironment: (
function { ... } -> package):- pythonProjectDir (
path): Required. Python project where bothpyproject.tomlandpoetry.lockare located. - pythonVersion (
str): Required. Python version used to build the environment. Supported versions are3.9,3.10,3.11and3.12. - preferWheels (
bool): Optional. Use pre-compiled wheels from PyPI. Defaults totrue. -
overrides (
function {...} -> package): Optional. Override build attributes for libraries within the environment. For more information see here. Defaults to(self: super: {}).Note
By default we override every python package deleting the
homeless-shelterdirectory and changing theHOMEvariable, we make this to assure purity of builds without sandboxing.
- pythonProjectDir (
Example:
Tip
Refer to makePythonLock
to learn how to generate a poetry.lock.
makePythonPoetryEnvironment
Create a Python virtual environment using poetry2nix.
Pre-requisites:
Having both pyproject.toml and poetry.lock.
Types:
- makePythonPoetryEnvironment: (
function { ... } -> poetry2nixBundle):- pythonProjectDir (
path): Required. Python project where bothpyproject.tomlandpoetry.lockare located. - pythonVersion (
str): Required. Python version used to build the environment. Supported versions are3.9,3.10,3.11and3.12. - preferWheels (
bool): Optional. Use pre-compiled wheels from PyPI. Defaults totrue. -
overrides (
function {...} -> package): Optional. Override build attributes for libraries within the environment. For more information see here. Defaults to(self: super: {}).Note
By default we override every python package deleting the
homeless-shelterdirectory and changing theHOMEvariable, we make this to assure purity of builds without sandboxing.
- pythonProjectDir (
Example:
Tip
Refer to makePythonLock
to learn how to generate a poetry.lock.
makePythonPyprojectPackage
Create a python package bundle using nixpkgs build functions. This bundle includes the package itself, some modifications over the tests and its python environments.
Types:
- makePythonPyprojectPackage:
Input -> Bundle - Input:
Attrs- buildEnv:
Attrs -> PythonEnvDerivationThe nixpkgs buildEnv.override function. Commonly found atnixpkgs."${python_version}".buildEnv.override - buildPythonPackage:
Attrs -> PythonPkgDerivationThe nixpkgs buildPythonPackage function. Commonly found atnixpkgs."${python_version}".pkgs.buildPythonPackage -
pkgDeps:
AttrsThe package dependencies. Usually other python packages build with nix, but can be also a nix derivation of a binary.- runtime_deps:
listOf Derivation - build_deps:
listOf Derivation - test_deps:
listOf Derivation- src:
NixPathThe nix path to the source code of the python package. i.e. not only the package itself, it should also contain a tests folder/module, the pyproject conf and any other meta-package data that the build or tests requires (e.g. custom mypy conf).
- src:
- Bundle:
Attrs- check:
AttrsBuilds of the package only including one test.
- check:
- tests:
Derivation - types:
Derivation- env:
Attrs
- env:
- dev:
PythonEnvDerivationThe python environment containing only runtime_deps and test_deps - runtime:
PythonEnvDerivationThe python environment containing only the package itself and its runtime_deps.- pkg:
PythonPkgDerivationThe output of the nixpkgs buildPythonPackage function i.e. the python package
- pkg:
- runtime_deps:
- buildEnv:
Tip
The default implemented tests require mypy and pytest as test_deps.
If you do not want the default, you can override the checkPhase
of the package i.e. using pythonOverrideUtils or using the
overridePythonAttrs function included on the derivation of
nix built python packages.
Example:
Tip
Because env.runtime include the package, all tests are triggered when building the environment. If is desirable only to trigger an specific check phase, then use the check derivations that override this phase.
Tip
To avoid performance issues use a shared cache system (e.g. cachix) or an override over the package to skip tests (unsafe way) to ensure that tests are executed only once (or never). This can also help on performance over heavy compilation/build processes.
makePythonVscodeSettings
Generate visual studio code configuration for python development.
Types:
- makePythonVscodeSettings:
Input -> SourceAble Input=Attrs- name:
str - env:
PythonEnvDerivationA python environment derivation. e.g. can be builded from nixpkgs standard builders or from someenvof the outputs ofmakePythonPyprojectPackage - bins:
listOf DerivationDerivations to include on thesearchPaths.binsinput
- name:
Example:
pythonOverrideUtils
Integrating python packages built with nix can create conflicts when integrating various into one environment. This utils helps unifying the dependencies into one and only one version per package.
Types:
-
PythonOverride=PythonPkgDerivation -> PythonPkgDerivationA functions that creates a new modifiedPythonPkgDerivationfrom the original. -
pythonOverrideUtils:
Attrs- compose:
(listOf functions) -> _A -> _ZFunction composition, the last function on the list is the first applied. For each function_R -> _Son the list, its predecessor must match their domain with the range of the function i.e._S -> _T. - no_check_override:
PythonOverrideSkips the python package tests that are triggered on the build process. This override is defined throughrecursive_python_pkg_override. - recursive_python_pkg_override:
(Derivation -> bool) -> PythonOverride -> PythonOverrideSearch over all the tree of sub-dependencies the derivation that evaluates to true as defined by the supplied first argument filterDerivation -> bool. If match, the suppliedPythonOverride(second arg) is applied. - replace_pkg:
(listOf str) -> PythonPkgDerivation -> PythonOverrideReplace all python packages that match the supplied list of names, with the supplied python package. The returned override is defined throughrecursive_python_pkg_override
- compose: