Packaging/Quirks and Caveats: Difference between revisions

From NixOS Wiki
imported>Mic92
No edit summary
imported>Fadenb
m Syntax highlighting
Line 7: Line 7:
Add <code>autoreconfHook</code> to <code>buildInputs</code> to automatically build software which uses <code>automake</code> and <code>autoconf</code>:
Add <code>autoreconfHook</code> to <code>buildInputs</code> to automatically build software which uses <code>automake</code> and <code>autoconf</code>:


<pre> buildInputs = [ ...  autoreconfHook ];</pre>
<syntaxhighlight lang="nix">
buildInputs = [ ...  autoreconfHook ];
</syntaxhighlight>
Examples in nixpkgs: * [https://github.com/NixOS/nixpkgs/blob/f4c253ff2f68fbe3e302f944e8347233d9dc8c9d/pkgs/tools/networking/samplicator/default.nix samplicator]
Examples in nixpkgs: * [https://github.com/NixOS/nixpkgs/blob/f4c253ff2f68fbe3e302f944e8347233d9dc8c9d/pkgs/tools/networking/samplicator/default.nix samplicator]


=== Package simple python scripts ===
=== Package simple python scripts ===


For scripts like a single Python file, it is not necessary to specify <code>src</code> in <code>mkDerivation</code>. When you want to use <code>buildPythonPackage</code> the sources need to provide a <code>setup.py</code> file which also is overkill for a lot of projects. The default <code>mkDerivation</code> will attempt to unpack your source code. This can be prevented that by applying <code>unpackPhase = &quot;:&quot;;</code> (<code>:</code> is a no-op in shell scripts).
For scripts like a single Python file, it is not necessary to specify <code>src</code> in <code>mkDerivation</code>. When you want to use <code>buildPythonPackage</code> the sources need to provide a <code>setup.py</code> file which also is overkill for a lot of projects. The default <code>mkDerivation</code> will attempt to unpack your source code. This can be prevented that by applying <code>unpackPhase = ":";</code> (<code>:</code> is a no-op in shell scripts).


<pre class="nix">myscript-package = pkgs.stdenv.mkDerivation {
<syntaxhighlight lang="nix">myscript-package = pkgs.stdenv.mkDerivation {
   name = &quot;myscript&quot;;
   name = "myscript";
   buildInputs = [
   buildInputs = [
     (pkgs.python36.withPackages (pythonPackages: with pythonPackages; [
     (pkgs.python36.withPackages (pythonPackages: with pythonPackages; [
Line 23: Line 25:
     ]))
     ]))
   ];
   ];
   unpackPhase = &quot;:&quot;;
   unpackPhase = ":";
   installPhase = &quot;install -m755 -D ${./myscript.py} $out/bin/myscript&quot;;
   installPhase = "install -m755 -D ${./myscript.py} $out/bin/myscript";
};</pre>
};</syntaxhighlight>
<code>nix</code> will automatically replace shebangs, for ex. <code>#!/usr/bin/env python3</code> with dependencies given in <code>buildInputs</code>. As the derivation got <code>pkgs.python36.withPackages (...)</code> in <code>buildInputs</code>, it will create a [https://virtualenv.pypa.io/en/stable/ virtualenv]-like python wrapper. The python wrapper will have all specified dependencies and will be used to call the script.
<code>nix</code> will automatically replace shebangs, for ex. <code>#!/usr/bin/env python3</code> with dependencies given in <code>buildInputs</code>. As the derivation got <code>pkgs.python36.withPackages (...)</code> in <code>buildInputs</code>, it will create a [https://virtualenv.pypa.io/en/stable/ virtualenv]-like python wrapper. The python wrapper will have all specified dependencies and will be used to call the script.



Revision as of 13:20, 27 August 2017

A good start for packaging your first piece if software is the Quickstart Chapter in the nixpkgs manual Also see the Generic Algorithm on doing Packaging


Build software with Autotools

Add autoreconfHook to buildInputs to automatically build software which uses automake and autoconf:

buildInputs = [ ...  autoreconfHook ];

Examples in nixpkgs: * samplicator

Package simple python scripts

For scripts like a single Python file, it is not necessary to specify src in mkDerivation. When you want to use buildPythonPackage the sources need to provide a setup.py file which also is overkill for a lot of projects. The default mkDerivation will attempt to unpack your source code. This can be prevented that by applying unpackPhase = ":"; (: is a no-op in shell scripts).

myscript-package = pkgs.stdenv.mkDerivation {
  name = "myscript";
  buildInputs = [
    (pkgs.python36.withPackages (pythonPackages: with pythonPackages; [
      consul
      six
      requests
    ]))
  ];
  unpackPhase = ":";
  installPhase = "install -m755 -D ${./myscript.py} $out/bin/myscript";
};

nix will automatically replace shebangs, for ex. #!/usr/bin/env python3 with dependencies given in buildInputs. As the derivation got pkgs.python36.withPackages (...) in buildInputs, it will create a virtualenv-like python wrapper. The python wrapper will have all specified dependencies and will be used to call the script.

In NixOS, the package can be put into environment.systemPackages, and myscript will be available as a global command.

Source: nh2 @ StackOverflow

A more lightweight alternative is to use nix-shell in the shebang line as described in this gist

Caveats

After packaging software and successfully generating an executable some functions of the package might still not work. This is a collection of error and how to fix them:


GLib-GIO-Message: Using the 'memory' GSettings backend. Your settings will not be saved or shared with

Fixed by adding wrapGAppsHook to buildInputs:

buildInputs = [ ...  wrapGAppsHook ];

Sample PR in nixpkgs:

ImportError: libstdc++.so.6: cannot open shared object file: No such file

This can happen when importing python libraries: Solution: add ${stdenv.cc.cc.lib}/lib/libstdc++.so.6 to the LD_LIBRARY_PATH.