From 1689efee41839b798994882181213a417a56d470 Mon Sep 17 00:00:00 2001
From: Julien Malka <julien.malka@me.com>
Date: Sun, 26 Dec 2021 21:07:40 +0100
Subject: [PATCH] Added mosh as a package

---
 flake.nix                                   |  5 ++-
 packages/mosh/bash_completion_datadir.patch | 20 ++++++++++++
 packages/mosh/default.nix                   | 36 +++++++--------------
 packages/mosh/ssh_path.patch                | 14 ++++++++
 utils.nix                                   | 18 +----------
 5 files changed, 50 insertions(+), 43 deletions(-)
 create mode 100644 packages/mosh/bash_completion_datadir.patch
 create mode 100644 packages/mosh/ssh_path.patch

diff --git a/flake.nix b/flake.nix
index 7029f26..043750f 100644
--- a/flake.nix
+++ b/flake.nix
@@ -43,6 +43,9 @@
         (builtins.attrNames (builtins.readDir ./modules)));
 
       nixosConfigurations = builtins.mapAttrs (name: value: (mkMachine name value self.nixosModules)) (importConfig ./machines);
-      packages."x86_64-linux".tinystatus = import ./packages/tinystatus { inherit pkgs; };
+      packages."x86_64-linux" = {
+        tinystatus = import ./packages/tinystatus { inherit pkgs; };
+        mosh = pkgs.callPackage ./packages/mosh {};
+      };
     };
 }
diff --git a/packages/mosh/bash_completion_datadir.patch b/packages/mosh/bash_completion_datadir.patch
new file mode 100644
index 0000000..d0c56bc
--- /dev/null
+++ b/packages/mosh/bash_completion_datadir.patch
@@ -0,0 +1,20 @@
+diff --git a/configure.ac b/configure.ac
+index 3ad983d..ff8ff96 100644
+--- a/configure.ac
++++ b/configure.ac
+@@ -476,13 +476,7 @@ AS_IF([echo "$protobuf_LIBS" | grep -q -- -pthread],
+ 
+ # Bash completion needs to ask where it goes if >= 2.0 is installed.
+ AS_IF([test "$install_completion" != no],
+-  [PKG_CHECK_MODULES([BASH_COMPLETION], [bash-completion >= 2.0],
+-     [if test "$prefix" = "NONE"; then
+-        completions="`pkg-config --variable=completionsdir bash-completion`"
+-      else
+-        completions="`pkg-config --define-variable=prefix=$prefix --variable=completionsdir bash-completion`"
+-      fi],
+-     [completions="${sysconfdir}/bash_completion.d"])
++   [completions="`pkg-config --define-variable=datadir=$datadir --variable=completionsdir bash-completion`"]
+    AC_SUBST([completions])])
+ 
+ AC_CONFIG_FILES([
+
diff --git a/packages/mosh/default.nix b/packages/mosh/default.nix
index 5febad7..25d5b4c 100644
--- a/packages/mosh/default.nix
+++ b/packages/mosh/default.nix
@@ -1,20 +1,20 @@
-#{ lib, stdenv, fetchFromGithub, fetchpatch, zlib, protobuf, ncurses, pkg-config
-#, makeWrapper, perlPackages, openssl, autoreconfHook, openssh, bash-completion
-#, withUtempter ? stdenv.isLinux, libutempter }:
+{ pkgs, lib, stdenv, fetchFromGithub, fetchpatch, zlib, protobuf, ncurses, pkg-config
+, makeWrapper, perlPackages, openssl, autoreconfHook, openssh, bash-completion
+, withUtempter ? stdenv.isLinux, libutempter }:
 
 stdenv.mkDerivation rec {
   pname = "mosh";
   version = "1.3.2";
 
-  src = fetchFromGitHub {
+  src = pkgs.fetchFromGitHub {
     owner = "mobile-shell";
     repo = pname;
     rev = "378dfa6aa5778cf168646ada7f52b6f4a8ec8e41"; 
-    sha256 = "05hjhlp6lk8yjcy59zywpf0r6s0h0b9zxq0lw66dh9x8vxrhaq6s";
+    sha256 = "LJssBMrICVgaZtTvZTO6bYMFO4fQ330lIUkWzDSyf7o=";
   };
 
   nativeBuildInputs = [ autoreconfHook pkg-config makeWrapper ];
-  buildInputs = [ protobuf ncurses zlib openssl bash-completion ]
+  buildInputs = [ protobuf ncurses zlib openssl ]
     ++ (with perlPackages; [ perl IOTty ])
     ++ lib.optional withUtempter libutempter;
 
@@ -22,31 +22,16 @@ stdenv.mkDerivation rec {
 
   patches = [
     ./ssh_path.patch
-    ./mosh-client_path.patch
-    ./utempter_path.patch
-    # Fix w/c++17, ::bind vs std::bind
-    (fetchpatch {
-      url = "https://github.com/mobile-shell/mosh/commit/e5f8a826ef9ff5da4cfce3bb8151f9526ec19db0.patch";
-      sha256 = "15518rb0r5w1zn4s6981bf1sz6ins6gpn2saizfzhmr13hw4gmhm";
-    })
-    # Fix build with bash-completion 2.10
     ./bash_completion_datadir.patch
   ];
 
-  postPatch = ''
-    # Fix build with Xcode 12.5 toolchain/case-insensitive filesystems
-    # Backport of https://github.com/mobile-shell/mosh/commit/12199114fe4234f791ef4c306163901643b40538;
-    # remove on next upstream release.
-    patch -p0 < ${fetchpatch {
-      url = "https://raw.githubusercontent.com/macports/macports-ports/70ca3f65e622c17582fd938602d800157ed951c3/net/mosh/files/patch-version-subdir.diff";
-      sha256 = "1yyh6d07y9zbdx4fb0r56zkq9nd9knwzj22v4dfi55k4k42qxapd";
-    }}
 
-    substituteInPlace scripts/mosh.pl \
-      --subst-var-by ssh "${openssh}/bin/ssh" \
-      --subst-var-by mosh-client "$out/bin/mosh-client"
+  preConfigure = ''
+  ./autogen.sh
   '';
 
+  NIX_CFLAGS_COMPILE = "-O2";
+
   configureFlags = [ "--enable-completion" ]
     ++ lib.optional withUtempter "--with-utempter";
 
@@ -74,3 +59,4 @@ stdenv.mkDerivation rec {
 }
 
 
+
diff --git a/packages/mosh/ssh_path.patch b/packages/mosh/ssh_path.patch
new file mode 100644
index 0000000..ff762a1
--- /dev/null
+++ b/packages/mosh/ssh_path.patch
@@ -0,0 +1,14 @@
+diff --git i/scripts/mosh.pl w/scripts/mosh.pl
+index c511482..55bf5f3 100755
+--- i/scripts/mosh.pl
++++ w/scripts/mosh.pl
+@@ -66,7 +66,7 @@ my $use_remote_ip = 'proxy';
+ my $family = 'prefer-inet';
+ my $port_request = undef;
+ 
+-my @ssh = ('ssh');
++my @ssh = ('@ssh@');
+ 
+ my $term_init = 1;
+ 
+
diff --git a/utils.nix b/utils.nix
index 06a94df..490c652 100644
--- a/utils.nix
+++ b/utils.nix
@@ -26,23 +26,7 @@ in
           (final: prev:
             {
               tinystatus = prev.pkgs.callPackage ./packages/tinystatus {};
-              mosh = prev.mosh.overrideAttrs (old: {
-                patches = (prev.lib.take 1 old.patches) ++ (prev.lib.sublist 4 4 old.patches);
-                postPatch = '''';
-                buildInputs = with prev; [ protobuf ncurses zlib openssl ]
-                  ++ (with perlPackages; [ perl IOTty ])
-                  ++ lib.optional true libutempter;
-                preConfigure = ''
-                  ./autogen.sh
-                '';
-                NIX_CFLAGS_COMPILE = "-O2";
-                src = prev.fetchFromGitHub {
-                  owner = "mobile-shell";
-                  repo = "mosh";
-                  rev = "378dfa6aa5778cf168646ada7f52b6f4a8ec8e41";
-                  sha256 = "LJssBMrICVgaZtTvZTO6bYMFO4fQ330lIUkWzDSyf7o=";
-                };
-              });
+              mosh = prev.pkgs.callPackage ./packages/mosh {};
             })
         ];
       }