···
<title>Adding Custom Packages</title>
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:
5
+
that case, you can do two things. Either you can package it with
6
+
Nix, or you can try to use prebuilt packages from upstream. Due to
7
+
the peculiarities of NixOS, it is important to note that building
8
+
software from source is often easier than using pre-built
11
+
<section xml:id="sec-custom-packages-nix">
12
+
<title>Building with Nix</title>
14
+
This can be done either in-tree or out-of-tree. For an in-tree
15
+
build, you can clone the Nixpkgs repository, add the package to
16
+
your clone, and (optionally) submit a patch or pull request to
17
+
have it accepted into the main Nixpkgs repository. This is
18
+
described in detail in the
19
+
<link xlink:href="https://nixos.org/nixpkgs/manual">Nixpkgs
20
+
manual</link>. In short, you clone Nixpkgs:
$ git clone https://github.com/NixOS/nixpkgs
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.
21
-
<programlisting language="bash">
27
+
Then you write and test the package as described in the Nixpkgs
28
+
manual. Finally, you add it to
29
+
<xref linkend="opt-environment.systemPackages" />, e.g.
31
+
<programlisting language="bash">
environment.systemPackages = [ pkgs.my-package ];
25
-
and you run <literal>nixos-rebuild</literal>, specifying your own
35
+
and you run <literal>nixos-rebuild</literal>, specifying your own
# nixos-rebuild switch -I nixpkgs=/path/to/my/nixpkgs
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>:
38
-
<programlisting language="bash">
42
+
The second possibility is to add the package outside of the
43
+
Nixpkgs tree. For instance, here is how you specify a build of the
44
+
<link xlink:href="https://www.gnu.org/software/hello/">GNU
45
+
Hello</link> package directly in
46
+
<literal>configuration.nix</literal>:
48
+
<programlisting language="bash">
environment.systemPackages =
my-hello = with pkgs; stdenv.mkDerivation rec {
···
52
-
Of course, you can also move the definition of
53
-
<literal>my-hello</literal> into a separate Nix expression, e.g.
55
-
<programlisting language="bash">
62
+
Of course, you can also move the definition of
63
+
<literal>my-hello</literal> into a separate Nix expression, e.g.
65
+
<programlisting language="bash">
environment.systemPackages = [ (import ./my-hello.nix) ];
59
-
where <literal>my-hello.nix</literal> contains:
61
-
<programlisting language="bash">
69
+
where <literal>my-hello.nix</literal> contains:
71
+
<programlisting language="bash">
with import <nixpkgs> {}; # bring all of Nixpkgs into scope
stdenv.mkDerivation rec {
···
73
-
This allows testing the package easily:
83
+
This allows testing the package easily:
91
+
<section xml:id="sec-custom-packages-prebuilt">
92
+
<title>Using pre-built executables</title>
94
+
Most pre-built executables will not work on NixOS. There are two
95
+
notable exceptions: flatpaks and AppImages. For flatpaks see the
96
+
<link linkend="module-services-flatpak">dedicated section</link>.
97
+
AppImages will not run <quote>as-is</quote> on NixOS. First you
98
+
need to install <literal>appimage-run</literal>: add to
99
+
<literal>/etc/nixos/configuration.nix</literal>
101
+
<programlisting language="bash">
102
+
environment.systemPackages = [ pkgs.appimage-run ];
105
+
Then instead of running the AppImage <quote>as-is</quote>, run
106
+
<literal>appimage-run foo.appimage</literal>.
109
+
To make other pre-built executables work on NixOS, you need to
110
+
package them with Nix and special helpers like
111
+
<literal>autoPatchelfHook</literal> or
112
+
<literal>buildFHSUserEnv</literal>. See the
113
+
<link xlink:href="https://nixos.org/nixpkgs/manual">Nixpkgs
114
+
manual</link> for details. This is complex and often doing a
115
+
source build is easier.