{
  pkgs,
  config,
  ...
}:

let
  nightly = true;
  extensions = [
    "rust-src"
    "rust-analyzer"
  ];
  targets = [
    "aarch64-apple-darwin"
    # "wasm32-wasi"
  ];
  overrideToolchain = t: t.default.override { inherit extensions targets; };
in
{
  home.sessionVariables.CARGO_HOME = "${config.xdg.dataHome}/cargo";
  home.sessionVariables.RUSTUP_HOME = "${config.xdg.dataHome}/rustup";

  home.packages = with pkgs; [
    (
      if nightly then
        (rust-bin.selectLatestNightlyWith overrideToolchain)
      else
        (overrideToolchain rust.bin.stable.latest)
    )
  ];

  programs.nixvim.plugins.lsp.servers.rust-analyzer = {
    enable = true;
    package = null;
    installCargo = false;
    installRustc = false;
    extraOptions = {
      settings = {
        rust-analyzer = {
          checkOnSave = {
            command = "clippy";
          };
        };
      };
    };
  };
}