From de3d26165f15219dfd007798b2c8085ba4795c30 Mon Sep 17 00:00:00 2001
From: Philipp Hausmann <ph_git@314.ch>
Date: Thu, 10 Aug 2017 09:46:29 +0200
Subject: [PATCH 1/5] FusionInventory: Init at 2.3.18

---
 .../monitoring/fusion-inventory/default.nix   | 67 ++++++++++++++
 .../remove_software_test.patch                | 25 ++++++
 pkgs/top-level/all-packages.nix               |  2 +
 pkgs/top-level/perl-packages.nix              | 90 +++++++++++++++++++
 4 files changed, 184 insertions(+)
 create mode 100644 pkgs/servers/monitoring/fusion-inventory/default.nix
 create mode 100644 pkgs/servers/monitoring/fusion-inventory/remove_software_test.patch

diff --git a/pkgs/servers/monitoring/fusion-inventory/default.nix b/pkgs/servers/monitoring/fusion-inventory/default.nix
new file mode 100644
index 00000000000..8b3f902b00c
--- /dev/null
+++ b/pkgs/servers/monitoring/fusion-inventory/default.nix
@@ -0,0 +1,67 @@
+{ stdenv, fetchurl, gnused, buildPerlPackage, perl, perlPackages
+}:
+
+buildPerlPackage rec {
+  version = "2.3.18";
+  name = "FusionInventory-Agent-${version}";
+  src = fetchurl {
+    url = "mirror://cpan/authors/id/G/GR/GROUSSE/${name}.tar.gz";
+    sha256 = "543d96fa61b8f2a2bc599fe9f694f19d1f2094dc5506bc514d00b8a445bc5401";
+  };
+
+  patches = [ ./remove_software_test.patch ];
+
+  postPatch = ''
+    patchShebangs bin
+  '';
+
+  buildTools = [];
+  buildInputs = with perlPackages; [
+    CGI
+    DataStructureUtil
+    FileCopyRecursive
+    HTTPProxy
+    HTTPServerSimple
+    HTTPServerSimpleAuthen
+    IOCapture
+    IOSocketSSL
+    IPCRun
+    JSON
+    LWPProtocolhttps
+    NetSNMP
+    TestCompile
+    TestDeep
+    TestException
+    TestMockModule
+    TestMockObject
+    TestNoWarnings
+  ];
+  propagatedBuildInputs = with perlPackages; [
+    FileWhich
+    LWP
+    NetIP
+    TextTemplate
+    UNIVERSALrequire
+    XMLTreePP
+  ];
+
+  installPhase = ''
+    mkdir -p $out
+
+    cp -r bin $out
+    cp -r lib $out
+
+    for cur in $out/bin/*; do
+      sed -e "s|./lib|$out/lib|" -i "$cur"
+    done
+  '';
+
+  outputs = [ "out" ];
+
+  meta = with stdenv.lib; {
+    homepage = http://www.fusioninventory.org;
+    description = "FusionInventory unified Agent for UNIX, Linux, Windows and MacOSX";
+    license = stdenv.lib.licenses.gpl2;
+    maintainers = [ maintainers.phile314 ];
+  };
+}
diff --git a/pkgs/servers/monitoring/fusion-inventory/remove_software_test.patch b/pkgs/servers/monitoring/fusion-inventory/remove_software_test.patch
new file mode 100644
index 00000000000..ba6fd54eb18
--- /dev/null
+++ b/pkgs/servers/monitoring/fusion-inventory/remove_software_test.patch
@@ -0,0 +1,25 @@
+diff --git a/t/apps/agent.t b/t/apps/agent.t
+index c0f6fc52f..c83837d70 100755
+--- a/t/apps/agent.t
++++ b/t/apps/agent.t
+@@ -12,7 +12,7 @@ use XML::TreePP;
+ use FusionInventory::Agent::Tools;
+ use FusionInventory::Test::Utils;
+ 
+-plan tests => 34;
++plan tests => 33;
+ 
+ my ($content, $out, $err, $rc);
+ 
+@@ -72,11 +72,6 @@ subtest "first inventory execution and content" => sub {
+ };
+ 
+ ok(
+-    exists $content->{REQUEST}->{CONTENT}->{SOFTWARES},
+-    'inventory has software'
+-);
+-
+-ok(
+     exists $content->{REQUEST}->{CONTENT}->{ENVS},
+     'inventory has environment variables'
+ );
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index d406141c41d..42d29de1d76 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -11369,6 +11369,8 @@ with pkgs;
     openssl = openssl_1_0_2;
   };
 
+  fusionInventory = callPackage ../servers/monitoring/fusion-inventory { };
+
   gatling = callPackage ../servers/http/gatling { };
 
   glabels = callPackage ../applications/graphics/glabels { };
diff --git a/pkgs/top-level/perl-packages.nix b/pkgs/top-level/perl-packages.nix
index ed78f67739e..91cd9fc0575 100644
--- a/pkgs/top-level/perl-packages.nix
+++ b/pkgs/top-level/perl-packages.nix
@@ -549,6 +549,19 @@ let self = _self // overrides; _self = with self; {
     };
   };
 
+  AuthenSimple = buildPerlPackage rec {
+    name = "Authen-Simple-0.5";
+    src = fetchurl {
+      url = "mirror://cpan/authors/id/C/CH/CHANSEN/${name}.tar.gz";
+      sha256 = "02cddab47f8bf1a1cbd4c9bf8d258f6d05111499c33f8315e7244812f72613aa";
+    };
+    propagatedBuildInputs = [ ClassAccessor ClassDataInheritable CryptPasswdMD5 ParamsValidate ];
+    meta = {
+      description = "Simple Authentication";
+      license = with stdenv.lib.licenses; [ artistic1 gpl1Plus ];
+    };
+  };
+
   autobox = buildPerlPackage rec {
     name = "autobox-2.84";
     src = fetchurl {
@@ -3177,6 +3190,19 @@ let self = _self // overrides; _self = with self; {
     };
   };
 
+  DataStructureUtil = buildPerlPackage rec {
+    name = "Data-Structure-Util-0.16";
+    src = fetchurl {
+      url = "mirror://cpan/authors/id/A/AN/ANDYA/${name}.tar.gz";
+      sha256 = "9cd42a13e65cb15f3a76296eb9a134da220168ec747c568d331a50ae7a2ddbc6";
+    };
+    buildInputs = [ TestPod ];
+    meta = {
+      description = "Change nature of data within a structure";
+      license = with stdenv.lib.licenses; [ artistic1 gpl1Plus ];
+    };
+  };
+
   DataTaxi = buildPerlPackage {
     name = "Data-Taxi-0.96";
     propagatedBuildInputs = [DebugShowStuff];
@@ -6824,6 +6850,20 @@ let self = _self // overrides; _self = with self; {
     buildInputs = [ TestMore ];
   };
 
+  HTTPProxy = buildPerlPackage rec {
+    name = "HTTP-Proxy-0.304";
+    src = fetchurl {
+      url = "mirror://cpan/authors/id/B/BO/BOOK/${name}.tar.gz";
+      sha256 = "b05290534ec73625c21a0565fc35170890dab163843d95331c292c23f504c69d";
+    };
+    buildInputs = [ HTMLParser ];
+    propagatedBuildInputs = [ HTTPDaemon HTTPDate HTTPMessage LWP ];
+    meta = {
+      description = "A pure Perl HTTP proxy";
+      license = with stdenv.lib.licenses; [ artistic1 gpl1Plus ];
+    };
+  };
+
   HTTPRequestAsCGI = buildPerlPackage rec {
     name = "HTTP-Request-AsCGI-1.2";
     src = fetchurl {
@@ -6857,6 +6897,18 @@ let self = _self // overrides; _self = with self; {
     };
   };
 
+  HTTPServerSimpleAuthen = buildPerlPackage rec {
+    name = "HTTP-Server-Simple-Authen-0.04";
+    src = fetchurl {
+      url = "mirror://cpan/authors/id/M/MI/MIYAGAWA/${name}.tar.gz";
+      sha256 = "2dddc8ab9dc8986980151e4ba836a6bbf091f45cf195be1768ebdb4a993ed59b";
+    };
+    propagatedBuildInputs = [ AuthenSimple HTTPServerSimple ];
+    meta = {
+      license = with stdenv.lib.licenses; [ artistic1 gpl1Plus ];
+    };
+  };
+
   HTTPServerSimpleMason = buildPerlPackage {
     name = "HTTP-Server-Simple-Mason-0.14";
     src = fetchurl {
@@ -6995,6 +7047,17 @@ let self = _self // overrides; _self = with self; {
     };
   };
 
+  IOCapture = buildPerlPackage rec {
+    name = "IO-Capture-0.05";
+    src = fetchurl {
+      url = "mirror://cpan/authors/id/R/RE/REYNOLDS/${name}.tar.gz";
+      sha256 = "c2c15a254ca74fb8c57d25d7b6cbcaff77a3b4fb5695423f1f80bb423abffea9";
+    };
+    meta = {
+      license = with stdenv.lib.licenses; [ artistic1 gpl1Plus ];
+    };
+  };
+
   IOCaptureOutput = buildPerlPackage rec {
     name = "IO-CaptureOutput-1.1104";
     src = fetchurl {
@@ -13314,6 +13377,20 @@ let self = _self // overrides; _self = with self; {
     };
   };
 
+  TestCompile = buildPerlPackage rec {
+    name = "Test-Compile-v1.3.0";
+    src = fetchurl {
+      url = "mirror://cpan/authors/id/E/EG/EGILES/${name}.tar.gz";
+      sha256 = "77527e9477ac5260443c756367a7f7bc3d8f6c6ebbc561b0b2fb3f79303bad33";
+    };
+    buildInputs = [ ModuleBuild ];
+    propagatedBuildInputs = [ UNIVERSALrequire ];
+    meta = {
+      description = "Check whether Perl files compile correctly";
+      license = with stdenv.lib.licenses; [ artistic1 gpl1Plus ];
+    };
+  };
+
   TestCPANMeta = buildPerlPackage {
     name = "Test-CPAN-Meta-0.23";
     src = fetchurl {
@@ -15852,6 +15929,19 @@ let self = _self // overrides; _self = with self; {
     };
   };
 
+  XMLTreePP = buildPerlPackage rec {
+    name = "XML-TreePP-0.43";
+    src = fetchurl {
+      url = "mirror://cpan/authors/id/K/KA/KAWASAKI/${name}.tar.gz";
+      sha256 = "7fbe2d6430860059894aeeebf75d4cacf1bf8d7b75294eb87d8e1502f81bd760";
+    };
+    propagatedBuildInputs = [ LWP ];
+    meta = {
+      description = "Pure Perl implementation for parsing/writing XML documents";
+      license = with stdenv.lib.licenses; [ artistic1 gpl1Plus ];
+    };
+  };
+
   XMLTwig = buildPerlPackage rec {
     name = "XML-Twig-3.49";
     src = fetchurl {

From 6b788e36dff4cdcb31c2e9ed526797e8fd1f0799 Mon Sep 17 00:00:00 2001
From: Philipp Hausmann <ph_git@314.ch>
Date: Thu, 10 Aug 2017 13:13:35 +0200
Subject: [PATCH 2/5] FusionInventory: Add NixOS module.

---
 nixos/modules/module-list.nix                 |  1 +
 .../services/monitoring/fusion-inventory.nix  | 71 +++++++++++++++++++
 2 files changed, 72 insertions(+)
 create mode 100644 nixos/modules/services/monitoring/fusion-inventory.nix

diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix
index d6684ad9511..c5fc71bcb01 100644
--- a/nixos/modules/module-list.nix
+++ b/nixos/modules/module-list.nix
@@ -355,6 +355,7 @@
   ./services/monitoring/collectd.nix
   ./services/monitoring/das_watchdog.nix
   ./services/monitoring/dd-agent/dd-agent.nix
+  ./services/monitoring/fusion-inventory.nix
   ./services/monitoring/grafana.nix
   ./services/monitoring/graphite.nix
   ./services/monitoring/hdaps.nix
diff --git a/nixos/modules/services/monitoring/fusion-inventory.nix b/nixos/modules/services/monitoring/fusion-inventory.nix
new file mode 100644
index 00000000000..fdd27938ea7
--- /dev/null
+++ b/nixos/modules/services/monitoring/fusion-inventory.nix
@@ -0,0 +1,71 @@
+# Fusion Inventory daemon.
+{ config, lib, pkgs, ... }:
+
+with lib;
+
+let
+  cfg = config.services.fusionInventory;
+
+  configFile = pkgs.writeText "fusion_inventory.conf" ''
+    server = ${concatStringsSep ", " cfg.servers}
+
+    logger = stderr
+
+    ${cfg.extraConfig}
+  '';
+
+in {
+
+  ###### interface
+
+  options = {
+
+    services.fusionInventory = {
+
+      enable = mkOption {
+        default = false;
+        description = ''
+          Whether to run the Fusion Inventory agent on this machine.
+        '';
+      };
+
+      servers = mkOption {
+        type = types.listOf types.string;
+        description = ''
+          The urls of the OCS/GLPI servers to connect to.
+        '';
+      };
+
+      extraConfig = mkOption {
+        default = "";
+        type = types.lines;
+        description = ''
+          Configuration that is injected verbatim into the configuration file.
+        '';
+      };
+    };
+  };
+
+
+  ###### implementation
+
+  config = mkIf cfg.enable {
+
+    users.extraUsers = singleton {
+      name = "fusion-inventory";
+      description = "FusionInventory user";
+    };
+
+    systemd.services."fusion-inventory" = {
+      description = "Fusion Inventory Agent";
+      wantedBy = [ "multi-user.target" ];
+
+      environment = {
+        OPTIONS = "--no-category=software";
+      };
+      serviceConfig = {
+        ExecStart = "${pkgs.fusionInventory}/bin/fusioninventory-agent --conf-file=${configFile} --daemon --no-fork";
+      };
+    };
+  };
+}

From 1a23ff8a13e4f833d3d9eea2837394e712736149 Mon Sep 17 00:00:00 2001
From: Philipp Hausmann <ph_git@314.ch>
Date: Fri, 25 Aug 2017 08:57:42 +0200
Subject: [PATCH 3/5] FusionInventory: Code cleanup

---
 nixos/modules/services/monitoring/fusion-inventory.nix | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

diff --git a/nixos/modules/services/monitoring/fusion-inventory.nix b/nixos/modules/services/monitoring/fusion-inventory.nix
index fdd27938ea7..1c00f3c299e 100644
--- a/nixos/modules/services/monitoring/fusion-inventory.nix
+++ b/nixos/modules/services/monitoring/fusion-inventory.nix
@@ -22,15 +22,10 @@ in {
 
     services.fusionInventory = {
 
-      enable = mkOption {
-        default = false;
-        description = ''
-          Whether to run the Fusion Inventory agent on this machine.
-        '';
-      };
+      enable = mkEnableOption "Fusion Inventory Agent";
 
       servers = mkOption {
-        type = types.listOf types.string;
+        type = types.listOf types.str;
         description = ''
           The urls of the OCS/GLPI servers to connect to.
         '';

From 2490dea9d0cb25979f738f5abaf02cfb9008c670 Mon Sep 17 00:00:00 2001
From: Philipp Hausmann <ph_git@314.ch>
Date: Fri, 25 Aug 2017 13:59:51 +0200
Subject: [PATCH 4/5] FusionInventory: Disable tests requiring network access.

---
 .../remove_software_test.patch                | 33 +++++++++++++++++--
 pkgs/top-level/perl-packages.nix              |  2 ++
 2 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/pkgs/servers/monitoring/fusion-inventory/remove_software_test.patch b/pkgs/servers/monitoring/fusion-inventory/remove_software_test.patch
index ba6fd54eb18..5449f4d3740 100644
--- a/pkgs/servers/monitoring/fusion-inventory/remove_software_test.patch
+++ b/pkgs/servers/monitoring/fusion-inventory/remove_software_test.patch
@@ -1,5 +1,34 @@
+diff --git a/t/agent/snmp/live.t b/t/agent/snmp/live.t
+index 8ee7ff02c..bd5551ab3 100755
+--- a/t/agent/snmp/live.t
++++ b/t/agent/snmp/live.t
+@@ -11,7 +11,7 @@ use Test::More;
+ use FusionInventory::Agent::XML::Response;
+ use FusionInventory::Agent::SNMP::Live;
+ 
+-plan tests => 12;
++plan tests => 11;
+ 
+ my $snmp;
+ throws_ok {
+@@ -52,15 +52,6 @@ throws_ok {
+ } qr/^Unable to resolve the UDP\/IPv4 address "none"/,
+ 'instanciation: unresolvable host';
+ 
+-throws_ok {
+-    $snmp = FusionInventory::Agent::SNMP::Live->new(
+-        version   => 1,
+-        community => 'public',
+-        hostname  => '1.1.1.1'
+-    );
+-} qr/no response from host 1.1.1.1/,
+-'instanciation: unresponding host';
+-
+ SKIP: {
+ skip 'live SNMP test disabled', 6 unless $ENV{TEST_LIVE_SNMP};
+ 
 diff --git a/t/apps/agent.t b/t/apps/agent.t
-index c0f6fc52f..c83837d70 100755
+index f417b4106..12207f192 100755
 --- a/t/apps/agent.t
 +++ b/t/apps/agent.t
 @@ -12,7 +12,7 @@ use XML::TreePP;
@@ -11,7 +40,7 @@ index c0f6fc52f..c83837d70 100755
  
  my ($content, $out, $err, $rc);
  
-@@ -72,11 +72,6 @@ subtest "first inventory execution and content" => sub {
+@@ -73,11 +73,6 @@ subtest "first inventory execution and content" => sub {
  };
  
  ok(
diff --git a/pkgs/top-level/perl-packages.nix b/pkgs/top-level/perl-packages.nix
index 91cd9fc0575..4ee98ad2dd4 100644
--- a/pkgs/top-level/perl-packages.nix
+++ b/pkgs/top-level/perl-packages.nix
@@ -6862,6 +6862,8 @@ let self = _self // overrides; _self = with self; {
       description = "A pure Perl HTTP proxy";
       license = with stdenv.lib.licenses; [ artistic1 gpl1Plus ];
     };
+    # tests fail because they require network access
+    doCheck = false;
   };
 
   HTTPRequestAsCGI = buildPerlPackage rec {

From 00bc46b72271c26d4af8df98c653335d38a18b6d Mon Sep 17 00:00:00 2001
From: Philipp Hausmann <ph_git@314.ch>
Date: Sun, 27 Aug 2017 11:51:02 +0200
Subject: [PATCH 5/5] FusionInventory: Remove unused parameters

---
 pkgs/servers/monitoring/fusion-inventory/default.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/servers/monitoring/fusion-inventory/default.nix b/pkgs/servers/monitoring/fusion-inventory/default.nix
index 8b3f902b00c..2e694ede497 100644
--- a/pkgs/servers/monitoring/fusion-inventory/default.nix
+++ b/pkgs/servers/monitoring/fusion-inventory/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, gnused, buildPerlPackage, perl, perlPackages
+{ stdenv, fetchurl, buildPerlPackage, perlPackages
 }:
 
 buildPerlPackage rec {