feat: init with rust lib and bin crate

This commit is contained in:
steveej 2024-12-23 11:55:36 +01:00
commit ca266531a9
21 changed files with 990 additions and 0 deletions

View file

@ -0,0 +1,27 @@
{
pkgs,
flake,
system,
}:
pkgs.mkShell {
# Add build dependencies
packages = [
flake.formatter.${system}
pkgs.jq
];
# Add environment variables
env = { };
# Load custom bash code
shellHook =
# TODO(blocked/upstream): remove this once https://github.com/isbecker/treefmt-vscode/issues/3 is resolved
(flake.inputs.nixago.lib.${system}.make {
data = flake.formatter.${system}.settings;
output = "treefmt.toml";
format = "toml";
}).shellHook
+ ''
echo $(git rev-parse --show-toplevel)
'';
}

View file

@ -0,0 +1,34 @@
{
flake,
system,
pkgs,
...
}:
let
craneLib = flake.lib.mkCraneLib { inherit pkgs system; };
in
craneLib.devShell {
inputsFrom =
[
flake.devShells.${system}.default
]
# Inherit inputs from rust-workspace on the platforms it's available.
++ (pkgs.lib.lists.optionals
(pkgs.lib.meta.availableOn pkgs.stdenv.hostPlatform flake.packages.${system}.rust-workspace)
(
[
flake.packages.${system}.rust-workspace
]
++ (builtins.attrValues flake.packages.${system}.rust-workspace.passthru.tests)
)
);
# 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.natscli
];
}

View file

@ -0,0 +1,70 @@
{
pkgs,
inputs,
perSystem,
...
}:
let
settingsNix = {
package = perSystem.nixpkgs.treefmt2;
projectRootFile = ".git/config";
programs = {
nixfmt.enable = true;
deadnix = {
enable = true;
no-underscore = true;
};
statix.enable = true;
rustfmt.enable = true;
gofmt.enable = true;
shfmt.enable = true;
prettier.enable = true;
} // pkgs.lib.optionalAttrs (pkgs.system != "riscv64-linux") { shellcheck.enable = true; };
settings = {
global.excludes = [
"LICENSE"
# unsupported extensions
"*.{gif,png,svg,tape,mts,lock,mod,sum,toml,env,envrc,gitignore}"
];
formatter = {
deadnix = {
priority = 1;
};
nixfmt = {
priority = 2;
};
statix = {
priority = 3;
};
prettier = {
options = [
"--tab-width"
"2"
];
includes = [ "*.{css,html,js,json,jsx,md,mdx,scss,ts,yaml}" ];
};
};
};
};
treefmtEval = inputs.treefmt-nix.lib.evalModule pkgs settingsNix;
in
treefmtEval.config.build.wrapper.overrideAttrs (_: {
passthru = {
inherit (treefmtEval.config) package settings;
inherit (treefmtEval) config;
inherit settingsNix;
};
})

View file

@ -0,0 +1,13 @@
{ inputs, flake, ... }:
{
mkCraneLib =
{ pkgs, system }:
let
craneLib = inputs.crane.mkLib pkgs;
toolchain = (inputs.rust-overlay.lib.mkRustBin { } pkgs).fromRustupToolchainFile (
flake + "/rust-toolchain.toml"
);
in
craneLib.overrideToolchain toolchain;
}

View file

@ -0,0 +1,83 @@
/*
This exposes all crates in the workspace as a single package attribute.
It also enforces various tests.
Losely following the tutorial at https://crane.dev/examples/quick-start-workspace.html
*/
{
flake,
pkgs,
system,
...
}:
let
craneLib = flake.lib.mkCraneLib { inherit pkgs system; };
src = craneLib.cleanCargoSource flake;
commonArgs = {
inherit src;
strictDeps = true;
buildInputs = pkgs.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";
meta.platforms = pkgs.lib.platforms.linux;
};
# Build *just* the cargo dependencies (of the entire workspace),
# so we can reuse all of that work (e.g. via cachix) when running in CI
# It is *highly* recommended to use something like cargo-hakari to avoid
# cache misses when building individual top-level-crates
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
in
craneLib.cargoBuild (
commonArgs
// {
inherit cargoArtifacts;
# NB: we disable tests since we'll run them all via cargo-nextest
doCheck = false;
passthru.tests = {
clippy = craneLib.cargoClippy (
commonArgs
// {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
}
);
doc = craneLib.cargoDoc (
commonArgs
// {
inherit cargoArtifacts;
}
);
# Audit licenses
deny = craneLib.cargoDeny {
inherit src;
};
nextest = craneLib.cargoNextest (
commonArgs
// {
inherit cargoArtifacts;
nativeBuildInputs = [
# native test binaries go here
];
partitions = 1;
partitionType = "count";
}
);
};
}
)