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.toml
andpoetry.lock
are located. - pythonVersion (
str
): Required. Python version used to build the environment. Supported versions are3.9
,3.10
,3.11
and3.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-shelter
directory and changing theHOME
variable, 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.toml
andpoetry.lock
are located. - pythonVersion (
str
): Required. Python version used to build the environment. Supported versions are3.9
,3.10
,3.11
and3.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-shelter
directory and changing theHOME
variable, 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 -> PythonEnvDerivation
The nixpkgs buildEnv.override function. Commonly found atnixpkgs."${python_version}".buildEnv.override
- buildPythonPackage:
Attrs -> PythonPkgDerivation
The nixpkgs buildPythonPackage function. Commonly found atnixpkgs."${python_version}".pkgs.buildPythonPackage
-
pkgDeps:
Attrs
The 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:
NixPath
The 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:
Attrs
Builds of the package only including one test.
- check:
- tests:
Derivation
- types:
Derivation
- env:
Attrs
- env:
- dev:
PythonEnvDerivation
The python environment containing only runtime_deps and test_deps - runtime:
PythonEnvDerivation
The python environment containing only the package itself and its runtime_deps.- pkg:
PythonPkgDerivation
The 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:
PythonEnvDerivation
A python environment derivation. e.g. can be builded from nixpkgs standard builders or from someenv
of the outputs ofmakePythonPyprojectPackage
- bins:
listOf Derivation
Derivations to include on thesearchPaths.bins
input
- 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 -> PythonPkgDerivation
A functions that creates a new modifiedPythonPkgDerivation
from the original. -
pythonOverrideUtils:
Attrs
- compose:
(listOf functions) -> _A -> _Z
Function composition, the last function on the list is the first applied. For each function_R -> _S
on the list, its predecessor must match their domain with the range of the function i.e._S -> _T
. - no_check_override:
PythonOverride
Skips 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 -> PythonOverride
Search 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 -> PythonOverride
Replace 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: