Skip to content

Commit c56a7cb

Browse files
committed
buildTauriPackage
1 parent 8b08e96 commit c56a7cb

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

lib/buildTauriPackage.nix

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
{ buildDepsOnly
2+
, crateNameFromCargoToml
3+
, mkCargoDerivation
4+
, installFromCargoBuildLogHook
5+
, removeReferencesToVendoredSourcesHook
6+
, cargo-tauri
7+
, writeText
8+
, jq
9+
, lib
10+
, vendorCargoDeps
11+
}:
12+
13+
{ pname ? null
14+
, tauriConfigPath
15+
, tauriDistDir
16+
, tauriConfigOverride ? { }
17+
, ...
18+
}@origArgs:
19+
let
20+
cleanedArgs = builtins.removeAttrs origArgs [
21+
"tauriConfigPath"
22+
"tauriConfigOverride"
23+
"tauriDistDir"
24+
];
25+
26+
origConfig = builtins.fromJSON (builtins.readFile tauriConfigPath);
27+
28+
crateName = crateNameFromCargoToml cleanedArgs;
29+
30+
buildConfig = lib.recursiveUpdate origConfig {
31+
package.productName = args.pname;
32+
build.distDir = tauriDistDir;
33+
build.beforeBuildCommand = "true";
34+
tauri.bundle.active = false;
35+
};
36+
37+
overrridenConfig = lib.recursiveUpdate buildConfig tauriConfigOverride;
38+
39+
# Avoid recomputing values when passing args down
40+
args = cleanedArgs // {
41+
pname = cleanedArgs.pname or crateName.pname;
42+
version = cleanedArgs.version or crateName.version;
43+
cargoVendorDir = cleanedArgs.cargoVendorDir or (vendorCargoDeps cleanedArgs);
44+
};
45+
in
46+
mkCargoDerivation (args // rec {
47+
cargoArtifacts = args.cargoArtifacts or (buildDepsOnly (args // {
48+
installCargoArtifactsMode = args.installCargoArtifactsMode or "use-zstd";
49+
doCheck = args.doCheck or false;
50+
}));
51+
52+
53+
buildPhaseCargoCommand = args.buildPhaseCommand or ''
54+
local profileArgs=""
55+
if [[ "$CARGO_PROFILE" == "release" ]]; then
56+
profileArgs="--release"
57+
fi
58+
cargoBuildLog=$(mktemp cargoBuildLogXXXX.json)
59+
cargo tauri build -c ${writeText "tauri.json" (builtins.toJSON overrridenConfig)} $profileArg -- --message-format json-render-diagnostics >"$cargoBuildLog"
60+
'';
61+
62+
installPhaseCommand = args.installPhaseCommand or ''
63+
if [ -n "$cargoBuildLog" -a -f "$cargoBuildLog" ]; then
64+
installFromCargoBuildLog "$out" "$cargoBuildLog"
65+
else
66+
echo ${lib.strings.escapeShellArg ''
67+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
68+
$cargoBuildLog is either undefined or does not point to a valid file location!
69+
By default `buildPackage` will capture cargo's output and use it to determine which binaries
70+
should be installed (instead of just guessing based on what is present in cargo's target directory).
71+
If you are overriding the derivation with a custom build step, you have two options:
72+
1. override `installPhaseCommand` with the appropriate installation steps
73+
2. ensure that cargo's build log is captured in a file and point $cargoBuildLog at it
74+
At a minimum, the latter option can be achieved with running:
75+
cargoBuildLog=$(mktemp cargoBuildLogXXXX.json)
76+
cargo build --release --message-format json-render-diagnostics >"$cargoBuildLog"
77+
''}
78+
79+
false
80+
fi
81+
'';
82+
83+
nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [
84+
cargo-tauri installFromCargoBuildLogHook removeReferencesToVendoredSourcesHook jq
85+
];
86+
})

lib/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ in
1616
buildDepsOnly = callPackage ./buildDepsOnly.nix { };
1717
buildPackage = callPackage ./buildPackage.nix { };
1818
buildTrunkPackage = callPackage ./buildTrunkPackage.nix { };
19+
buildTauriPackage = callPackage ./buildTauriPackage.nix { };
1920
cargoAudit = callPackage ./cargoAudit.nix { };
2021
cargoBuild = callPackage ./cargoBuild.nix { };
2122
cargoClippy = callPackage ./cargoClippy.nix { };

0 commit comments

Comments
 (0)