From e86ef91b58d23d28c327b59c9ed4b6041bd8c9cb Mon Sep 17 00:00:00 2001
From: Doron Behar <doron.behar@gmail.com>
Date: Fri, 4 Oct 2019 10:13:42 +0300
Subject: [PATCH 1/8] gaia: init at 2.4.5

---
 pkgs/development/libraries/gaia/default.nix | 81 +++++++++++++++++++++
 pkgs/top-level/all-packages.nix             |  2 +
 2 files changed, 83 insertions(+)
 create mode 100644 pkgs/development/libraries/gaia/default.nix

diff --git a/pkgs/development/libraries/gaia/default.nix b/pkgs/development/libraries/gaia/default.nix
new file mode 100644
index 000000000000..d35feb62158a
--- /dev/null
+++ b/pkgs/development/libraries/gaia/default.nix
@@ -0,0 +1,81 @@
+{ stdenv
+, lib
+, fetchFromGitHub
+, libyaml
+, swig
+, pkgconfig
+, wafHook
+, makeWrapper
+, qt4
+, python
+, pythonSupport ? true
+# Default to false since it brakes the build
+, stlfacadeSupport ? false
+, assertsSupport ? true
+, cyclopsSupport ? true
+}:
+
+assert pythonSupport -> python != null;
+
+stdenv.mkDerivation rec {
+  pname = "gaia";
+  version = "2.4.5";
+
+  src = fetchFromGitHub {
+    owner = "MTG";
+    repo = "gaia";
+    rev = "v${version}";
+    sha256 = "12jxb354s2dblr2ghnl3w05m23jgzvrrgywfj8jaa32j3gw48fv2";
+  };
+
+  # Fix installation error when waf tries to put files in /etc/
+  prePatch = ''
+  '' + lib.optionalString cyclopsSupport ''
+    substituteInPlace src/wscript \
+      --replace "/etc/cyclops" "$out/etc/cyclops" \
+      --replace "/etc/init.d" "$out/etc/init.d"
+  '';
+
+  # This is not exactly specified in upstream's README but it's needed by the
+  # resultings $out/bin/gaiafusion script
+  pythonEnv = (if pythonSupport then
+    python.withPackages(ps: with ps; [
+      pyyaml
+    ])
+  else null);
+
+  nativeBuildInputs = [
+    wafHook
+    pkgconfig
+    swig
+    makeWrapper
+  ];
+  buildInputs = [
+    libyaml
+    qt4
+  ]
+    ++ lib.optionals (pythonSupport) [
+      pythonEnv
+    ]
+  ;
+  wafConfigureFlags = [
+  ]
+    ++ lib.optionals (pythonSupport) [ "--with-python-bindings" ]
+    ++ lib.optionals (stlfacadeSupport) [ "--with-stlfacade" ]
+    ++ lib.optionals (assertsSupport) [ "--with-asserts" ]
+    ++ lib.optionals (cyclopsSupport) [ "--with-cyclops" ]
+  ;
+  # only gaiafusion is a python executable that needs patchShebangs
+  postInstall = (if pythonSupport then ''
+    # We can't use patchShebangs because it will use bare bones $python/bin/python
+    # and we need a python environment with pyyaml
+    wrapProgram $out/bin/gaiafusion --prefix PYTHONPATH : $out/${python.sitePackages}:${pythonEnv}/${python.sitePackages}
+  '' else "");
+
+  meta = with lib; {
+    homepage = "https://github.com/MTG/gaia";
+    description = "C++ library with python bindings which implements similarity measures and classifications on the results of audio analysis, and generates classification models that Essentia can use to compute high-level description of music";
+    maintainers = with maintainers; [ doronbehar ];
+    license = licenses.agpl3;
+  };
+}
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 4ac2434f9627..47171eefba5b 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -841,6 +841,8 @@ in
 
   genymotion = callPackage ../development/mobile/genymotion { };
 
+  gaia = callPackage ../development/libraries/gaia { };
+
   gamecube-tools = callPackage ../development/tools/gamecube-tools { };
 
   gams = callPackage ../tools/misc/gams (config.gams or {});

From 94eafa02bf2a334b713e6814452e3c6182fadd84 Mon Sep 17 00:00:00 2001
From: Doron Behar <doron.behar@gmail.com>
Date: Sat, 5 Oct 2019 07:58:56 +0300
Subject: [PATCH 2/8] Link upstream issue about stlfacadeSupport defaulting
 false

---
 pkgs/development/libraries/gaia/default.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/development/libraries/gaia/default.nix b/pkgs/development/libraries/gaia/default.nix
index d35feb62158a..341c4ad911c1 100644
--- a/pkgs/development/libraries/gaia/default.nix
+++ b/pkgs/development/libraries/gaia/default.nix
@@ -9,7 +9,7 @@
 , qt4
 , python
 , pythonSupport ? true
-# Default to false since it brakes the build
+# Default to false since it breaks the build, see https://github.com/MTG/gaia/issues/11
 , stlfacadeSupport ? false
 , assertsSupport ? true
 , cyclopsSupport ? true

From f9391a2ca8cabd9bf8ca3311a05bd284127e2f0b Mon Sep 17 00:00:00 2001
From: Doron Behar <doron.behar@gmail.com>
Date: Sat, 5 Oct 2019 08:14:40 +0300
Subject: [PATCH 3/8] Use upstream's short description in meta

---
 pkgs/development/libraries/gaia/default.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/development/libraries/gaia/default.nix b/pkgs/development/libraries/gaia/default.nix
index 341c4ad911c1..fae96568dcf1 100644
--- a/pkgs/development/libraries/gaia/default.nix
+++ b/pkgs/development/libraries/gaia/default.nix
@@ -74,7 +74,7 @@ stdenv.mkDerivation rec {
 
   meta = with lib; {
     homepage = "https://github.com/MTG/gaia";
-    description = "C++ library with python bindings which implements similarity measures and classifications on the results of audio analysis, and generates classification models that Essentia can use to compute high-level description of music";
+    description = "General library to work with points in a semimetric space";
     maintainers = with maintainers; [ doronbehar ];
     license = licenses.agpl3;
   };

From cf6dd6cc0c27428746ee372aa55760e70f1c1c43 Mon Sep 17 00:00:00 2001
From: Doron Behar <doron.behar@gmail.com>
Date: Sat, 5 Oct 2019 08:15:16 +0300
Subject: [PATCH 4/8] Use lib.optionalString for postInstall

---
 pkgs/development/libraries/gaia/default.nix | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/pkgs/development/libraries/gaia/default.nix b/pkgs/development/libraries/gaia/default.nix
index fae96568dcf1..0809f4d62a1f 100644
--- a/pkgs/development/libraries/gaia/default.nix
+++ b/pkgs/development/libraries/gaia/default.nix
@@ -66,11 +66,11 @@ stdenv.mkDerivation rec {
     ++ lib.optionals (cyclopsSupport) [ "--with-cyclops" ]
   ;
   # only gaiafusion is a python executable that needs patchShebangs
-  postInstall = (if pythonSupport then ''
+  postInstall = lib.optionalString ''
     # We can't use patchShebangs because it will use bare bones $python/bin/python
     # and we need a python environment with pyyaml
     wrapProgram $out/bin/gaiafusion --prefix PYTHONPATH : $out/${python.sitePackages}:${pythonEnv}/${python.sitePackages}
-  '' else "");
+  '';
 
   meta = with lib; {
     homepage = "https://github.com/MTG/gaia";

From a246b74f21765bf08b57c6e1ca2d5bef6226ca1c Mon Sep 17 00:00:00 2001
From: Doron Behar <doron.behar@gmail.com>
Date: Sat, 5 Oct 2019 08:38:15 +0300
Subject: [PATCH 5/8] Improve pythonSupport handling

Use pythonPackages instead of just python and add an attribute to
python-packages.nix.
---
 pkgs/development/libraries/gaia/default.nix | 14 +++++++-------
 pkgs/top-level/python-packages.nix          |  5 +++++
 2 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/pkgs/development/libraries/gaia/default.nix b/pkgs/development/libraries/gaia/default.nix
index 0809f4d62a1f..9195317b7b4e 100644
--- a/pkgs/development/libraries/gaia/default.nix
+++ b/pkgs/development/libraries/gaia/default.nix
@@ -7,15 +7,15 @@
 , wafHook
 , makeWrapper
 , qt4
-, python
-, pythonSupport ? true
+, pythonPackages
+, pythonSupport ? false
 # Default to false since it breaks the build, see https://github.com/MTG/gaia/issues/11
 , stlfacadeSupport ? false
 , assertsSupport ? true
 , cyclopsSupport ? true
 }:
 
-assert pythonSupport -> python != null;
+assert pythonSupport -> pythonPackages != null;
 
 stdenv.mkDerivation rec {
   pname = "gaia";
@@ -37,9 +37,9 @@ stdenv.mkDerivation rec {
   '';
 
   # This is not exactly specified in upstream's README but it's needed by the
-  # resultings $out/bin/gaiafusion script
+  # resulting $out/bin/gaiafusion script
   pythonEnv = (if pythonSupport then
-    python.withPackages(ps: with ps; [
+    pythonPackages.python.withPackages(ps: with ps; [
       pyyaml
     ])
   else null);
@@ -66,10 +66,10 @@ stdenv.mkDerivation rec {
     ++ lib.optionals (cyclopsSupport) [ "--with-cyclops" ]
   ;
   # only gaiafusion is a python executable that needs patchShebangs
-  postInstall = lib.optionalString ''
+  postInstall = lib.optionalString (pythonSupport) ''
     # We can't use patchShebangs because it will use bare bones $python/bin/python
     # and we need a python environment with pyyaml
-    wrapProgram $out/bin/gaiafusion --prefix PYTHONPATH : $out/${python.sitePackages}:${pythonEnv}/${python.sitePackages}
+    wrapProgram $out/bin/gaiafusion --prefix PYTHONPATH : $out/${pythonPackages.python.sitePackages}:${pythonEnv}/${pythonPackages.python.sitePackages}
   '';
 
   meta = with lib; {
diff --git a/pkgs/top-level/python-packages.nix b/pkgs/top-level/python-packages.nix
index c269c830fdfb..3c9786fa45ba 100644
--- a/pkgs/top-level/python-packages.nix
+++ b/pkgs/top-level/python-packages.nix
@@ -2372,6 +2372,11 @@ in {
 
   fx2 = callPackage ../development/python-modules/fx2 { };
 
+  gaia = (toPythonModule (pkgs.gaia.override {
+    pythonPackages = self;
+    pythonSupport = true;
+  }));
+
   gateone = callPackage ../development/python-modules/gateone { };
 
   GeoIP = callPackage ../development/python-modules/GeoIP { };

From 97769d4cfd9417c6669ef03ac70b70a3397871d9 Mon Sep 17 00:00:00 2001
From: Doron Behar <doron.behar@gmail.com>
Date: Sat, 5 Oct 2019 10:20:54 +0300
Subject: [PATCH 6/8] Add platforms to meta

---
 pkgs/development/libraries/gaia/default.nix | 1 +
 1 file changed, 1 insertion(+)

diff --git a/pkgs/development/libraries/gaia/default.nix b/pkgs/development/libraries/gaia/default.nix
index 9195317b7b4e..7565ee7a06e6 100644
--- a/pkgs/development/libraries/gaia/default.nix
+++ b/pkgs/development/libraries/gaia/default.nix
@@ -76,6 +76,7 @@ stdenv.mkDerivation rec {
     homepage = "https://github.com/MTG/gaia";
     description = "General library to work with points in a semimetric space";
     maintainers = with maintainers; [ doronbehar ];
+    platforms = platforms.all;
     license = licenses.agpl3;
   };
 }

From a45c9f541c35ae52ef72afdce41402e6096da87d Mon Sep 17 00:00:00 2001
From: Doron Behar <doron.behar@gmail.com>
Date: Tue, 8 Oct 2019 10:10:31 +0300
Subject: [PATCH 7/8] Make gaia unsupported with python2

---
 pkgs/top-level/python-packages.nix | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/pkgs/top-level/python-packages.nix b/pkgs/top-level/python-packages.nix
index 3c9786fa45ba..534632ea1db3 100644
--- a/pkgs/top-level/python-packages.nix
+++ b/pkgs/top-level/python-packages.nix
@@ -2372,7 +2372,8 @@ in {
 
   fx2 = callPackage ../development/python-modules/fx2 { };
 
-  gaia = (toPythonModule (pkgs.gaia.override {
+  # gaia isn't supported with python3 and it's not available from pypi
+  gaia = disabledIf (isPyPy || isPy3k) (toPythonModule (pkgs.gaia.override {
     pythonPackages = self;
     pythonSupport = true;
   }));

From d4c7ab3d85bce04d59a398136a9e89e93297d2be Mon Sep 17 00:00:00 2001
From: Doron Behar <doron.behar@gmail.com>
Date: Tue, 8 Oct 2019 10:20:53 +0300
Subject: [PATCH 8/8] Use wrapPython in postFixup instead of wrapProgram

---
 pkgs/development/libraries/gaia/default.nix | 43 +++++++++++----------
 1 file changed, 23 insertions(+), 20 deletions(-)

diff --git a/pkgs/development/libraries/gaia/default.nix b/pkgs/development/libraries/gaia/default.nix
index 7565ee7a06e6..9411f76908eb 100644
--- a/pkgs/development/libraries/gaia/default.nix
+++ b/pkgs/development/libraries/gaia/default.nix
@@ -36,41 +36,44 @@ stdenv.mkDerivation rec {
       --replace "/etc/init.d" "$out/etc/init.d"
   '';
 
-  # This is not exactly specified in upstream's README but it's needed by the
-  # resulting $out/bin/gaiafusion script
-  pythonEnv = (if pythonSupport then
-    pythonPackages.python.withPackages(ps: with ps; [
-      pyyaml
-    ])
-  else null);
-
   nativeBuildInputs = [
     wafHook
     pkgconfig
     swig
-    makeWrapper
-  ];
+  ]
+    # The gaiafusion binary inside $out/bin needs a shebangs patch, and
+    # wrapping with the appropriate $PYTHONPATH
+    ++ lib.optionals (pythonSupport) [
+      pythonPackages.wrapPython
+    ]
+  ;
+
   buildInputs = [
     libyaml
     qt4
-  ]
+  ];
+
+  propagatedBuildInputs = []
     ++ lib.optionals (pythonSupport) [
-      pythonEnv
+      # This is not exactly specified in upstream's README but it's needed by the
+      # resulting $out/bin/gaiafusion script
+      pythonPackages.pyyaml
     ]
   ;
-  wafConfigureFlags = [
-  ]
+
+  wafConfigureFlags = []
     ++ lib.optionals (pythonSupport) [ "--with-python-bindings" ]
     ++ lib.optionals (stlfacadeSupport) [ "--with-stlfacade" ]
     ++ lib.optionals (assertsSupport) [ "--with-asserts" ]
     ++ lib.optionals (cyclopsSupport) [ "--with-cyclops" ]
   ;
-  # only gaiafusion is a python executable that needs patchShebangs
-  postInstall = lib.optionalString (pythonSupport) ''
-    # We can't use patchShebangs because it will use bare bones $python/bin/python
-    # and we need a python environment with pyyaml
-    wrapProgram $out/bin/gaiafusion --prefix PYTHONPATH : $out/${pythonPackages.python.sitePackages}:${pythonEnv}/${pythonPackages.python.sitePackages}
-  '';
+
+  postFixup = ''
+  ''
+    + lib.optionalString pythonSupport ''
+      wrapPythonPrograms
+    ''
+  ;
 
   meta = with lib; {
     homepage = "https://github.com/MTG/gaia";