flake.nix (4646B)
1 { 2 description = "A Nix-flake-based Rust web application"; 3 4 inputs = { 5 nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; 6 rust-overlay = { 7 url = "github:oxalica/rust-overlay"; 8 inputs.nixpkgs.follows = "nixpkgs"; 9 }; 10 crane.url = "github:ipetkov/crane"; 11 }; 12 13 outputs = 14 { 15 self, 16 nixpkgs, 17 rust-overlay, 18 crane, 19 ... 20 }: 21 let 22 inherit (nixpkgs) lib; 23 forAllSystems = lib.genAttrs lib.systems.flakeExposed; 24 25 mkPkgs = 26 system: 27 import nixpkgs { 28 inherit system; 29 overlays = [ (import rust-overlay) ]; 30 }; 31 in 32 { 33 packages = forAllSystems ( 34 system: 35 let 36 pkgs = mkPkgs system; 37 rustToolchain = pkgs.rust-bin.stable.latest.default; 38 craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain; 39 40 # Common arguments for crane builds 41 commonArgs = { 42 src = ./.; 43 pname = "simple-web-app"; 44 version = "0.1.0"; 45 strictDeps = true; 46 47 buildInputs = with pkgs; [ 48 sqlite 49 ] ++ lib.optionals pkgs.stdenv.isDarwin [ 50 pkgs.darwin.apple_sdk.frameworks.Security 51 pkgs.darwin.apple_sdk.frameworks.SystemConfiguration 52 ]; 53 54 nativeBuildInputs = with pkgs; [ 55 pkg-config 56 ]; 57 }; 58 59 # Build just the cargo dependencies 60 cargoArtifacts = craneLib.buildDepsOnly commonArgs; 61 62 # Build the actual crate 63 simple-web-app = craneLib.buildPackage ( 64 commonArgs 65 // { 66 inherit cargoArtifacts; 67 } 68 ); 69 in 70 { 71 default = simple-web-app; 72 inherit simple-web-app; 73 } 74 ); 75 76 # Make simple-web-app runnable with `nix run` 77 apps = forAllSystems ( 78 system: 79 let 80 pkgs = mkPkgs system; 81 in 82 { 83 default = { 84 type = "app"; 85 program = "${self.packages.${system}.default}/bin/simple-web-app"; 86 }; 87 } 88 ); 89 90 # Run checks with `nix flake check` 91 checks = forAllSystems ( 92 system: 93 let 94 pkgs = mkPkgs system; 95 rustToolchain = pkgs.rust-bin.stable.latest.default; 96 craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain; 97 98 commonArgs = { 99 src = ./.; 100 pname = "simple-web-app"; 101 version = "0.1.0"; 102 strictDeps = true; 103 104 buildInputs = with pkgs; [ 105 sqlite 106 ] ++ lib.optionals pkgs.stdenv.isDarwin [ 107 pkgs.darwin.apple_sdk.frameworks.Security 108 pkgs.darwin.apple_sdk.frameworks.SystemConfiguration 109 ]; 110 111 nativeBuildInputs = with pkgs; [ 112 pkg-config 113 ]; 114 }; 115 116 cargoArtifacts = craneLib.buildDepsOnly commonArgs; 117 in 118 { 119 # Build the crate as part of checks 120 simple-web-app = self.packages.${system}.default; 121 122 # Run clippy 123 clippy = craneLib.cargoClippy ( 124 commonArgs 125 // { 126 inherit cargoArtifacts; 127 cargoClippyExtraArgs = "--all-targets -- --deny warnings"; 128 } 129 ); 130 131 # Check formatting 132 fmt = craneLib.cargoFmt { src = ./.; }; 133 134 # Run tests 135 test = craneLib.cargoTest ( 136 commonArgs 137 // { 138 inherit cargoArtifacts; 139 } 140 ); 141 } 142 ); 143 144 devShells = forAllSystems ( 145 system: 146 let 147 pkgs = mkPkgs system; 148 rustToolchain = pkgs.rust-bin.stable.latest.default.override { 149 extensions = [ 150 "rust-src" 151 "rust-analyzer" 152 ]; 153 }; 154 in 155 { 156 default = pkgs.mkShell { 157 packages = with pkgs; [ 158 rustToolchain 159 pkg-config 160 sqlite 161 cargo-watch 162 ] ++ lib.optionals pkgs.stdenv.isDarwin [ 163 pkgs.darwin.apple_sdk.frameworks.Security 164 pkgs.darwin.apple_sdk.frameworks.SystemConfiguration 165 ]; 166 167 env = { 168 RUST_BACKTRACE = "1"; 169 RUST_LOG = "debug"; 170 }; 171 172 shellHook = '' 173 echo "Rust development environment loaded" 174 echo "Run 'cargo run' to start the server" 175 echo "Run 'cargo watch -x run' for auto-reload" 176 ''; 177 }; 178 } 179 ); 180 }; 181 }