1<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xml:id="sec-custom-packages">
2 <title>Adding Custom Packages</title>
3 <para>
4 It’s possible that a package you need is not available in NixOS. In
5 that case, you can do two things. First, you can clone the Nixpkgs
6 repository, add the package to your clone, and (optionally) submit a
7 patch or pull request to have it accepted into the main Nixpkgs
8 repository. This is described in detail in the
9 <link xlink:href="https://nixos.org/nixpkgs/manual">Nixpkgs
10 manual</link>. In short, you clone Nixpkgs:
11 </para>
12 <programlisting>
13$ git clone https://github.com/NixOS/nixpkgs
14$ cd nixpkgs
15</programlisting>
16 <para>
17 Then you write and test the package as described in the Nixpkgs
18 manual. Finally, you add it to
19 <xref linkend="opt-environment.systemPackages" />, e.g.
20 </para>
21 <programlisting language="bash">
22environment.systemPackages = [ pkgs.my-package ];
23</programlisting>
24 <para>
25 and you run <literal>nixos-rebuild</literal>, specifying your own
26 Nixpkgs tree:
27 </para>
28 <programlisting>
29# nixos-rebuild switch -I nixpkgs=/path/to/my/nixpkgs
30</programlisting>
31 <para>
32 The second possibility is to add the package outside of the Nixpkgs
33 tree. For instance, here is how you specify a build of the
34 <link xlink:href="https://www.gnu.org/software/hello/">GNU
35 Hello</link> package directly in
36 <literal>configuration.nix</literal>:
37 </para>
38 <programlisting language="bash">
39environment.systemPackages =
40 let
41 my-hello = with pkgs; stdenv.mkDerivation rec {
42 name = "hello-2.8";
43 src = fetchurl {
44 url = "mirror://gnu/hello/${name}.tar.gz";
45 sha256 = "0wqd8sjmxfskrflaxywc7gqw7sfawrfvdxd9skxawzfgyy0pzdz6";
46 };
47 };
48 in
49 [ my-hello ];
50</programlisting>
51 <para>
52 Of course, you can also move the definition of
53 <literal>my-hello</literal> into a separate Nix expression, e.g.
54 </para>
55 <programlisting language="bash">
56environment.systemPackages = [ (import ./my-hello.nix) ];
57</programlisting>
58 <para>
59 where <literal>my-hello.nix</literal> contains:
60 </para>
61 <programlisting language="bash">
62with import <nixpkgs> {}; # bring all of Nixpkgs into scope
63
64stdenv.mkDerivation rec {
65 name = "hello-2.8";
66 src = fetchurl {
67 url = "mirror://gnu/hello/${name}.tar.gz";
68 sha256 = "0wqd8sjmxfskrflaxywc7gqw7sfawrfvdxd9skxawzfgyy0pzdz6";
69 };
70}
71</programlisting>
72 <para>
73 This allows testing the package easily:
74 </para>
75 <programlisting>
76$ nix-build my-hello.nix
77$ ./result/bin/hello
78Hello, world!
79</programlisting>
80</section>