summary refs log tree commit diff
path: root/nixos/doc/manual/from_md/configuration/adding-custom-packages.section.xml
blob: 4fa40d61966e7ea8dd53f7ff8bf4c8ddd1c53541 (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
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xml:id="sec-custom-packages">
  <title>Adding Custom Packages</title>
  <para>
    It’s possible that a package you need is not available in NixOS. In
    that case, you can do two things. First, you can clone the Nixpkgs
    repository, add the package to your clone, and (optionally) submit a
    patch or pull request to have it accepted into the main Nixpkgs
    repository. This is described in detail in the
    <link xlink:href="https://nixos.org/nixpkgs/manual">Nixpkgs
    manual</link>. In short, you clone Nixpkgs:
  </para>
  <programlisting>
$ git clone https://github.com/NixOS/nixpkgs
$ cd nixpkgs
</programlisting>
  <para>
    Then you write and test the package as described in the Nixpkgs
    manual. Finally, you add it to
    <xref linkend="opt-environment.systemPackages" />, e.g.
  </para>
  <programlisting language="bash">
environment.systemPackages = [ pkgs.my-package ];
</programlisting>
  <para>
    and you run <literal>nixos-rebuild</literal>, specifying your own
    Nixpkgs tree:
  </para>
  <programlisting>
# nixos-rebuild switch -I nixpkgs=/path/to/my/nixpkgs
</programlisting>
  <para>
    The second possibility is to add the package outside of the Nixpkgs
    tree. For instance, here is how you specify a build of the
    <link xlink:href="https://www.gnu.org/software/hello/">GNU
    Hello</link> package directly in
    <literal>configuration.nix</literal>:
  </para>
  <programlisting language="bash">
environment.systemPackages =
  let
    my-hello = with pkgs; stdenv.mkDerivation rec {
      name = &quot;hello-2.8&quot;;
      src = fetchurl {
        url = &quot;mirror://gnu/hello/${name}.tar.gz&quot;;
        sha256 = &quot;0wqd8sjmxfskrflaxywc7gqw7sfawrfvdxd9skxawzfgyy0pzdz6&quot;;
      };
    };
  in
  [ my-hello ];
</programlisting>
  <para>
    Of course, you can also move the definition of
    <literal>my-hello</literal> into a separate Nix expression, e.g.
  </para>
  <programlisting language="bash">
environment.systemPackages = [ (import ./my-hello.nix) ];
</programlisting>
  <para>
    where <literal>my-hello.nix</literal> contains:
  </para>
  <programlisting language="bash">
with import &lt;nixpkgs&gt; {}; # bring all of Nixpkgs into scope

stdenv.mkDerivation rec {
  name = &quot;hello-2.8&quot;;
  src = fetchurl {
    url = &quot;mirror://gnu/hello/${name}.tar.gz&quot;;
    sha256 = &quot;0wqd8sjmxfskrflaxywc7gqw7sfawrfvdxd9skxawzfgyy0pzdz6&quot;;
  };
}
</programlisting>
  <para>
    This allows testing the package easily:
  </para>
  <programlisting>
$ nix-build my-hello.nix
$ ./result/bin/hello
Hello, world!
</programlisting>
</section>