From b4bc4ce59960c5271f31ecb042582f8c12f67d13 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Mon, 23 Dec 2024 20:52:28 -0600 Subject: [PATCH] Rust template --- templates/rust/flake.nix | 85 ++++-------------------------- templates/rust/nix/boilerplate.nix | 16 ++++++ templates/rust/nix/checks.nix | 25 +++++++++ templates/rust/nix/packages.nix | 52 ++++++++++++++++++ templates/rust/nix/shells.nix | 22 ++++++++ 5 files changed, 124 insertions(+), 76 deletions(-) create mode 100644 templates/rust/nix/boilerplate.nix create mode 100644 templates/rust/nix/checks.nix create mode 100644 templates/rust/nix/packages.nix create mode 100644 templates/rust/nix/shells.nix diff --git a/templates/rust/flake.nix b/templates/rust/flake.nix index 03a065f..af7240e 100644 --- a/templates/rust/flake.nix +++ b/templates/rust/flake.nix @@ -1,81 +1,14 @@ { - inputs = { - nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; - git-hooks.url = "github:cachix/git-hooks.nix"; - git-hooks.inputs.nixpkgs.follows = "nixpkgs"; - }; - outputs = { - self, - git-hooks, - nixpkgs, - }: let - inherit (self) outputs; - systems = ["aarch64-linux" "aarch64-darwin" "x86_64-darwin" "x86_64-linux"]; - forSystems = nixpkgs.lib.genAttrs systems; - pkgsFor = system: (import nixpkgs {inherit system;}).extend outputs.overlays.default; - genPkgs = func: (forSystems (system: func (pkgsFor system))); + inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + inputs.git-hooks.url = "github:cachix/git-hooks.nix"; + inputs.git-hooks.inputs.nixpkgs.follows = "nixpkgs"; + outputs = inputs: let + inherit (import nix/boilerplate.nix inputs) fullImport genPkgs; in { - checks = genPkgs (pkgs: { - git-hooks = git-hooks.lib.${pkgs.system}.run { - src = ./.; - hooks = { - alejandra.enable = true; - cargo-check.enable = true; - clippy = { - enable = true; - packageOverrides.cargo = pkgs.cargo; - packageOverrides.clippy = pkgs.rustPackages.clippy; - }; - rustfmt = { - enable = true; - packageOverrides.rustfmt = pkgs.rustfmt; - }; - }; - }; - }); - - packages = genPkgs (pkgs: { - my-package = pkgs.rustPlatform.buildRustPackage { - pname = "my-package"; - version = "0.1.0"; - - /* - nativeBuildInputs = with pkgs; [ - pkg-config - clang - ]; - - buildInputs = with pkgs; [ - ]; - */ - - src = ./.; - hash = pkgs.lib.fakeHash; - cargoHash = "sha256-W7VQlMktGsRPQL9VGVmxYV6C5u2eJ48S7eTpOM+3n8U="; - - RUSTFLAGS = pkgs.lib.optionalString pkgs.stdenv.isLinux "-C link-arg=-fuse-ld=mold"; - }; - - default = outputs.packages.${pkgs.system}.my-package; - }); - - devShells = genPkgs (pkgs: { - default = pkgs.mkShell { - inherit (self.checks.${pkgs.system}.git-hooks) shellHook; - inputsFrom = [outputs.packages.${pkgs.system}.default]; - packages = with pkgs; [ - rustPackages.clippy - rust-analyzer - rustfmt - lldb - ]; - }; - }); - - overlays = { - default = final: prev: {}; - }; - + # overlays = import nix/overlays.nix; + checks = fullImport nix/checks.nix; + packages = fullImport nix/packages.nix; + devShells = fullImport nix/shells.nix; formatter = genPkgs (p: p.alejandra); }; } diff --git a/templates/rust/nix/boilerplate.nix b/templates/rust/nix/boilerplate.nix new file mode 100644 index 0000000..2778326 --- /dev/null +++ b/templates/rust/nix/boilerplate.nix @@ -0,0 +1,16 @@ +inputs @ { + nixpkgs, + self, + ... +}: let + forSelfOverlay = + if builtins.hasAttr "forSelf" self.overlays + then self.overlays.forSelf + else (_: p: p); +in rec { + systems = ["aarch64-linux" "x86_64-linux" "x86_64-darwin" "aarch64-darwin"]; + forSystems = nixpkgs.lib.genAttrs systems; + pkgsFor = system: ((import nixpkgs {inherit system;}).extend forSelfOverlay); + genPkgs = func: (forSystems (system: func (pkgsFor system))); + call = imported: genPkgs (pkgs: imported (inputs // {inherit pkgs;})); +} diff --git a/templates/rust/nix/checks.nix b/templates/rust/nix/checks.nix new file mode 100644 index 0000000..6d6b07b --- /dev/null +++ b/templates/rust/nix/checks.nix @@ -0,0 +1,25 @@ +{ + pkgs, + git-hooks, + ... +}: { + git-hooks = git-hooks.lib.${pkgs.system}.run { + src = ./..; + hooks = { + alejandra.enable = true; + cargo-check.enable = true; + convco.enable = true; + cargo-test = { + enable = true; + name = "cargo-test"; + entry = "cargo test"; + # types = ["rust"]; + # language = "rust"; + pass_filenames = false; + stages = ["pre-commit"]; + }; + clippy.enable = true; + rustfmt.enable = true; + }; + }; +} diff --git a/templates/rust/nix/packages.nix b/templates/rust/nix/packages.nix new file mode 100644 index 0000000..7d1b63b --- /dev/null +++ b/templates/rust/nix/packages.nix @@ -0,0 +1,52 @@ +{pkgs, ...}: rec { + lyrs = pkgs.rustPlatform.buildRustPackage { + pname = "lyrs"; + version = "0.1.0"; + + /* + nativeBuildInputs = with pkgs; [ + pkg-config + clang + ]; + + buildInputs = with pkgs; [ + ]; + */ + + src = ./..; + hash = pkgs.lib.fakeHash; + cargoHash = "sha256-XHCXOlG4sdr1A3lqIK/7bB3soms1jxMIdfsFABmHVog="; + }; + + pwatch = pkgs.writeShellScriptBin "pwatch" '' + dir="$(dirname "$(cargo locate-project --workspace --message-format plain)")" + pushd "$dir" + + additional_watchexec_args="" + if [[ -f apps/$pkg/build.rs ]]; then + additional_watchexec_args="--watch apps/$pkg/build.rs" + fi + + pkg="$1"; shift + cargo_subcmd="$1"; shift + cargo_subcmd_args="$@"; shift + + argfile="apps/$pkg/.watchexec.argfile" + argfile_args="" + + if [[ -f $argfile ]]; then + argfile_args="@$argfile" + fi + watchexec $argfile_args --stop-timeout 0s --restart \ + --watch Cargo.toml \ + --watch libs \ + --watch apps/$pkg/src/ \ + --watch apps/$pkg/Cargo.toml \ + $additional_watchexec_args \ + cargo "$cargo_subcmd" --package "$pkg" "$cargo_subcmd_args" + + popd + ''; + + default = lyrs; +} diff --git a/templates/rust/nix/shells.nix b/templates/rust/nix/shells.nix new file mode 100644 index 0000000..a5f3e2b --- /dev/null +++ b/templates/rust/nix/shells.nix @@ -0,0 +1,22 @@ +{ + self, + pkgs, + ... +}: let + inherit (pkgs) system; +in rec { + lyrs-dev = pkgs.mkShell { + inherit (self.checks.${system}.git-hooks) shellHook; + inputsFrom = [self.packages.${system}.lyrs]; + packages = with pkgs; [ + convco + rustPackages.clippy + typescript-language-server + rust-analyzer + rustfmt + nixd + lldb + ]; + }; + default = lyrs-dev; +}