From ed51574014d4af183497cb901ab01f8dd0fc9b3d Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Fri, 3 May 2024 19:35:19 +0200 Subject: [PATCH] chore: update template --- Cargo.toml | 3 +- flake.lock | 92 ++++++++++++-------------------- flake.nix | 124 ++++++++++++++++++++++++++------------------ rust-toolchain.toml | 3 ++ treefmt.nix | 8 +++ 5 files changed, 117 insertions(+), 113 deletions(-) create mode 100644 rust-toolchain.toml create mode 100644 treefmt.nix diff --git a/Cargo.toml b/Cargo.toml index ad4f76e..f8a067d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,5 @@ [package] -authors = ["Sridhar Ratnakumar "] +authors = ["Toby "] edition = "2021" -# If you change the name here, you must also do it in flake.nix (and run `cargo generate-lockfile` afterwards) name = "rust-nix-template" version = "0.1.0" diff --git a/flake.lock b/flake.lock index 5a82d44..71d9551 100644 --- a/flake.lock +++ b/flake.lock @@ -1,64 +1,33 @@ { "nodes": { - "flake-parts": { + "fenix": { "inputs": { - "nixpkgs-lib": "nixpkgs-lib" + "nixpkgs": [ + "nixpkgs" + ], + "rust-analyzer-src": "rust-analyzer-src" }, "locked": { - "lastModified": 1696343447, - "narHash": "sha256-B2xAZKLkkeRFG5XcHHSXXcP7To9Xzr59KXeZiRf4vdQ=", - "owner": "hercules-ci", - "repo": "flake-parts", - "rev": "c9afaba3dfa4085dbd2ccb38dfade5141e33d9d4", + "lastModified": 1714631076, + "narHash": "sha256-at4+1R9gx3CGvX0ZJo9GwDZyt3RzOft7qDCTsYHjI4M=", + "owner": "nix-community", + "repo": "fenix", + "rev": "22a9eb3f20dd340d084cee4426f386a90b1351ca", "type": "github" }, "original": { - "owner": "hercules-ci", - "repo": "flake-parts", + "owner": "nix-community", + "repo": "fenix", "type": "github" } }, "nixpkgs": { "locked": { - "lastModified": 1704626572, - "narHash": "sha256-VwRTEKzK4wSSv64G+g3RLF3t6yBHrhR2VK3kZ5UWisU=", + "lastModified": 1714635257, + "narHash": "sha256-4cPymbty65RvF1DWQfc+Bc8B233A1BWxJnNULJKQ1EY=", "owner": "nixos", "repo": "nixpkgs", - "rev": "24fe8bb4f552ad3926274d29e083b79d84707da6", - "type": "github" - }, - "original": { - "owner": "nixos", - "ref": "nixos-unstable", - "repo": "nixpkgs", - "type": "github" - } - }, - "nixpkgs-lib": { - "locked": { - "dir": "lib", - "lastModified": 1696019113, - "narHash": "sha256-X3+DKYWJm93DRSdC5M6K5hLqzSya9BjibtBsuARoPco=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "f5892ddac112a1e9b3612c39af1b72987ee5783a", - "type": "github" - }, - "original": { - "dir": "lib", - "owner": "NixOS", - "ref": "nixos-unstable", - "repo": "nixpkgs", - "type": "github" - } - }, - "nixpkgs_2": { - "locked": { - "lastModified": 1695644571, - "narHash": "sha256-asS9dCCdlt1lPq0DLwkVBbVoEKuEuz+Zi3DG7pR/RxA=", - "owner": "nixos", - "repo": "nixpkgs", - "rev": "6500b4580c2a1f3d0f980d32d285739d8e156d92", + "rev": "63c3a29ca82437c87573e4c6919b09a24ea61b0f", "type": "github" }, "original": { @@ -70,37 +39,40 @@ }, "root": { "inputs": { - "flake-parts": "flake-parts", + "fenix": "fenix", "nixpkgs": "nixpkgs", - "systems": "systems", "treefmt-nix": "treefmt-nix" } }, - "systems": { + "rust-analyzer-src": { + "flake": false, "locked": { - "lastModified": 1681028828, - "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", - "owner": "nix-systems", - "repo": "default", - "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "lastModified": 1714572655, + "narHash": "sha256-xjD8vmit0Nz1qaSSSpeXOK3saSvAZtOGHS2SHZE75Ek=", + "owner": "rust-lang", + "repo": "rust-analyzer", + "rev": "cfce2bb46da62950a8b70ddb0b2a12332da1b1e1", "type": "github" }, "original": { - "owner": "nix-systems", - "repo": "default", + "owner": "rust-lang", + "ref": "nightly", + "repo": "rust-analyzer", "type": "github" } }, "treefmt-nix": { "inputs": { - "nixpkgs": "nixpkgs_2" + "nixpkgs": [ + "nixpkgs" + ] }, "locked": { - "lastModified": 1697388351, - "narHash": "sha256-63N2eBpKaziIy4R44vjpUu8Nz5fCJY7okKrkixvDQmY=", + "lastModified": 1714058656, + "narHash": "sha256-Qv4RBm4LKuO4fNOfx9wl40W2rBbv5u5m+whxRYUMiaA=", "owner": "numtide", "repo": "treefmt-nix", - "rev": "aae39f64f5ecbe89792d05eacea5cb241891292a", + "rev": "c6aaf729f34a36c445618580a9f95a48f5e4e03f", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 4c63665..2e81b1d 100644 --- a/flake.nix +++ b/flake.nix @@ -1,62 +1,84 @@ { + description = "A Nix-flake-based Rust development environment"; + inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; - flake-parts.url = "github:hercules-ci/flake-parts"; - systems.url = "github:nix-systems/default"; - # Dev tools - treefmt-nix.url = "github:numtide/treefmt-nix"; + treefmt-nix = { + url = "github:numtide/treefmt-nix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + + fenix = { + url = "github:nix-community/fenix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; }; - outputs = inputs: - inputs.flake-parts.lib.mkFlake { inherit inputs; } { - systems = import inputs.systems; - imports = [ - inputs.treefmt-nix.flakeModule + outputs = + { + self, + nixpkgs, + treefmt-nix, + fenix, + }: + let + supportedSystems = [ + "x86_64-linux" + "aarch64-linux" + "x86_64-darwin" + "aarch64-darwin" ]; - perSystem = { config, self', pkgs, lib, system, ... }: - let - cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml); - nonRustDeps = [ - ]; - rust-toolchain = pkgs.symlinkJoin { - name = "rust-toolchain"; - paths = [ pkgs.rustc pkgs.cargo pkgs.rust-analyzer pkgs.rustPlatform.rustcSrc ]; - }; - in + forEachSupportedSystem = + f: + nixpkgs.lib.genAttrs supportedSystems ( + system: + f ( + let + pkgs = import nixpkgs { inherit system; }; + fenix' = (import fenix { inherit system pkgs; }); + toolchain = fenix'.complete.withComponents ( + (builtins.fromTOML (builtins.readFile ./rust-toolchain.toml)).toolchain.components + ); + in + { + inherit pkgs toolchain; + } + ) + ); + in + { + formatter = forEachSupportedSystem ( + { pkgs, ... }: (treefmt-nix.lib.evalModule pkgs ./treefmt.nix).config.build.wrapper + ); + devShells = forEachSupportedSystem ( + { pkgs, toolchain }: { - # Rust package - packages.default = pkgs.rustPlatform.buildRustPackage { - inherit (cargoToml.package) name version; - src = ./.; - cargoLock.lockFile = ./Cargo.lock; + default = pkgs.mkShell { + nativeBuildInputs = [ toolchain ]; + buildInputs = [ toolchain ]; }; - - # Rust dev environment - devShells.default = pkgs.mkShell { - inputsFrom = [ - config.treefmt.build.devShell - ]; - shellHook = '' - # For rust-analyzer 'hover' tooltips to work. - export RUST_SRC_PATH=${pkgs.rustPlatform.rustLibSrc} - ''; - buildInputs = nonRustDeps; - nativeBuildInputs = [ - rust-toolchain - ]; - RUST_BACKTRACE = 1; - }; - - # Add your auto-formatters here. - # cf. https://numtide.github.io/treefmt/ - treefmt.config = { - projectRootFile = "flake.nix"; - programs = { - nixpkgs-fmt.enable = true; - rustfmt.enable = true; - }; - }; - }; + } + ); + packages = forEachSupportedSystem ( + { pkgs, toolchain }: + { + default = ( + let + manifest = (pkgs.lib.importTOML ./Cargo.toml).package; + rustPlatform = pkgs.makeRustPlatform { + cargo = toolchain; + rustc = toolchain; + }; + in + rustPlatform.buildRustPackage { + pname = manifest.name; + version = manifest.version; + cargoLock.lockFile = ./Cargo.lock; + src = pkgs.lib.cleanSource ./.; + } + ); + } + ); }; } diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..7fd885e --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,3 @@ +[toolchain] +channel = "stable" +components = ["rustfmt", "cargo", "rustc", "rust-src"] diff --git a/treefmt.nix b/treefmt.nix new file mode 100644 index 0000000..a54f850 --- /dev/null +++ b/treefmt.nix @@ -0,0 +1,8 @@ +{ ... }: +{ + projectRootFile = "flake.nix"; + programs = { + nixfmt-rfc-style.enable = true; + rustfmt.enable = true; + }; +}