|
| 1 | +{ |
| 2 | + description = "A modern Rust project environment using Nix and Crane"; |
| 3 | + |
| 4 | + inputs = { |
| 5 | + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; |
| 6 | + crane.url = "github:ipetkov/crane"; |
| 7 | + systems.url = "github:nix-systems/default"; |
| 8 | + }; |
| 9 | + |
| 10 | + outputs = { self, nixpkgs, crane, systems, ... }: |
| 11 | + let |
| 12 | + forEachSystem = f: nixpkgs.lib.genAttrs (import systems) (system: f rec { |
| 13 | + pkgs = import nixpkgs { inherit system; }; |
| 14 | + craneLib = crane.mkLib pkgs; |
| 15 | + src = let |
| 16 | + fixturesFilter = path: _type: builtins.match ".*/examples/.*" path != null; |
| 17 | + fixturesOrCargo = path: type: (fixturesFilter path type) || (craneLib.filterCargoSources path type); |
| 18 | + in pkgs.lib.cleanSourceWith { |
| 19 | + src = craneLib.path ./.; |
| 20 | + filter = fixturesOrCargo; |
| 21 | + }; |
| 22 | + }); |
| 23 | + in |
| 24 | + { |
| 25 | + packages = forEachSystem ({ pkgs, craneLib, src }: |
| 26 | + let |
| 27 | + commonArgs = { |
| 28 | + inherit src; |
| 29 | + strictDeps = true; |
| 30 | + nativeBuildInputs = with pkgs; [ pkg-config ]; |
| 31 | + buildInputs = with pkgs; [ |
| 32 | + openssl |
| 33 | + ] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [ |
| 34 | + pkgs.darwin.apple_sdk.frameworks.Security |
| 35 | + pkgs.darwin.apple_sdk.frameworks.SystemConfiguration |
| 36 | + ]; |
| 37 | + }; |
| 38 | + |
| 39 | + # Build *only* the dependencies to cache them in the Nix store |
| 40 | + cargoArtifacts = craneLib.buildDepsOnly commonArgs; |
| 41 | + in |
| 42 | + { |
| 43 | + default = craneLib.buildPackage (commonArgs // { |
| 44 | + inherit cargoArtifacts; |
| 45 | + }); |
| 46 | + } |
| 47 | + ); |
| 48 | + |
| 49 | + devShells = forEachSystem ({ pkgs, craneLib, src }: { |
| 50 | + default = craneLib.devShell { |
| 51 | + inputsFrom = [ self.packages.${pkgs.system}.default ]; |
| 52 | + packages = with pkgs; [ |
| 53 | + cargo |
| 54 | + rustc |
| 55 | + rust-analyzer |
| 56 | + rustfmt |
| 57 | + clippy |
| 58 | + ]; |
| 59 | + env = { |
| 60 | + RUST_SRC_PATH = "${pkgs.rustPlatform.rustLibSrc}"; |
| 61 | + RUST_BACKTRACE = "1"; |
| 62 | + }; |
| 63 | + }; |
| 64 | + }); |
| 65 | + }; |
| 66 | +} |
0 commit comments