feat: move to a flake-parts based flake where templates import flake modules from the root flake

This commit is contained in:
2026-04-26 00:44:51 -05:00
parent f3d81b82f0
commit b1a2a8d97b
3 changed files with 206 additions and 73 deletions
+20 -5
View File
@@ -1,12 +1,27 @@
{
description = "A collection of templates for various programming languages";
outputs = {self}: {
templates = {
rust = {
path = ./rust;
description = "A rust flake with crane and templates for command args and configs";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
treefmt-nix = {
url = "github:numtide/treefmt-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
crane.url = "github:ipetkov/crane";
};
outputs = {flake-parts, ...} @ inputs:
flake-parts.lib.mkFlake {inherit inputs;} {
imports = [
./rust.nix
];
systems = ["aarch64-linux" "x86_64-linux" "aarch64-darwin" "x86_64-darwin"];
perSystem = {pkgs, ...}: {formatter = pkgs.alejandra;};
};
}
+174
View File
@@ -0,0 +1,174 @@
{
flake-parts-lib,
inputs,
...
}: {
flake = {
templates = {
rust = {
path = ./rust;
description = "A rust flake with crane and templates for command args and configs";
};
};
flakeModules.rust = {
imports = [
inputs.treefmt-nix.flakeModule
];
options.perSystem = flake-parts-lib.mkPerSystemOption ({
lib,
config,
pkgs,
self',
...
}: let
cfg = config.krantz.rust;
craneLib = inputs.crane.mkLib pkgs;
src = cfg.srcFiltered;
manifest = (lib.importTOML "${src}/Cargo.toml").package;
buildInputs = cfg.runtimeDeps;
# build dependencies
nativeBuildInputs = cfg.buildDeps;
# Build *just* the cargo dependencies, so we can reuse
# all of that work (e.g. via cachix) when running in CI
cargoArtifacts = craneLib.buildDepsOnly {
inherit src buildInputs nativeBuildInputs;
};
in {
options.krantz.rust = {
enable = lib.mkEnableOption "building a cargo project with crane";
src = lib.mkOption {
description = "The path to the root of the crate.";
type = lib.types.path;
example = lib.literalExpression "./.";
};
srcFiltered = lib.mkOption {
description = "A path to the filtered directory of the root of the crate.";
type = lib.types.path;
default = craneLib.cleanCargoSource cfg.src;
defaultText = lib.literalExpression "config.krantz.rust.craneLib cfg.src";
example = lib.literalExpression "./.";
};
runtimeDeps = lib.mkOption {
description = "Packages needed for the app to run.";
type = lib.types.listOf lib.types.package;
default = [];
example = with pkgs; [
openssl
];
};
buildDeps = lib.mkOption {
description = "Packages needed for the app to build.";
type = lib.types.listOf lib.types.package;
default = [];
example = with pkgs; [
pkg-config
rustPlatform.bindgenHook
];
};
devDeps = lib.mkOption {
description = "Packages to include only in the devShell.";
type = lib.types.listOf lib.types.package;
default = with pkgs; [
rust-analyzer
];
};
devShell = {
enable = lib.mkEnableOption "a rust development environment" // {default = true;};
name = lib.mkOption {
description = "The name of the attribute in devShells to configure";
type = lib.types.str;
default = "default";
example = "rust";
};
extraAttrs = lib.mkOption {
description = "Extra attributes to merge into the devshell. See https://crane.dev/API.html#cranelibdevshell";
type = lib.types.attrs;
default = {};
example = {
shellHook = ''
export DEBUG=1
'';
};
};
};
package = {
enable = lib.mkEnableOption "the compiled cargo project" // {default = true;};
name = lib.mkOption {
description = "The name of the attribute in packages to configure";
type = lib.types.str;
default = "default";
example = "lib";
};
extraAttrs = lib.mkOption {
description = "Extra attributes to merge into the package. See https://crane.dev/API.html#cranelibbuildpackage";
type = lib.types.attrs;
default = {};
example = {
doCheck = false;
};
};
};
craneLib = lib.mkOption {
description = "The crane lib instance this module is using. Useful for overriding options";
default = craneLib;
defaultText = lib.literalExpression "crane.mkLib pkgs";
readOnly = true;
};
};
config = lib.mkIf cfg.enable {
# Dev Shell that lets you enter an environment with all the necessary utilites
# Available through 'nix develop'
# Also can be activated automatically if direnv is installed on the system with 'direnv allow'
devShells.${cfg.devShell.name} = lib.mkIf cfg.devShell.enable (craneLib.devShell {
inherit (self') checks;
# extra tooling dependencies
packages = cfg.devDeps;
inputsFrom = [self'.packages.${cfg.package.name}];
}
// cfg.devShell.extraAttrs);
# Formatter for nix files, available through 'nix fmt'
treefmt = {
programs.alejandra.enable = lib.mkDefault true;
programs.rustfmt = {
enable = true;
edition = manifest.edition;
};
};
# Your custom packages
# Accessible through 'nix build', 'nix shell', 'nix run', etc
packages.${cfg.package.name} = lib.mkIf cfg.package.enable (craneLib.buildPackage {
inherit src buildInputs nativeBuildInputs cargoArtifacts;
}
// cfg.package.extraAttrs);
checks = {
clippy = craneLib.cargoClippy {
inherit src buildInputs nativeBuildInputs cargoArtifacts;
cargoClippyExtraArgs = "-- --deny warnings";
};
};
};
});
};
};
}
+10 -66
View File
@@ -6,88 +6,32 @@
flake-parts.url = "github:hercules-ci/flake-parts";
treefmt-nix = {
url = "github:numtide/treefmt-nix";
project-templates = {
url = "git+ssh://gitea@git.krantz.one/reed/project-templates";
inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-parts.follows = "flake-parts";
};
crane.url = "github:ipetkov/crane";
};
outputs = {
flake-parts,
treefmt-nix,
crane,
project-templates,
...
} @ inputs:
flake-parts.lib.mkFlake {inherit inputs;} {
imports = [
treefmt-nix.flakeModule
project-templates.flakeModules.rust
];
systems = ["aarch64-linux" "x86_64-linux" "aarch64-darwin" "x86_64-darwin"];
perSystem = {
pkgs,
lib,
...
}: let
craneLib = crane.mkLib pkgs;
src = craneLib.cleanCargoSource ./.;
manifest = (lib.importTOML "${src}/Cargo.toml").package;
# runtime dependencies
buildInputs = with pkgs; [
# openssl
];
# build dependencies
nativeBuildInputs = with pkgs; [
# pkg-config
];
# Build *just* the cargo dependencies, so we can reuse
# all of that work (e.g. via cachix) when running in CI
cargoArtifacts = craneLib.buildDepsOnly {
inherit src buildInputs nativeBuildInputs;
};
in rec {
# Dev Shell that lets you enter an environment with all the necessary utilites
# Available through 'nix develop'
# Also can be activated automatically if direnv is installed on the system with 'direnv allow'
devShells.default = craneLib.devShell {
inherit checks;
# extra tooling dependencies
packages = with pkgs; [
rust-analyzer
];
inputsFrom = [packages.default];
};
# Formatter for nix files, available through 'nix fmt'
treefmt = {
programs.alejandra.enable = true;
programs.rustfmt = {
perSystem = {pkgs, ...}: {
krantz.rust = {
enable = true;
edition = manifest.edition;
};
};
src = ./.;
# Your custom packages
# Accessible through 'nix build', 'nix shell', 'nix run', etc
packages = {
default = craneLib.buildPackage {
inherit src buildInputs nativeBuildInputs cargoArtifacts;
};
};
checks = {
clippy = craneLib.cargoClippy {
inherit src buildInputs nativeBuildInputs cargoArtifacts;
cargoClippyExtraArgs = "-- --deny warnings";
};
# runtimeDeps = with pkgs; [];
# buildDeps = with pkgs; [];
};
};
};