Run a program from maven using jbang

Building Java programs with Nix is a nightmare. I got the compiled jar to work with this self-contained example:

quarkus-cli.nix

{ pkgs ? <nixpkgs>
}:
let
  quarkus-cli = { stdenv, fetchurl, writeScript, bash, openjdk }: 
    stdenv.mkDerivation rec {
      pname = "quarkus-cli";
      version = "2.10.1.Final";

      src = fetchurl {
        url = "https://github.com/quarkusio/quarkus/releases/download/2.10.1.Final/${pname}-${version}.tar.gz";
        sha256 = "0cl641g5vgw94ac7sj8mhz21s6rkjdfjdzhh02dq64cd5cxwcmc5";
      };

      launcher = writeScript pname ''
        #! ${bash}/bin/bash

        export CLASSPATH=@LIB@/${pname}-${version}-runner.jar

        exec ${openjdk}/bin/java $JAVA_OPTS -classpath $CLASSPATH -Dapp.name="quarkus" -Dapp-pid="$$" -Dapp.repo="@LIB@" -Dapp.home="@OUT@" -Dbasedir="@OUT@" io.quarkus.cli.Main "$@"
      '';

      installPhase = '' 
        mkdir -p $out/{lib,bin}

        cp ./lib/${pname}-${version}-runner.jar $out/lib
        cp ${launcher} $out/bin/${pname}
        substituteInPlace $out/bin/${pname} --replace @LIB@ $out/lib --replace @OUT@ $out
      ''; 
   };
in
  (import pkgs {}).callPackage quarkus-cli {}
2 Likes