chore: update template

This commit is contained in:
Tobias Berger 2024-05-03 19:35:19 +02:00
parent 9af4e6c6c2
commit ed51574014
Signed by: toby
GPG key ID: 2D05EFAB764D6A88
5 changed files with 117 additions and 113 deletions

View file

@ -1,6 +1,5 @@
[package] [package]
authors = ["Sridhar Ratnakumar <srid@srid.ca>"] authors = ["Toby <toby@tobot.dev>"]
edition = "2021" 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" name = "rust-nix-template"
version = "0.1.0" version = "0.1.0"

92
flake.lock generated
View file

@ -1,64 +1,33 @@
{ {
"nodes": { "nodes": {
"flake-parts": { "fenix": {
"inputs": { "inputs": {
"nixpkgs-lib": "nixpkgs-lib" "nixpkgs": [
"nixpkgs"
],
"rust-analyzer-src": "rust-analyzer-src"
}, },
"locked": { "locked": {
"lastModified": 1696343447, "lastModified": 1714631076,
"narHash": "sha256-B2xAZKLkkeRFG5XcHHSXXcP7To9Xzr59KXeZiRf4vdQ=", "narHash": "sha256-at4+1R9gx3CGvX0ZJo9GwDZyt3RzOft7qDCTsYHjI4M=",
"owner": "hercules-ci", "owner": "nix-community",
"repo": "flake-parts", "repo": "fenix",
"rev": "c9afaba3dfa4085dbd2ccb38dfade5141e33d9d4", "rev": "22a9eb3f20dd340d084cee4426f386a90b1351ca",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "hercules-ci", "owner": "nix-community",
"repo": "flake-parts", "repo": "fenix",
"type": "github" "type": "github"
} }
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1704626572, "lastModified": 1714635257,
"narHash": "sha256-VwRTEKzK4wSSv64G+g3RLF3t6yBHrhR2VK3kZ5UWisU=", "narHash": "sha256-4cPymbty65RvF1DWQfc+Bc8B233A1BWxJnNULJKQ1EY=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "24fe8bb4f552ad3926274d29e083b79d84707da6", "rev": "63c3a29ca82437c87573e4c6919b09a24ea61b0f",
"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",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -70,37 +39,40 @@
}, },
"root": { "root": {
"inputs": { "inputs": {
"flake-parts": "flake-parts", "fenix": "fenix",
"nixpkgs": "nixpkgs", "nixpkgs": "nixpkgs",
"systems": "systems",
"treefmt-nix": "treefmt-nix" "treefmt-nix": "treefmt-nix"
} }
}, },
"systems": { "rust-analyzer-src": {
"flake": false,
"locked": { "locked": {
"lastModified": 1681028828, "lastModified": 1714572655,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", "narHash": "sha256-xjD8vmit0Nz1qaSSSpeXOK3saSvAZtOGHS2SHZE75Ek=",
"owner": "nix-systems", "owner": "rust-lang",
"repo": "default", "repo": "rust-analyzer",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", "rev": "cfce2bb46da62950a8b70ddb0b2a12332da1b1e1",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "nix-systems", "owner": "rust-lang",
"repo": "default", "ref": "nightly",
"repo": "rust-analyzer",
"type": "github" "type": "github"
} }
}, },
"treefmt-nix": { "treefmt-nix": {
"inputs": { "inputs": {
"nixpkgs": "nixpkgs_2" "nixpkgs": [
"nixpkgs"
]
}, },
"locked": { "locked": {
"lastModified": 1697388351, "lastModified": 1714058656,
"narHash": "sha256-63N2eBpKaziIy4R44vjpUu8Nz5fCJY7okKrkixvDQmY=", "narHash": "sha256-Qv4RBm4LKuO4fNOfx9wl40W2rBbv5u5m+whxRYUMiaA=",
"owner": "numtide", "owner": "numtide",
"repo": "treefmt-nix", "repo": "treefmt-nix",
"rev": "aae39f64f5ecbe89792d05eacea5cb241891292a", "rev": "c6aaf729f34a36c445618580a9f95a48f5e4e03f",
"type": "github" "type": "github"
}, },
"original": { "original": {

120
flake.nix
View file

@ -1,62 +1,84 @@
{ {
description = "A Nix-flake-based Rust development environment";
inputs = { inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; 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 = {
treefmt-nix.url = "github:numtide/treefmt-nix"; url = "github:numtide/treefmt-nix";
inputs.nixpkgs.follows = "nixpkgs";
}; };
outputs = inputs: fenix = {
inputs.flake-parts.lib.mkFlake { inherit inputs; } { url = "github:nix-community/fenix";
systems = import inputs.systems; inputs.nixpkgs.follows = "nixpkgs";
imports = [ };
inputs.treefmt-nix.flakeModule };
];
perSystem = { config, self', pkgs, lib, system, ... }: outputs =
{
self,
nixpkgs,
treefmt-nix,
fenix,
}:
let let
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml); supportedSystems = [
nonRustDeps = [ "x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
]; ];
rust-toolchain = pkgs.symlinkJoin { forEachSupportedSystem =
name = "rust-toolchain"; f:
paths = [ pkgs.rustc pkgs.cargo pkgs.rust-analyzer pkgs.rustPlatform.rustcSrc ]; 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 in
{ {
# Rust package inherit pkgs toolchain;
packages.default = pkgs.rustPlatform.buildRustPackage { }
inherit (cargoToml.package) name version; )
src = ./.; );
in
{
formatter = forEachSupportedSystem (
{ pkgs, ... }: (treefmt-nix.lib.evalModule pkgs ./treefmt.nix).config.build.wrapper
);
devShells = forEachSupportedSystem (
{ pkgs, toolchain }:
{
default = pkgs.mkShell {
nativeBuildInputs = [ toolchain ];
buildInputs = [ toolchain ];
};
}
);
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; cargoLock.lockFile = ./Cargo.lock;
}; src = pkgs.lib.cleanSource ./.;
}
# 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;
};
};
};
}; };
} }

3
rust-toolchain.toml Normal file
View file

@ -0,0 +1,3 @@
[toolchain]
channel = "stable"
components = ["rustfmt", "cargo", "rustc", "rust-src"]

8
treefmt.nix Normal file
View file

@ -0,0 +1,8 @@
{ ... }:
{
projectRootFile = "flake.nix";
programs = {
nixfmt-rfc-style.enable = true;
rustfmt.enable = true;
};
}