Skip to content

Instantly share code, notes, and snippets.

@bitonic
Created June 21, 2023 08:03
Show Gist options
  • Select an option

  • Save bitonic/a14cdcce5c9c731f793611cf3e3c113d to your computer and use it in GitHub Desktop.

Select an option

Save bitonic/a14cdcce5c9c731f793611cf3e3c113d to your computer and use it in GitHub Desktop.

Revisions

  1. bitonic created this gist Jun 21, 2023.
    110 changes: 110 additions & 0 deletions ib-tws.nix
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,110 @@
    # From <https://github.com/clefru/nur-packages/tree/f633986f5e31afdd130595b6dc0dea6824d69ef9/pkgs/ib-tws>
    { pkgs ? import <nixpkgs> {} }:
    with pkgs;

    let
    twsWrap = builtins.toFile "tws-wrap.sh" ''
    #!/bin/sh
    export INSTALL4J_JAVA_HOME_OVERRIDE='__JAVAHOME__'
    mkdir -p $HOME/.tws
    VMOPTIONS=$HOME/.tws/tws.vmoptions
    if [ ! -e tws.vmoptions ]; then
    cp __OUT__/libexec/tws.vmoptions $HOME/.tws
    fi
    # The vm options file should always refer to itself.
    sed -i -e 's#-DvmOptionsPath=.*#-DvmOptionsPath=$VMOPTIONS#' $VMOPTIONS
    export LD_LIBRARY_PATH=__GTK__/lib:__CCLIBS__/lib''${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
    exec "__OUT__/libexec/tws" -J-DjtsConfigDir=$HOME/.tws -J-Djxbrowser.chromium.dir=/tmp/jxbrowser -J-Dawt.useSystemAAFontSettings=lcd -J-Dswing.aatext=true "$@"
    '';

    ibDerivation = stdenv.mkDerivation rec {
    version = "10.23.2a";
    pname = "ib-tws-native";

    src = fetchurl {
    url = "https://download2.interactivebrokers.com/installers/tws/latest-standalone/tws-latest-standalone-linux-x64.sh";
    sha256 = "0jb5cxi8xr7l5nlfxdk4j6va5n29l4pw4055jb4pzn3yscyxxc9z";
    executable = true;
    };

    # Only build locally for license reasons.
    preferLocalBuild = true;

    phases = [ "installPhase" ];

    nativeBuildInputs = [ makeWrapper ];

    installPhase = ''
    # We use an installer FHS environment because the shell script unpacks
    # a binary, and immediately calls that binary. There is little hope
    # for us to patchelf ld-linux in between. An FHS env is easier.
    ${buildFHSUserEnv { name = "fhs"; }}/bin/fhs ${src} -q -dir $out/libexec
    # The following disables the JRE compatability check inside the tws script
    # so that we can use Oracle JRE pkgs of nixpkgs.
    sed -i 's#test_jvm "$INSTALL4J_JAVA_HOME_OVERRIDE"#app_java_home="$INSTALL4J_JAVA_HOME_OVERRIDE"#' $out/libexec/tws
    # Make the tws launcher script read $HOME/.tws/tws.vmoptions
    # instead of the unmutable version in $out.
    sed -i -e 's#read_vmoptions "$prg_dir/$progname.vmoptions"#read_vmoptions "$HOME/.tws/$progname.vmoptions"#' $out/libexec/tws
    # We set a bunch of flags found in the Arch PKGBUILD. The flags
    # releated to AA fonts seem to make a positive difference.
    # -Dawt.useSystemAAFontSettings=lcd or -Dawt.useSystemAAFontSettings=on
    # -Dsun.java2d.xrender=True not applied. Results in WARNING: The version of libXrender.so cannot be detected.
    # -Dsun.java2d.opengl=False not applied. Why would I disable that?
    # -Dswing.aatext=true applied
    mkdir $out/bin
    sed -e s#__OUT__#$out# -e s#__JAVAHOME__#${pkgs.oraclejre8.home}# -e s#__GTK__#${pkgs.gtk3}# -e s#__CCLIBS__#${pkgs.stdenv.cc.cc.lib}# ${twsWrap} > $out/bin/ib-tws-native
    chmod a+rx $out/bin/ib-tws-native
    # FIXME Fixup .desktop starter.
    '';

    meta = with lib; {
    description = "Trader Work Station of Interactive Brokers";
    homepage = "https://www.interactivebrokers.com";
    license = licenses.unfree;
    maintainers = [ maintainers.clefru ];
    platforms = platforms.linux;
    };
    };
    # IB TWS packages the JxBrowser component. It unpacks a pre-built
    # Chromium binary (yikes!) that needs an FHS environment. For me, that
    # doesn't yet work, and the chromium fails to launch with an error
    # code.
    in buildFHSUserEnv {
    name = "ib-tws";
    targetPkgs = pkgs1: [
    ibDerivation

    # Chromium dependencies. This might be incomplete.
    xorg.libXfixes
    alsa-lib
    xorg.libXcomposite
    cairo
    xorg.libxcb
    pango
    glib
    atk
    at-spi2-core
    at-spi2-atk
    xorg.libXext
    libdrm
    nspr
    #xorg.libxkbcommon
    nss
    cups
    mesa
    expat
    dbus
    xorg.libXdamage
    xorg.libXrandr
    xorg.libX11
    xorg.libxshmfence
    libxkbcommon
    ];
    runScript = "/usr/bin/ib-tws-native";
    }