bisq-desktop: utilize the built-in Tor handling

Bisq comes with an embedded Tor binary (provided by a third party), but we don't use it in this package because it's build for a FHS-abiding Linux distro; Meaning, Tor won't execute because it tried to load libraries from standard locations.

To address this problem, the Nix package launches an ephemeral Tor instance for Bisq. The approach works, but it does mean having to manage the tor process, something which is already handled well by Bisq.

This change modifies the Bisq Jar archive such that it launches the Tor binary from Nixpkgs, allowing Bisq to manage Tor as it does on other Linux distros and operating systems.

In a nutshell, when Bisq is launched it extracts a copy of the tor binary from its Jar file and saves it in the Bisq data directory. It is then executed from there. Since Nix doesn't know that Bisq has a runtime dependency on Tor, this change modifies the launcher script to contain a reference to Tor, thus convincing Nix that Tor is a runtime dependency.
This commit is contained in:
Emmanuel Rosa 2021-07-17 11:27:31 +07:00
parent 6cb0749fb1
commit c82be07e04

View file

@ -8,42 +8,29 @@
, openjdk11 , openjdk11
, dpkg , dpkg
, writeScript , writeScript
, coreutils
, bash , bash
, tor , tor
, psmisc , gnutar
, zip
, xz
}: }:
let let
bisq-launcher = writeScript "bisq-launcher" '' bisq-launcher = writeScript "bisq-launcher" ''
#! ${bash}/bin/bash #! ${bash}/bin/bash
# Setup a temporary Tor instance # This is just a comment to convince Nix that Tor is a
TMPDIR=$(${coreutils}/bin/mktemp -d) # runtime dependency; The Tor binary is in a *.jar file,
CONTROLPORT=$(${coreutils}/bin/shuf -i 9100-9499 -n 1) # whereas Nix only scans for hashes in uncompressed text.
SOCKSPORT=$(${coreutils}/bin/shuf -i 9500-9999 -n 1) # ${bisq-tor}
${coreutils}/bin/head -c 1024 < /dev/urandom > $TMPDIR/cookie
${tor}/bin/tor --SocksPort $SOCKSPORT --ControlPort $CONTROLPORT \ JAVA_TOOL_OPTIONS="-XX:MaxRAM=4g" bisq-desktop-wrapped "$@"
--ControlPortWriteToFile $TMPDIR/port --CookieAuthFile $TMPDIR/cookie \ '';
--CookieAuthentication 1 >$TMPDIR/tor.log --RunAsDaemon 1
torpid=$(${psmisc}/bin/fuser $CONTROLPORT/tcp) bisq-tor = writeScript "bisq-tor" ''
#! ${bash}/bin/bash
echo Temp directory: $TMPDIR exec ${tor}/bin/tor "$@"
echo Tor PID: $torpid
echo Tor control port: $CONTROLPORT
echo Tor SOCKS port: $SOCKSPORT
echo Tor log: $TMPDIR/tor.log
echo Bisq log file: $TMPDIR/bisq.log
JAVA_TOOL_OPTIONS="-XX:MaxRAM=4g" bisq-desktop-wrapped \
--torControlCookieFile=$TMPDIR/cookie \
--torControlUseSafeCookieAuth \
--torControlPort $CONTROLPORT "$@" > $TMPDIR/bisq.log
echo Bisq exited. Killing Tor...
kill $torpid
''; '';
in in
stdenv.mkDerivation rec { stdenv.mkDerivation rec {
@ -55,7 +42,7 @@ stdenv.mkDerivation rec {
sha256 = "0crry5k7crmrqn14wxiyrnhk09ac8a9ksqrwwky7jsnyah0bx5k4"; sha256 = "0crry5k7crmrqn14wxiyrnhk09ac8a9ksqrwwky7jsnyah0bx5k4";
}; };
nativeBuildInputs = [ makeWrapper copyDesktopItems dpkg ]; nativeBuildInputs = [ makeWrapper copyDesktopItems imagemagick dpkg gnutar zip xz ];
desktopItems = [ desktopItems = [
(makeDesktopItem { (makeDesktopItem {
@ -72,6 +59,16 @@ stdenv.mkDerivation rec {
dpkg -x $src . dpkg -x $src .
''; '';
buildPhase = ''
# Replace the embedded Tor binary (which is in a Tar archive)
# with one from Nixpkgs.
mkdir -p native/linux/x64/
cp ${bisq-tor} ./tor
tar -cJf native/linux/x64/tor.tar.xz tor
zip -r opt/bisq/lib/app/desktop-${version}-all.jar native
'';
installPhase = '' installPhase = ''
runHook preInstall runHook preInstall
@ -86,7 +83,7 @@ stdenv.mkDerivation rec {
for n in 16 24 32 48 64 96 128 256; do for n in 16 24 32 48 64 96 128 256; do
size=$n"x"$n size=$n"x"$n
${imagemagick}/bin/convert opt/bisq/lib/Bisq.png -resize $size bisq.png convert opt/bisq/lib/Bisq.png -resize $size bisq.png
install -Dm644 -t $out/share/icons/hicolor/$size/apps bisq.png install -Dm644 -t $out/share/icons/hicolor/$size/apps bisq.png
done; done;