@@ -0,0 +1,668 @@
1+# Python
2+
3+## User Guide
4+
5+Several versions of Python are available on Nix as well as a high amount of
6+packages. The default interpreter is CPython 2.7.
7+
8+### Using Python
9+
10+#### Installing Python and packages
11+
12+It is important to make a distinction between Python packages that are
13+used as libraries, and applications that are written in Python.
14+
15+Applications on Nix are installed typically into your user
16+profile imperatively using `nix-env -i`, and on NixOS declaratively by adding the
17+package name to `environment.systemPackages` in `/etc/nixos/configuration.nix`.
18+Dependencies such as libraries are automatically installed and should not be
19+installed explicitly.
20+
21+The same goes for Python applications and libraries. Python applications can be
22+installed in your profile, but Python libraries you would like to use to develop
23+cannot. If you do install libraries in your profile, then you will end up with
24+import errors.
25+
26+#### Python environments using `nix-shell`
27+
28+The recommended method for creating Python environments for development is with
29+`nix-shell`. Executing
30+
31+```sh
32+$ nix-shell -p python35Packages.numpy python35Packages.toolz
33+```
34+
35+opens a Nix shell which has available the requested packages and dependencies.
36+Now you can launch the Python interpreter (which is itself a dependency)
37+
38+```sh
39+[nix-shell:~] python3
40+```
41+
42+If the packages were not available yet in the Nix store, Nix would download or
43+build them automatically. A convenient option with `nix-shell` is the `--run`
44+option, with which you can execute a command in the `nix-shell`. Let's say we
45+want the above environment and directly run the Python interpreter
46+
47+```sh
48+$ nix-shell -p python35Packages.numpy python35Packages.toolz --run "python3"
49+```
50+
51+This way you can use the `--run` option also to directly run a script
52+
53+```sh
54+$ nix-shell -p python35Packages.numpy python35Packages.toolz --run "python3 myscript.py"
55+```
56+
57+In fact, for this specific use case there is a more convenient method. You can
58+add a [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) to your script
59+specifying which dependencies Nix shell needs. With the following shebang, you
60+can use `nix-shell myscript.py` and it will make available all dependencies and
61+run the script in the `python3` shell.
62+
63+```py
64+#! /usr/bin/env nix-shell
65+#! nix-shell -i python3 -p python3Packages.numpy
66+
67+import numpy
68+
69+print(numpy.__version__)
70+```
71+
72+Likely you do not want to type your dependencies each and every time. What you
73+can do is write a simple Nix expression which sets up an environment for you,
74+requiring you only to type `nix-shell`. Say we want to have Python 3.5, `numpy`
75+and `toolz`, like before, in an environment. With a `shell.nix` file
76+containing
77+
78+```nix
79+with import <nixpkgs> {};
80+
81+(pkgs.python35.buildEnv.override {
82+ extraLibs = with pkgs.python35Packages; [ numpy toolz ];
83+}).env
84+```
85+executing `nix-shell` gives you again a Nix shell from which you can run Python.
86+
87+What's happening here?
88+
89+1. We begin with importing the Nix Packages collections. `import <nixpkgs>` import the `<nixpkgs>` function, `{}` calls it and the `with` statement brings all attributes of `nixpkgs` in the local scope. Therefore we can now use `pkgs`.
90+2. Then we create a Python 3.5 environment with `pkgs.buildEnv`. Because we want to use it with a custom set of Python packages, we override it.
91+3. The `extraLibs` argument of the original `buildEnv` function can be used to specify which packages should be included. We want `numpy` and `toolz`. Again, we use the `with` statement to bring a set of attributes into the local scope.
92+4. And finally, for in interactive use we return the environment.
93+
94+### Developing with Python
95+
96+
97+Now that you know how to get a working Python environment on Nix, it is time to go forward and start actually developing with Python.
98+We will first have a look at how Python packages are packaged on Nix. Then, we will look how you can use development mode with your code.
99+
100+#### Python packaging on Nix
101+
102+On Nix all packages are built by functions. The main function in Nix for building Python packages is [`buildPythonPackage`](https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/python-modules/generic/default.nix).
103+Let's see how we would build the `toolz` package. According to [`python-packages.nix`](https://raw.githubusercontent.com/NixOS/nixpkgs/master/pkgs/top-level/python-packages.nix) `toolz` is build using
104+
105+```nix
106+toolz = buildPythonPackage rec{
107+ name = "toolz-${version}";
108+ version = "0.7.4";
109+
110+ src = pkgs.fetchurl{
111+ url = "https://pypi.python.org/packages/source/t/toolz/toolz-${version}.tar.gz";
112+ sha256 = "43c2c9e5e7a16b6c88ba3088a9bfc82f7db8e13378be7c78d6c14a5f8ed05afd";
113+ };
114+
115+ meta = {
116+ homepage = "http://github.com/pytoolz/toolz/";
117+ description = "List processing tools and functional utilities";
118+ license = licenses.bsd3;
119+ maintainers = with maintainers; [ fridh ];
120+ };
121+};
122+```
123+
124+What happens here? The function `buildPythonPackage` is called and as argument
125+it accepts a set. In this case the set is a recursive set ([`rec`](http://nixos.org/nix/manual/#sec-constructs)).
126+One of the arguments is the name of the package, which consists of a basename
127+(generally following the name on PyPi) and a version. Another argument, `src`
128+specifies the source, which in this case is fetched from an url. `fetchurl` not
129+only downloads the target file, but also validates its hash. Furthermore, we
130+specify some (optional) [meta information](http://nixos.org/nixpkgs/manual/#chap-meta).
131+
132+The output of the function is a derivation, which is an attribute with the name
133+`toolz` of the set `pythonPackages`. Actually, sets are created for all interpreter versions,
134+so `python27Packages`, `python34Packages`, `python35Packages` and `pypyPackages`.
135+
136+The above example works when you're directly working on
137+`pkgs/top-level/python-packages.nix` in the Nixpkgs repository. Often though,
138+you will want to test a Nix expression outside of the Nixpkgs tree. If you
139+create a `shell.nix` file with the following contents
140+
141+```nix
142+with import <nixpkgs> {};
143+
144+pkgs.python35Packages.buildPythonPackage rec {
145+ name = "toolz-${version}";
146+ version = "0.7.4";
147+
148+ src = pkgs.fetchurl{
149+ url = "https://pypi.python.org/packages/source/t/toolz/toolz-${version}.tar.gz";
150+ sha256 = "43c2c9e5e7a16b6c88ba3088a9bfc82f7db8e13378be7c78d6c14a5f8ed05afd";
151+ };
152+
153+ meta = {
154+ homepage = "http://github.com/pytoolz/toolz/";
155+ description = "List processing tools and functional utilities";
156+ license = licenses.bsd3;
157+ maintainers = with maintainers; [ fridh ];
158+ };
159+}
160+```
161+
162+and then execute `nix-shell` will result in an environment in which you can use
163+Python 3.5 and the `toolz` package. As you can see we had to explicitly mention
164+for which Python version we want to build a package.
165+
166+The above example considered only a single package. Generally you will want to use multiple packages.
167+If we create a `shell.nix` file with the following contents
168+
169+```nix
170+with import <nixpkgs> {};
171+
172+( let
173+ toolz = pkgs.python35Packages.buildPythonPackage rec {
174+ name = "toolz-${version}";
175+ version = "0.7.4";
176+
177+ src = pkgs.fetchurl{
178+ url = "https://pypi.python.org/packages/source/t/toolz/toolz-${version}.tar.gz";
179+ sha256 = "43c2c9e5e7a16b6c88ba3088a9bfc82f7db8e13378be7c78d6c14a5f8ed05afd";
180+ };
181+
182+ meta = {
183+ homepage = "http://github.com/pytoolz/toolz/";
184+ description = "List processing tools and functional utilities";
185+ license = licenses.bsd3;
186+ maintainers = with maintainers; [ fridh ];
187+ };
188+ };
189+
190+ in pkgs.python35.buildEnv.override rec {
191+
192+ extraLibs = [ pkgs.python35Packages.numpy toolz ];
193+}
194+).env
195+```
196+
197+and again execute `nix-shell`, then we get a Python 3.5 environment with our
198+locally defined package as well as `numpy` which is build according to the
199+definition in Nixpkgs. What did we do here? Well, we took the Nix expression
200+that we used earlier to build a Python environment, and said that we wanted to
201+include our own version of `toolz`. To introduce our own package in the scope of
202+`buildEnv.override` we used a
203+[`let`](http://nixos.org/nix/manual/#sec-constructs) expression.
204+
205+### Handling dependencies
206+
207+Our example, `toolz`, doesn't have any dependencies on other Python
208+packages or system libraries. According to the manual, `buildPythonPackage`
209+uses the arguments `buildInputs` and `propagatedBuildInputs` to specify dependencies. If something is
210+exclusively a build-time dependency, then the dependency should be included as a
211+`buildInput`, but if it is (also) a runtime dependency, then it should be added
212+to `propagatedBuildInputs`. Test dependencies are considered build-time dependencies.
213+
214+The following example shows which arguments are given to `buildPythonPackage` in
215+order to build [`datashape`](https://github.com/blaze/datashape).
216+
217+```nix
218+datashape = buildPythonPackage rec {
219+ name = "datashape-${version}";
220+ version = "0.4.7";
221+
222+ src = pkgs.fetchurl {
223+ url = "https://pypi.python.org/packages/source/D/DataShape/${name}.tar.gz";
224+ sha256 = "14b2ef766d4c9652ab813182e866f493475e65e558bed0822e38bf07bba1a278";
225+ };
226+
227+ buildInputs = with self; [ pytest ];
228+ propagatedBuildInputs = with self; [ numpy multipledispatch dateutil ];
229+
230+ meta = {
231+ homepage = https://github.com/ContinuumIO/datashape;
232+ description = "A data description language";
233+ license = licenses.bsd2;
234+ maintainers = with maintainers; [ fridh ];
235+ };
236+};
237+```
238+
239+We can see several runtime dependencies, `numpy`, `multipledispatch`, and
240+`dateutil`. Furthermore, we have one `buildInput`, i.e. `pytest`. `pytest` is a
241+test runner and is only used during the `checkPhase` and is therefore not added
242+to `propagatedBuildInputs`.
243+
244+In the previous case we had only dependencies on other Python packages to consider.
245+Occasionally you have also system libraries to consider. E.g., `lxml` provides
246+Python bindings to `libxml2` and `libxslt`. These libraries are only required
247+when building the bindings and are therefore added as `buildInputs`.
248+
249+```nix
250+lxml = buildPythonPackage rec {
251+ name = "lxml-3.4.4";
252+
253+ src = pkgs.fetchurl {
254+ url = "http://pypi.python.org/packages/source/l/lxml/${name}.tar.gz";
255+ sha256 = "16a0fa97hym9ysdk3rmqz32xdjqmy4w34ld3rm3jf5viqjx65lxk";
256+ };
257+
258+ buildInputs = with self; [ pkgs.libxml2 pkgs.libxslt ];
259+
260+ meta = {
261+ description = "Pythonic binding for the libxml2 and libxslt libraries";
262+ homepage = http://lxml.de;
263+ license = licenses.bsd3;
264+ maintainers = with maintainers; [ sjourdois ];
265+ };
266+};
267+```
268+
269+In this example `lxml` and Nix are able to work out exactly where the relevant
270+files of the dependencies are. This is not always the case.
271+
272+The example below shows bindings to The Fastest Fourier Transform in the West, commonly known as
273+FFTW. On Nix we have separate packages of FFTW for the different types of floats
274+(`"single"`, `"double"`, `"long-double"`). The bindings need all three types,
275+and therefore we add all three as `buildInputs`. The bindings don't expect to
276+find each of them in a different folder, and therefore we have to set `LDFLAGS`
277+and `CFLAGS`.
278+
279+```nix
280+pyfftw = buildPythonPackage rec {
281+ name = "pyfftw-${version}";
282+ version = "0.9.2";
283+
284+ src = pkgs.fetchurl {
285+ url = "https://pypi.python.org/packages/source/p/pyFFTW/pyFFTW-${version}.tar.gz";
286+ sha256 = "f6bbb6afa93085409ab24885a1a3cdb8909f095a142f4d49e346f2bd1b789074";
287+ };
288+
289+ buildInputs = [ pkgs.fftw pkgs.fftwFloat pkgs.fftwLongDouble];
290+
291+ propagatedBuildInputs = with self; [ numpy scipy ];
292+
293+ # Tests cannot import pyfftw. pyfftw works fine though.
294+ doCheck = false;
295+
296+ LDFLAGS="-L${pkgs.fftw}/lib -L${pkgs.fftwFloat}/lib -L${pkgs.fftwLongDouble}/lib"
297+ CFLAGS="-I${pkgs.fftw}/include -I${pkgs.fftwFloat}/include -I${pkgs.fftwLongDouble}/include"
298+ '';
299+
300+ meta = {
301+ description = "A pythonic wrapper around FFTW, the FFT library, presenting a unified interface for all the supported transforms";
302+ homepage = http://hgomersall.github.com/pyFFTW/;
303+ license = with licenses; [ bsd2 bsd3 ];
304+ maintainer = with maintainers; [ fridh ];
305+ };
306+};
307+```
308+Note also the line `doCheck = false;`, we explicitly disabled running the test-suite.
309+
310+
311+#### Develop local package
312+
313+As a Python developer you're likely aware of [development mode](http://pythonhosted.org/setuptools/setuptools.html#development-mode) (`python setup.py develop`);
314+instead of installing the package this command creates a special link to the project code.
315+That way, you can run updated code without having to reinstall after each and every change you make.
316+Development mode is also available on Nix as [explained](http://nixos.org/nixpkgs/manual/#ssec-python-development) in the Nixpkgs manual.
317+Let's see how you can use it.
318+
319+In the previous Nix expression the source was fetched from an url. We can also refer to a local source instead using
320+
321+```nix
322+src = ./path/to/source/tree;
323+```
324+
325+If we create a `shell.nix` file which calls `buildPythonPackage`, and if `src`
326+is a local source, and if the local source has a `setup.py`, then development
327+mode is activated.
328+
329+In the following example we create a simple environment that
330+has a Python 3.5 version of our package in it, as well as its dependencies and
331+other packages we like to have in the environment, all specified with `propagatedBuildInputs`.
332+Indeed, we can just add any package we like to have in our environment to `propagatedBuildInputs`.
333+
334+```nix
335+with import <nixpkgs>;
336+with pkgs.python35Packages;
337+
338+buildPythonPackage rec {
339+ name = "mypackage";
340+ src = ./path/to/package/source;
341+ propagatedBuildInputs = [ pytest numpy pkgs.libsndfile ];
342+};
343+```
344+
345+It is important to note that due to how development mode is implemented on Nix it is not possible to have multiple packages simultaneously in development mode.
346+
347+
348+### Organising your packages
349+
350+So far we discussed how you can use Python on Nix, and how you can develop with
351+it. We've looked at how you write expressions to package Python packages, and we
352+looked at how you can create environments in which specified packages are
353+available.
354+
355+At some point you'll likely have multiple packages which you would
356+like to be able to use in different projects. In order to minimise unnecessary
357+duplication we now look at how you can maintain yourself a repository with your
358+own packages. The important functions here are `import` and `callPackage`.
359+
360+### Including a derivation using `callPackage`
361+
362+Earlier we created a Python environment using `buildEnv`, and included the
363+`toolz` package via a `let` expression.
364+Let's split the package definition from the environment definition.
365+
366+We first create a function that builds `toolz` in `~/path/to/toolz/release.nix`
367+
368+```nix
369+{ pkgs, buildPythonPackage }:
370+
371+buildPythonPackage rec {
372+ name = "toolz-${version}";
373+ version = "0.7.4";
374+
375+ src = pkgs.fetchurl{
376+ url = "https://pypi.python.org/packages/source/t/toolz/toolz-${version}.tar.gz";
377+ sha256 = "43c2c9e5e7a16b6c88ba3088a9bfc82f7db8e13378be7c78d6c14a5f8ed05afd";
378+ };
379+
380+ meta = {
381+ homepage = "http://github.com/pytoolz/toolz/";
382+ description = "List processing tools and functional utilities";
383+ license = licenses.bsd3;
384+ maintainers = with maintainers; [ fridh ];
385+ };
386+};
387+```
388+
389+It takes two arguments, `pkgs` and `buildPythonPackage`.
390+We now call this function using `callPackage` in the definition of our environment
391+
392+```nix
393+with import <nixpkgs> {};
394+
395+( let
396+ toolz = pkgs.callPackage ~/path/to/toolz/release.nix { pkgs=pkgs; buildPythonPackage=pkgs.python35Packages.buildPythonPackage; };
397+ in pkgs.python35.buildEnv.override rec {
398+ extraLibs = [ pkgs.python35Packages.numpy toolz ];
399+}
400+).env
401+```
402+
403+Important to remember is that the Python version for which the package is made
404+depends on the `python` derivation that is passed to `buildPythonPackage`. Nix
405+tries to automatically pass arguments when possible, which is why generally you
406+don't explicitly define which `python` derivation should be used. In the above
407+example we use `buildPythonPackage` that is part of the set `python35Packages`,
408+and in this case the `python35` interpreter is automatically used.
409+
410+
411+
412+## Reference
413+
414+### Interpreters
415+
416+Versions 2.6, 2.7, 3.3, 3.4 and 3.5 of the CPython interpreter are available on
417+Nix and are available as `python26`, `python27`, `python33`, `python34` and
418+`python35`. The PyPy interpreter is also available as `pypy`. Currently, the
419+aliases `python` and `python3` correspond to respectively `python27` and
420+`python35`. The Nix expressions for the interpreters can be found in
421+`pkgs/development/interpreters/python`.
422+
423+
424+#### Missing modules standard library
425+
426+The interpreters `python26` and `python27` do not include modules that
427+require external dependencies. This is done in order to reduce the closure size.
428+The following modules need to be added as `buildInput` explicitly:
429+
430+* `python.modules.bsddb`
431+* `python.modules.curses`
432+* `python.modules.curses_panel`
433+* `python.modules.crypt`
434+* `python.modules.gdbm`
435+* `python.modules.sqlite3`
436+* `python.modules.tkinter`
437+* `python.modules.readline`
438+
439+For convenience `python27Full` and `python26Full` are provided with all
440+modules included.
441+
442+All packages depending on any Python interpreter get appended
443+`out/{python.sitePackages}` to `$PYTHONPATH` if such directory
444+exists.
445+
446+#### Attributes on interpreters packages
447+
448+Each interpreter has the following attributes:
449+
450+- `libPrefix`. Name of the folder in `${python}/lib/` for corresponding interpreter.
451+- `interpreter`. Alias for `${python}/bin/${executable}`.
452+- `buildEnv`. Function to build python interpreter environments with extra packages bundled together. See section *python.buildEnv function* for usage and documentation.
453+- `sitePackages`. Alias for `lib/${libPrefix}/site-packages`.
454+- `executable`. Name of the interpreter executable, ie `python3.4`.
455+
456+### Building packages and applications
457+
458+Python packages (libraries) and applications that use `setuptools` or
459+`distutils` are typically built with respectively the `buildPythonPackage` and
460+`buildPythonApplication` functions.
461+
462+All Python packages reside in `pkgs/top-level/python-packages.nix` and all
463+applications elsewhere. Some packages are also defined in
464+`pkgs/development/python-modules`. It is important that these packages are
465+called in `pkgs/top-level/python-packages.nix` and not elsewhere, to guarantee
466+the right version of the package is built.
467+
468+Based on the packages defined in `pkgs/top-level/python-packages.nix` an
469+attribute set is created for each available Python interpreter. The available
470+sets are
471+
472+* `pkgs.python26Packages`
473+* `pkgs.python27Packages`
474+* `pkgs.python33Packages`
475+* `pkgs.python34Packages`
476+* `pkgs.python35Packages`
477+* `pkgs.pypyPackages`
478+
479+and the aliases
480+
481+* `pkgs.pythonPackages` pointing to `pkgs.python27Packages`
482+* `pkgs.python3Packages` pointing to `pkgs.python35Packages`
483+
484+#### `buildPythonPackage` function
485+
486+The `buildPythonPackage` function is implemented in
487+`pkgs/development/python-modules/generic/default.nix`
488+
489+and can be used as:
490+
491+ twisted = buildPythonPackage {
492+ name = "twisted-8.1.0";
493+
494+ src = pkgs.fetchurl {
495+ url = http://tmrc.mit.edu/mirror/twisted/Twisted/8.1/Twisted-8.1.0.tar.bz2;
496+ sha256 = "0q25zbr4xzknaghha72mq57kh53qw1bf8csgp63pm9sfi72qhirl";
497+ };
498+
499+ propagatedBuildInputs = [ self.ZopeInterface ];
500+
501+ meta = {
502+ homepage = http://twistedmatrix.com/;
503+ description = "Twisted, an event-driven networking engine written in Python";
504+ license = stdenv.lib.licenses.mit; };
505+ };
506+
507+The `buildPythonPackage` mainly does four things:
508+
509+* In the `buildPhase`, it calls `${python.interpreter} setup.py bdist_wheel` to build a wheel binary zipfile.
510+* In the `installPhase`, it installs the wheel file using `pip install *.whl`.
511+* In the `postFixup` phase, the `wrapPythonPrograms` bash function is called to wrap all programs in the `$out/bin/*` directory to include `$PYTHONPATH` and `$PATH` environment variables.
512+* In the `installCheck` phase, `${python.interpreter} setup.py test` is ran.
513+
514+As in Perl, dependencies on other Python packages can be specified in the
515+`buildInputs` and `propagatedBuildInputs` attributes. If something is
516+exclusively a build-time dependency, use `buildInputs`; if it’s (also) a runtime
517+dependency, use `propagatedBuildInputs`.
518+
519+By default tests are run because `doCheck = true`. Test dependencies, like
520+e.g. the test runner, should be added to `buildInputs`.
521+
522+By default `meta.platforms` is set to the same value
523+as the interpreter unless overriden otherwise.
524+
525+##### `buildPythonPackage` parameters
526+
527+All parameters from `mkDerivation` function are still supported.
528+
529+* `namePrefix`: Prepended text to `${name}` parameter. Defaults to `"python3.3-"` for Python 3.3, etc. Set it to `""` if you're packaging an application or a command line tool.
530+* `disabled`: If `true`, package is not build for particular python interpreter version. Grep around `pkgs/top-level/python-packages.nix` for examples.
531+* `setupPyBuildFlags`: List of flags passed to `setup.py build_ext` command.
532+* `pythonPath`: List of packages to be added into `$PYTHONPATH`. Packages in `pythonPath` are not propagated (contrary to `propagatedBuildInputs`).
533+* `preShellHook`: Hook to execute commands before `shellHook`.
534+* `postShellHook`: Hook to execute commands after `shellHook`.
535+* `makeWrapperArgs`: A list of strings. Arguments to be passed to `makeWrapper`, which wraps generated binaries. By default, the arguments to `makeWrapper` set `PATH` and `PYTHONPATH` environment variables before calling the binary. Additional arguments here can allow a developer to set environment variables which will be available when the binary is run. For example, `makeWrapperArgs = ["--set FOO BAR" "--set BAZ QUX"]`.
536+* `installFlags`: A list of strings. Arguments to be passed to `pip install`. To pass options to `python setup.py install`, use `--install-option`. E.g., `installFlags=["--install-option='--cpp_implementation'"].
537+
538+#### `buildPythonApplication` function
539+
540+The `buildPythonApplication` function is practically the same as `buildPythonPackage`.
541+The difference is that `buildPythonPackage` by default prefixes the names of the packages with the version of the interpreter.
542+Because with an application we're not interested in multiple version the prefix is dropped.
543+
544+#### python.buildEnv function
545+
546+Python environments can be created using the low-level `pkgs.buildEnv` function.
547+This example shows how to create an environment that has the Pyramid Web Framework.
548+Saving the following as `default.nix`
549+
550+ with import {};
551+
552+ python.buildEnv.override {
553+ extraLibs = [ pkgs.pythonPackages.pyramid ];
554+ ignoreCollisions = true;
555+ }
556+
557+and running `nix-build` will create
558+
559+ /nix/store/cf1xhjwzmdki7fasgr4kz6di72ykicl5-python-2.7.8-env
560+
561+with wrapped binaries in `bin/`.
562+
563+You can also use the `env` attribute to create local environments with needed
564+packages installed. This is somewhat comparable to `virtualenv`. For example,
565+running `nix-shell` with the following `shell.nix`
566+
567+ with import {};
568+
569+ (python3.buildEnv.override {
570+ extraLibs = with python3Packages; [ numpy requests ];
571+ }).env
572+
573+will drop you into a shell where Python will have the
574+specified packages in its path.
575+
576+
577+##### `python.buildEnv` arguments
578+
579+* `extraLibs`: List of packages installed inside the environment.
580+* `postBuild`: Shell command executed after the build of environment.
581+* `ignoreCollisions`: Ignore file collisions inside the environment (default is `false`).
582+
583+### Development mode
584+
585+Development or editable mode is supported. To develop Python packages
586+`buildPythonPackage` has additional logic inside `shellPhase` to run `pip
587+install -e . --prefix $TMPDIR/`for the package.
588+
589+Warning: `shellPhase` is executed only if `setup.py` exists.
590+
591+Given a `default.nix`:
592+
593+ with import {};
594+
595+ buildPythonPackage { name = "myproject";
596+
597+ buildInputs = with pkgs.pythonPackages; [ pyramid ];
598+
599+ src = ./.; }
600+
601+Running `nix-shell` with no arguments should give you
602+the environment in which the package would be build with
603+`nix-build`.
604+
605+Shortcut to setup environments with C headers/libraries and python packages:
606+
607+ $ nix-shell -p pythonPackages.pyramid zlib libjpeg git
608+
609+Note: There is a boolean value `lib.inNixShell` set to `true` if nix-shell is invoked.
610+
611+### Tools
612+
613+Packages inside nixpkgs are written by hand. However many tools exist in
614+community to help save time. No tool is preferred at the moment.
615+
616+- [python2nix](https://github.com/proger/python2nix) by Vladimir Kirillov
617+- [pypi2nix](https://github.com/garbas/pypi2nix) by Rok Garbas
618+- [pypi2nix](https://github.com/offlinehacker/pypi2nix) by Jaka Hudoklin
619+
620+## FAQ
621+
622+### How to solve circular dependencies?
623+
624+Consider the packages `A` and `B` that depend on each other. When packaging `B`,
625+a solution is to override package `A` not to depend on `B` as an input. The same
626+should also be done when packaging `A`.
627+
628+### `install_data` / `data_files` problems
629+
630+If you get the following error:
631+
632+ could not create '/nix/store/6l1bvljpy8gazlsw2aw9skwwp4pmvyxw-python-2.7.8/etc':
633+ Permission denied
634+
635+This is a [known bug](https://bitbucket.org/pypa/setuptools/issue/130/install_data-doesnt-respect-prefix) in setuptools.
636+Setuptools `install_data` does not respect `--prefix`. An example of such package using the feature is `pkgs/tools/X11/xpra/default.nix`.
637+As workaround install it as an extra `preInstall` step:
638+
639+ ${python.interpreter} setup.py install_data --install-dir=$out --root=$out
640+ sed -i '/ = data\_files/d' setup.py
641+
642+
643+### Rationale of non-existent global site-packages
644+
645+On most operating systems a global `site-packages` is maintained. This however
646+becomes problematic if you want to run multiple Python versions or have multiple
647+versions of certain libraries for your projects. Generally, you would solve such
648+issues by creating virtual environments using `virtualenv`.
649+
650+On Nix each package has an isolated dependency tree which, in the case of
651+Python, guarantees the right versions of the interpreter and libraries or
652+packages are available. There is therefore no need to maintain a global `site-packages`.
653+
654+If you want to create a Python environment for development, then the recommended
655+method is to use `nix-shell`, either with or without the `python.buildEnv`
656+function.
657+
658+
659+## Contributing
660+
661+### Contributing guidelines
662+
663+Following rules are desired to be respected:
664+
665+* Make sure package builds for all python interpreters. Use `disabled` argument to `buildPythonPackage` to set unsupported interpreters.
666+* If tests need to be disabled for a package, make sure you leave a comment about reasoning.
667+* Packages in `pkgs/top-level/python-packages.nix` are sorted quasi-alphabetically to avoid merge conflicts.
668+* Python libraries are supposed to be in `python-packages.nix` and packaged with `buildPythonPackage`. Python applications live outside of `python-packages.nix` and are packaged with `buildPythonApplication`.