summary refs log tree commit diff
path: root/doc/coding-conventions.xml
blob: 48356247a49e1bf07d86462998eec4293b944745 (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
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
<chapter xmlns="http://docbook.org/ns/docbook"
         xmlns:xlink="http://www.w3.org/1999/xlink"
         xml:id="chap-conventions">
 <title>Coding conventions</title>
 <section xml:id="sec-syntax">
  <title>Syntax</title>

  <itemizedlist>
   <listitem>
    <para>
     Use 2 spaces of indentation per indentation level in Nix expressions, 4
     spaces in shell scripts.
    </para>
   </listitem>
   <listitem>
    <para>
     Do not use tab characters, i.e. configure your editor to use soft tabs.
     For instance, use <literal>(setq-default indent-tabs-mode nil)</literal>
     in Emacs. Everybody has different tab settings so it’s asking for
     trouble.
    </para>
   </listitem>
   <listitem>
    <para>
     Use <literal>lowerCamelCase</literal> for variable names, not
     <literal>UpperCamelCase</literal>. Note, this rule does not apply to
     package attribute names, which instead follow the rules in
     <xref linkend="sec-package-naming"/>.
    </para>
   </listitem>
   <listitem>
    <para>
     Function calls with attribute set arguments are written as
<programlisting>
foo {
  arg = ...;
}
</programlisting>
     not
<programlisting>
foo
{
  arg = ...;
}
</programlisting>
     Also fine is
<programlisting>
foo { arg = ...; }
</programlisting>
     if it's a short call.
    </para>
   </listitem>
   <listitem>
    <para>
     In attribute sets or lists that span multiple lines, the attribute names
     or list elements should be aligned:
<programlisting>
# A long list.
list = [
  elem1
  elem2
  elem3
];

# A long attribute set.
attrs = {
  attr1 = short_expr;
  attr2 =
    if true then big_expr else big_expr;
};

# Combined
listOfAttrs = [
  {
    attr1 = 3;
    attr2 = "fff";
  }
  {
    attr1 = 5;
    attr2 = "ggg";
  }
];
</programlisting>
    </para>
   </listitem>
   <listitem>
    <para>
     Short lists or attribute sets can be written on one line:
<programlisting>
# A short list.
list = [ elem1 elem2 elem3 ];

# A short set.
attrs = { x = 1280; y = 1024; };
</programlisting>
    </para>
   </listitem>
   <listitem>
    <para>
     Breaking in the middle of a function argument can give hard-to-read code,
     like
<programlisting>
someFunction { x = 1280;
  y = 1024; } otherArg
  yetAnotherArg
</programlisting>
     (especially if the argument is very large, spanning multiple lines).
    </para>
    <para>
     Better:
<programlisting>
someFunction
  { x = 1280; y = 1024; }
  otherArg
  yetAnotherArg
</programlisting>
     or
<programlisting>
let res = { x = 1280; y = 1024; };
in someFunction res otherArg yetAnotherArg
</programlisting>
    </para>
   </listitem>
   <listitem>
    <para>
     The bodies of functions, asserts, and withs are not indented to prevent a
     lot of superfluous indentation levels, i.e.
<programlisting>
{ arg1, arg2 }:
assert system == "i686-linux";
stdenv.mkDerivation { ...
</programlisting>
     not
<programlisting>
{ arg1, arg2 }:
  assert system == "i686-linux";
    stdenv.mkDerivation { ...
</programlisting>
    </para>
   </listitem>
   <listitem>
    <para>
     Function formal arguments are written as:
<programlisting>
{ arg1, arg2, arg3 }:
</programlisting>
     but if they don't fit on one line they're written as:
<programlisting>
{ arg1, arg2, arg3
, arg4, ...
, # Some comment...
  argN
}:
</programlisting>
    </para>
   </listitem>
   <listitem>
    <para>
     Functions should list their expected arguments as precisely as possible.
     That is, write
<programlisting>
{ stdenv, fetchurl, perl }: <replaceable>...</replaceable>
</programlisting>
     instead of
<programlisting>
args: with args; <replaceable>...</replaceable>
</programlisting>
     or
<programlisting>
{ stdenv, fetchurl, perl, ... }: <replaceable>...</replaceable>
</programlisting>
    </para>
    <para>
     For functions that are truly generic in the number of arguments (such as
     wrappers around <varname>mkDerivation</varname>) that have some required
     arguments, you should write them using an <literal>@</literal>-pattern:
<programlisting>
{ stdenv, doCoverageAnalysis ? false, ... } @ args:

stdenv.mkDerivation (args // {
  <replaceable>...</replaceable> if doCoverageAnalysis then "bla" else "" <replaceable>...</replaceable>
})
</programlisting>
     instead of
<programlisting>
args:

args.stdenv.mkDerivation (args // {
  <replaceable>...</replaceable> if args ? doCoverageAnalysis &amp;&amp; args.doCoverageAnalysis then "bla" else "" <replaceable>...</replaceable>
})
</programlisting>
    </para>
   </listitem>
  </itemizedlist>
 </section>
 <section xml:id="sec-package-naming">
  <title>Package naming</title>

  <para>
   The key words <emphasis>must</emphasis>, <emphasis>must not</emphasis>,
   <emphasis>required</emphasis>, <emphasis>shall</emphasis>, <emphasis>shall
   not</emphasis>, <emphasis>should</emphasis>, <emphasis>should
   not</emphasis>, <emphasis>recommended</emphasis>, <emphasis>may</emphasis>,
   and <emphasis>optional</emphasis> in this section are to be interpreted as
   described in <link xlink:href="https://tools.ietf.org/html/rfc2119">RFC
   2119</link>. Only <emphasis>emphasized</emphasis> words are to be
   interpreted in this way.
  </para>

  <para>
   In Nixpkgs, there are generally three different names associated with a
   package:
   <itemizedlist>
    <listitem>
     <para>
      The <varname>name</varname> attribute of the derivation (excluding the
      version part). This is what most users see, in particular when using
      <command>nix-env</command>.
     </para>
    </listitem>
    <listitem>
     <para>
      The variable name used for the instantiated package in
      <filename>all-packages.nix</filename>, and when passing it as a
      dependency to other functions. Typically this is called the
      <emphasis>package attribute name</emphasis>. This is what Nix expression
      authors see. It can also be used when installing using <command>nix-env
      -iA</command>.
     </para>
    </listitem>
    <listitem>
     <para>
      The filename for (the directory containing) the Nix expression.
     </para>
    </listitem>
   </itemizedlist>
   Most of the time, these are the same. For instance, the package
   <literal>e2fsprogs</literal> has a <varname>name</varname> attribute
   <literal>"e2fsprogs-<replaceable>version</replaceable>"</literal>, is bound
   to the variable name <varname>e2fsprogs</varname> in
   <filename>all-packages.nix</filename>, and the Nix expression is in
   <filename>pkgs/os-specific/linux/e2fsprogs/default.nix</filename>.
  </para>

  <para>
   There are a few naming guidelines:
   <itemizedlist>
    <listitem>
     <para>
      The <literal>name</literal> attribute <emphasis>should</emphasis> be
      identical to the upstream package name.
     </para>
    </listitem>
    <listitem>
     <para>
      The <literal>name</literal> attribute <emphasis>must not</emphasis>
      contain uppercase letters  e.g., <literal>"mplayer-1.0rc2"</literal>
      instead of <literal>"MPlayer-1.0rc2"</literal>.
     </para>
    </listitem>
    <listitem>
     <para>
      The version part of the <literal>name</literal> attribute
      <emphasis>must</emphasis> start with a digit (following a dash)  e.g.,
      <literal>"hello-0.3.1rc2"</literal>.
     </para>
    </listitem>
    <listitem>
     <para>
      If a package is not a release but a commit from a repository, then the
      version part of the name <emphasis>must</emphasis> be the date of that
      (fetched) commit. The date <emphasis>must</emphasis> be in
      <literal>"YYYY-MM-DD"</literal> format. Also append
      <literal>"unstable"</literal> to the name - e.g.,
      <literal>"pkgname-unstable-2014-09-23"</literal>.
     </para>
    </listitem>
    <listitem>
     <para>
      Dashes in the package name <emphasis>should</emphasis> be preserved in
      new variable names, rather than converted to underscores or camel cased
       e.g., <varname>http-parser</varname> instead of
      <varname>http_parser</varname> or <varname>httpParser</varname>. The
      hyphenated style is preferred in all three package names.
     </para>
    </listitem>
    <listitem>
     <para>
      If there are multiple versions of a package, this
      <emphasis>should</emphasis> be reflected in the variable names in
      <filename>all-packages.nix</filename>, e.g. <varname>json-c-0-9</varname>
      and <varname>json-c-0-11</varname>. If there is an obvious “default”
      version, make an attribute like <literal>json-c = json-c-0-9;</literal>.
      See also <xref linkend="sec-versioning" />
     </para>
    </listitem>
   </itemizedlist>
  </para>
 </section>
 <section xml:id="sec-organisation">
  <title>File naming and organisation</title>

  <para>
   Names of files and directories should be in lowercase, with dashes between
   words  not in camel case. For instance, it should be
   <filename>all-packages.nix</filename>, not
   <filename>allPackages.nix</filename> or
   <filename>AllPackages.nix</filename>.
  </para>

  <section xml:id="sec-hierarchy">
   <title>Hierarchy</title>

   <para>
    Each package should be stored in its own directory somewhere in the
    <filename>pkgs/</filename> tree, i.e. in
    <filename>pkgs/<replaceable>category</replaceable>/<replaceable>subcategory</replaceable>/<replaceable>...</replaceable>/<replaceable>pkgname</replaceable></filename>.
    Below are some rules for picking the right category for a package. Many
    packages fall under several categories; what matters is the
    <emphasis>primary</emphasis> purpose of a package. For example, the
    <literal>libxml2</literal> package builds both a library and some tools;
    but it’s a library foremost, so it goes under
    <filename>pkgs/development/libraries</filename>.
   </para>

   <para>
    When in doubt, consider refactoring the <filename>pkgs/</filename> tree,
    e.g. creating new categories or splitting up an existing category.
   </para>

   <variablelist>
    <varlistentry>
     <term>
      If it’s used to support <emphasis>software development</emphasis>:
     </term>
     <listitem>
      <variablelist>
       <varlistentry>
        <term>
         If it’s a <emphasis>library</emphasis> used by other packages:
        </term>
        <listitem>
         <para>
          <filename>development/libraries</filename> (e.g.
          <filename>libxml2</filename>)
         </para>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term>
         If it’s a <emphasis>compiler</emphasis>:
        </term>
        <listitem>
         <para>
          <filename>development/compilers</filename> (e.g.
          <filename>gcc</filename>)
         </para>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term>
         If it’s an <emphasis>interpreter</emphasis>:
        </term>
        <listitem>
         <para>
          <filename>development/interpreters</filename> (e.g.
          <filename>guile</filename>)
         </para>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term>
         If it’s a (set of) development <emphasis>tool(s)</emphasis>:
        </term>
        <listitem>
         <variablelist>
          <varlistentry>
           <term>
            If it’s a <emphasis>parser generator</emphasis> (including lexers):
           </term>
           <listitem>
            <para>
             <filename>development/tools/parsing</filename> (e.g.
             <filename>bison</filename>, <filename>flex</filename>)
            </para>
           </listitem>
          </varlistentry>
          <varlistentry>
           <term>
            If it’s a <emphasis>build manager</emphasis>:
           </term>
           <listitem>
            <para>
             <filename>development/tools/build-managers</filename> (e.g.
             <filename>gnumake</filename>)
            </para>
           </listitem>
          </varlistentry>
          <varlistentry>
           <term>
            Else:
           </term>
           <listitem>
            <para>
             <filename>development/tools/misc</filename> (e.g.
             <filename>binutils</filename>)
            </para>
           </listitem>
          </varlistentry>
         </variablelist>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term>
         Else:
        </term>
        <listitem>
         <para>
          <filename>development/misc</filename>
         </para>
        </listitem>
       </varlistentry>
      </variablelist>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term>
      If it’s a (set of) <emphasis>tool(s)</emphasis>:
     </term>
     <listitem>
      <para>
       (A tool is a relatively small program, especially one intended to be
       used non-interactively.)
      </para>
      <variablelist>
       <varlistentry>
        <term>
         If it’s for <emphasis>networking</emphasis>:
        </term>
        <listitem>
         <para>
          <filename>tools/networking</filename> (e.g.
          <filename>wget</filename>)
         </para>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term>
         If it’s for <emphasis>text processing</emphasis>:
        </term>
        <listitem>
         <para>
          <filename>tools/text</filename> (e.g. <filename>diffutils</filename>)
         </para>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term>
         If it’s a <emphasis>system utility</emphasis>, i.e., something related or essential to the operation of a system:
        </term>
        <listitem>
         <para>
          <filename>tools/system</filename> (e.g. <filename>cron</filename>)
         </para>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term>
         If it’s an <emphasis>archiver</emphasis> (which may include a compression function):
        </term>
        <listitem>
         <para>
          <filename>tools/archivers</filename> (e.g. <filename>zip</filename>,
          <filename>tar</filename>)
         </para>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term>
         If it’s a <emphasis>compression</emphasis> program:
        </term>
        <listitem>
         <para>
          <filename>tools/compression</filename> (e.g.
          <filename>gzip</filename>, <filename>bzip2</filename>)
         </para>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term>
         If it’s a <emphasis>security</emphasis>-related program:
        </term>
        <listitem>
         <para>
          <filename>tools/security</filename> (e.g. <filename>nmap</filename>,
          <filename>gnupg</filename>)
         </para>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term>
         Else:
        </term>
        <listitem>
         <para>
          <filename>tools/misc</filename>
         </para>
        </listitem>
       </varlistentry>
      </variablelist>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term>
      If it’s a <emphasis>shell</emphasis>:
     </term>
     <listitem>
      <para>
       <filename>shells</filename> (e.g. <filename>bash</filename>)
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term>
      If it’s a <emphasis>server</emphasis>:
     </term>
     <listitem>
      <variablelist>
       <varlistentry>
        <term>
         If it’s a web server:
        </term>
        <listitem>
         <para>
          <filename>servers/http</filename> (e.g.
          <filename>apache-httpd</filename>)
         </para>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term>
         If it’s an implementation of the X Windowing System:
        </term>
        <listitem>
         <para>
          <filename>servers/x11</filename> (e.g. <filename>xorg</filename>           this includes the client libraries and programs)
         </para>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term>
         Else:
        </term>
        <listitem>
         <para>
          <filename>servers/misc</filename>
         </para>
        </listitem>
       </varlistentry>
      </variablelist>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term>
      If it’s a <emphasis>desktop environment</emphasis>:
     </term>
     <listitem>
      <para>
       <filename>desktops</filename> (e.g. <filename>kde</filename>,
       <filename>gnome</filename>, <filename>enlightenment</filename>)
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term>
      If it’s a <emphasis>window manager</emphasis>:
     </term>
     <listitem>
      <para>
       <filename>applications/window-managers</filename> (e.g.
       <filename>awesome</filename>, <filename>stumpwm</filename>)
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term>
      If it’s an <emphasis>application</emphasis>:
     </term>
     <listitem>
      <para>
       A (typically large) program with a distinct user interface, primarily
       used interactively.
      </para>
      <variablelist>
       <varlistentry>
        <term>
         If it’s a <emphasis>version management system</emphasis>:
        </term>
        <listitem>
         <para>
          <filename>applications/version-management</filename> (e.g.
          <filename>subversion</filename>)
         </para>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term>
         If it’s for <emphasis>video playback / editing</emphasis>:
        </term>
        <listitem>
         <para>
          <filename>applications/video</filename> (e.g.
          <filename>vlc</filename>)
         </para>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term>
         If it’s for <emphasis>graphics viewing / editing</emphasis>:
        </term>
        <listitem>
         <para>
          <filename>applications/graphics</filename> (e.g.
          <filename>gimp</filename>)
         </para>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term>
         If it’s for <emphasis>networking</emphasis>:
        </term>
        <listitem>
         <variablelist>
          <varlistentry>
           <term>
            If it’s a <emphasis>mailreader</emphasis>:
           </term>
           <listitem>
            <para>
             <filename>applications/networking/mailreaders</filename> (e.g.
             <filename>thunderbird</filename>)
            </para>
           </listitem>
          </varlistentry>
          <varlistentry>
           <term>
            If it’s a <emphasis>newsreader</emphasis>:
           </term>
           <listitem>
            <para>
             <filename>applications/networking/newsreaders</filename> (e.g.
             <filename>pan</filename>)
            </para>
           </listitem>
          </varlistentry>
          <varlistentry>
           <term>
            If it’s a <emphasis>web browser</emphasis>:
           </term>
           <listitem>
            <para>
             <filename>applications/networking/browsers</filename> (e.g.
             <filename>firefox</filename>)
            </para>
           </listitem>
          </varlistentry>
          <varlistentry>
           <term>
            Else:
           </term>
           <listitem>
            <para>
             <filename>applications/networking/misc</filename>
            </para>
           </listitem>
          </varlistentry>
         </variablelist>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term>
         Else:
        </term>
        <listitem>
         <para>
          <filename>applications/misc</filename>
         </para>
        </listitem>
       </varlistentry>
      </variablelist>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term>
      If it’s <emphasis>data</emphasis> (i.e., does not have a straight-forward executable semantics):
     </term>
     <listitem>
      <variablelist>
       <varlistentry>
        <term>
         If it’s a <emphasis>font</emphasis>:
        </term>
        <listitem>
         <para>
          <filename>data/fonts</filename>
         </para>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term>
         If it’s related to <emphasis>SGML/XML processing</emphasis>:
        </term>
        <listitem>
         <variablelist>
          <varlistentry>
           <term>
            If it’s an <emphasis>XML DTD</emphasis>:
           </term>
           <listitem>
            <para>
             <filename>data/sgml+xml/schemas/xml-dtd</filename> (e.g.
             <filename>docbook</filename>)
            </para>
           </listitem>
          </varlistentry>
          <varlistentry>
           <term>
            If it’s an <emphasis>XSLT stylesheet</emphasis>:
           </term>
           <listitem>
            <para>
             (Okay, these are executable...)
            </para>
            <para>
             <filename>data/sgml+xml/stylesheets/xslt</filename> (e.g.
             <filename>docbook-xsl</filename>)
            </para>
           </listitem>
          </varlistentry>
         </variablelist>
        </listitem>
       </varlistentry>
      </variablelist>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term>
      If it’s a <emphasis>game</emphasis>:
     </term>
     <listitem>
      <para>
       <filename>games</filename>
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term>
      Else:
     </term>
     <listitem>
      <para>
       <filename>misc</filename>
      </para>
     </listitem>
    </varlistentry>
   </variablelist>
  </section>

  <section xml:id="sec-versioning">
   <title>Versioning</title>

   <para>
    Because every version of a package in Nixpkgs creates a potential
    maintenance burden, old versions of a package should not be kept unless
    there is a good reason to do so. For instance, Nixpkgs contains several
    versions of GCC because other packages don’t build with the latest
    version of GCC. Other examples are having both the latest stable and latest
    pre-release version of a package, or to keep several major releases of an
    application that differ significantly in functionality.
   </para>

   <para>
    If there is only one version of a package, its Nix expression should be
    named <filename>e2fsprogs/default.nix</filename>. If there are multiple
    versions, this should be reflected in the filename, e.g.
    <filename>e2fsprogs/1.41.8.nix</filename> and
    <filename>e2fsprogs/1.41.9.nix</filename>. The version in the filename
    should leave out unnecessary detail. For instance, if we keep the latest
    Firefox 2.0.x and 3.5.x versions in Nixpkgs, they should be named
    <filename>firefox/2.0.nix</filename> and
    <filename>firefox/3.5.nix</filename>, respectively (which, at a given
    point, might contain versions <literal>2.0.0.20</literal> and
    <literal>3.5.4</literal>). If a version requires many auxiliary files, you
    can use a subdirectory for each version, e.g.
    <filename>firefox/2.0/default.nix</filename> and
    <filename>firefox/3.5/default.nix</filename>.
   </para>

   <para>
    All versions of a package <emphasis>must</emphasis> be included in
    <filename>all-packages.nix</filename> to make sure that they evaluate
    correctly.
   </para>
  </section>
 </section>
 <section xml:id="sec-sources">
  <title>Fetching Sources</title>

  <para>
   There are multiple ways to fetch a package source in nixpkgs. The general
   guideline is that you should package reproducible sources with a high degree
   of availability. Right now there is only one fetcher which has mirroring
   support and that is <literal>fetchurl</literal>. Note that you should also
   prefer protocols which have a corresponding proxy environment variable.
  </para>

  <para>
   You can find many source fetch helpers in
   <literal>pkgs/build-support/fetch*</literal>.
  </para>

  <para>
   In the file <literal>pkgs/top-level/all-packages.nix</literal> you can find
   fetch helpers, these have names on the form <literal>fetchFrom*</literal>.
   The intention of these are to provide snapshot fetches but using the same
   api as some of the version controlled fetchers from
   <literal>pkgs/build-support/</literal>. As an example going from bad to
   good:
   <itemizedlist>
    <listitem>
     <para>
      Bad: Uses <literal>git://</literal> which won't be proxied.
<programlisting>
src = fetchgit {
  url = "git://github.com/NixOS/nix.git";
  rev = "1f795f9f44607cc5bec70d1300150bfefcef2aae";
  sha256 = "1cw5fszffl5pkpa6s6wjnkiv6lm5k618s32sp60kvmvpy7a2v9kg";
}
</programlisting>
     </para>
    </listitem>
    <listitem>
     <para>
      Better: This is ok, but an archive fetch will still be faster.
<programlisting>
src = fetchgit {
  url = "https://github.com/NixOS/nix.git";
  rev = "1f795f9f44607cc5bec70d1300150bfefcef2aae";
  sha256 = "1cw5fszffl5pkpa6s6wjnkiv6lm5k618s32sp60kvmvpy7a2v9kg";
}
</programlisting>
     </para>
    </listitem>
    <listitem>
     <para>
      Best: Fetches a snapshot archive and you get the rev you want.
<programlisting>
src = fetchFromGitHub {
  owner = "NixOS";
  repo = "nix";
  rev = "1f795f9f44607cc5bec70d1300150bfefcef2aae";
  sha256 = "1i2yxndxb6yc9l6c99pypbd92lfq5aac4klq7y2v93c9qvx2cgpc";
}
</programlisting>
      Find the value to put as <literal>sha256</literal> by running
      <literal>nix run -f '&lt;nixpkgs&gt;' nix-prefetch-github -c
      nix-prefetch-github --rev 1f795f9f44607cc5bec70d1300150bfefcef2aae NixOS
      nix</literal> or <literal>nix-prefetch-url --unpack
      https://github.com/NixOS/nix/archive/1f795f9f44607cc5bec70d1300150bfefcef2aae.tar.gz</literal>.
     </para>
    </listitem>
   </itemizedlist>
  </para>
 </section>
 <section xml:id="sec-source-hashes">
  <title>Obtaining source hash</title>

  <para>
   Preferred source hash type is sha256. There are several ways to get it.
  </para>

  <orderedlist>
   <listitem>
    <para>
     Prefetch URL (with <literal>nix-prefetch-<replaceable>XXX</replaceable>
     <replaceable>URL</replaceable></literal>, where
     <replaceable>XXX</replaceable> is one of <literal>url</literal>,
     <literal>git</literal>, <literal>hg</literal>, <literal>cvs</literal>,
     <literal>bzr</literal>, <literal>svn</literal>). Hash is printed to
     stdout.
    </para>
   </listitem>
   <listitem>
    <para>
     Prefetch by package source (with <literal>nix-prefetch-url
     '&lt;nixpkgs&gt;' -A <replaceable>PACKAGE</replaceable>.src</literal>,
     where <replaceable>PACKAGE</replaceable> is package attribute name). Hash
     is printed to stdout.
    </para>
    <para>
     This works well when you've upgraded existing package version and want to
     find out new hash, but is useless if package can't be accessed by
     attribute or package has multiple sources (<literal>.srcs</literal>,
     architecture-dependent sources, etc).
    </para>
   </listitem>
   <listitem>
    <para>
     Upstream provided hash: use it when upstream provides
     <literal>sha256</literal> or <literal>sha512</literal> (when upstream
     provides <literal>md5</literal>, don't use it, compute
     <literal>sha256</literal> instead).
    </para>
    <para>
     A little nuance is that <literal>nix-prefetch-*</literal> tools produce
     hash encoded with <literal>base32</literal>, but upstream usually provides
     hexadecimal (<literal>base16</literal>) encoding. Fetchers understand both
     formats. Nixpkgs does not standardize on any one format.
    </para>
    <para>
     You can convert between formats with nix-hash, for example:
<screen>
<prompt>$ </prompt>nix-hash --type sha256 --to-base32 <replaceable>HASH</replaceable>
</screen>
    </para>
   </listitem>
   <listitem>
    <para>
     Extracting hash from local source tarball can be done with
     <literal>sha256sum</literal>. Use <literal>nix-prefetch-url
     file:///path/to/tarball </literal> if you want base32 hash.
    </para>
   </listitem>
   <listitem>
    <para>
     Fake hash: set fake hash in package expression, perform build and extract
     correct hash from error Nix prints.
    </para>
    <para>
     For package updates it is enough to change one symbol to make hash fake.
     For new packages, you can use <literal>lib.fakeSha256</literal>,
     <literal>lib.fakeSha512</literal> or any other fake hash.
    </para>
    <para>
     This is last resort method when reconstructing source URL is non-trivial
     and <literal>nix-prefetch-url -A</literal> isn't applicable (for example,
     <link xlink:href="https://github.com/NixOS/nixpkgs/blob/d2ab091dd308b99e4912b805a5eb088dd536adb9/pkgs/applications/video/kodi/default.nix#L73">
     one of <literal>kodi</literal> dependencies</link>). The easiest way then
     would be replace hash with a fake one and rebuild. Nix build will fail and
     error message will contain desired hash.
    </para>
    <warning>
     <para>
      This method has security problems. Check below for details.
     </para>
    </warning>
   </listitem>
  </orderedlist>

  <section xml:id="sec-source-hashes-security">
   <title>Obtaining hashes securely</title>

   <para>
    Let's say Man-in-the-Middle (MITM) sits close to your network. Then instead
    of fetching source you can fetch malware, and instead of source hash you
    get hash of malware. Here are security considerations for this scenario:
   </para>

   <itemizedlist>
    <listitem>
     <para>
      <literal>http://</literal> URLs are not secure to prefetch hash from;
     </para>
    </listitem>
    <listitem>
     <para>
      hashes from upstream (in method 3) should be obtained via secure
      protocol;
     </para>
    </listitem>
    <listitem>
     <para>
      <literal>https://</literal> URLs are secure in methods 1, 2, 3;
     </para>
    </listitem>
    <listitem>
     <para>
      <literal>https://</literal> URLs are not secure in method 5. When
      obtaining hashes with fake hash method, TLS checks are disabled. So
      refetch source hash from several different networks to exclude MITM
      scenario. Alternatively, use fake hash method to make Nix error, but
      instead of extracting hash from error, extract
      <literal>https://</literal> URL and prefetch it with method 1.
     </para>
    </listitem>
   </itemizedlist>
  </section>
 </section>
 <section xml:id="sec-patches">
  <title>Patches</title>

  <para>
   Patches available online should be retrieved using
   <literal>fetchpatch</literal>.
  </para>

  <para>
<programlisting>
patches = [
  (fetchpatch {
    name = "fix-check-for-using-shared-freetype-lib.patch";
    url = "http://git.ghostscript.com/?p=ghostpdl.git;a=patch;h=8f5d285";
    sha256 = "1f0k043rng7f0rfl9hhb89qzvvksqmkrikmm38p61yfx51l325xr";
  })
];
</programlisting>
  </para>

  <para>
   Otherwise, you can add a <literal>.patch</literal> file to the
   <literal>nixpkgs</literal> repository. In the interest of keeping our
   maintenance burden to a minimum, only patches that are unique to
   <literal>nixpkgs</literal> should be added in this way.
  </para>

  <para>
<programlisting>
patches = [ ./0001-changes.patch ];
</programlisting>
  </para>

  <para>
   If you do need to do create this sort of patch file, one way to do so is
   with git:
   <orderedlist>
    <listitem>
     <para>
      Move to the root directory of the source code you're patching.
<screen>
<prompt>$ </prompt>cd the/program/source</screen>
     </para>
    </listitem>
    <listitem>
     <para>
      If a git repository is not already present, create one and stage all of
      the source files.
<screen>
<prompt>$ </prompt>git init
<prompt>$ </prompt>git add .</screen>
     </para>
    </listitem>
    <listitem>
     <para>
      Edit some files to make whatever changes need to be included in the
      patch.
     </para>
    </listitem>
    <listitem>
     <para>
      Use git to create a diff, and pipe the output to a patch file:
<screen>
<prompt>$ </prompt>git diff > nixpkgs/pkgs/the/package/0001-changes.patch</screen>
     </para>
    </listitem>
   </orderedlist>
  </para>
 </section>
</chapter>