mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-06-10 03:23:29 +03:00
doc: update Nix code snippets format
Command: `mdcr --config doc/tests/mdcr-config.toml doc/`
This commit is contained in:
parent
5d979e79ce
commit
bcea0cf344
86 changed files with 2485 additions and 1478 deletions
|
@ -78,24 +78,25 @@ using setup hooks.
|
|||
The following is an example:
|
||||
|
||||
```nix
|
||||
{ lib
|
||||
, buildPythonPackage
|
||||
, fetchPypi
|
||||
{
|
||||
lib,
|
||||
buildPythonPackage,
|
||||
fetchPypi,
|
||||
|
||||
# build-system
|
||||
, setuptools
|
||||
, setuptools-scm
|
||||
# build-system
|
||||
setuptools,
|
||||
setuptools-scm,
|
||||
|
||||
# dependencies
|
||||
, attrs
|
||||
, pluggy
|
||||
, py
|
||||
, setuptools
|
||||
, six
|
||||
# dependencies
|
||||
attrs,
|
||||
pluggy,
|
||||
py,
|
||||
setuptools,
|
||||
six,
|
||||
|
||||
# tests
|
||||
, hypothesis
|
||||
}:
|
||||
# tests
|
||||
hypothesis,
|
||||
}:
|
||||
|
||||
buildPythonPackage rec {
|
||||
pname = "pytest";
|
||||
|
@ -134,7 +135,12 @@ buildPythonPackage rec {
|
|||
description = "Framework for writing tests";
|
||||
homepage = "https://github.com/pytest-dev/pytest";
|
||||
license = lib.licenses.mit;
|
||||
maintainers = with lib.maintainers; [ domenkozar lovek323 madjar lsix ];
|
||||
maintainers = with lib.maintainers; [
|
||||
domenkozar
|
||||
lovek323
|
||||
madjar
|
||||
lsix
|
||||
];
|
||||
};
|
||||
}
|
||||
```
|
||||
|
@ -231,23 +237,31 @@ override first the Python interpreter and pass `packageOverrides` which contains
|
|||
the overrides for packages in the package set.
|
||||
|
||||
```nix
|
||||
with import <nixpkgs> {};
|
||||
with import <nixpkgs> { };
|
||||
|
||||
(let
|
||||
python = let
|
||||
packageOverrides = self: super: {
|
||||
pandas = super.pandas.overridePythonAttrs(old: rec {
|
||||
version = "0.19.1";
|
||||
src = fetchPypi {
|
||||
pname = "pandas";
|
||||
inherit version;
|
||||
hash = "sha256-JQn+rtpy/OA2deLszSKEuxyttqBzcAil50H+JDHUdCE=";
|
||||
(
|
||||
let
|
||||
python =
|
||||
let
|
||||
packageOverrides = self: super: {
|
||||
pandas = super.pandas.overridePythonAttrs (old: rec {
|
||||
version = "0.19.1";
|
||||
src = fetchPypi {
|
||||
pname = "pandas";
|
||||
inherit version;
|
||||
hash = "sha256-JQn+rtpy/OA2deLszSKEuxyttqBzcAil50H+JDHUdCE=";
|
||||
};
|
||||
});
|
||||
};
|
||||
});
|
||||
};
|
||||
in pkgs.python3.override {inherit packageOverrides; self = python;};
|
||||
in
|
||||
pkgs.python3.override {
|
||||
inherit packageOverrides;
|
||||
self = python;
|
||||
};
|
||||
|
||||
in python.withPackages(ps: [ ps.blaze ])).env
|
||||
in
|
||||
python.withPackages (ps: [ ps.blaze ])
|
||||
).env
|
||||
```
|
||||
|
||||
The next example shows a non trivial overriding of the `blas` implementation to
|
||||
|
@ -258,12 +272,16 @@ be used through out all of the Python package set:
|
|||
python3MyBlas = pkgs.python3.override {
|
||||
packageOverrides = self: super: {
|
||||
# We need toPythonModule for the package set to evaluate this
|
||||
blas = super.toPythonModule(super.pkgs.blas.override {
|
||||
blasProvider = super.pkgs.mkl;
|
||||
});
|
||||
lapack = super.toPythonModule(super.pkgs.lapack.override {
|
||||
lapackProvider = super.pkgs.mkl;
|
||||
});
|
||||
blas = super.toPythonModule (
|
||||
super.pkgs.blas.override {
|
||||
blasProvider = super.pkgs.mkl;
|
||||
}
|
||||
);
|
||||
lapack = super.toPythonModule (
|
||||
super.pkgs.lapack.override {
|
||||
lapackProvider = super.pkgs.mkl;
|
||||
}
|
||||
);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
@ -290,9 +308,10 @@ called with `callPackage` and passed `python3` or `python3Packages` (possibly
|
|||
specifying an interpreter version), like this:
|
||||
|
||||
```nix
|
||||
{ lib
|
||||
, python3Packages
|
||||
, fetchPypi
|
||||
{
|
||||
lib,
|
||||
python3Packages,
|
||||
fetchPypi,
|
||||
}:
|
||||
|
||||
python3Packages.buildPythonApplication rec {
|
||||
|
@ -302,7 +321,7 @@ python3Packages.buildPythonApplication rec {
|
|||
|
||||
src = fetchPypi {
|
||||
inherit pname version;
|
||||
hash = "sha256-Pe229rT0aHwA98s+nTHQMEFKZPo/yw6sot8MivFDvAw=";
|
||||
hash = "sha256-Pe229rT0aHwA98s+nTHQMEFKZPo/yw6sot8MivFDvAw=";
|
||||
};
|
||||
|
||||
build-system = with python3Packages; [
|
||||
|
@ -356,10 +375,12 @@ modifications.
|
|||
|
||||
```nix
|
||||
{
|
||||
opencv = toPythonModule (pkgs.opencv.override {
|
||||
enablePython = true;
|
||||
pythonPackages = self;
|
||||
});
|
||||
opencv = toPythonModule (
|
||||
pkgs.opencv.override {
|
||||
enablePython = true;
|
||||
pythonPackages = self;
|
||||
}
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -394,8 +415,10 @@ The `build-system`'s provided will instead become runtime dependencies of the ed
|
|||
|
||||
Note that overriding packages deeper in the dependency graph _can_ work, but it's not the primary use case and overriding existing packages can make others break in unexpected ways.
|
||||
|
||||
``` nix
|
||||
{ pkgs ? import <nixpkgs> { } }:
|
||||
```nix
|
||||
{
|
||||
pkgs ? import <nixpkgs> { },
|
||||
}:
|
||||
|
||||
let
|
||||
pyproject = pkgs.lib.importTOML ./pyproject.toml;
|
||||
|
@ -418,9 +441,10 @@ let
|
|||
};
|
||||
};
|
||||
|
||||
pythonEnv = myPython.withPackages (ps: [ ps.my-editable ]);
|
||||
pythonEnv = myPython.withPackages (ps: [ ps.my-editable ]);
|
||||
|
||||
in pkgs.mkShell {
|
||||
in
|
||||
pkgs.mkShell {
|
||||
packages = [ pythonEnv ];
|
||||
}
|
||||
```
|
||||
|
@ -432,7 +456,7 @@ This example shows how to create an environment that has the Pyramid Web Framewo
|
|||
Saving the following as `default.nix`
|
||||
|
||||
```nix
|
||||
with import <nixpkgs> {};
|
||||
with import <nixpkgs> { };
|
||||
|
||||
python3.buildEnv.override {
|
||||
extraLibs = [ python3Packages.pyramid ];
|
||||
|
@ -453,7 +477,7 @@ packages installed. This is somewhat comparable to `virtualenv`. For example,
|
|||
running `nix-shell` with the following `shell.nix`
|
||||
|
||||
```nix
|
||||
with import <nixpkgs> {};
|
||||
with import <nixpkgs> { };
|
||||
|
||||
(python3.buildEnv.override {
|
||||
extraLibs = with python3Packages; [
|
||||
|
@ -483,7 +507,7 @@ of the packages to be included in the environment. Using the [`withPackages`](#p
|
|||
example for the Pyramid Web Framework environment can be written like this:
|
||||
|
||||
```nix
|
||||
with import <nixpkgs> {};
|
||||
with import <nixpkgs> { };
|
||||
|
||||
python.withPackages (ps: [ ps.pyramid ])
|
||||
```
|
||||
|
@ -493,7 +517,7 @@ version as an argument to the function. In the above example, `ps` equals
|
|||
`pythonPackages`. But you can also easily switch to using python3:
|
||||
|
||||
```nix
|
||||
with import <nixpkgs> {};
|
||||
with import <nixpkgs> { };
|
||||
|
||||
python3.withPackages (ps: [ ps.pyramid ])
|
||||
```
|
||||
|
@ -505,12 +529,14 @@ supports the `env` attribute. The `shell.nix` file from the previous section can
|
|||
thus be also written like this:
|
||||
|
||||
```nix
|
||||
with import <nixpkgs> {};
|
||||
with import <nixpkgs> { };
|
||||
|
||||
(python3.withPackages (ps: with ps; [
|
||||
numpy
|
||||
requests
|
||||
])).env
|
||||
(python3.withPackages (
|
||||
ps: with ps; [
|
||||
numpy
|
||||
requests
|
||||
]
|
||||
)).env
|
||||
```
|
||||
|
||||
In contrast to [`python.buildEnv`](#python.buildenv-function), [`python.withPackages`](#python.withpackages-function) does not support the
|
||||
|
@ -758,11 +784,13 @@ Say we want to have Python 3.12, `numpy` and `toolz`, like before,
|
|||
in an environment. We can add a `shell.nix` file describing our dependencies:
|
||||
|
||||
```nix
|
||||
with import <nixpkgs> {};
|
||||
(python312.withPackages (ps: with ps; [
|
||||
numpy
|
||||
toolz
|
||||
])).env
|
||||
with import <nixpkgs> { };
|
||||
(python312.withPackages (
|
||||
ps: with ps; [
|
||||
numpy
|
||||
toolz
|
||||
]
|
||||
)).env
|
||||
```
|
||||
|
||||
And then at the command line, just typing `nix-shell` produces the same
|
||||
|
@ -785,13 +813,14 @@ What's happening here?
|
|||
To combine this with `mkShell` you can:
|
||||
|
||||
```nix
|
||||
with import <nixpkgs> {};
|
||||
with import <nixpkgs> { };
|
||||
let
|
||||
pythonEnv = python312.withPackages (ps: [
|
||||
ps.numpy
|
||||
ps.toolz
|
||||
]);
|
||||
in mkShell {
|
||||
in
|
||||
mkShell {
|
||||
packages = [
|
||||
pythonEnv
|
||||
|
||||
|
@ -868,10 +897,16 @@ For the sake of completeness, here's how to install the environment system-wide
|
|||
on NixOS.
|
||||
|
||||
```nix
|
||||
{ # ...
|
||||
{
|
||||
# ...
|
||||
|
||||
environment.systemPackages = with pkgs; [
|
||||
(python310.withPackages(ps: with ps; [ numpy toolz ]))
|
||||
(python310.withPackages (
|
||||
ps: with ps; [
|
||||
numpy
|
||||
toolz
|
||||
]
|
||||
))
|
||||
];
|
||||
}
|
||||
```
|
||||
|
@ -891,10 +926,11 @@ building Python libraries is [`buildPythonPackage`](#buildpythonpackage-function
|
|||
`toolz` package.
|
||||
|
||||
```nix
|
||||
{ lib
|
||||
, buildPythonPackage
|
||||
, fetchPypi
|
||||
, setuptools
|
||||
{
|
||||
lib,
|
||||
buildPythonPackage,
|
||||
fetchPypi,
|
||||
setuptools,
|
||||
}:
|
||||
|
||||
buildPythonPackage rec {
|
||||
|
@ -952,9 +988,10 @@ The following expression creates a derivation for the `toolz` package,
|
|||
and adds it along with a `numpy` package to a Python environment.
|
||||
|
||||
```nix
|
||||
with import <nixpkgs> {};
|
||||
with import <nixpkgs> { };
|
||||
|
||||
( let
|
||||
(
|
||||
let
|
||||
my_toolz = python312.pkgs.buildPythonPackage rec {
|
||||
pname = "toolz";
|
||||
version = "0.10.0";
|
||||
|
@ -979,10 +1016,13 @@ with import <nixpkgs> {};
|
|||
};
|
||||
};
|
||||
|
||||
in python312.withPackages (ps: with ps; [
|
||||
numpy
|
||||
my_toolz
|
||||
])
|
||||
in
|
||||
python312.withPackages (
|
||||
ps: with ps; [
|
||||
numpy
|
||||
my_toolz
|
||||
]
|
||||
)
|
||||
).env
|
||||
```
|
||||
|
||||
|
@ -1014,18 +1054,21 @@ The following example shows which arguments are given to [`buildPythonPackage`](
|
|||
order to build [`datashape`](https://github.com/blaze/datashape).
|
||||
|
||||
```nix
|
||||
{ lib
|
||||
, buildPythonPackage
|
||||
, fetchPypi
|
||||
{
|
||||
lib,
|
||||
buildPythonPackage,
|
||||
fetchPypi,
|
||||
|
||||
# build dependencies
|
||||
, setuptools
|
||||
# build dependencies
|
||||
setuptools,
|
||||
|
||||
# dependencies
|
||||
, numpy, multipledispatch, python-dateutil
|
||||
# dependencies
|
||||
numpy,
|
||||
multipledispatch,
|
||||
python-dateutil,
|
||||
|
||||
# tests
|
||||
, pytestCheckHook
|
||||
# tests
|
||||
pytestCheckHook,
|
||||
}:
|
||||
|
||||
buildPythonPackage rec {
|
||||
|
@ -1072,12 +1115,13 @@ Python bindings to `libxml2` and `libxslt`. These libraries are only required
|
|||
when building the bindings and are therefore added as [`buildInputs`](#var-stdenv-buildInputs).
|
||||
|
||||
```nix
|
||||
{ lib
|
||||
, buildPythonPackage
|
||||
, fetchPypi
|
||||
, setuptools
|
||||
, libxml2
|
||||
, libxslt
|
||||
{
|
||||
lib,
|
||||
buildPythonPackage,
|
||||
fetchPypi,
|
||||
setuptools,
|
||||
libxml2,
|
||||
libxslt,
|
||||
}:
|
||||
|
||||
buildPythonPackage rec {
|
||||
|
@ -1128,19 +1172,20 @@ The bindings don't expect to find each of them in a different folder, and
|
|||
therefore we have to set `LDFLAGS` and `CFLAGS`.
|
||||
|
||||
```nix
|
||||
{ lib
|
||||
, buildPythonPackage
|
||||
, fetchPypi
|
||||
{
|
||||
lib,
|
||||
buildPythonPackage,
|
||||
fetchPypi,
|
||||
|
||||
# build dependencies
|
||||
, setuptools
|
||||
# build dependencies
|
||||
setuptools,
|
||||
|
||||
# dependencies
|
||||
, fftw
|
||||
, fftwFloat
|
||||
, fftwLongDouble
|
||||
, numpy
|
||||
, scipy
|
||||
# dependencies
|
||||
fftw,
|
||||
fftwFloat,
|
||||
fftwLongDouble,
|
||||
numpy,
|
||||
scipy,
|
||||
}:
|
||||
|
||||
buildPythonPackage rec {
|
||||
|
@ -1182,7 +1227,10 @@ buildPythonPackage rec {
|
|||
changelog = "https://github.com/pyFFTW/pyFFTW/releases/tag/v${version}";
|
||||
description = "Pythonic wrapper around FFTW, the FFT library, presenting a unified interface for all the supported transforms";
|
||||
homepage = "http://hgomersall.github.com/pyFFTW";
|
||||
license = with lib.licenses; [ bsd2 bsd3 ];
|
||||
license = with lib.licenses; [
|
||||
bsd2
|
||||
bsd3
|
||||
];
|
||||
};
|
||||
}
|
||||
```
|
||||
|
@ -1360,17 +1408,20 @@ This is especially helpful to select tests or specify flags conditionally:
|
|||
|
||||
```nix
|
||||
{
|
||||
disabledTests = [
|
||||
# touches network
|
||||
"download"
|
||||
"update"
|
||||
] ++ lib.optionals (pythonAtLeast "3.8") [
|
||||
# broken due to python3.8 async changes
|
||||
"async"
|
||||
] ++ lib.optionals stdenv.buildPlatform.isDarwin [
|
||||
# can fail when building with other packages
|
||||
"socket"
|
||||
];
|
||||
disabledTests =
|
||||
[
|
||||
# touches network
|
||||
"download"
|
||||
"update"
|
||||
]
|
||||
++ lib.optionals (pythonAtLeast "3.8") [
|
||||
# broken due to python3.8 async changes
|
||||
"async"
|
||||
]
|
||||
++ lib.optionals stdenv.buildPlatform.isDarwin [
|
||||
# can fail when building with other packages
|
||||
"socket"
|
||||
];
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -1495,7 +1546,9 @@ automatically add `pythonRelaxDepsHook` if either `pythonRelaxDeps` or
|
|||
];
|
||||
|
||||
unittestFlags = [
|
||||
"-s" "tests" "-v"
|
||||
"-s"
|
||||
"tests"
|
||||
"-v"
|
||||
];
|
||||
}
|
||||
```
|
||||
|
@ -1576,10 +1629,11 @@ Let's split the package definition from the environment definition.
|
|||
We first create a function that builds `toolz` in `~/path/to/toolz/release.nix`
|
||||
|
||||
```nix
|
||||
{ lib
|
||||
, buildPythonPackage
|
||||
, fetchPypi
|
||||
, setuptools
|
||||
{
|
||||
lib,
|
||||
buildPythonPackage,
|
||||
fetchPypi,
|
||||
setuptools,
|
||||
}:
|
||||
|
||||
buildPythonPackage rec {
|
||||
|
@ -1609,13 +1663,15 @@ It takes an argument [`buildPythonPackage`](#buildpythonpackage-function). We no
|
|||
`callPackage` in the definition of our environment
|
||||
|
||||
```nix
|
||||
with import <nixpkgs> {};
|
||||
with import <nixpkgs> { };
|
||||
|
||||
( let
|
||||
(
|
||||
let
|
||||
toolz = callPackage /path/to/toolz/release.nix {
|
||||
buildPythonPackage = python3Packages.buildPythonPackage;
|
||||
};
|
||||
in python3.withPackages (ps: [
|
||||
in
|
||||
python3.withPackages (ps: [
|
||||
ps.numpy
|
||||
toolz
|
||||
])
|
||||
|
@ -1643,20 +1699,27 @@ We can override the interpreter and pass `packageOverrides`. In the following
|
|||
example we rename the `pandas` package and build it.
|
||||
|
||||
```nix
|
||||
with import <nixpkgs> {};
|
||||
with import <nixpkgs> { };
|
||||
|
||||
(let
|
||||
python = let
|
||||
packageOverrides = self: super: {
|
||||
pandas = super.pandas.overridePythonAttrs(old: {name="foo";});
|
||||
};
|
||||
in pkgs.python310.override {
|
||||
inherit packageOverrides;
|
||||
};
|
||||
(
|
||||
let
|
||||
python =
|
||||
let
|
||||
packageOverrides = self: super: {
|
||||
pandas = super.pandas.overridePythonAttrs (old: {
|
||||
name = "foo";
|
||||
});
|
||||
};
|
||||
in
|
||||
pkgs.python310.override {
|
||||
inherit packageOverrides;
|
||||
};
|
||||
|
||||
in python.withPackages (ps: [
|
||||
ps.pandas
|
||||
])).env
|
||||
in
|
||||
python.withPackages (ps: [
|
||||
ps.pandas
|
||||
])
|
||||
).env
|
||||
```
|
||||
|
||||
Using `nix-build` on this expression will build an environment that contains the
|
||||
|
@ -1670,17 +1733,20 @@ environment that uses it. All packages in the Python package set will now use
|
|||
the updated `scipy` version.
|
||||
|
||||
```nix
|
||||
with import <nixpkgs> {};
|
||||
with import <nixpkgs> { };
|
||||
|
||||
( let
|
||||
(
|
||||
let
|
||||
packageOverrides = self: super: {
|
||||
scipy = super.scipy_0_17;
|
||||
};
|
||||
in (pkgs.python310.override {
|
||||
in
|
||||
(pkgs.python310.override {
|
||||
inherit packageOverrides;
|
||||
}).withPackages (ps: [
|
||||
ps.blaze
|
||||
])
|
||||
}).withPackages
|
||||
(ps: [
|
||||
ps.blaze
|
||||
])
|
||||
).env
|
||||
```
|
||||
|
||||
|
@ -1692,15 +1758,22 @@ If you want the whole of Nixpkgs to use your modifications, then you can use
|
|||
|
||||
```nix
|
||||
let
|
||||
pkgs = import <nixpkgs> {};
|
||||
newpkgs = import pkgs.path { overlays = [ (self: super: {
|
||||
python310 = let
|
||||
packageOverrides = python-self: python-super: {
|
||||
numpy = python-super.numpy_1_18;
|
||||
};
|
||||
in super.python310.override {inherit packageOverrides;};
|
||||
} ) ]; };
|
||||
in newpkgs.inkscape
|
||||
pkgs = import <nixpkgs> { };
|
||||
newpkgs = import pkgs.path {
|
||||
overlays = [
|
||||
(self: super: {
|
||||
python310 =
|
||||
let
|
||||
packageOverrides = python-self: python-super: {
|
||||
numpy = python-super.numpy_1_18;
|
||||
};
|
||||
in
|
||||
super.python310.override { inherit packageOverrides; };
|
||||
})
|
||||
];
|
||||
};
|
||||
in
|
||||
newpkgs.inkscape
|
||||
```
|
||||
|
||||
### `python setup.py bdist_wheel` cannot create .whl {#python-setup.py-bdist_wheel-cannot-create-.whl}
|
||||
|
@ -1790,7 +1863,8 @@ with import <nixpkgs> { };
|
|||
|
||||
let
|
||||
pythonPackages = python3Packages;
|
||||
in pkgs.mkShell rec {
|
||||
in
|
||||
pkgs.mkShell rec {
|
||||
name = "impurePythonEnv";
|
||||
venvDir = "./.venv";
|
||||
buildInputs = [
|
||||
|
@ -1845,7 +1919,8 @@ with import <nixpkgs> { };
|
|||
let
|
||||
venvDir = "./.venv";
|
||||
pythonPackages = python3Packages;
|
||||
in pkgs.mkShell rec {
|
||||
in
|
||||
pkgs.mkShell rec {
|
||||
name = "impurePythonEnv";
|
||||
buildInputs = [
|
||||
pythonPackages.python
|
||||
|
@ -1957,13 +2032,11 @@ The following overlay overrides the call to [`buildPythonPackage`](#buildpythonp
|
|||
```nix
|
||||
final: prev: {
|
||||
pythonPackagesExtensions = prev.pythonPackagesExtensions ++ [
|
||||
(
|
||||
python-final: python-prev: {
|
||||
foo = python-prev.foo.overridePythonAttrs (oldAttrs: {
|
||||
# ...
|
||||
});
|
||||
}
|
||||
)
|
||||
(python-final: python-prev: {
|
||||
foo = python-prev.foo.overridePythonAttrs (oldAttrs: {
|
||||
# ...
|
||||
});
|
||||
})
|
||||
];
|
||||
}
|
||||
```
|
||||
|
@ -1989,13 +2062,14 @@ interpreter of interest, e.g using
|
|||
|
||||
```nix
|
||||
let
|
||||
pkgs = import ./. {};
|
||||
pkgs = import ./. { };
|
||||
mypython = pkgs.python3.override {
|
||||
enableOptimizations = true;
|
||||
reproducibleBuild = false;
|
||||
self = mypython;
|
||||
};
|
||||
in mypython
|
||||
in
|
||||
mypython
|
||||
```
|
||||
|
||||
### How to add optional dependencies? {#python-optional-dependencies}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue