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 6d342e20d1
4 changed files with 304 additions and 73 deletions
Generated
+98
View File
@@ -0,0 +1,98 @@
{
"nodes": {
"crane": {
"locked": {
"lastModified": 1777149905,
"narHash": "sha256-WMC5ktOHtCyJL/jtlf46P+I4Vhw938+oLlSpM8gwQwU=",
"owner": "ipetkov",
"repo": "crane",
"rev": "60c829383710f7e26454e6677c4d3adbc56dd3b8",
"type": "github"
},
"original": {
"owner": "ipetkov",
"repo": "crane",
"type": "github"
}
},
"flake-parts": {
"inputs": {
"nixpkgs-lib": "nixpkgs-lib"
},
"locked": {
"lastModified": 1775087534,
"narHash": "sha256-91qqW8lhL7TLwgQWijoGBbiD4t7/q75KTi8NxjVmSmA=",
"owner": "hercules-ci",
"repo": "flake-parts",
"rev": "3107b77cd68437b9a76194f0f7f9c55f2329ca5b",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "flake-parts",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1776877367,
"narHash": "sha256-EHq1/OX139R1RvBzOJ0aMRT3xnWyqtHBRUBuO1gFzjI=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "0726a0ecb6d4e08f6adced58726b95db924cef57",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-lib": {
"locked": {
"lastModified": 1774748309,
"narHash": "sha256-+U7gF3qxzwD5TZuANzZPeJTZRHS29OFQgkQ2kiTJBIQ=",
"owner": "nix-community",
"repo": "nixpkgs.lib",
"rev": "333c4e0545a6da976206c74db8773a1645b5870a",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "nixpkgs.lib",
"type": "github"
}
},
"root": {
"inputs": {
"crane": "crane",
"flake-parts": "flake-parts",
"nixpkgs": "nixpkgs",
"treefmt-nix": "treefmt-nix"
}
},
"treefmt-nix": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1775636079,
"narHash": "sha256-pc20NRoMdiar8oPQceQT47UUZMBTiMdUuWrYu2obUP0=",
"owner": "numtide",
"repo": "treefmt-nix",
"rev": "790751ff7fd3801feeaf96d7dc416a8d581265ba",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "treefmt-nix",
"type": "github"
}
}
},
"root": "root",
"version": 7
}
+21 -6
View File
@@ -1,12 +1,27 @@
{ {
description = "A collection of templates for various programming languages"; description = "A collection of templates for various programming languages";
outputs = {self}: { inputs = {
templates = { nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
rust = {
path = ./rust; flake-parts.url = "github:hercules-ci/flake-parts";
description = "A rust flake with crane and templates for command args and configs";
}; 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";
};
};
};
});
};
};
}
+11 -67
View File
@@ -6,88 +6,32 @@
flake-parts.url = "github:hercules-ci/flake-parts"; flake-parts.url = "github:hercules-ci/flake-parts";
treefmt-nix = { project-templates = {
url = "github:numtide/treefmt-nix"; url = "git+ssh://gitea@git.krantz.one/reed/project-templates";
inputs.nixpkgs.follows = "nixpkgs"; inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-parts.follows = "flake-parts";
}; };
crane.url = "github:ipetkov/crane";
}; };
outputs = { outputs = {
flake-parts, flake-parts,
treefmt-nix, project-templates,
crane,
... ...
} @ inputs: } @ inputs:
flake-parts.lib.mkFlake {inherit inputs;} { flake-parts.lib.mkFlake {inherit inputs;} {
imports = [ imports = [
treefmt-nix.flakeModule project-templates.flakeModules.rust
]; ];
systems = ["aarch64-linux" "x86_64-linux" "aarch64-darwin" "x86_64-darwin"]; systems = ["aarch64-linux" "x86_64-linux" "aarch64-darwin" "x86_64-darwin"];
perSystem = { perSystem = {pkgs, ...}: {
pkgs, krantz.rust = {
lib, enable = true;
... src = ./.;
}: let
craneLib = crane.mkLib pkgs;
src = craneLib.cleanCargoSource ./.;
manifest = (lib.importTOML "${src}/Cargo.toml").package;
# runtime dependencies # runtimeDeps = with pkgs; [];
buildInputs = with pkgs; [ # buildDeps = 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 = {
enable = true;
edition = manifest.edition;
};
};
# 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";
};
}; };
}; };
}; };