Unverified Commit b833d6a1 authored by seth's avatar seth
Browse files

cargo-tauri.hook: init

parent c1c013bd
Loading
Loading
Loading
Loading
+56 −0
Original line number Diff line number Diff line
{
  lib,
  stdenv,
  makeSetupHook,
  cargo,
  cargo-tauri,
  rust,
  # The subdirectory of `target/` from which to copy the build artifacts
  targetSubdirectory ? stdenv.hostPlatform.rust.cargoShortTarget,
}:

let
  kernelName = stdenv.hostPlatform.parsed.kernel.name;
in
makeSetupHook {
  name = "tauri-hook";

  propagatedBuildInputs = [
    cargo
    cargo-tauri
  ];

  substitutions = {
    inherit targetSubdirectory;
    inherit (rust.envVars) rustHostPlatformSpec setEnv;

    # A map of the bundles used for Tauri's different supported platforms
    # https://github.com/tauri-apps/tauri/blob/23a912bb84d7c6088301e1ffc59adfa8a799beab/README.md#platforms
    defaultTauriBundleType =
      {
        darwin = "app";
        linux = "deb";
      }
      .${kernelName} or (throw "${kernelName} is not supported by cargo-tauri.hook");

    # $targetDir is the path to the build artifacts (i.e., `./target/release`)
    installScript =
      {
        darwin = ''
          mkdir $out
          mv "$targetDir"/bundle/macos $out/Applications
        '';

        linux = ''
          mv "$targetDir"/bundle/deb/*/data/usr $out
        '';
      }
      .${kernelName} or (throw "${kernelName} is not supported by cargo-tauri.hook");
  };

  meta = {
    inherit (cargo-tauri.meta) maintainers broken;
    # Platforms that Tauri supports bundles for
    platforms = lib.platforms.darwin ++ lib.platforms.linux;
  };
} ./hook.sh
+101 −0
Original line number Diff line number Diff line
# shellcheck shell=bash disable=SC2034,SC2154,SC2164

# We replace these
export dontCargoBuild=true
export dontCargoInstall=true

tauriBuildHook() {
    echo "Executing tauriBuildHook"

    runHook preBuild

    ## The following is lifted from rustPlatform.cargoBuildHook
    ## As we're replacing it, we should also be respecting its options.

    # Account for running outside of mkRustPackage where this may not be set
    cargoBuildType="${cargoBuildType:-release}"

    # Let stdenv handle stripping, for consistency and to not break
    # separateDebugInfo.
    export "CARGO_PROFILE_${cargoBuildType@U}_STRIP"=false

    if [ -n "${buildAndTestSubdir-}" ]; then
        # ensure the output doesn't end up in the subdirectory
        CARGO_TARGET_DIR="$(pwd)/target"
        export CARGO_TARGET_DIR

        # Tauri doesn't respect $CARGO_TARGET_DIR, but does respect the cargo
        # argument...but that doesn't respect `--target`, so we have to use the
        # config file
        # https://github.com/tauri-apps/tauri/issues/10190
        printf '\nbuild.target-dir = "%s"' "$CARGO_TARGET_DIR" >>config.toml

        pushd "${buildAndTestSubdir}"
    fi

    local cargoFlagsArray=(
        "-j" "$NIX_BUILD_CORES"
        "--target" "@rustHostPlatformSpec@"
        "--offline"
    )
    local tauriFlagsArray=(
        "--bundles" "${tauriBundleType:-@defaultTauriBundleType@}"
        "--target" "@rustHostPlatformSpec@"
    )

    if [ "${cargoBuildType}" != "debug" ]; then
        cargoFlagsArray+=("--profile" "${cargoBuildType}")
    fi

    if [ -n "${cargoBuildNoDefaultFeatures-}" ]; then
        cargoFlagsArray+=("--no-default-features")
    fi

    if [ -n "${cargoBuildFeatures-}" ]; then
        cargoFlagsArray+=("--features=$(concatStringsSep "," cargoBuildFeatures)")
    fi

    concatTo cargoFlagsArray cargoBuildFlags

    if [ "${cargoBuildType:-}" == "debug" ]; then
        tauriFlagsArray+=("--debug")
    fi

    concatTo tauriFlagsArray tauriBuildFlags

    echoCmd 'cargo-tauri.hook cargoFlags' "${cargoFlagsArray[@]}"
    echoCmd 'cargo-tauri.hook tauriFlags' "${tauriFlagsArray[@]}"

    @setEnv@ cargo tauri build "${tauriFlagsArray[@]}" -- "${cargoFlagsArray[@]}"

    if [ -n "${buildAndTestSubdir-}" ]; then
        popd
    fi

    runHook postBuild

    echo "Finished tauriBuildHook"
}

tauriInstallHook() {
    echo "Executing tauriInstallHook"

    runHook preInstall

    # Use a nice variable for our target directory in the following script
    targetDir=target/@targetSubdirectory@/$cargoBuildType

    @installScript@

    runHook postInstall

    echo "Finished tauriInstallHook"
}

if [ -z "${dontTauriBuild:-}" ] && [ -z "${buildPhase:-}" ]; then
    buildPhase=tauriBuildHook
fi

if [ -z "${dontTauriInstall:-}" ] && [ -z "${installPhase:-}" ]; then
    installPhase=tauriInstallHook
fi
+6 −0
Original line number Diff line number Diff line
{
  lib,
  stdenv,
  callPackage,
  rustPlatform,
  fetchFromGitHub,
  darwin,
@@ -46,6 +47,11 @@ rustPlatform.buildRustPackage rec {
      ]
    );

  passthru = {
    # See ./doc/hooks/tauri.section.md
    hook = callPackage ./hook.nix { };
  };

  meta = {
    description = "Build smaller, faster, and more secure desktop applications with a web frontend";
    homepage = "https://tauri.app/";