summary refs log tree commit diff
path: root/doc/language-support.xml
blob: 1e1bdf75eda5a29801231b83183f2fac8f402034 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
<chapter xmlns="http://docbook.org/ns/docbook"
         xmlns:xlink="http://www.w3.org/1999/xlink"
         xml:id="chap-language-support">

<title>Support for specific programming languages</title>

<para>The <link linkend="chap-stdenv">standard build
environment</link> makes it easy to build typical Autotools-based
packages with very little code.  Any other kind of package can be
accomodated by overriding the appropriate phases of
<literal>stdenv</literal>.  However, there are specialised functions
in Nixpkgs to easily build packages for other programming languages,
such as Perl or Haskell.  These are described in this chapter.</para>


<section xml:id="ssec-language-perl"><title>Perl</title>

<para>Nixpkgs provides a function <varname>buildPerlPackage</varname>,
a generic package builder function for any Perl package that has a
standard <varname>Makefile.PL</varname>.  It’s implemented in <link
xlink:href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/perl-modules/generic"><filename>pkgs/development/perl-modules/generic</filename></link>.</para>

<para>Perl packages from CPAN are defined in <link
xlink:href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/top-level/perl-packages.nix"><filename>pkgs/top-level/perl-packages.nix</filename></link>,
rather than <filename>pkgs/all-packages.nix</filename>.  Most Perl
packages are so straight-forward to build that they are defined here
directly, rather than having a separate function for each package
called from <filename>perl-packages.nix</filename>.  However, more
complicated packages should be put in a separate file, typically in
<filename>pkgs/development/perl-modules</filename>.  Here is an
example of the former:

<programlisting>
ClassC3 = buildPerlPackage rec {
  name = "Class-C3-0.21";
  src = fetchurl {
    url = "mirror://cpan/authors/id/F/FL/FLORA/${name}.tar.gz";
    sha256 = "1bl8z095y4js66pwxnm7s853pi9czala4sqc743fdlnk27kq94gz";
  };
};
</programlisting>

Note the use of <literal>mirror://cpan/</literal>, and the
<literal>${name}</literal> in the URL definition to ensure that the
name attribute is consistent with the source that we’re actually
downloading.  Perl packages are made available in
<filename>all-packages.nix</filename> through the variable
<varname>perlPackages</varname>.  For instance, if you have a package
that needs <varname>ClassC3</varname>, you would typically write

<programlisting>
foo = import ../path/to/foo.nix {
  inherit stdenv fetchurl ...;
  inherit (perlPackages) ClassC3;
};
</programlisting>

in <filename>all-packages.nix</filename>.  You can test building a
Perl package as follows:

<screen>
$ nix-build -A perlPackages.ClassC3
</screen>

<varname>buildPerlPackage</varname> adds <literal>perl-</literal> to
the start of the name attribute, so the package above is actually
called <literal>perl-Class-C3-0.21</literal>.  So to install it, you
can say:

<screen>
$ nix-env -i perl-Class-C3
</screen>

(Of course you can also install using the attribute name:
<literal>nix-env -i -A perlPackages.ClassC3</literal>.)</para>

<para>So what does <varname>buildPerlPackage</varname> do?  It does
the following:

<orderedlist>

  <listitem><para>In the configure phase, it calls <literal>perl
  Makefile.PL</literal> to generate a Makefile.  You can set the
  variable <varname>makeMakerFlags</varname> to pass flags to
  <filename>Makefile.PL</filename></para></listitem>

  <listitem><para>It adds the contents of the <envar>PERL5LIB</envar>
  environment variable to <literal>#! .../bin/perl</literal> line of
  Perl scripts as <literal>-I<replaceable>dir</replaceable></literal>
  flags.  This ensures that a script can find its
  dependencies.</para></listitem>

  <listitem><para>In the fixup phase, it writes the propagated build
  inputs (<varname>propagatedBuildInputs</varname>) to the file
  <filename>$out/nix-support/propagated-user-env-packages</filename>.
  <command>nix-env</command> recursively installs all packages listed
  in this file when you install a package that has it.  This ensures
  that a Perl package can find its dependencies.</para></listitem>

</orderedlist>

</para>

<para><varname>buildPerlPackage</varname> is built on top of
<varname>stdenv</varname>, so everything can be customised in the
usual way.  For instance, the <literal>BerkeleyDB</literal> module has
a <varname>preConfigure</varname> hook to generate a configuration
file used by <filename>Makefile.PL</filename>:

<programlisting>
{ buildPerlPackage, fetchurl, db }:

buildPerlPackage rec {
  name = "BerkeleyDB-0.36";

  src = fetchurl {
    url = "mirror://cpan/authors/id/P/PM/PMQS/${name}.tar.gz";
    sha256 = "07xf50riarb60l1h6m2dqmql8q5dij619712fsgw7ach04d8g3z1";
  };

  preConfigure = ''
    echo "LIB = ${db}/lib" > config.in
    echo "INCLUDE = ${db}/include" >> config.in
  '';
}
</programlisting>

</para>

<para>Dependencies on other Perl packages can be specified in the
<varname>buildInputs</varname> and
<varname>propagatedBuildInputs</varname> attributes.  If something is
exclusively a build-time dependency, use
<varname>buildInputs</varname>; if it’s (also) a runtime dependency,
use <varname>propagatedBuildInputs</varname>.  For instance, this
builds a Perl module that has runtime dependencies on a bunch of other
modules:

<programlisting>
ClassC3Componentised = buildPerlPackage rec {
  name = "Class-C3-Componentised-1.0004";
  src = fetchurl {
    url = "mirror://cpan/authors/id/A/AS/ASH/${name}.tar.gz";
    sha256 = "0xql73jkcdbq4q9m0b0rnca6nrlvf5hyzy8is0crdk65bynvs8q1";
  };
  propagatedBuildInputs = [
    ClassC3 ClassInspector TestException MROCompat
  ];
};
</programlisting>

</para>

<section><title>Generation from CPAN</title>

<para>Nix expressions for Perl packages can be generated (almost)
automatically from CPAN.  This is done by the program
<command>nix-generate-from-cpan</command>, which can be installed
as follows:</para>

<screen>
$ nix-env -i nix-generate-from-cpan
</screen>

<para>This program takes a Perl module name, looks it up on CPAN,
fetches and unpacks the corresponding package, and prints a Nix
expression on standard output.  For example:

<screen>
$ nix-generate-from-cpan XML::Simple
  XMLSimple = buildPerlPackage {
    name = "XML-Simple-2.20";
    src = fetchurl {
      url = mirror://cpan/authors/id/G/GR/GRANTM/XML-Simple-2.20.tar.gz;
      sha256 = "5cff13d0802792da1eb45895ce1be461903d98ec97c9c953bc8406af7294434a";
    };
    propagatedBuildInputs = [ XMLNamespaceSupport XMLSAX XMLSAXExpat ];
    meta = {
      description = "Easily read/write XML (esp config files)";
      license = "perl";
    };
  };
</screen>

The output can be pasted into
<filename>pkgs/top-level/perl-packages.nix</filename> or wherever else
you need it.</para>

</section>

</section>


<section xml:id="python"><title>Python</title>

<para>
  Currently supported interpreters are <varname>python26</varname>, <varname>python27</varname>,
  <varname>python32</varname>, <varname>python33</varname>, <varname>python34</varname>
  and <varname>pypy</varname>.
</para>

<para>
  <varname>python</varname> is an alias of <varname>python27</varname> and <varname>python3</varname> is an alias of <varname>python34</varname>.
</para>

<para>
  <varname>python26</varname> and <varname>python27</varname> do not include modules that require
  external dependencies (to reduce dependency bloat). Following modules need to be added as
  <varname>buildInput</varname> explicitly:
</para>

<itemizedlist>
  <listitem><para><varname>python.modules.bsddb</varname></para></listitem>
  <listitem><para><varname>python.modules.curses</varname></para></listitem>
  <listitem><para><varname>python.modules.curses_panel</varname></para></listitem>
  <listitem><para><varname>python.modules.crypt</varname></para></listitem>
  <listitem><para><varname>python.modules.gdbm</varname></para></listitem>
  <listitem><para><varname>python.modules.sqlite3</varname></para></listitem>
  <listitem><para><varname>python.modules.tkinter</varname></para></listitem>
  <listitem><para><varname>python.modules.readline</varname></para></listitem>
</itemizedlist>

<para>For convenience <varname>python27Full</varname> and <varname>python26Full</varname>
are provided with all modules included.</para>

<para>
  Python packages that
  use <link xlink:href="http://pypi.python.org/pypi/setuptools/"><literal>setuptools</literal></link> or <literal>distutils</literal>,
  can be built using the <varname>buildPythonPackage</varname> function as documented below.
</para>

<para>
 All packages depending on any Python interpreter get appended <varname>$out/${python.libPrefix}/site-packages</varname>
 to <literal>$PYTHONPATH</literal> if such directory exists.
</para>

<variablelist>
  <title>
     Useful attributes on interpreters packages:
  </title>

  <varlistentry>
    <term><varname>libPrefix</varname></term>
    <listitem><para>
        Name of the folder in <literal>${python}/lib/</literal> for corresponding interpreter.
    </para></listitem>
  </varlistentry>

  <varlistentry>
    <term><varname>interpreter</varname></term>
    <listitem><para>
        Alias for <literal>${python}/bin/${executable}.</literal>
    </para></listitem>
  </varlistentry>

  <varlistentry>
    <term><varname>buildEnv</varname></term>
    <listitem><para>
        Function to build python interpreter environments with extra packages bundled together.
        See <xref linkend="python-build-env" /> for usage and documentation.
    </para></listitem>
  </varlistentry>

  <varlistentry>
    <term><varname>sitePackages</varname></term>
    <listitem><para>
      Alias for <literal>lib/${libPrefix}/site-packages</literal>.
    </para></listitem>
  </varlistentry>

  <varlistentry>
    <term><varname>executable</varname></term>
    <listitem><para>
      Name of the interpreter executable, ie <literal>python3.4</literal>.
    </para></listitem>
  </varlistentry>

</variablelist>
<section xml:id="build-python-package"><title><varname>buildPythonPackage</varname> function</title>

  <para>
  The function is implemented in <link xlink:href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/python-modules/generic/default.nix">
  <filename>pkgs/development/python-modules/generic/default.nix</filename></link>.
  Example usage:

<programlisting language="nix">
twisted = buildPythonPackage {
  name = "twisted-8.1.0";

  src = pkgs.fetchurl {
    url = http://tmrc.mit.edu/mirror/twisted/Twisted/8.1/Twisted-8.1.0.tar.bz2;
    sha256 = "0q25zbr4xzknaghha72mq57kh53qw1bf8csgp63pm9sfi72qhirl";
  };

  propagatedBuildInputs = [ self.ZopeInterface ];

  meta = {
    homepage = http://twistedmatrix.com/;
    description = "Twisted, an event-driven networking engine written in Python";
    license = stdenv.lib.licenses.mit;
  };
};
</programlisting>

  Most of Python packages that use <varname>buildPythonPackage</varname> are defined
  in <link xlink:href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/top-level/python-packages.nix"><filename>pkgs/top-level/python-packages.nix</filename></link>
  and generated for each python interpreter separately into attribute sets <varname>python26Packages</varname>,
  <varname>python27Packages</varname>, <varname>python32Packages</varname>, <varname>python33Packages</varname>,
  <varname>python34Packages</varname> and <varname>pypyPackages</varname>.
  </para>

  <para>
    <function>buildPythonPackage</function> mainly does four things:

    <orderedlist>
      <listitem><para>
        In the <varname>configurePhase</varname>, it patches
        <literal>setup.py</literal> to always include setuptools before
        distutils for monkeypatching machinery to take place.
      </para></listitem>

      <listitem><para>
        In the <varname>buildPhase</varname>, it calls
        <literal>${python.interpreter} setup.py build ...</literal>
      </para></listitem>

      <listitem><para>
        In the <varname>installPhase</varname>, it calls
        <literal>${python.interpreter} setup.py install ...</literal>
      </para></listitem>

      <listitem><para>
        In the <varname>postFixup</varname> phase, <literal>wrapPythonPrograms</literal>
        bash function is called to wrap all programs in <filename>$out/bin/*</filename>
        directory to include <literal>$PYTHONPATH</literal> and <literal>$PATH</literal>
        environment variables.
      </para></listitem>
    </orderedlist>
  </para>

  <para>By default <varname>doCheck = true</varname> is set and tests are run with
  <literal>${python.interpreter} setup.py test</literal> command in <varname>checkPhase</varname>.</para>

  <para><varname>propagatedBuildInputs</varname> packages are propagated to user environment.</para>

  <para>
    By default <varname>meta.platforms</varname> is set to the same value
    as the interpreter unless overriden otherwise.
  </para>

  <variablelist>
    <title>
      <varname>buildPythonPackage</varname> parameters
      (all parameters from <varname>mkDerivation</varname> function are still supported)
    </title>

    <varlistentry>
      <term><varname>namePrefix</varname></term>
      <listitem><para>
        Prepended text to <varname>${name}</varname> parameter.
        Defaults to <literal>"python3.3-"</literal> for Python 3.3, etc. Set it to
        <literal>""</literal>
        if you're packaging an application or a command line tool.
      </para></listitem>
    </varlistentry>

    <varlistentry>
      <term><varname>disabled</varname></term>
      <listitem><para>
        If <varname>true</varname>, package is not build for
        particular python interpreter version. Grep around
        <filename>pkgs/top-level/python-packages.nix</filename>
        for examples.
      </para></listitem>
    </varlistentry>

    <varlistentry>
      <term><varname>setupPyInstallFlags</varname></term>
      <listitem><para>
        List of flags passed to <command>setup.py install</command> command.
      </para></listitem>
    </varlistentry>

    <varlistentry>
      <term><varname>setupPyBuildFlags</varname></term>
      <listitem><para>
        List of flags passed to <command>setup.py build</command> command.
      </para></listitem>
    </varlistentry>

    <varlistentry>
      <term><varname>pythonPath</varname></term>
      <listitem><para>
        List of packages to be added into <literal>$PYTHONPATH</literal>.
        Packages in <varname>pythonPath</varname> are not propagated into user environment
        (contrary to <varname>propagatedBuildInputs</varname>).
      </para></listitem>
    </varlistentry>

    <varlistentry>
      <term><varname>preShellHook</varname></term>
      <listitem><para>
        Hook to execute commands before <varname>shellHook</varname>.
      </para></listitem>
    </varlistentry>

    <varlistentry>
      <term><varname>postShellHook</varname></term>
      <listitem><para>
        Hook to execute commands after <varname>shellHook</varname>.
      </para></listitem>
    </varlistentry>

    <varlistentry>
      <term><varname>distutilsExtraCfg</varname></term>
      <listitem><para>
        Extra lines passed to <varname>[easy_install]</varname> section of
        <filename>distutils.cfg</filename> (acts as global setup.cfg
        configuration).
      </para></listitem>
    </varlistentry>

    <varlistentry>
      <term><varname>makeWrapperArgs</varname></term>
      <listitem><para>
        A list of strings. Arguments to be passed to
        <varname>makeWrapper</varname>, which wraps generated binaries. By
        default, the arguments to <varname>makeWrapper</varname> set
        <varname>PATH</varname> and <varname>PYTHONPATH</varname> 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,
        <varname>makeWrapperArgs = ["--set FOO BAR" "--set BAZ QUX"]</varname>.
      </para></listitem>
    </varlistentry>

  </variablelist>

</section>

<section xml:id="python-build-env"><title><function>python.buildEnv</function> function</title>
  <para>
    Create Python environments using low-level <function>pkgs.buildEnv</function> function. Example <filename>default.nix</filename>:

<programlisting language="nix">
<![CDATA[with import <nixpkgs> {};

python.buildEnv.override {
  extraLibs = [ pkgs.pythonPackages.pyramid ];
  ignoreCollisions = true;
}]]>
</programlisting>

    Running <command>nix-build</command> will create
    <filename>/nix/store/cf1xhjwzmdki7fasgr4kz6di72ykicl5-python-2.7.8-env</filename>
    with wrapped binaries in <filename>bin/</filename>.
  </para>

  <variablelist>
    <title>
      <function>python.buildEnv</function> arguments
    </title>

    <varlistentry>
      <term><varname>extraLibs</varname></term>
      <listitem><para>
        List of packages installed inside the environment.
      </para></listitem>
    </varlistentry>

    <varlistentry>
      <term><varname>postBuild</varname></term>
      <listitem><para>
        Shell command executed after the build of environment.
      </para></listitem>
    </varlistentry>

    <varlistentry>
      <term><varname>ignoreCollisions</varname></term>
      <listitem><para>
         Ignore file collisions inside the environment (default is <varname>false</varname>).
      </para></listitem>
    </varlistentry>
  </variablelist>
</section>

<section xml:id="python-tools"><title>Tools</title>

<para>Packages inside nixpkgs are written by hand. However many tools
exist in community to help save time. No tool is preferred at the moment.
</para>

<itemizedlist>

  <listitem><para>
    <link xlink:href="https://github.com/proger/python2nix">python2nix</link>
    by Vladimir Kirillov
  </para></listitem>

  <listitem><para>
    <link xlink:href="https://github.com/garbas/pypi2nix">pypi2nix</link>
    by Rok Garbas
  </para></listitem>

  <listitem><para>
    <link xlink:href="https://github.com/offlinehacker/pypi2nix">pypi2nix</link>
    by Jaka Hudoklin
  </para></listitem>

</itemizedlist>

</section>

<section xml:id="python-development"><title>Development</title>

  <para>
    To develop Python packages <function>buildPythonPackage</function> has
    additional logic inside <varname>shellPhase</varname> to run
    <command>${python.interpreter} setup.py develop</command> for the package.
  </para>

  <warning><para><varname>shellPhase</varname> is executed only if <filename>setup.py</filename>
  exists.</para></warning>

  <para>
    Given a <filename>default.nix</filename>:

<programlisting language="nix">
<![CDATA[with import <nixpkgs> {};

buildPythonPackage {
  name = "myproject";

  buildInputs = with pkgs.pythonPackages; [ pyramid ];

  src = ./.;
}]]>
</programlisting>

    Running <command>nix-shell</command> with no arguments should give you
    the environment in which the package would be build with
    <command>nix-build</command>.
  </para>

  <para>
    Shortcut to setup environments with C headers/libraries and python packages:

    <programlisting language="bash">$ nix-shell -p pythonPackages.pyramid zlib libjpeg git</programlisting>
  </para>

  <note><para>
    There is a boolean value <varname>lib.inNixShell</varname> set to
    <varname>true</varname> if nix-shell is invoked.
  </para></note>

</section>

<section xml:id="python-faq"><title>FAQ</title>

<variablelist>

  <varlistentry>
    <term>How to solve circular dependencies?</term>
    <listitem><para>
      If you have packages <varname>A</varname> and <varname>B</varname> that
      depend on each other, when packaging <varname>B</varname> override package
      <varname>A</varname> not to depend on <varname>B</varname> as input
      (and also the other way around).
    </para></listitem>
  </varlistentry>

  <varlistentry>
    <term><varname>install_data / data_files</varname> problems resulting into <literal>error: could not create '/nix/store/6l1bvljpy8gazlsw2aw9skwwp4pmvyxw-python-2.7.8/etc': Permission denied</literal></term>
    <listitem><para>
      <link xlink:href="https://bitbucket.org/pypa/setuptools/issue/130/install_data-doesnt-respect-prefix">
      Known bug in setuptools <varname>install_data</varname> does not respect --prefix</link>. Example of
      such package using the feature is <filename>pkgs/tools/X11/xpra/default.nix</filename>. As workaround
      install it as an extra <varname>preInstall</varname> step:

      <programlisting>${python.interpreter} setup.py install_data --install-dir=$out --root=$out
sed -i '/ = data_files/d' setup.py</programlisting>
    </para></listitem>
  </varlistentry>

  <varlistentry>
    <term>Rationale of non-existent global site-packages</term>
    <listitem><para>
      There is no need to have global site-packages in Nix. Each package has isolated
      dependency tree and installing any python package will only populate <varname>$PATH</varname>
      inside user environment. See <xref linkend="python-build-env" /> to create self-contained
      interpreter with a set of packages.
    </para></listitem>
  </varlistentry>

</variablelist>

</section>


<section xml:id="python-contrib"><title>Contributing guidelines</title>
<para>
  Following rules are desired to be respected:
</para>

<itemizedlist>

  <listitem><para>
    Make sure package builds for all python interpreters. Use <varname>disabled</varname> argument to
    <function>buildPythonPackage</function> to set unsupported interpreters.
  </para></listitem>

  <listitem><para>
    If tests need to be disabled for a package, make sure you leave a comment about reasoning.
  </para></listitem>

  <listitem><para>
    Packages in <link xlink:href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/top-level/python-packages.nix"><filename>pkgs/top-level/python-packages.nix</filename></link>
    are sorted quasi-alphabetically to avoid merge conflicts.
  </para></listitem>

</itemizedlist>

</section>

</section>


<section xml:id="ssec-language-ruby"><title>Ruby</title>
  <para>There currently is support to bundle applications that are packaged as Ruby gems. The utility "bundix" allows you to write a <filename>Gemfile</filename>, let bundler create a <filename>Gemfile.lock</filename>, and then convert
  this into a nix expression that contains all Gem dependencies automatically.</para>

  <para>For example, to package sensu, we did:</para>

<screen>
<![CDATA[$ cd pkgs/servers/monitoring
$ mkdir sensu
$ cat > Gemfile
source 'https://rubygems.org'
gem 'sensu'
$ bundler package --path /tmp/vendor/bundle
$ $(nix-build '<nixpkgs>' -A bundix)/bin/bundix
$ cat > default.nix
{ lib, bundlerEnv, ruby }:

bundlerEnv {
  name = "sensu-0.17.1";

  inherit ruby;
  gemfile = ./Gemfile;
  lockfile = ./Gemfile.lock;
  gemset = ./gemset.nix;

  meta = with lib; {
    description = "A monitoring framework that aims to be simple, malleable,
and scalable.";
    homepage    = http://sensuapp.org/;
    license     = with licenses; mit;
    maintainers = with maintainers; [ theuni ];
    platforms   = platforms.unix;
  };
}]]>
</screen>

<para>Please check in the <filename>Gemfile</filename>, <filename>Gemfile.lock</filename> and the <filename>gemset.nix</filename> so future updates can be run easily.
</para>

</section>

<section xml:id="ssec-language-go"><title>Go</title>

<para>The function <varname>buildGoPackage</varname> builds
standard Go packages.
</para>

<example xml:id='ex-buildGoPackage'><title>buildGoPackage</title>
<programlisting>
net = buildGoPackage rec {
  name = "go.net-${rev}";
  goPackagePath = "golang.org/x/net"; <co xml:id='ex-buildGoPackage-1' />
  subPackages = [ "ipv4" "ipv6" ]; <co xml:id='ex-buildGoPackage-2' />
  rev = "e0403b4e005";
  src = fetchFromGitHub {
    inherit rev;
    owner = "golang";
    repo = "net";
    sha256 = "1g7cjzw4g4301a3yqpbk8n1d4s97sfby2aysl275x04g0zh8jxqp";
  };
  goPackageAliases = [ "code.google.com/p/go.net" ]; <co xml:id='ex-buildGoPackage-3' />
  propagatedBuildInputs = [ goPackages.text ]; <co xml:id='ex-buildGoPackage-4' />
  buildFlags = "--tags release"; <co xml:id='ex-buildGoPackage-5' />
  disabled = isGo13;<co xml:id='ex-buildGoPackage-6' />
};
</programlisting>
</example>

<para><xref linkend='ex-buildGoPackage'/> is an example expression using buildGoPackage,
the following arguments are of special significance to the function:

<calloutlist>

  <callout arearefs='ex-buildGoPackage-1'>
    <para>
      <varname>goPackagePath</varname> specifies the package's canonical Go import path.
    </para>
  </callout>

  <callout arearefs='ex-buildGoPackage-2'>
    <para>
      <varname>subPackages</varname> limits the builder from building child packages that
      have not been listed. If <varname>subPackages</varname> is not specified, all child
      packages will be built.
    </para>
    <para>
      In this example only <literal>code.google.com/p/go.net/ipv4</literal> and
      <literal>code.google.com/p/go.net/ipv4</literal> will be built.
    </para>
  </callout>

  <callout arearefs='ex-buildGoPackage-3'>
    <para>
      <varname>goPackageAliases</varname> is a list of alternative import paths
      that are valid for this library.
      Packages that depend on this library will automatically rename
      import paths that match any of the aliases to <literal>goPackagePath</literal>.
    </para>
    <para>
      In this example imports will be renamed from
      <literal>code.google.com/p/go.net</literal> to
      <literal>golang.org/x/net</literal> in every package that depend on the
      <literal>go.net</literal> library.
    </para>
  </callout>

  <callout arearefs='ex-buildGoPackage-4'>
    <para>
      <varname>propagatedBuildInputs</varname> is where the dependencies of a Go library are
      listed. Only libraries should list <varname>propagatedBuildInputs</varname>. If a standalone
      program is being build instead, use <varname>buildInputs</varname>. If a library's tests require
      additional dependencies that are not propagated, they should be listed in <varname>buildInputs</varname>.
    </para>
  </callout>

  <callout arearefs='ex-buildGoPackage-5'>
    <para>
      <varname>buildFlags</varname> is a list of flags passed to the go build command.
    </para>
  </callout>

  <callout arearefs='ex-buildGoPackage-6'>
    <para>
      If <varname>disabled</varname> is <literal>true</literal>,
      nix will refuse to build this package.
    </para>
    <para>
      In this example the package will not be built for go 1.3. The <literal>isGo13</literal>
      is an utility function that returns <literal>true</literal> if go used to build the
      package has version 1.3.x.
    </para>
  </callout>

</calloutlist>

</para>

<para>
Reusable Go libraries may be found in the <varname>goPackages</varname> set. You can test
build a Go package as follows:

<screen>
$ nix-build -A goPackages.net
</screen>

</para>

<para>
You may use Go packages installed into the active Nix profiles by adding
the following to your ~/.bashrc:

<screen>
for p in $NIX_PROFILES; do
    GOPATH="$p/share/go:$GOPATH"
done
</screen>
</para>

  <para>To extract dependency information from a Go package in automated way use <link xlink:href="https://github.com/cstrahan/go2nix">go2nix</link>.</para>
</section>


<section xml:id="ssec-language-java"><title>Java</title>

<para>Ant-based Java packages are typically built from source as follows:

<programlisting>
stdenv.mkDerivation {
  name = "...";
  src = fetchurl { ... };

  buildInputs = [ jdk ant ];

  buildPhase = "ant";
}
</programlisting>

Note that <varname>jdk</varname> is an alias for the OpenJDK.</para>

<para>JAR files that are intended to be used by other packages should
be installed in <filename>$out/share/java</filename>.  The OpenJDK has
a stdenv setup hook that adds any JARs in the
<filename>share/java</filename> directories of the build inputs to the
<envar>CLASSPATH</envar> environment variable.  For instance, if the
package <literal>libfoo</literal> installs a JAR named
<filename>foo.jar</filename> in its <filename>share/java</filename>
directory, and another package declares the attribute

<programlisting>
buildInputs = [ jdk libfoo ];
</programlisting>

then <envar>CLASSPATH</envar> will be set to
<filename>/nix/store/...-libfoo/share/java/foo.jar</filename>.</para>

<para>Private JARs
should be installed in a location like
<filename>$out/share/<replaceable>package-name</replaceable></filename>.</para>

<para>If your Java package provides a program, you need to generate a
wrapper script to run it using the OpenJRE.  You can use
<literal>makeWrapper</literal> for this:

<programlisting>
buildInputs = [ makeWrapper ];

installPhase =
  ''
    mkdir -p $out/bin
    makeWrapper ${jre}/bin/java $out/bin/foo \
      --add-flags "-cp $out/share/java/foo.jar org.foo.Main"
  '';
</programlisting>

Note the use of <literal>jre</literal>, which is the part of the
OpenJDK package that contains the Java Runtime Environment.  By using
<literal>${jre}/bin/java</literal> instead of
<literal>${jdk}/bin/java</literal>, you prevent your package from
depending on the JDK at runtime.</para>

<para>It is possible to use a different Java compiler than
<command>javac</command> from the OpenJDK.  For instance, to use the
Eclipse Java Compiler:

<programlisting>
buildInputs = [ jre ant ecj ];
</programlisting>

(Note that here you don’t need the full JDK as an input, but just the
JRE.)  The ECJ has a stdenv setup hook that sets some environment
variables to cause Ant to use ECJ, but this doesn’t work with all Ant
files.  Similarly, you can use the GNU Java Compiler:

<programlisting>
buildInputs = [ gcj ant ];
</programlisting>

Here, Ant will automatically use <command>gij</command> (the GNU Java
Runtime) instead of the OpenJRE.</para>

</section>


<section xml:id="ssec-language-lua"><title>Lua</title>

<para>
  Lua packages are built by the <varname>buildLuaPackage</varname> function.  This function is
  implemented
  in <link xlink:href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/lua-modules/generic/default.nix">
  <filename>pkgs/development/lua-modules/generic/default.nix</filename></link>
  and works similarly to <varname>buildPerlPackage</varname>. (See
  <xref linkend="ssec-language-perl"/> for details.)
</para>

<para>
  Lua packages are defined
  in <link xlink:href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/top-level/lua-packages.nix"><filename>pkgs/top-level/lua-packages.nix</filename></link>.
  Most of them are simple. For example:

  <programlisting>
fileSystem = buildLuaPackage {
  name = "filesystem-1.6.2";
  src = fetchurl {
    url = "https://github.com/keplerproject/luafilesystem/archive/v1_6_2.tar.gz";
    sha256 = "1n8qdwa20ypbrny99vhkmx8q04zd2jjycdb5196xdhgvqzk10abz";
  };
  meta = {
    homepage = "https://github.com/keplerproject/luafilesystem";
    hydraPlatforms = stdenv.lib.platforms.linux;
    maintainers = with maintainers; [ flosse ];
  };
};
  </programlisting>
</para>

<para>
  Though, more complicated package should be placed in a seperate file in
  <link
  xlink:href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/lua-modules"><filename>pkgs/development/lua-modules</filename></link>.
</para>
<para>
  Lua packages accept additional parameter <varname>disabled</varname>, which defines
  the condition of disabling package from luaPackages. For example, if package has
  <varname>disabled</varname> assigned to <literal>lua.luaversion != "5.1"</literal>,
  it will not be included in any luaPackages except lua51Packages, making it
  only be built for lua 5.1.
</para>

</section>

<section xml:id="ssec-language-coq"><title>Coq</title>
  <para>
    Coq libraries should be installed in
    <literal>$(out)/lib/coq/${coq.coq-version}/user-contrib/</literal>.
    Such directories are automatically added to the
    <literal>$COQPATH</literal> environment variable by the hook defined
    in the Coq derivation.
  </para>
  <para>
    Some libraries require OCaml and sometimes also Camlp5. The exact
    versions that were used to build Coq are saved in the
    <literal>coq.ocaml</literal> and <literal>coq.camlp5</literal>
    attributes.
  </para>
  <para>
    Here is a simple package example. It is a pure Coq library, thus it
    only depends on Coq. Its <literal>makefile</literal> has been
    generated using <literal>coq_makefile</literal> so we only have to
    set the <literal>$COQLIB</literal> variable at install time.
  </para>
  <programlisting>
{stdenv, fetchurl, coq}:
stdenv.mkDerivation {
  src = fetchurl {
    url = http://coq.inria.fr/pylons/contribs/files/Karatsuba/v8.4/Karatsuba.tar.gz;
    sha256 = "0ymfpv4v49k4fm63nq6gcl1hbnnxrvjjp7yzc4973n49b853c5b1";
  };

  name = "coq-karatsuba";

  buildInputs = [ coq ];

  installFlags = "COQLIB=$(out)/lib/coq/${coq.coq-version}/";
}
</programlisting>
</section>

<!--
<section><title>Haskell</title>

<para>TODO</para>

</section>


<section><title>TeX / LaTeX</title>

<para>* Special support for building TeX documents</para>

</section>
-->


</chapter>