atproto blogging
29
fork

Configure Feed

Select the types of activity you want to include in your feed.

at 2f2ed57a6f22fc85bef9f2f1285b11aaa3e2fbcb 275 lines 8.9 kB view raw
1{ 2 description = "Build a cargo workspace"; 3 4 inputs = { 5 nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; 6 flake-utils.url = "github:numtide/flake-utils"; 7 8 advisory-db = { 9 url = "github:rustsec/advisory-db"; 10 flake = false; 11 }; 12 13 rust-overlay.url = "github:oxalica/rust-overlay"; 14 crane.url = "github:ipetkov/crane"; 15 dioxus.url = "github:DioxusLabs/dioxus"; 16 }; 17 18 outputs = { 19 self, 20 nixpkgs, 21 crane, 22 rust-overlay, 23 flake-utils, 24 advisory-db, 25 dioxus, 26 ... 27 }: let 28 name = "weaver"; 29 in 30 flake-utils.lib.eachDefaultSystem (system: let 31 pkgs = import nixpkgs { 32 inherit system; 33 overlays = [ 34 (import rust-overlay) 35 (_: prev: { 36 dioxus-cli = dioxus.packages.${prev.system}.dioxus-cli; 37 }) 38 ]; 39 }; 40 inherit (pkgs) lib; 41 42 rustToolchainFor = p: 43 p.rust-bin.selectLatestNightlyWith (toolchain: 44 toolchain.default.override { 45 # Set the build targets supported by the toolchain, 46 # wasm32-unknown-unknown is required for trunk. 47 targets = ["wasm32-unknown-unknown" "wasm32-wasip1" "wasm32-wasip2"]; 48 extensions = [ 49 "rust-src" 50 "rust-analyzer" 51 "clippy" 52 ]; 53 }); 54 craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchainFor; 55 src = craneLib.cleanCargoSource ./.; 56 57 # Common arguments can be set here to avoid repeating them later 58 commonArgs = { 59 inherit src; 60 strictDeps = true; 61 62 buildInputs = with pkgs; 63 [ 64 # Add additional build inputs here 65 sqlite 66 pkg-config 67 openssl 68 ] 69 ++ lib.optionals pkgs.stdenv.isDarwin [ 70 # Additional darwin specific inputs can be set here 71 pkgs.libiconv 72 ]; 73 nativeBuildInputs = with pkgs; [ 74 sqlite 75 pkg-config 76 openssl 77 ]; 78 # Additional environment variables can be set directly 79 # MY_CUSTOM_VAR = "some value"; 80 }; 81 82 # Build *just* the cargo dependencies (of the entire workspace), 83 # so we can reuse all of that work (e.g. via cachix) when running in CI 84 # It is *highly* recommended to use something like cargo-hakari to avoid 85 # cache misses when building individual top-level-crates 86 cargoArtifacts = craneLib.buildDepsOnly commonArgs; 87 88 individualCrateArgs = 89 commonArgs 90 // { 91 inherit cargoArtifacts; 92 inherit (craneLib.crateNameFromCargoToml {inherit src;}) version; 93 # NB: we disable tests since we'll run them all via cargo-nextest 94 doCheck = false; 95 }; 96 97 fileSetForCrate = crate: 98 lib.fileset.toSource { 99 root = ./.; 100 fileset = lib.fileset.unions [ 101 ./Cargo.toml 102 ./Cargo.lock 103 (lib.fileset.maybeMissing ./crates/weaver-app/Dioxus.lock) 104 (craneLib.fileset.commonCargoSources ./crates/weaver-common) 105 (craneLib.fileset.commonCargoSources crate) 106 ]; 107 }; 108 109 # Build the top-level crates of the workspace as individual derivations. 110 # This allows consumers to only depend on (and build) only what they need. 111 # Though it is possible to build the entire workspace as a single derivation, 112 # so this is left up to you on how to organize things 113 # 114 # Note that the cargo workspace must define `workspace.members` using wildcards, 115 # otherwise, omitting a crate (like we do below) will result in errors since 116 # cargo won't be able to find the sources for all members. 117 weaver-cli = craneLib.buildPackage (individualCrateArgs 118 // { 119 pname = "${name}"; 120 cargoExtraArgs = "-p ${name}-cli"; 121 src = fileSetForCrate ./crates/weaver-cli; 122 }); 123 weaver-app = craneLib.buildPackage (individualCrateArgs 124 // { 125 pname = "${name}-app"; 126 cargoExtraArgs = "-p ${name}-app"; 127 src = fileSetForCrate ./crates/weaver-app; 128 }); 129 weaver-renderer = craneLib.buildPackage (individualCrateArgs 130 // { 131 pname = "${name}-renderer"; 132 cargoExtraArgs = "-p ${name}-renderer"; 133 src = fileSetForCrate ./crates/weaver-renderer; 134 }); 135 in { 136 checks = { 137 # Build the crates as part of `nix flake check` for convenience 138 inherit weaver-cli weaver-app weaver-renderer; 139 140 # Run clippy (and deny all warnings) on the workspace source, 141 # again, reusing the dependency artifacts from above. 142 # 143 # Note that this is done as a separate derivation so that 144 # we can block the CI if there are issues here, but not 145 # prevent downstream consumers from building our crate by itself. 146 "${name}-workspace-clippy" = craneLib.cargoClippy (commonArgs 147 // { 148 inherit cargoArtifacts; 149 cargoClippyExtraArgs = "--all-targets -- --deny warnings"; 150 }); 151 152 "${name}-workspace-doc" = craneLib.cargoDoc (commonArgs 153 // { 154 inherit cargoArtifacts; 155 }); 156 157 # Check formatting 158 "${name}-workspace-fmt" = craneLib.cargoFmt { 159 inherit src; 160 }; 161 162 # "${name}-workspace-toml-fmt" = craneLib.taploFmt { 163 # src = pkgs.lib.sources.sourceFilesBySuffices src [".toml"]; 164 # # taplo arguments can be further customized below as needed 165 # taploExtraArgs = "--config ./taplo.toml"; 166 # }; 167 168 # Audit dependencies 169 "${name}-workspace-audit" = craneLib.cargoAudit { 170 inherit src advisory-db; 171 }; 172 173 # Audit licenses 174 "${name}-workspace-deny" = craneLib.cargoDeny { 175 inherit src; 176 }; 177 178 # Run tests with cargo-nextest 179 # Consider setting `doCheck = false` on other crate derivations 180 # if you do not want the tests to run twice 181 "${name}-workspace-nextest" = craneLib.cargoNextest (commonArgs 182 // { 183 inherit cargoArtifacts; 184 partitions = 1; 185 partitionType = "count"; 186 cargoNextestPartitionsExtraArgs = "--no-tests=pass"; 187 }); 188 }; 189 190 packages = 191 { 192 inherit weaver-cli weaver-app; 193 } 194 // lib.optionalAttrs (!pkgs.stdenv.isDarwin) { 195 # weaver-workspace-llvm-coverage = craneLibLLvmTools.cargoLlvmCov (commonArgs // { 196 # inherit cargoArtifacts; 197 # }); 198 }; 199 200 apps = { 201 weaver-cli = flake-utils.lib.mkApp { 202 drv = weaver-cli; 203 }; 204 weaver-app = flake-utils.lib.mkApp { 205 drv = weaver-app; 206 }; 207 }; 208 209 devShells.default = let 210 # dioxus-cli = pkgs.dioxus-cli.overrideAttrs (_: { 211 # postPatch = '' 212 # rm Cargo.lock 213 # cp ${./crates/weaver-app/Dioxus.lock} Cargo.lock 214 # ''; 215 # cargoDeps = pkgs.rustPlatform.importCargoLock { 216 # lockFile = ./crates/weaver-app/Dioxus.lock; 217 # }; 218 # }); 219 cargoLock = builtins.fromTOML (builtins.readFile ./Cargo.lock); 220 221 wasmBindgen = 222 pkgs.lib.findFirst 223 (pkg: pkg.name == "wasm-bindgen") 224 (throw "Could not find wasm-bindgen package") 225 cargoLock.package; 226 227 wasm-bindgen-cli = pkgs.buildWasmBindgenCli rec { 228 src = pkgs.fetchCrate { 229 pname = "wasm-bindgen-cli"; 230 version = wasmBindgen.version; 231 hash = "sha256-zLPFFgnqAWq5R2KkaTGAYqVQswfBEYm9x3OPjx8DJRY="; 232 }; 233 234 cargoDeps = pkgs.rustPlatform.fetchCargoVendor { 235 inherit src; 236 inherit (src) pname version; 237 hash = "sha256-a2X9bzwnMWNt0fTf30qAiJ4noal/ET1jEtf5fBFj5OU="; 238 }; 239 }; 240 in 241 craneLib.devShell { 242 inherit name; 243 # Inherit inputs from checks. 244 checks = self.checks.${system}; 245 NIX_LD_LIBRARY_PATH = with pkgs; 246 lib.makeLibraryPath [ 247 stdenv.cc.cc 248 openssl 249 # ... 250 ]; 251 NIX_LD = lib.fileContents "${pkgs.stdenv.cc}/nix-support/dynamic-linker"; 252 253 LD_LIBRARY_PATH = "$LD_LIBRARY_PATH:$NIX_LD_LIBRARY_PATH"; 254 255 # Additional dev-shell environment variables can be set directly 256 # MY_CUSTOM_DEVELOPMENT_VAR = "something else"; 257 258 # Extra inputs can be added here; cargo and rustc are provided by default. 259 packages = with pkgs; [ 260 nixd 261 alejandra 262 diesel-cli 263 postgresql 264 cargo-insta 265 cargo-bloat 266 jq 267 dioxus-cli 268 wasm-bindgen-cli 269 wasm-pack 270 twiggy 271 binaryen.out 272 ]; 273 }; 274 }); 275}