diff --git a/flake.lock b/flake.lock index b77be47..361dd06 100644 --- a/flake.lock +++ b/flake.lock @@ -1,33 +1,28 @@ { "nodes": { - "cargo-doc-live": { + "advisory-db": { + "flake": false, "locked": { - "lastModified": 1724704668, - "narHash": "sha256-kJFYXlWUodg5WhJ0NuvrP0mCvOT/2AOIo8oGeYLXocs=", - "owner": "srid", - "repo": "cargo-doc-live", - "rev": "b09d5d258d2498829e03014931fc19aed499b86f", + "lastModified": 1738539423, + "narHash": "sha256-qb4FLJFuIHdzI1oeVor69678RugKy8YwsLRQd38fJnc=", + "owner": "rustsec", + "repo": "advisory-db", + "rev": "08617accdc251d22bc8fd4e3bd62cf53eeddf611", "type": "github" }, "original": { - "owner": "srid", - "repo": "cargo-doc-live", + "owner": "rustsec", + "repo": "advisory-db", "type": "github" } }, "crane": { - "inputs": { - "nixpkgs": [ - "rust-flake", - "nixpkgs" - ] - }, "locked": { - "lastModified": 1718474113, - "narHash": "sha256-UKrfy/46YF2TRnxTtKCYzqf2f5ZPRRWwKCCJb7O5X8U=", + "lastModified": 1738652123, + "narHash": "sha256-zdZek5FXK/k95J0vnLF0AMnYuZl4AjARq83blKuJBYY=", "owner": "ipetkov", "repo": "crane", - "rev": "0095fd8ea00ae0a9e6014f39c375e40c2fbd3386", + "rev": "c7e015a5fcefb070778c7d91734768680188a9cd", "type": "github" }, "original": { @@ -36,127 +31,68 @@ "type": "github" } }, - "flake-parts": { + "fenix": { "inputs": { - "nixpkgs-lib": [ + "nixpkgs": [ "nixpkgs" - ] + ], + "rust-analyzer-src": [] }, "locked": { - "lastModified": 1725234343, - "narHash": "sha256-+ebgonl3NbiKD2UD0x4BszCZQ6sTfL4xioaM49o5B3Y=", - "owner": "hercules-ci", - "repo": "flake-parts", - "rev": "567b938d64d4b4112ee253b9274472dc3a346eb6", + "lastModified": 1738737274, + "narHash": "sha256-NhaD1K37YwlzN60kvJxlJ/VlYz8IiWk/yYusdvxTZsE=", + "owner": "nix-community", + "repo": "fenix", + "rev": "f82de9980822f3b1efcf54944939b1d514386827", "type": "github" }, "original": { - "owner": "hercules-ci", - "repo": "flake-parts", + "owner": "nix-community", + "repo": "fenix", "type": "github" } }, - "nixpkgs": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, "locked": { - "lastModified": 1725816686, - "narHash": "sha256-0Kq2MkQ/sQX1rhWJ/ySBBQlBJBUK8mPMDcuDhhdBkSU=", - "owner": "nixos", - "repo": "nixpkgs", - "rev": "add0443ee587a0c44f22793b8c8649a0dbc3bb00", + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", "type": "github" }, "original": { - "owner": "nixos", - "ref": "nixpkgs-unstable", - "repo": "nixpkgs", + "owner": "numtide", + "repo": "flake-utils", "type": "github" } }, - "nixpkgs_2": { + "nixpkgs": { "locked": { - "lastModified": 1725103162, - "narHash": "sha256-Ym04C5+qovuQDYL/rKWSR+WESseQBbNAe5DsXNx5trY=", + "lastModified": 1736693123, + "narHash": "sha256-9lIfXCaBPwUA7FnfDnoH4gxxdOvXG78k6UlUw0+ZDxc=", "owner": "nixos", "repo": "nixpkgs", - "rev": "12228ff1752d7b7624a54e9c1af4b222b3c1073b", + "rev": "2fdec2c2e68b7b7845d1ea4e0894c63143e3261b", "type": "github" }, "original": { "owner": "nixos", - "ref": "nixos-unstable", + "ref": "nixpkgs-unstable", "repo": "nixpkgs", "type": "github" } }, - "process-compose-flake": { - "locked": { - "lastModified": 1724606023, - "narHash": "sha256-rdGeNa/lCS8E1lXzPqgl+vZUUvnbEZT11Bqkx5jfYug=", - "owner": "Platonic-Systems", - "repo": "process-compose-flake", - "rev": "f6ce9481df9aec739e4e06b67492401a5bb4f0b1", - "type": "github" - }, - "original": { - "owner": "Platonic-Systems", - "repo": "process-compose-flake", - "type": "github" - } - }, "root": { "inputs": { - "cargo-doc-live": "cargo-doc-live", - "flake-parts": "flake-parts", - "nixpkgs": "nixpkgs", - "process-compose-flake": "process-compose-flake", - "rust-flake": "rust-flake", - "rust-overlay": "rust-overlay", - "systems": "systems", - "treefmt-nix": "treefmt-nix" - } - }, - "rust-flake": { - "inputs": { + "advisory-db": "advisory-db", "crane": "crane", - "nixpkgs": [ - "nixpkgs" - ], - "rust-overlay": [ - "rust-overlay" - ] - }, - "locked": { - "lastModified": 1725924735, - "narHash": "sha256-pMrezXpLk6WPhHKJmFekoga6rQZPgfpwYABbJtMESEY=", - "owner": "juspay", - "repo": "rust-flake", - "rev": "c5c73ff9f4daa7dcc22b8089ea2a57420db60016", - "type": "github" - }, - "original": { - "owner": "juspay", - "repo": "rust-flake", - "type": "github" - } - }, - "rust-overlay": { - "inputs": { - "nixpkgs": [ - "nixpkgs" - ] - }, - "locked": { - "lastModified": 1727836133, - "narHash": "sha256-JE0zciM5IGWvK8J/pE2VldNBf7oyMH5WrU8tZArefbg=", - "owner": "oxalica", - "repo": "rust-overlay", - "rev": "02321540b0c8000b36889b1b974d1fec585b25a4", - "type": "github" - }, - "original": { - "owner": "oxalica", - "repo": "rust-overlay", - "type": "github" + "fenix": "fenix", + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" } }, "systems": { @@ -173,24 +109,6 @@ "repo": "default", "type": "github" } - }, - "treefmt-nix": { - "inputs": { - "nixpkgs": "nixpkgs_2" - }, - "locked": { - "lastModified": 1725271838, - "narHash": "sha256-VcqxWT0O/gMaeWTTjf1r4MOyG49NaNxW4GHTO3xuThE=", - "owner": "numtide", - "repo": "treefmt-nix", - "rev": "9fb342d14b69aefdf46187f6bb80a4a0d97007cd", - "type": "github" - }, - "original": { - "owner": "numtide", - "repo": "treefmt-nix", - "type": "github" - } } }, "root": "root", diff --git a/flake.nix b/flake.nix index f6f4a80..d49d801 100644 --- a/flake.nix +++ b/flake.nix @@ -1,30 +1,148 @@ { inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; - flake-parts.url = "github:hercules-ci/flake-parts"; - flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs"; - systems.url = "github:nix-systems/default"; - rust-overlay.url = "github:oxalica/rust-overlay"; - rust-overlay.inputs.nixpkgs.follows = "nixpkgs"; - rust-flake.url = "github:juspay/rust-flake"; - rust-flake.inputs.nixpkgs.follows = "nixpkgs"; - rust-flake.inputs.rust-overlay.follows = "rust-overlay"; - process-compose-flake.url = "github:Platonic-Systems/process-compose-flake"; - cargo-doc-live.url = "github:srid/cargo-doc-live"; - - # Dev tools - treefmt-nix.url = "github:numtide/treefmt-nix"; - }; - outputs = inputs: - inputs.flake-parts.lib.mkFlake { inherit inputs; } { - systems = import inputs.systems; + crane.url = "github:ipetkov/crane"; + + fenix = { + url = "github:nix-community/fenix"; + inputs.nixpkgs.follows = "nixpkgs"; + inputs.rust-analyzer-src.follows = ""; + }; - # See ./nix/modules/*.nix for the modules that are imported here. - imports = with builtins; - map - (fn: ./nix/modules/${fn}) - (attrNames (readDir ./nix/modules)); + flake-utils.url = "github:numtide/flake-utils"; + advisory-db = { + url = "github:rustsec/advisory-db"; + flake = false; }; + }; + + outputs = { self, nixpkgs, crane, fenix, flake-utils, advisory-db, ... }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + + inherit (pkgs) lib; + + craneLib = crane.mkLib pkgs; + + unfilteredRoot = ./.; + src = lib.fileset.toSource { + root = unfilteredRoot; + fileset = lib.fileset.unions [ + # Default files from crane (Rust and cargo files) + (craneLib.fileset.commonCargoSources unfilteredRoot) + # Also keep any graphql files + (lib.fileset.fileFilter (file: file.hasExt "graphql") + unfilteredRoot) + # SQL files + ./sql + # Static resources + (lib.fileset.maybeMissing ./static) + ]; + }; + + # Common arguments + commonArgs = { + inherit src; + strictDeps = true; + + buildInputs = with pkgs; + [ + # Additional build inputs + openssl + ] ++ lib.optionals pkgs.stdenv.isDarwin + (with pkgs.darwin.apple_sdk.frameworks; [ IOKit Security ]); + + # Additional runtime dependencies + nativeBuildInputs = with pkgs; [ pkg-config sqlx-cli ]; + }; + + craneLibLLvmTools = craneLib.overrideToolchain + (fenix.packages.${system}.complete.withComponents [ + "cargo" + "llvm-tools" + "rustc" + ]); + + # 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. + mina-mesh = + craneLib.buildPackage (commonArgs // { inherit cargoArtifacts; }); + in { + checks = { + # Build the crate as part of `nix flake check` for convenience + inherit mina-mesh; + + # Run clippy (and deny all warnings) on the crate source, + # again, reusing the dependency artifacts from above. + # + # Note that this is done as a separate derivation so that + # we can block the CI if there are issues here, but not + # prevent downstream consumers from building our crate by itself. + mina-mesh-clippy = craneLib.cargoClippy (commonArgs // { + inherit cargoArtifacts; + cargoClippyExtraArgs = "--all-targets -- --deny warnings"; + }); + + mina-mesh-doc = + craneLib.cargoDoc (commonArgs // { inherit cargoArtifacts; }); + + # Check formatting + mina-mesh-fmt = craneLib.cargoFmt { inherit src; }; + + mina-mesh-toml-fmt = craneLib.taploFmt { + src = pkgs.lib.sources.sourceFilesBySuffices src [ ".toml" ]; + # taplo arguments can be further customized below as needed + # taploExtraArgs = "--config ./taplo.toml"; + }; + + # Audit dependencies + mina-mesh-audit = craneLib.cargoAudit { inherit src advisory-db; }; + + # Audit licenses + mina-mesh-deny = craneLib.cargoDeny { inherit src; }; + + # Run tests with cargo-nextest + # Consider setting `doCheck = false` on `my-crate` if you do not want + # the tests to run twice + mina-mesh-nextest = craneLib.cargoNextest (commonArgs // { + inherit cargoArtifacts; + partitions = 1; + partitionType = "count"; + cargoNextestPartitionsExtraArgs = "--no-tests=pass"; + }); + }; + + packages = { + default = mina-mesh; + } // lib.optionalAttrs (!pkgs.stdenv.isDarwin) { + mina-mesh-llvm-coverage = craneLibLLvmTools.cargoLlvmCov + (commonArgs // { inherit cargoArtifacts; }); + }; + + apps.default = flake-utils.lib.mkApp { drv = mina-mesh; }; + + 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 = with pkgs; [ + just + nixd # Nix language server + nixfmt # Nix formatter + cargo-watch + postgresql + ]; + }; + }); } diff --git a/nix/modules/devshell.nix b/nix/modules/devshell.nix deleted file mode 100644 index fcfbdc7..0000000 --- a/nix/modules/devshell.nix +++ /dev/null @@ -1,21 +0,0 @@ -{ inputs, ... }: -{ - perSystem = { config, self', pkgs, lib, ... }: { - devShells.default = pkgs.mkShell { - name = "rust-nix-mina_mesh-shell"; - inputsFrom = [ - self'.devShells.rust - config.treefmt.build.devShell - ]; - nativeBuildInputs = with pkgs; [ - sqlx-cli - ]; - packages = with pkgs; [ - nixd # Nix language server - nixfmt # Nix formatter - cargo-watch - config.process-compose.cargo-doc-live.outputs.package - ]; - }; - }; -} diff --git a/nix/modules/formatting.nix b/nix/modules/formatting.nix deleted file mode 100644 index 318cbae..0000000 --- a/nix/modules/formatting.nix +++ /dev/null @@ -1,17 +0,0 @@ -{ inputs, ... }: -{ - imports = [ - inputs.treefmt-nix.flakeModule - ]; - perSystem = { config, self', pkgs, lib, ... }: { - # Add your auto-formatters here. - # cf. https://nixos.asia/en/treefmt - treefmt.config = { - projectRootFile = "flake.nix"; - programs = { - nixpkgs-fmt.enable = true; - rustfmt.enable = true; - }; - }; - }; -} diff --git a/nix/modules/rust.nix b/nix/modules/rust.nix deleted file mode 100644 index d2abebc..0000000 --- a/nix/modules/rust.nix +++ /dev/null @@ -1,20 +0,0 @@ -{ inputs, ... }: -{ - imports = [ - inputs.rust-flake.flakeModules.default - inputs.rust-flake.flakeModules.nixpkgs - inputs.process-compose-flake.flakeModule - inputs.cargo-doc-live.flakeModule - ]; - perSystem = { config, self', pkgs, lib, ... }: { - rust-project.crates."mina_mesh".crane.args = { - buildInputs = lib.optionals pkgs.stdenv.isDarwin ( - with pkgs.darwin.apple_sdk.frameworks; [ - IOKit - ] - ); - nativeBuildInputs = [ pkgs.openssl ]; - }; - packages.default = self'.packages.mina_mesh; - }; -}