{ description = "Build a cargo project which uses SQLx"; inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; crane.url = "github:ipetkov/crane"; flake-utils.url = "github:numtide/flake-utils"; }; outputs = { self, nixpkgs, crane, flake-utils, ... }: flake-utils.lib.eachDefaultSystem (system: let pkgs = nixpkgs.legacyPackages.${system}; inherit (pkgs) lib; craneLib = crane.mkLib pkgs; sqlFilter = path: _type: null != builtins.match ".*sql$" path; sqlOrCargo = path: type: (sqlFilter path type) || (craneLib.filterCargoSources path type); src = lib.cleanSourceWith { src = ./.; # The original, unfiltered source filter = sqlOrCargo; name = "source"; # Be reproducible, regardless of the directory name }; # Common arguments can be set here to avoid repeating them later commonArgs = { inherit src; strictDeps = true; nativeBuildInputs = [ pkgs.pkg-config ]; buildInputs = [ # Add additional build inputs here pkgs.openssl ] ++ lib.optionals pkgs.stdenv.isDarwin [ # Additional darwin specific inputs can be set here pkgs.libiconv pkgs.darwin.apple_sdk.frameworks.Security ]; # Additional environment variables can be set directly # MY_CUSTOM_VAR = "some value"; }; # Build *just* the cargo dependencies, so we can reuse # all of that work (e.g. via cachix) when running in CI cargoArtifacts = craneLib.buildDepsOnly commonArgs; # Build the actual crate itself, reusing the dependency # artifacts from above. my-crate = craneLib.buildPackage (commonArgs // { inherit cargoArtifacts; nativeBuildInputs = (commonArgs.nativeBuildInputs or [ ]) ++ [ pkgs.sqlx-cli ]; preBuild = '' export DATABASE_URL=sqlite:./db.sqlite3 sqlx database create sqlx migrate run ''; }); in { checks = { # Build the crate as part of `nix flake check` for convenience inherit my-crate; }; packages = { default = my-crate; inherit my-crate; }; devShells.default = craneLib.devShell { # Inherit inputs from checks. checks = self.checks.${system}; # Additional dev-shell environment variables can be set directly # MY_CUSTOM_DEVELOPMENT_VAR = "something else"; # Extra inputs can be added here; cargo and rustc are provided by default. packages = [ pkgs.sqlx-cli ]; }; }); }