git.iko.soy/typeable/nixpkgs
on master help sign in
doc: fix code highlighting, use valid Nix syntax

Fix code syntax highlighting by specifying language in every code block and adding some context to Nix code blocks to make them valid expressions. Use the same markup style for all code blocks. Reformat some code blocks. fixes #23535

commit34afc31c49b067879c3c49b29c67e2a1bc806685 authorAlexey Muranov <alexeymuranov@users.noreply.github.com> dateSun 5 Mar 2017, 17:16 (10 years ago) parent97c9ed0
1 file changed+170−139
doc/languages-frameworks/python.md+170−139
doc/languages-frameworks/python.md+170−139
@@ -74,7 +74,6 @@ can do is write a simple Nix expression which sets up an environment for you,
7474requiring you only to type `nix-shell`. Say we want to have Python 3.5, `numpy`
7575and `toolz`, like before, in an environment. With a `shell.nix` file
7676containing
77−
7877```nix
7978with import <nixpkgs> {};
8079
@@ -101,22 +100,25 @@ On Nix all packages are built by functions. The main function in Nix for buildin
101100Let'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
102101
103102```nix
104−toolz = buildPythonPackage rec{
105− name = "toolz-${version}";
106− version = "0.7.4";
103+{ # ...
107104
108− src = pkgs.fetchurl{
109− url = "mirror://pypi/t/toolz/toolz-${version}.tar.gz";
110− sha256 = "43c2c9e5e7a16b6c88ba3088a9bfc82f7db8e13378be7c78d6c14a5f8ed05afd";
111− };
105+ toolz = buildPythonPackage rec {
106+ name = "toolz-${version}";
107+ version = "0.7.4";
112108
113− meta = {
114− homepage = "http://github.com/pytoolz/toolz/";
115− description = "List processing tools and functional utilities";
116− license = licenses.bsd3;
117− maintainers = with maintainers; [ fridh ];
109+ src = pkgs.fetchurl {
110+ url = "mirror://pypi/t/toolz/toolz-${version}.tar.gz";
111+ sha256 = "43c2c9e5e7a16b6c88ba3088a9bfc82f7db8e13378be7c78d6c14a5f8ed05afd";
112+ };
113+
114+ meta = {
115+ homepage = "http://github.com/pytoolz/toolz/";
116+ description = "List processing tools and functional utilities";
117+ license = licenses.bsd3;
118+ maintainers = with maintainers; [ fridh ];
119+ };
118120 };
119−};
121+}
120122```
121123
122124What happens here? The function `buildPythonPackage` is called and as argument
@@ -143,7 +145,7 @@ pkgs.python35Packages.buildPythonPackage rec {
143145 name = "toolz-${version}";
144146 version = "0.8.0";
145147
146− src = pkgs.fetchurl{
148+ src = pkgs.fetchurl {
147149 url = "mirror://pypi/t/toolz/toolz-${version}.tar.gz";
148150 sha256 = "e8451af61face57b7c5d09e71c0d27b8005f001ead56e9fdf470417e5cc6d479";
149151 };
@@ -174,7 +176,7 @@ with import <nixpkgs> {};
174176 name = "toolz-${version}";
175177 version = "0.8.0";
176178
177− src = pkgs.fetchurl{
179+ src = pkgs.fetchurl {
178180 url = "mirror://pypi/t/toolz/toolz-${version}.tar.gz";
179181 sha256 = "e8451af61face57b7c5d09e71c0d27b8005f001ead56e9fdf470417e5cc6d479";
180182 };
@@ -215,25 +217,28 @@ The following example shows which arguments are given to `buildPythonPackage` in
215217order to build [`datashape`](https://github.com/blaze/datashape).
216218
217219```nix
218−datashape = buildPythonPackage rec {
219− name = "datashape-${version}";
220− version = "0.4.7";
220+{ # ...
221221
222− src = pkgs.fetchurl {
223− url = "mirror://pypi/D/DataShape/${name}.tar.gz";
224− sha256 = "14b2ef766d4c9652ab813182e866f493475e65e558bed0822e38bf07bba1a278";
225− };
222+ datashape = buildPythonPackage rec {
223+ name = "datashape-${version}";
224+ version = "0.4.7";
226225
227− buildInputs = with self; [ pytest ];
228− propagatedBuildInputs = with self; [ numpy multipledispatch dateutil ];
226+ src = pkgs.fetchurl {
227+ url = "mirror://pypi/D/DataShape/${name}.tar.gz";
228+ sha256 = "14b2ef766d4c9652ab813182e866f493475e65e558bed0822e38bf07bba1a278";
229+ };
229230
230− meta = {
231− homepage = https://github.com/ContinuumIO/datashape;
232− description = "A data description language";
233− license = licenses.bsd2;
234− maintainers = with maintainers; [ fridh ];
231+ buildInputs = with self; [ pytest ];
232+ propagatedBuildInputs = with self; [ numpy multipledispatch dateutil ];
233+
234+ meta = {
235+ homepage = https://github.com/ContinuumIO/datashape;
236+ description = "A data description language";
237+ license = licenses.bsd2;
238+ maintainers = with maintainers; [ fridh ];
239+ };
235240 };
236−};
241+}
237242```
238243
239244We can see several runtime dependencies, `numpy`, `multipledispatch`, and
@@ -247,23 +252,26 @@ Python bindings to `libxml2` and `libxslt`. These libraries are only required
247252when building the bindings and are therefore added as `buildInputs`.
248253
249254```nix
250−lxml = buildPythonPackage rec {
251− name = "lxml-3.4.4";
255+{ # ...
252256
253− src = pkgs.fetchurl {
254− url = "mirror://pypi/l/lxml/${name}.tar.gz";
255− sha256 = "16a0fa97hym9ysdk3rmqz32xdjqmy4w34ld3rm3jf5viqjx65lxk";
256− };
257+ lxml = buildPythonPackage rec {
258+ name = "lxml-3.4.4";
257259
258− buildInputs = with self; [ pkgs.libxml2 pkgs.libxslt ];
260+ src = pkgs.fetchurl {
261+ url = "mirror://pypi/l/lxml/${name}.tar.gz";
262+ sha256 = "16a0fa97hym9ysdk3rmqz32xdjqmy4w34ld3rm3jf5viqjx65lxk";
263+ };
259264
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+ buildInputs = with self; [ pkgs.libxml2 pkgs.libxslt ];
266+
267+ meta = {
268+ description = "Pythonic binding for the libxml2 and libxslt libraries";
269+ homepage = http://lxml.de;
270+ license = licenses.bsd3;
271+ maintainers = with maintainers; [ sjourdois ];
272+ };
265273 };
266−};
274+}
267275```
268276
269277In this example `lxml` and Nix are able to work out exactly where the relevant
@@ -277,33 +285,37 @@ find each of them in a different folder, and therefore we have to set `LDFLAGS`
277285and `CFLAGS`.
278286
279287```nix
280−pyfftw = buildPythonPackage rec {
281− name = "pyfftw-${version}";
282− version = "0.9.2";
288+{ # ...
283289
284− src = pkgs.fetchurl {
285− url = "mirror://pypi/p/pyFFTW/pyFFTW-${version}.tar.gz";
286− sha256 = "f6bbb6afa93085409ab24885a1a3cdb8909f095a142f4d49e346f2bd1b789074";
287− };
290+ pyfftw = buildPythonPackage rec {
291+ name = "pyfftw-${version}";
292+ version = "0.9.2";
288293
289− buildInputs = [ pkgs.fftw pkgs.fftwFloat pkgs.fftwLongDouble];
294+ src = pkgs.fetchurl {
295+ url = "mirror://pypi/p/pyFFTW/pyFFTW-${version}.tar.gz";
296+ sha256 = "f6bbb6afa93085409ab24885a1a3cdb8909f095a142f4d49e346f2bd1b789074";
297+ };
290298
291− propagatedBuildInputs = with self; [ numpy scipy ];
299+ buildInputs = [ pkgs.fftw pkgs.fftwFloat pkgs.fftwLongDouble];
292300
293− # Tests cannot import pyfftw. pyfftw works fine though.
294− doCheck = false;
301+ propagatedBuildInputs = with self; [ numpy scipy ];
295302
296− LDFLAGS="-L${pkgs.fftw.dev}/lib -L${pkgs.fftwFloat.out}/lib -L${pkgs.fftwLongDouble.out}/lib"
297− CFLAGS="-I${pkgs.fftw.dev}/include -I${pkgs.fftwFloat.dev}/include -I${pkgs.fftwLongDouble.dev}/include"
298− '';
303+ # Tests cannot import pyfftw. pyfftw works fine though.
304+ doCheck = false;
299305
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 ];
306+ preConfigure = ''
307+ export LDFLAGS="-L${pkgs.fftw.dev}/lib -L${pkgs.fftwFloat.out}/lib -L${pkgs.fftwLongDouble.out}/lib"
308+ export CFLAGS="-I${pkgs.fftw.dev}/include -I${pkgs.fftwFloat.dev}/include -I${pkgs.fftwLongDouble.dev}/include"
309+ '';
310+
311+ meta = {
312+ description = "A pythonic wrapper around FFTW, the FFT library, presenting a unified interface for all the supported transforms";
313+ homepage = http://hgomersall.github.com/pyFFTW/;
314+ license = with licenses; [ bsd2 bsd3 ];
315+ maintainer = with maintainers; [ fridh ];
316+ };
305317 };
306−};
318+}
307319```
308320Note also the line `doCheck = false;`, we explicitly disabled running the test-suite.
309321
@@ -316,10 +328,7 @@ That way, you can run updated code without having to reinstall after each and ev
316328Development mode is also available. Let's see how you can use it.
317329
318330In the previous Nix expression the source was fetched from an url. We can also refer to a local source instead using
319−
320−```nix
321−src = ./path/to/source/tree;
322−```
331+`src = ./path/to/source/tree;`
323332
324333If we create a `shell.nix` file which calls `buildPythonPackage`, and if `src`
325334is a local source, and if the local source has a `setup.py`, then development
@@ -338,7 +347,7 @@ buildPythonPackage rec {
338347 name = "mypackage";
339348 src = ./path/to/package/source;
340349 propagatedBuildInputs = [ pytest numpy pkgs.libsndfile ];
341−};
350+}
342351```
343352
344353It 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.
@@ -371,7 +380,7 @@ buildPythonPackage rec {
371380 name = "toolz-${version}";
372381 version = "0.7.4";
373382
374− src = pkgs.fetchurl{
383+ src = pkgs.fetchurl {
375384 url = "mirror://pypi/t/toolz/toolz-${version}.tar.gz";
376385 sha256 = "43c2c9e5e7a16b6c88ba3088a9bfc82f7db8e13378be7c78d6c14a5f8ed05afd";
377386 };
@@ -382,7 +391,7 @@ buildPythonPackage rec {
382391 license = licenses.bsd3;
383392 maintainers = with maintainers; [ fridh ];
384393 };
385−};
394+}
386395```
387396
388397It takes two arguments, `pkgs` and `buildPythonPackage`.
@@ -392,7 +401,10 @@ We now call this function using `callPackage` in the definition of our environme
392401with import <nixpkgs> {};
393402
394403( let
395− toolz = pkgs.callPackage ~/path/to/toolz/release.nix { pkgs=pkgs; buildPythonPackage=pkgs.python35Packages.buildPythonPackage; };
404+ toolz = pkgs.callPackage /path/to/toolz/release.nix {
405+ pkgs = pkgs;
406+ buildPythonPackage = pkgs.python35Packages.buildPythonPackage;
407+ };
396408 in pkgs.python35.withPackages (ps: [ ps.numpy toolz ])
397409).env
398410```
@@ -474,22 +486,27 @@ The `buildPythonPackage` function is implemented in
474486`pkgs/development/interpreters/python/build-python-package.nix`
475487
476488The following is an example:
489+```nix
490+{ # ...
477491
478− twisted = buildPythonPackage {
479− name = "twisted-8.1.0";
492+ twisted = buildPythonPackage {
493+ name = "twisted-8.1.0";
480494
481− src = pkgs.fetchurl {
482− url = http://tmrc.mit.edu/mirror/twisted/Twisted/8.1/Twisted-8.1.0.tar.bz2;
483− sha256 = "0q25zbr4xzknaghha72mq57kh53qw1bf8csgp63pm9sfi72qhirl";
484− };
495+ src = pkgs.fetchurl {
496+ url = http://tmrc.mit.edu/mirror/twisted/Twisted/8.1/Twisted-8.1.0.tar.bz2;
497+ sha256 = "0q25zbr4xzknaghha72mq57kh53qw1bf8csgp63pm9sfi72qhirl";
498+ };
485499
486− propagatedBuildInputs = [ self.ZopeInterface ];
500+ propagatedBuildInputs = [ self.ZopeInterface ];
487501
488− meta = {
489− homepage = http://twistedmatrix.com/;
490− description = "Twisted, an event-driven networking engine written in Python";
491− license = stdenv.lib.licenses.mit; };
492− };
502+ meta = {
503+ homepage = http://twistedmatrix.com/;
504+ description = "Twisted, an event-driven networking engine written in Python";
505+ license = stdenv.lib.licenses.mit;
506+ };
507+ };
508+}
509+```
493510
494511The `buildPythonPackage` mainly does four things:
495512
@@ -539,29 +556,32 @@ Because with an application we're not interested in multiple version the prefix
539556Python environments can be created using the low-level `pkgs.buildEnv` function.
540557This example shows how to create an environment that has the Pyramid Web Framework.
541558Saving the following as `default.nix`
559+```nix
560+with import <nixpkgs> {};
542561
543− with import <nixpkgs> {};
544−
545− python.buildEnv.override {
546− extraLibs = [ pkgs.pythonPackages.pyramid ];
547− ignoreCollisions = true;
548− }
562+python.buildEnv.override {
563+ extraLibs = [ pkgs.pythonPackages.pyramid ];
564+ ignoreCollisions = true;
565+}
566+```
549567
550568and running `nix-build` will create
551−
552− /nix/store/cf1xhjwzmdki7fasgr4kz6di72ykicl5-python-2.7.8-env
569+```
570+/nix/store/cf1xhjwzmdki7fasgr4kz6di72ykicl5-python-2.7.8-env
571+```
553572
554573with wrapped binaries in `bin/`.
555574
556575You can also use the `env` attribute to create local environments with needed
557576packages installed. This is somewhat comparable to `virtualenv`. For example,
558577running `nix-shell` with the following `shell.nix`
578+```nix
579+with import <nixpkgs> {};
559580
560− with import <nixpkgs> {};
561−
562− (python3.buildEnv.override {
563− extraLibs = with python3Packages; [ numpy requests2 ];
564− }).env
581+(python3.buildEnv.override {
582+ extraLibs = with python3Packages; [ numpy requests2 ];
583+}).env
584+```
565585
566586will drop you into a shell where Python will have the
567587specified packages in its path.
@@ -579,27 +599,30 @@ The `python.withPackages` function provides a simpler interface to the `python.b
579599It takes a function as an argument that is passed the set of python packages and returns the list
580600of the packages to be included in the environment. Using the `withPackages` function, the previous
581601example for the Pyramid Web Framework environment can be written like this:
602+```nix
603+with import <nixpkgs> {};
582604
583− with import <nixpkgs> {};
584−
585− python.withPackages (ps: [ps.pyramid])
605+python.withPackages (ps: [ps.pyramid])
606+```
586607
587608`withPackages` passes the correct package set for the specific interpreter version as an
588609argument to the function. In the above example, `ps` equals `pythonPackages`.
589610But you can also easily switch to using python3:
611+```nix
612+with import <nixpkgs> {};
590613
591− with import <nixpkgs> {};
592−
593− python3.withPackages (ps: [ps.pyramid])
614+python3.withPackages (ps: [ps.pyramid])
615+```
594616
595617Now, `ps` is set to `python3Packages`, matching the version of the interpreter.
596618
597619As `python.withPackages` simply uses `python.buildEnv` under the hood, it also supports the `env`
598620attribute. The `shell.nix` file from the previous section can thus be also written like this:
621+```nix
622+with import <nixpkgs> {};
599623
600− with import <nixpkgs> {};
601−
602− (python33.withPackages (ps: [ps.numpy ps.requests2])).env
624+(python33.withPackages (ps: [ps.numpy ps.requests2])).env
625+```
603626
604627In contrast to `python.buildEnv`, `python.withPackages` does not support the more advanced options
605628such as `ignoreCollisions = true` or `postBuild`. If you need them, you have to use `python.buildEnv`.
@@ -613,22 +636,24 @@ install -e . --prefix $TMPDIR/`for the package.
613636Warning: `shellPhase` is executed only if `setup.py` exists.
614637
615638Given a `default.nix`:
639+```nix
640+with import <nixpkgs> {};
616641
617− with import <nixpkgs> {};
618−
619− buildPythonPackage { name = "myproject";
642+buildPythonPackage { name = "myproject";
620643
621− buildInputs = with pkgs.pythonPackages; [ pyramid ];
644+buildInputs = with pkgs.pythonPackages; [ pyramid ];
622645
623− src = ./.; }
646+src = ./.; }
647+```
624648
625649Running `nix-shell` with no arguments should give you
626650the environment in which the package would be built with
627651`nix-build`.
628652
629653Shortcut to setup environments with C headers/libraries and python packages:
630−
631− $ nix-shell -p pythonPackages.pyramid zlib libjpeg git
654+```shell
655+nix-shell -p pythonPackages.pyramid zlib libjpeg git
656+```
632657
633658Note: There is a boolean value `lib.inNixShell` set to `true` if nix-shell is invoked.
634659
@@ -676,7 +701,7 @@ with import <nixpkgs> {};
676701pkgs.python35.withPackages (ps: with ps; [ numpy ipython ])
677702```
678703and install it in your profile with
679−```
704+```shell
680705nix-env -if build.nix
681706```
682707Now you can use the Python interpreter, as well as the extra packages that you added to the environment.
@@ -684,15 +709,19 @@ Now you can use the Python interpreter, as well as the extra packages that you a
684709#### Environment defined in `~/.nixpkgs/config.nix`
685710
686711If you prefer to, you could also add the environment as a package override to the Nixpkgs set.
687−```
712+```nix
713+{ # ...
714+
688715 packageOverrides = pkgs: with pkgs; {
689716 myEnv = python35.withPackages (ps: with ps; [ numpy ipython ]);
690717 };
718+}
691719```
692720and install it in your profile with
693−```
721+```shell
694722nix-env -iA nixpkgs.myEnv
695723```
724+
696725We're installing using the attribute path and assume the channels is named `nixpkgs`.
697726Note that I'm using the attribute path here.
698727
@@ -701,9 +730,12 @@ Note that I'm using the attribute path here.
701730For the sake of completeness, here's another example how to install the environment system-wide.
702731
703732```nix
704−environment.systemPackages = with pkgs; [
705− (python35.withPackages(ps: with ps; [ numpy ipython ]))
706−];
733+{ # ...
734+
735+ environment.systemPackages = with pkgs; [
736+ (python35.withPackages(ps: with ps; [ numpy ipython ]))
737+ ];
738+}
707739```
708740
709741### How to solve circular dependencies?
@@ -740,19 +772,18 @@ All packages in the Python package set will now use the updated `scipy` version.
740772```nix
741773with import <nixpkgs> {};
742774
743−(
744−let
745− packageOverrides = self: super: {
746− scipy = super.scipy_0_17;
747− };
748−in (pkgs.python35.override {inherit packageOverrides;}).withPackages (ps: [ps.blaze])
775+( let
776+ packageOverrides = self: super: {
777+ scipy = super.scipy_0_17;
778+ };
779+ in (pkgs.python35.override {inherit packageOverrides;}).withPackages (ps: [ps.blaze])
749780).env
750781```
751782The requested package `blaze` depends on `pandas` which itself depends on `scipy`.
752783
753784If you want the whole of Nixpkgs to use your modifications, then you can use `overlays`
754785as explained in this manual. In the following example we build a `inkscape` using a different version of `numpy`.
755−```
786+```nix
756787let
757788 pkgs = import <nixpkgs> {};
758789 newpkgs = import pkgs.path { overlays = [ (pkgsself: pkgssuper: {
@@ -775,32 +806,32 @@ This is because files are included that depend on items in the Nix store which h
775806The command `bdist_wheel` takes into account `SOURCE_DATE_EPOCH`, and `nix-shell` sets this to 1. By setting it to a value corresponding to 1980 or later, or by unsetting it, it is possible to build wheels.
776807
777808Use 1980 as timestamp:
778−```
809+```shell
779810nix-shell --run "SOURCE_DATE_EPOCH=315532800 python3 setup.py bdist_wheel"
780811```
781812or the current time:
782−```
813+```shell
783814nix-shell --run "SOURCE_DATE_EPOCH=$(date +%s) python3 setup.py bdist_wheel"
784815```
785816or unset:
786−```
817+```shell
787818nix-shell --run "unset SOURCE_DATE_EPOCH; python3 setup.py bdist_wheel"
788819```
789820
790821### `install_data` / `data_files` problems
791822
792823If you get the following error:
793−
794− could not create '/nix/store/6l1bvljpy8gazlsw2aw9skwwp4pmvyxw-python-2.7.8/etc':
795− Permission denied
796−
797−This is a [known bug](https://github.com/pypa/setuptools/issues/130) in setuptools.
824+```
825+could not create '/nix/store/6l1bvljpy8gazlsw2aw9skwwp4pmvyxw-python-2.7.8/etc':
826+Permission denied
827+```
828+This is a [known bug](https://github.com/pypa/setuptools/issues/130) in `setuptools`.
798829Setuptools `install_data` does not respect `--prefix`. An example of such package using the feature is `pkgs/tools/X11/xpra/default.nix`.
799830As workaround install it as an extra `preInstall` step:
800−
801− ${python.interpreter} setup.py install_data --install-dir=$out --root=$out
802− sed -i '/ = data\_files/d' setup.py
803−
831+```shell
832+${python.interpreter} setup.py install_data --install-dir=$out --root=$out
833+sed -i '/ = data\_files/d' setup.py
834+```
804835
805836### Rationale of non-existent global site-packages
806837
@@ -824,7 +855,7 @@ and install python modules through `pip` the traditional way.
824855
825856Create this `default.nix` file, together with a `requirements.txt` and simply execute `nix-shell`.
826857
827−```
858+```nix
828859with import <nixpkgs> {};
829860with pkgs.python27Packages;
830861