nixos/doc: explain how to run appimages

This commit is contained in:
Guillaume Girol 2022-06-06 12:00:00 +00:00
parent 236cc2971a
commit fd3fc309f4
2 changed files with 107 additions and 44 deletions

View File

@ -1,11 +1,18 @@
# Adding Custom Packages {#sec-custom-packages} # Adding Custom Packages {#sec-custom-packages}
It's possible that a package you need is not available in NixOS. In that 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 case, you can do two things. Either you can package it with Nix, or you can try
repository, add the package to your clone, and (optionally) submit a to use prebuilt packages from upstream. Due to the peculiarities of NixOS, it
patch or pull request to have it accepted into the main Nixpkgs repository. is important to note that building software from source is often easier than
This is described in detail in the [Nixpkgs manual](https://nixos.org/nixpkgs/manual). using pre-built executables.
In short, you clone Nixpkgs:
## Building with Nix {#sec-custom-packages-nix}
This can be done either in-tree or out-of-tree. For an in-tree build, 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 [Nixpkgs
manual](https://nixos.org/nixpkgs/manual). In short, you clone Nixpkgs:
```ShellSession ```ShellSession
$ git clone https://github.com/NixOS/nixpkgs $ git clone https://github.com/NixOS/nixpkgs
@ -72,3 +79,21 @@ $ nix-build my-hello.nix
$ ./result/bin/hello $ ./result/bin/hello
Hello, world! Hello, world!
``` ```
## Using pre-built executables {#sec-custom-packages-prebuilt}
Most pre-built executables will not work on NixOS. There are two notable
exceptions: flatpaks and AppImages. For flatpaks see the [dedicated
section](#module-services-flatpak). AppImages will not run "as-is" on NixOS.
First you need to install `appimage-run`: add to `/etc/nixos/configuration.nix`
```nix
environment.systemPackages = [ pkgs.appimage-run ];
```
Then instead of running the AppImage "as-is", run `appimage-run foo.appimage`.
To make other pre-built executables work on NixOS, you need to package them
with Nix and special helpers like `autoPatchelfHook` or `buildFHSUserEnv`. See
the [Nixpkgs manual](https://nixos.org/nixpkgs/manual) for details. This
is complex and often doing a source build is easier.

View File

@ -2,40 +2,50 @@
<title>Adding Custom Packages</title> <title>Adding Custom Packages</title>
<para> <para>
Its possible that a package you need is not available in NixOS. In Its 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 that case, you can do two things. Either you can package it with
repository, add the package to your clone, and (optionally) submit a Nix, or you can try to use prebuilt packages from upstream. Due to
patch or pull request to have it accepted into the main Nixpkgs the peculiarities of NixOS, it is important to note that building
repository. This is described in detail in the software from source is often easier than using pre-built
<link xlink:href="https://nixos.org/nixpkgs/manual">Nixpkgs executables.
manual</link>. In short, you clone Nixpkgs:
</para> </para>
<programlisting> <section xml:id="sec-custom-packages-nix">
<title>Building with Nix</title>
<para>
This can be done either in-tree or out-of-tree. For an in-tree
build, 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 $ git clone https://github.com/NixOS/nixpkgs
$ cd nixpkgs $ cd nixpkgs
</programlisting> </programlisting>
<para> <para>
Then you write and test the package as described in the Nixpkgs Then you write and test the package as described in the Nixpkgs
manual. Finally, you add it to manual. Finally, you add it to
<xref linkend="opt-environment.systemPackages" />, e.g. <xref linkend="opt-environment.systemPackages" />, e.g.
</para> </para>
<programlisting language="bash"> <programlisting language="bash">
environment.systemPackages = [ pkgs.my-package ]; environment.systemPackages = [ pkgs.my-package ];
</programlisting> </programlisting>
<para> <para>
and you run <literal>nixos-rebuild</literal>, specifying your own and you run <literal>nixos-rebuild</literal>, specifying your own
Nixpkgs tree: Nixpkgs tree:
</para> </para>
<programlisting> <programlisting>
# nixos-rebuild switch -I nixpkgs=/path/to/my/nixpkgs # nixos-rebuild switch -I nixpkgs=/path/to/my/nixpkgs
</programlisting> </programlisting>
<para> <para>
The second possibility is to add the package outside of the Nixpkgs The second possibility is to add the package outside of the
tree. For instance, here is how you specify a build 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 <link xlink:href="https://www.gnu.org/software/hello/">GNU
Hello</link> package directly in Hello</link> package directly in
<literal>configuration.nix</literal>: <literal>configuration.nix</literal>:
</para> </para>
<programlisting language="bash"> <programlisting language="bash">
environment.systemPackages = environment.systemPackages =
let let
my-hello = with pkgs; stdenv.mkDerivation rec { my-hello = with pkgs; stdenv.mkDerivation rec {
@ -48,17 +58,17 @@ environment.systemPackages =
in in
[ my-hello ]; [ my-hello ];
</programlisting> </programlisting>
<para> <para>
Of course, you can also move the definition of Of course, you can also move the definition of
<literal>my-hello</literal> into a separate Nix expression, e.g. <literal>my-hello</literal> into a separate Nix expression, e.g.
</para> </para>
<programlisting language="bash"> <programlisting language="bash">
environment.systemPackages = [ (import ./my-hello.nix) ]; environment.systemPackages = [ (import ./my-hello.nix) ];
</programlisting> </programlisting>
<para> <para>
where <literal>my-hello.nix</literal> contains: where <literal>my-hello.nix</literal> contains:
</para> </para>
<programlisting language="bash"> <programlisting language="bash">
with import &lt;nixpkgs&gt; {}; # bring all of Nixpkgs into scope with import &lt;nixpkgs&gt; {}; # bring all of Nixpkgs into scope
stdenv.mkDerivation rec { stdenv.mkDerivation rec {
@ -69,12 +79,40 @@ stdenv.mkDerivation rec {
}; };
} }
</programlisting> </programlisting>
<para> <para>
This allows testing the package easily: This allows testing the package easily:
</para> </para>
<programlisting> <programlisting>
$ nix-build my-hello.nix $ nix-build my-hello.nix
$ ./result/bin/hello $ ./result/bin/hello
Hello, world! Hello, world!
</programlisting> </programlisting>
</section>
<section xml:id="sec-custom-packages-prebuilt">
<title>Using pre-built executables</title>
<para>
Most pre-built executables will not work on NixOS. There are two
notable exceptions: flatpaks and AppImages. For flatpaks see the
<link linkend="module-services-flatpak">dedicated section</link>.
AppImages will not run <quote>as-is</quote> on NixOS. First you
need to install <literal>appimage-run</literal>: add to
<literal>/etc/nixos/configuration.nix</literal>
</para>
<programlisting language="bash">
environment.systemPackages = [ pkgs.appimage-run ];
</programlisting>
<para>
Then instead of running the AppImage <quote>as-is</quote>, run
<literal>appimage-run foo.appimage</literal>.
</para>
<para>
To make other pre-built executables work on NixOS, you need to
package them with Nix and special helpers like
<literal>autoPatchelfHook</literal> or
<literal>buildFHSUserEnv</literal>. See the
<link xlink:href="https://nixos.org/nixpkgs/manual">Nixpkgs
manual</link> for details. This is complex and often doing a
source build is easier.
</para>
</section>
</section> </section>