Help Needed: Repackaging Proprietary launcher

Hey There! I am really enjoying my nixos experience so far! But right now I am stuck trying to repackage a software I regularly use with FHS environments. This is the script I worked out so far:

{ lib
, stdenv
, buildFHSUserEnvBubblewrap
, fetchurl
, dpkg
, pkgs
}:

let
  amsel-suite = stdenv.mkDerivation rec {
    pname = "amsel-suite";
    version = "1.4.2";

    src = fetchurl {
      url = "https://github.com/OllamTechnologies/launcher-releases/releases/download/v${version}/amsel-suite_${version}_amd64.deb";
      hash = "sha256:d723659cdca890ee5ca15325bf3fd8d0feb346367541e1ca154704699daddc26";
    };

    nativeBuildInputs = [ dpkg ];

    unpackCmd = "dpkg -x $curSrc $TMPDIR/src || true";

    installPhase = ''
      mkdir -p $out
      cp -r $TMPDIR/src/* $out
    '';

    meta = with lib; {
      description = "Launcher for the Amsel Suite by Ollam Technologies";
      homepage = "https://ollam.ai/";
      license = licenses.unfree; # falls unklar, unfree setzen
      platforms = [ "x86_64-linux" ];
      maintainers = with maintainers; [ yourGithubHandle ];
      sourceProvenance = with sourceTypes; [ binaryNativeCode ];
    };
  };

  amsel-suite-fhs = buildFHSUserEnvBubblewrap {
    name = "amsel-suite-fhs";

    targetPkgs = pkgs: with pkgs; [
      glib
      nss
      nspr
      dbus.lib
      at-spi2-atk
      cups.lib
      gtk3
      pango
      cairo
      xorg.libX11
      xorg.libXcomposite
      xorg.libXdamage
      xorg.libXfixes
      xorg.libXrandr
      libgbm
      xorg.libxcb
      libxkbcommon
      alsa-lib
      ffmpeg
      libdrm
      xorg.libXext
      expat
      amsel-suite
    ];

    # Wrapper, der persistente Ordner anlegt
    runScript = pkgs.writeShellScript "amsel-suite-wrapper" ''
      set -euo pipefail

      PERSIST_BASE="$HOME/.local/share/amsel-suite"

      # Unterordner wie bei Steam anlegen
      for dir in home opt usr-local; do
        mkdir -p "$PERSIST_BASE/$dir"
      done

      exec amsel-suite "$@"
    '';

    extraMounts = [
      {
        source = "${builtins.getEnv "HOME"}/.local/share/amsel-suite/home";
        target = "/home/amsel";
        writable = true;
      }
      {
        source = "${builtins.getEnv "HOME"}/.local/share/amsel-suite/opt";
        target = "/opt";
        writable = true;
      }
      {
        source = "${builtins.getEnv "HOME"}/.local/share/amsel-suite/usr-local";
        target = "/usr/local";
        writable = true;
      }
    ];
  };
in
amsel-suite // {
  passthru = { fhsEnv = amsel-suite-fhs; };
}

There are multiple issues with this script, but I cannot figure out how to get it working.

First off, when unpacking the deb, tar complains about being unable to set a sticky bit on chrome-sandbox. Then, when executing the binary in the nix-store it crashes with broken pipes.

The software itself is a launcher which downloads further apps to /opt and executes them. I am wondering what I need to change in order to get a running application in the nix-store. I am relatively new to repackaging binaries, so please feel free to point out if all I have done is crap and point me towards correct ways of implementing this!

Thank you very much in advance!

Some of my references include:

https://nixos.wiki/wiki/Packaging/Binaries

So I’ve made some progress. Following is my current derivation:

{
  lib,
  stdenv,
  buildFHSEnvBubblewrap,
  fetchurl,
  dpkg,
  pkgs,
  makeWrapper,
  wrapGAppsHook,
  extraPkgs ? pkgs: [],
}:
stdenv.mkDerivation rec {
  pname = "amsel-suite";
  version = "1.4.2";

  src = fetchurl {
    url = "https://github.com/OllamTechnologies/launcher-releases/releases/download/v${version}/amsel-suite_${version}_amd64.deb";
    hash = "sha256:d723659cdca890ee5ca15325bf3fd8d0feb346367541e1ca154704699daddc26";
  };

  nativeBuildInputs = [dpkg wrapGAppsHook makeWrapper];

  unpackCmd = ''dpkg -x $curSrc src || true'';

  fhsEnv = buildFHSEnvBubblewrap {
    pname = "${pname}-fhs-env";
    inherit version;

    targetPkgs = pkgs: let
      sudoShim = pkgs.writeShellScriptBin "sudo" ''
        #!/bin/sh
        echo "[amsel-fhsenv] sudo shim: running without privileges"
        # Führt den Befehl ohne Rechte aus (nur innerhalb des FHS-Env)
        exec "$@"
      '';
    in
      with pkgs;
        [
          glib
          gsettings-desktop-schemas
          hicolor-icon-theme
          nss
          nspr
          dbus.lib
          at-spi2-atk
          cups.lib
          gtk3
          pango
          cairo
          xorg.libX11
          xorg.libXcomposite
          xorg.libXdamage
          xorg.libXfixes
          xorg.libXrandr
          libgbm
          xorg.libxcb
          libxkbcommon
          alsa-lib
          ffmpeg
          libdrm
          xorg.libXext
          libGL
          udev
          expat
        ]
        ++ extraPkgs pkgs;

    # Wrapper, der persistente Ordner anlegt
    # erzeuge die Host-Ordner (wird beim Start des wrappers ausgeführt)
    extraPreBwrapCmds = ''
      mkdir -p "$HOME/.local/share/amsel-suite/opt"
    '';

    # bwrap-Argumente: binden der Host-Pfade in das FHS-Environment
    # $HOME bleibt eine Laufzeit-Variable (wird vom erzeugten Startskript expandiert)
    extraBwrapArgs = [
      "--bind-try $HOME/.local/share/amsel-suite/opt /opt"
    ];
    runScript = "";
  };

  dontConfigure = true;
  dontBuild = true;

  installPhase = ''
    runHook preInstall

    mkdir -p $out
    mv usr/ $out
    makeWrapper ${fhsEnv}/bin/${pname}-fhs-env $out/usr/lib/amsel-suite/amsel-suite \
      --add-flags "$out/usr/lib/amsel-suite/Amsel\ Suite" \
      "''${gappsWrapperArgs[@]}"

    rm $out/usr/bin/amsel-suite
    ln -s $out/usr/lib/amsel-suite/amsel-suite $out/usr/bin/amsel-suite

    runHook postInstall
  '';

  meta = with lib; {
    description = "Launcher for the Amsel Suite by Ollam Technologies";
    homepage = "https://ollam.ai/";
    license = licenses.unfree; # falls unklar, unfree setzen
    platforms = ["x86_64-linux"];
    maintainers = with maintainers; [fstracke];
    sourceProvenance = with sourceTypes; [binaryNativeCode];
  };
}

However, when the program tries to install downloaded binaries after extraction, it fails returning an error to the user. Has anyone an Idea how I can enable the program to install further binaries especially under /opt?

You can’t make it install anything to /opt, but you could make it install to $out/opt, which could get symlinked by buildFHSEnv.

Out of curiosity, did you try building this app without all the FHS stuff? Is it just unfeasible to do?

yep, the fhs env is definitly necessary. Tested it only using derivations and it fails loading node modules delivered with the app.

Okay, so further progress:
I was able to figure out that the binary is using the npm package sudo-prompt (https://www.npmjs.com/package/sudo-prompt?activeTab=code), which internally is trying to run pkexec or kdesudo. I’ll try and create simple wrappers for these programs and see if it works.

That was it. Still has some errors but works mostly for now.

Do you have a gist or something to share for those who are trying to packing the amsel suite? I tried by mocking their api before buying it but still can’t make it.

Sorry for the late reply. I haven’t updated my derivation for the new AppImage release since I have not had time yet. This is my current derivation, but fair warning: it is majorly hacky:

{
  lib,
  stdenv,
  buildFHSEnvBubblewrap,
  fetchurl,
  dpkg,
  pkgs,
  makeWrapper,
  wrapGAppsHook3,
  extraPkgs ? pkgs: [],
}:
stdenv.mkDerivation rec {
  pname = "amsel-suite";
  version = "1.6.3";

  src = fetchurl {
    url = "https://github.com/OllamTechnologies/launcher-releases/releases/download/v${version}/amsel-suite_${version}_amd64.deb";
    hash = "sha256:l2yWGwEiRtf6AmlCrnQmrIhDHfanKwBGhAGjbhlpCbg=";
  };

  nativeBuildInputs = [dpkg wrapGAppsHook3 makeWrapper];

  unpackCmd = ''dpkg -x $curSrc src || true'';

  fhsEnv = buildFHSEnvBubblewrap {
    pname = "${pname}-fhs-env";
    inherit version;

    targetPkgs = pkgs: let
      pkexecShim = pkgs.writeShellScriptBin "pkexec" ''
        #!/bin/sh
        echo "[amsel-fhsenv] sudo shim: running without privileges"
        # Führt den Befehl ohne Rechte aus (nur innerhalb des FHS-Env)
        shift;
        exec "$@";
      '';
    in
      with pkgs;
        [
          pkexecShim
          uutils-coreutils-noprefix
          glib
          gsettings-desktop-schemas
          hicolor-icon-theme
          nss
          nspr
          dbus
          dbus.lib
          at-spi2-atk
          cups.lib
          gtk3
          pango
          cairo
          libx11
          libxcomposite
          libxdamage
          libxfixes
          libxrandr
          libgbm
          libxcb
          libxkbcommon
          alsa-lib
          ffmpeg
          libdrm
          libxext
          libGL
          udev
          expat
        ]
        ++ extraPkgs pkgs;

    # Wrapper, der persistente Ordner anlegt
    # erzeuge die Host-Ordner (wird beim Start des wrappers ausgeführt)
    extraPreBwrapCmds = ''
      mkdir -p "$HOME/.local/share/amsel-suite/opt"
    '';

    # bwrap-Argumente: binden der Host-Pfade in das FHS-Environment
    # $HOME bleibt eine Laufzeit-Variable (wird vom erzeugten Startskript expandiert)
    extraBwrapArgs = [
      "--bind-try $HOME/.local/share/amsel-suite/opt /opt"
      "--bind-try $HOME $HOME"
      "--bind-try /tmp/testing /tmp/testing"
    ];
    runScript = "";
    profile = ''
      export NIXOS_OZONE_WL=1
    '';
  };

  dontConfigure = true;
  dontBuild = true;

  installPhase = ''
    runHook preInstall

    mkdir -p $out
    mkdir -p $out/opt
    cp -R usr/bin usr/share usr/lib $out/
    makeWrapper ${fhsEnv}/bin/${pname}-fhs-env $out/lib/amsel-suite/amsel-suite \
      --add-flags "$out/lib/amsel-suite/Amsel\ Suite \''${NIXOS_OZONE_WL:+\''${WAYLAND_DISPLAY:+--ozone-platform=wayland --enable-features=WaylandWindowDecorations}}" \
      "''${gappsWrapperArgs[@]}"

    rm $out/bin/amsel-suite
    ln -s $out/lib/amsel-suite/amsel-suite $out/bin/amsel-suite

    runHook postInstall
  '';

  meta = with lib; {
    description = "Launcher for the Amsel Suite by Ollam Technologies";
    homepage = "https://www.amsel-suite.com/";
    license = licenses.unfree;
    platforms = ["x86_64-linux"];
    maintainers = with maintainers; [fstracke];
    sourceProvenance = with sourceTypes; [binaryNativeCode];
  };
}

The gist of it is: Create an /opt in the home dir of the user, which is mounted into the FHS env. The launcher than can install the packages there and read configs from it (such as license keys). Additionally, since they use (or used in that version) an old npm package which relied on pkexec, I made a thin wrapper (which is far from perfect), that just runs the command without sudo privileges. Since the return code is wrong (not equivalent to the one from pkexec) the launcher throws an error, but that can be ignored. Should the UI not update just restart the launcher. Hope this helps!

I used the AppImage, seems much easier to install.
The only issue I had was the app icon that you need to add with the script : icon = ./icon.png;

(Extracted from the AppImage)

{
  lib,
  stdenv,
  fetchurl,
  appimage-run,
  makeWrapper,
  makeDesktopItem,
}:

let
  pname = "amsel-suite";
  version = "1.9.2";

  src = fetchurl {
    url = "https://github.com/OllamTechnologies/launcher-releases/releases/download/v${version}/Amsel.Suite-${version}-x64.AppImage";
    sha256 = "sha256:a57e69bc726864be834b4d17efd7b6d796022ff4a15d46b9ffe118a9ff0ccab1";
  };

  icon = ./icon.png;

  desktopItem = makeDesktopItem {
    name = pname;
    desktopName = "Amsel Suite";
    exec = "${pname}-wrapped %U";
    icon = pname;
    categories = [
      "Game"
      "Office"
    ];
  };
in
stdenv.mkDerivation {
  inherit pname version src;

  nativeBuildInputs = [ makeWrapper ];

  dontUnpack = true;
  dontStrip = true;

  installPhase = ''
    runHook preInstall

    mkdir -p \
      $out/bin \
      $out/share/applications \
      $out/share/icons/hicolor/256x256/apps

    cp $src $out/bin/${pname}
    chmod +x $out/bin/${pname}

    cp ${icon} $out/share/icons/hicolor/256x256/apps/${pname}.png
    cp ${desktopItem}/share/applications/*.desktop $out/share/applications/

    makeWrapper ${appimage-run}/bin/appimage-run $out/bin/${pname}-wrapped \
      --add-flags "$out/bin/${pname}"

    runHook postInstall
  '';

  meta = with lib; {
    description = "Amsel Suite launcher";
    homepage = "https://github.com/OllamTechnologies/launcher-releases";
    license = licenses.unfree;
    platforms = [ "x86_64-linux" ];
    mainProgram = "${pname}-wrapped";
    sourceProvenance = with sourceTypes; [ binaryNativeCode ];
  };
}

That’s way better! Pinned your solution because mine was quite outdated! Thanks for sharing!

This is not the ideal solution. You should use appimageTools (extract and wrapAppImage like example 350, although on latest unstable you should use finalAttrs.contents with wrapType2 instead, see https://github.com/NixOS/nixpkgs/pull/498806) instead of manually unpacking the icon outside Nix and using makeWrapper with appimage-run:
Nixpkgs Reference Manual

Thanks for your valuable input! I didn’t heed too much attention yet to the appimageTools api, as the manual labels them as unstable. Good to know that it is the recommended way!

Maybe a sidenote: why write in such a discouraging tone? This thread was started by myself when I had little to no experience using nixos and tried to package an app which wouldn’t let itself be packed using the regular documented means. All messages in this thread so far have been meant with a kind spirit and only for personal usage - not weighing down on maintainers or any other project resources. So I feel coming in with a “This is wrong!” Top-down approach feels a little hypocritical. It feels like after finally figuring out a puzzle on your own your big brother comes in and berates you on why you are dumb and did it the wrong way. This is majorly discouraging! Why not try to phrase it more helpful: “Hey, this is the simpler, recommended way!” instead of using “wrong!” For something unlikely to affect more than five people as of now.

Sorry for the rant! Have a great day!

Sorry about that! Wasn’t trying to come off that way. Changed it to “This is not the ideal solution” which is hopefully better. Also, appimage-run is actually based off of some parts of appimageTools.