|
| 1 | +{ |
| 2 | + description = "Rust Nix flake"; |
| 3 | + |
| 4 | + # Must first enable Garnix GitHub app for the repo |
| 5 | + #nixConfig = { |
| 6 | + # extra-substituters = [ |
| 7 | + # "https://cache.garnix.io" |
| 8 | + # ]; |
| 9 | + # extra-trusted-public-keys = [ |
| 10 | + # "cache.garnix.io:CTFPyKSLcx5RMJKfLo5EEPUObbA78b0YQ2DTCJXqr9g=" |
| 11 | + # ]; |
| 12 | + #}; |
| 13 | + |
| 14 | + inputs = { |
| 15 | + # System packages |
| 16 | + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; |
| 17 | + |
| 18 | + # Helper: flake-parts for easier outputs |
| 19 | + flake-parts.url = "github:hercules-ci/flake-parts"; |
| 20 | + |
| 21 | + # Rust-related inputs |
| 22 | + fenix = { |
| 23 | + url = "github:nix-community/fenix"; |
| 24 | + # Follow top-level nixpkgs so we stay in sync |
| 25 | + inputs.nixpkgs.follows = "nixpkgs"; |
| 26 | + }; |
| 27 | + |
| 28 | + crane.url = "github:ipetkov/crane"; |
| 29 | + flake-utils.url = "github:numtide/flake-utils"; |
| 30 | + }; |
| 31 | + |
| 32 | + outputs = inputs @ { nixpkgs, flake-parts, fenix, crane, ... }: |
| 33 | + flake-parts.lib.mkFlake { inherit inputs; } { |
| 34 | + # Systems we want to build for |
| 35 | + systems = [ |
| 36 | + "aarch64-darwin" |
| 37 | + "aarch64-linux" |
| 38 | + "x86_64-darwin" |
| 39 | + "x86_64-linux" |
| 40 | + ]; |
| 41 | + |
| 42 | + perSystem = { system, pkgs, ... }: |
| 43 | + let |
| 44 | + # Pins the Rust toolchain |
| 45 | + rustToolchain = fenix.packages.${system}.fromToolchainFile { |
| 46 | + file = ./rust-toolchain.toml; |
| 47 | + # Update this hash when `rust-toolchain.toml` changes |
| 48 | + # Just copy the expected hash from the `nix build` error message |
| 49 | + sha256 = "sha256-Qxt8XAuaUR2OMdKbN4u8dBJOhSHxS+uS06Wl9+flVEk="; |
| 50 | + }; |
| 51 | + # Rust package |
| 52 | + craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain; |
| 53 | + commonArgs = { |
| 54 | + src = craneLib.cleanCargoSource ./.; |
| 55 | + strictDeps = true; |
| 56 | + |
| 57 | + buildInputs = [ |
| 58 | + # Add additional build inputs here |
| 59 | + ] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [ |
| 60 | + # Additional darwin specific inputs can be set here |
| 61 | + pkgs.libiconv |
| 62 | + ]; |
| 63 | + }; |
| 64 | + craneLibLLvmTools = craneLib.overrideToolchain rustToolchain; |
| 65 | + cargoArtifacts = craneLib.buildDepsOnly commonArgs; |
| 66 | + |
| 67 | + template = craneLib.buildPackage (commonArgs // { |
| 68 | + inherit cargoArtifacts; |
| 69 | + }); |
| 70 | + |
| 71 | + # Run tests with cargo-nextest |
| 72 | + # Consider setting `doCheck = false` on `my-crate` if you do not want |
| 73 | + # the tests to run twice |
| 74 | + template-nextest = craneLib.cargoNextest ( |
| 75 | + commonArgs |
| 76 | + // { |
| 77 | + inherit cargoArtifacts; |
| 78 | + partitions = 1; |
| 79 | + partitionType = "count"; |
| 80 | + cargoNextestPartitionsExtraArgs = "--no-tests=pass"; |
| 81 | + } |
| 82 | + ); |
| 83 | + |
| 84 | + # Workspace example for `client` and `server` subcrates |
| 85 | + # serverPkg = craneLib.buildPackage (commonArgs // { |
| 86 | + # inherit cargoArtifacts; |
| 87 | + # pname = "server"; |
| 88 | + # cargoExtraArgs = "-p server"; |
| 89 | + # }); |
| 90 | + # clientPkg = craneLib.buildPackage (commonArgs // { |
| 91 | + # inherit cargoArtifacts; |
| 92 | + # pname = "client"; |
| 93 | + # cargoExtraArgs = "-p client"; |
| 94 | + # }); |
| 95 | + in |
| 96 | + { |
| 97 | + checks = { |
| 98 | + inherit template template-nextest; |
| 99 | + }; |
| 100 | + |
| 101 | + packages = { |
| 102 | + default = template-nextest; |
| 103 | + |
| 104 | + # Workspace example |
| 105 | + # server = serverPkg; |
| 106 | + # client = clientPkg; |
| 107 | + }; |
| 108 | + |
| 109 | + # Provide a dev shell with `cargo` and the pinned toolchain |
| 110 | + devShells.default = pkgs.mkShell { |
| 111 | + packages = with pkgs; [ |
| 112 | + pkg-config |
| 113 | + openssl |
| 114 | + ocl-icd |
| 115 | + gcc |
| 116 | + clang |
| 117 | + rustToolchain |
| 118 | + rust-analyzer |
| 119 | + cargo-nextest |
| 120 | + ]; |
| 121 | + }; |
| 122 | + }; |
| 123 | + }; |
| 124 | +} |
0 commit comments