-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
cargo-tauri.hook: init #335751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
philiptaron
merged 16 commits into
NixOS:master
from
getchoo-contrib:pkgs/tauriBuildHook/init
Oct 1, 2024
Merged
cargo-tauri.hook: init #335751
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e4efc38
cargo-tauri: migrate to by-name
getchoo 4a78651
cargo-tauri: refactor
getchoo c1c013b
cargo-tauri: add getchoo as a maintainer
getchoo b833d6a
cargo-tauri.hook: init
getchoo f70fb77
cargo-tauri: add test for setup hooks
getchoo ec696bd
doc: init tauri hook section
getchoo 5b5c954
modrinth-app: use cargo-tauri.hook
getchoo 154d776
cinny-desktop: use cargo-tauri.hook
getchoo 4325632
gitbutler: use cargo-tauri.hook
getchoo 686cff4
pot: use cargo-tauri.hook
getchoo 4f58959
surrealist: use cargo-tauri.hook
getchoo 83edf5a
kiwitalk: use cargo-tauri.hook
getchoo 328e517
insulator2: use cargo-tauri.hook & yarnConfigHook
getchoo 0539d52
doc/release-notes: add section on `cargo-tauri.hook`
getchoo a678b77
en-croissant: use cargo-tauri.hook
getchoo 26b1cef
mouse-actions-gui: use cargo-tauri.hook
getchoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| # cargo-tauri.hook {#tauri-hook} | ||
|
|
||
| [Tauri](https://tauri.app/) is a framework for building smaller, faster, and | ||
| more secure desktop applications with a web frontend. | ||
|
|
||
| In Nixpkgs, `cargo-tauri.hook` overrides the default build and install phases. | ||
|
|
||
| ## Example code snippet {#tauri-hook-example-code-snippet} | ||
|
|
||
| ```nix | ||
| { | ||
| lib, | ||
| stdenv, | ||
| rustPlatform, | ||
| fetchNpmDeps, | ||
| cargo-tauri, | ||
| darwin, | ||
| glib-networking, | ||
| libsoup, | ||
| nodejs, | ||
| npmHooks, | ||
| openssl, | ||
| pkg-config, | ||
| webkitgtk, | ||
| wrapGAppsHook3, | ||
| }: | ||
|
|
||
| rustPlatform.buildRustPackage rec { | ||
| # . . . | ||
|
|
||
| cargoHash = "..."; | ||
|
|
||
| # Assuming our app's frontend uses `npm` as a package manager | ||
| npmDeps = fetchNpmDeps { | ||
| name = "${pname}-npm-deps-${version}"; | ||
| inherit src; | ||
| hash = "..."; | ||
| }; | ||
|
|
||
| nativeBuildInputs = [ | ||
| # Pull in our main hook | ||
| cargo-tauri.hook | ||
|
|
||
| # Setup npm | ||
| nodejs | ||
| npmHooks.npmConfigHook | ||
|
|
||
| # Make sure we can find our libraries | ||
| pkg-config | ||
| wrapGAppsHook3 | ||
| ]; | ||
|
|
||
| buildInputs = | ||
| [ openssl ] | ||
| ++ lib.optionals stdenv.isLinux [ | ||
| glib-networking # Most Tauri apps need networking | ||
| libsoup | ||
| webkitgtk | ||
| ] | ||
| ++ lib.optionals stdenv.isDarwin ( | ||
| with darwin.apple_sdk.frameworks; | ||
| [ | ||
| AppKit | ||
| CoreServices | ||
| Security | ||
| WebKit | ||
| ] | ||
| ); | ||
|
|
||
| # Set our Tauri source directory | ||
| cargoRoot = "src-tauri"; | ||
| # And make sure we build there too | ||
| buildAndTestSubdir = cargoRoot; | ||
|
|
||
| # . . . | ||
| } | ||
| ``` | ||
|
|
||
| ## Variables controlling cargo-tauri {#tauri-hook-variables-controlling} | ||
|
|
||
| ### Tauri Exclusive Variables {#tauri-hook-exclusive-variables} | ||
|
|
||
| #### `tauriBuildFlags` {#tauri-build-flags} | ||
|
|
||
| Controls the flags passed to `cargo tauri build`. | ||
|
|
||
| #### `tauriBundleType` {#tauri-bundle-type} | ||
|
|
||
| The [bundle type](https://tauri.app/v1/guides/building/) to build. | ||
|
|
||
| #### `dontTauriBuild` {#dont-tauri-build} | ||
|
|
||
| Disables using `tauriBuildHook`. | ||
|
|
||
| #### `dontTauriInstall` {#dont-tauri-install} | ||
|
|
||
| Disables using `tauriInstallPostBuildHook` and `tauriInstallHook`. | ||
|
|
||
| ### Honored Variables {#tauri-hook-honored-variables} | ||
|
|
||
| Along with those found in [](#compiling-rust-applications-with-cargo), the | ||
| following variables used by `cargoBuildHook` and `cargoInstallHook` are honored | ||
| by the cargo-tauri setup hook. | ||
|
|
||
| - `buildAndTestSubdir` | ||
| - `cargoBuildType` | ||
| - `cargoBuildNoDefaultFeatures` | ||
| - `cargoBuildFeatures` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| { | ||
| 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 | ||
getchoo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 | ||
getchoo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| # 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 | ||
getchoo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.