|
| 1 | +{ |
| 2 | + description = "Build a cargo project"; |
| 3 | + |
| 4 | + inputs = { |
| 5 | + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; |
| 6 | + |
| 7 | + crane = { |
| 8 | + url = "path:/tmp/crane"; |
| 9 | + inputs.nixpkgs.follows = "nixpkgs"; |
| 10 | + }; |
| 11 | + |
| 12 | + flake-utils.url = "github:numtide/flake-utils"; |
| 13 | + |
| 14 | + rust-overlay = { |
| 15 | + url = "github:oxalica/rust-overlay"; |
| 16 | + inputs = { |
| 17 | + nixpkgs.follows = "nixpkgs"; |
| 18 | + flake-utils.follows = "flake-utils"; |
| 19 | + }; |
| 20 | + }; |
| 21 | + }; |
| 22 | + |
| 23 | + outputs = { self, nixpkgs, crane, flake-utils, rust-overlay, ... }: |
| 24 | + flake-utils.lib.eachDefaultSystem (system: |
| 25 | + let |
| 26 | + pkgs = import nixpkgs { |
| 27 | + inherit system; |
| 28 | + overlays = [ (import rust-overlay) ]; |
| 29 | + }; |
| 30 | + |
| 31 | + inherit (pkgs) lib; |
| 32 | + |
| 33 | + rustToolchain = pkgs.rust-bin.stable.latest.default.override { |
| 34 | + # Set the build targets supported by the toolchain, |
| 35 | + # wasm32-unknown-unknown is required for trunk |
| 36 | + targets = [ "wasm32-unknown-unknown" ]; |
| 37 | + }; |
| 38 | + craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain; |
| 39 | + |
| 40 | + # When filtering sources, we want to allow assets other than .rs files |
| 41 | + src = lib.cleanSourceWith { |
| 42 | + src = ./.; # The original, unfiltered source |
| 43 | + filter = path: type: |
| 44 | + (lib.hasSuffix "\.html" path) || |
| 45 | + (lib.hasSuffix "\.scss" path) || |
| 46 | + (lib.hasSuffix "\.css" path) || |
| 47 | + (lib.hasSuffix "\.json" path) || |
| 48 | + (lib.hasSuffix "\.png" path) || |
| 49 | + # Example of a folder for images, icons, etc |
| 50 | + (lib.hasInfix "/assets/" path) || |
| 51 | + # Default filter from crane (allow .rs files) |
| 52 | + (craneLib.filterCargoSources path type) |
| 53 | + ; |
| 54 | + }; |
| 55 | + |
| 56 | + # Common arguments can be set here to avoid repeating them later |
| 57 | + commonArgs = { |
| 58 | + inherit src; |
| 59 | + }; |
| 60 | + |
| 61 | + yewArgs = commonArgs // { |
| 62 | + src = "${src}/yew"; |
| 63 | + # We must force the target, otherwise cargo will attempt to use your native target |
| 64 | + CARGO_BUILD_TARGET = "wasm32-unknown-unknown"; |
| 65 | + }; |
| 66 | + |
| 67 | + tauriArgs = with pkgs; commonArgs // { |
| 68 | + src = "${src}/tauri"; |
| 69 | + nativeBuildInputs = [ pkgconfig ]; |
| 70 | + buildInputs = [ webkitgtk gtk3 cairo gdk-pixbuf glib dbus ]; |
| 71 | + }; |
| 72 | + |
| 73 | + # Build *just* the cargo dependencies, so we can reuse |
| 74 | + # all of that work (e.g. via cachix) when running in CI |
| 75 | + cargoArtifacts = craneLib.buildDepsOnly (tauriArgs // { |
| 76 | + # You cannot run cargo test on a wasm build |
| 77 | + doCheck = false; |
| 78 | + }); |
| 79 | + |
| 80 | + # Build *just* the cargo dependencies, so we can reuse |
| 81 | + # all of that work (e.g. via cachix) when running in CI |
| 82 | + cargoArtifactsWasm = craneLib.buildDepsOnly (yewArgs // { |
| 83 | + # You cannot run cargo test on a wasm build |
| 84 | + doCheck = false; |
| 85 | + }); |
| 86 | + |
| 87 | + # Build the actual crate itself, reusing the dependency |
| 88 | + # artifacts from above. |
| 89 | + # This derivation is a directory you can put on a webserver. |
| 90 | + yew = craneLib.buildTrunkPackage (yewArgs // { |
| 91 | + pname = "yew"; |
| 92 | + inherit cargoArtifactsWasm; |
| 93 | + }); |
| 94 | + |
| 95 | + tauri = craneLib.buildTauriPackage (tauriArgs // { |
| 96 | + pname = "tauri"; |
| 97 | + inherit cargoArtifacts; |
| 98 | + tauriConfigPath = ./tauri/tauri.conf.json; |
| 99 | + tauriDistDir = yew; |
| 100 | + }); |
| 101 | + in |
| 102 | + { |
| 103 | + checks = { |
| 104 | + # Build the crate as part of `nix flake check` for convenience |
| 105 | + inherit yew tauri; |
| 106 | + |
| 107 | + # Run clippy (and deny all warnings) on the crate source, |
| 108 | + # again, reusing the dependency artifacts from above. |
| 109 | + # |
| 110 | + # Note that this is done as a separate derivation so that |
| 111 | + # we can block the CI if there are issues here, but not |
| 112 | + # prevent downstream consumers from building our crate by itself. |
| 113 | + yew-clippy = craneLib.cargoClippy (yewArgs // { |
| 114 | + inherit cargoArtifactsWasm; |
| 115 | + cargoClippyExtraArgs = "--all-targets -- --deny warnings"; |
| 116 | + }); |
| 117 | + |
| 118 | + tauri-clippy = craneLib.cargoClippy (tauriArgs // { |
| 119 | + inherit cargoArtifacts; |
| 120 | + cargoClippyExtraArgs = "--all-targets -- --deny warnings"; |
| 121 | + }); |
| 122 | + |
| 123 | + # Check formatting |
| 124 | + tauri-yew-fmt = craneLib.cargoFmt { |
| 125 | + inherit src; |
| 126 | + }; |
| 127 | + }; |
| 128 | + |
| 129 | + packages = { |
| 130 | + inherit tauri yew; |
| 131 | + }; |
| 132 | + |
| 133 | + apps.default = flake-utils.lib.mkApp { |
| 134 | + drv = tauri; |
| 135 | + }; |
| 136 | + |
| 137 | + devShells.default = pkgs.mkShell { |
| 138 | + inputsFrom = builtins.attrValues self.checks; |
| 139 | + |
| 140 | + # Extra inputs can be added here |
| 141 | + nativeBuildInputs = with pkgs; [ |
| 142 | + cargo |
| 143 | + cargo-tauri |
| 144 | + rustc |
| 145 | + trunk |
| 146 | + ]; |
| 147 | + }; |
| 148 | + }); |
| 149 | +} |
0 commit comments