From f9f36dbea32847ab749e04c96ec8e261df892a15 Mon Sep 17 00:00:00 2001 From: xnm Date: Thu, 14 Dec 2023 22:18:29 +0200 Subject: [PATCH] changed `flake.nix` in template for nightly rust projects --- .../optimized-pre-config-rust/flake.lock | 130 --------------- .../optimized-pre-config-rust/flake.nix | 151 +++++++++++++++--- nixos/rust.nix | 4 +- 3 files changed, 131 insertions(+), 154 deletions(-) delete mode 100644 home/Projects/optimized-pre-config-rust/flake.lock diff --git a/home/Projects/optimized-pre-config-rust/flake.lock b/home/Projects/optimized-pre-config-rust/flake.lock deleted file mode 100644 index d512402..0000000 --- a/home/Projects/optimized-pre-config-rust/flake.lock +++ /dev/null @@ -1,130 +0,0 @@ -{ - "nodes": { - "flake-utils": { - "inputs": { - "systems": "systems" - }, - "locked": { - "lastModified": 1694529238, - "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "ff7b65b44d01cf9ba6a71320833626af21126384", - "type": "github" - }, - "original": { - "owner": "numtide", - "repo": "flake-utils", - "type": "github" - } - }, - "flake-utils_2": { - "inputs": { - "systems": "systems_2" - }, - "locked": { - "lastModified": 1681202837, - "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "cfacdce06f30d2b68473a46042957675eebb3401", - "type": "github" - }, - "original": { - "owner": "numtide", - "repo": "flake-utils", - "type": "github" - } - }, - "nixpkgs": { - "locked": { - "lastModified": 1701253981, - "narHash": "sha256-ztaDIyZ7HrTAfEEUt9AtTDNoCYxUdSd6NrRHaYOIxtk=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "e92039b55bcd58469325ded85d4f58dd5a4eaf58", - "type": "github" - }, - "original": { - "owner": "NixOS", - "ref": "nixos-unstable", - "repo": "nixpkgs", - "type": "github" - } - }, - "nixpkgs_2": { - "locked": { - "lastModified": 1681358109, - "narHash": "sha256-eKyxW4OohHQx9Urxi7TQlFBTDWII+F+x2hklDOQPB50=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "96ba1c52e54e74c3197f4d43026b3f3d92e83ff9", - "type": "github" - }, - "original": { - "owner": "NixOS", - "ref": "nixpkgs-unstable", - "repo": "nixpkgs", - "type": "github" - } - }, - "root": { - "inputs": { - "flake-utils": "flake-utils", - "nixpkgs": "nixpkgs", - "rust-overlay": "rust-overlay" - } - }, - "rust-overlay": { - "inputs": { - "flake-utils": "flake-utils_2", - "nixpkgs": "nixpkgs_2" - }, - "locked": { - "lastModified": 1701397143, - "narHash": "sha256-nYUJxZXwCWWVBYZXPgRxGDuQcZRhKTtD/Jp5Jl+9EWU=", - "owner": "oxalica", - "repo": "rust-overlay", - "rev": "bb71557c93cad40f5921b2342d7fd69f9e6497ab", - "type": "github" - }, - "original": { - "owner": "oxalica", - "repo": "rust-overlay", - "type": "github" - } - }, - "systems": { - "locked": { - "lastModified": 1681028828, - "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", - "owner": "nix-systems", - "repo": "default", - "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", - "type": "github" - }, - "original": { - "owner": "nix-systems", - "repo": "default", - "type": "github" - } - }, - "systems_2": { - "locked": { - "lastModified": 1681028828, - "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", - "owner": "nix-systems", - "repo": "default", - "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", - "type": "github" - }, - "original": { - "owner": "nix-systems", - "repo": "default", - "type": "github" - } - } - }, - "root": "root", - "version": 7 -} diff --git a/home/Projects/optimized-pre-config-rust/flake.nix b/home/Projects/optimized-pre-config-rust/flake.nix index 7c82522..20413c2 100644 --- a/home/Projects/optimized-pre-config-rust/flake.nix +++ b/home/Projects/optimized-pre-config-rust/flake.nix @@ -1,50 +1,157 @@ { - description = "A devShell for Rust Development"; + description = "Flake for my-crate"; inputs = { - nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; - rust-overlay.url = "github:oxalica/rust-overlay"; - flake-utils.url = "github:numtide/flake-utils"; + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + + crane = { + url = "github:ipetkov/crane"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + + flake-utils.url = "github:numtide/flake-utils"; + + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs = { + nixpkgs.follows = "nixpkgs"; + flake-utils.follows = "flake-utils"; + }; + }; + + advisory-db = { + url = "github:rustsec/advisory-db"; + flake = false; + }; }; - outputs = { nixpkgs, rust-overlay, flake-utils, ... }: + outputs = { self, nixpkgs, crane, flake-utils, rust-overlay, advisory-db, ... }: flake-utils.lib.eachDefaultSystem (system: let - overlays = [ (import rust-overlay) ]; pkgs = import nixpkgs { - inherit system overlays; + inherit system; + overlays = [ (import rust-overlay) ]; }; - in - with pkgs; - { - devShells.default = mkShell { + + inherit (pkgs) lib; + + rustCustomToolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; + + craneLib = (crane.mkLib pkgs).overrideToolchain rustCustomToolchain; + src = craneLib.cleanCargoSource (craneLib.path ./.); + + # Common arguments can be set here to avoid repeating them later + commonArgs = { + inherit src; + strictDeps = true; + + nativeBuildInputs = [ + pkgs.pkg-config + ]; + buildInputs = [ - just - (rust-bin.fromRustupToolchainFile ./rust-toolchain.toml) - mold - cargo-deny + # Add additional build inputs here + pkgs.openssl + pkgs.taplo + ] ++ lib.optionals pkgs.stdenv.isDarwin [ + # Additional darwin specific inputs can be set here + pkgs.libiconv + ]; + + # 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; + }); + in + { + checks = { + # Build the crate as part of `nix flake check` for convenience + inherit my-crate; + + # Run clippy (and deny all warnings) on the crate source, + # again, resuing 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. + my-crate-clippy = craneLib.cargoClippy (commonArgs // { + inherit cargoArtifacts; + cargoClippyExtraArgs = "--all-targets -- --deny warnings"; + }); + + my-crate-doc = craneLib.cargoDoc (commonArgs // { + inherit cargoArtifacts; + }); + + # Check formatting + my-crate-fmt = craneLib.cargoFmt { + inherit src; + }; + + # Audit dependencies + my-crate-audit = craneLib.cargoAudit { + inherit src advisory-db; + }; + + # Audit licenses + my-crate-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 + my-crate-nextest = craneLib.cargoNextest (commonArgs // { + inherit cargoArtifacts; + partitions = 1; + partitionType = "count"; + }); + }; + + packages = { + default = my-crate; + }; + + apps.default = flake-utils.lib.mkApp { + drv = 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 = with pkgs; [ cargo-watch + cargo-deny cargo-audit cargo-update cargo-edit cargo-outdated cargo-license cargo-tarpaulin - cargo-cross cargo-zigbuild cargo-nextest cargo-spellcheck cargo-modules cargo-bloat cargo-unused-features - taplo bacon helix - pkg-config - openssl - sqlite + just ]; }; - } - ); + }); } diff --git a/nixos/rust.nix b/nixos/rust.nix index d736144..927a445 100644 --- a/nixos/rust.nix +++ b/nixos/rust.nix @@ -8,7 +8,7 @@ environment.systemPackages = with pkgs; [ (rust-bin.fromRustupToolchainFile ./rust-toolchain.toml) - evcxr #rust repl + lldb_16 taplo #toml formatter & lsp cargo-watch cargo-deny @@ -26,6 +26,6 @@ cargo-bloat cargo-unused-features bacon - lldb_16 + evcxr #rust repl ]; }