diff --git a/modules/treefmt/check.nix b/modules/treefmt/check.nix new file mode 100644 index 0000000..9ca84d5 --- /dev/null +++ b/modules/treefmt/check.nix @@ -0,0 +1,21 @@ +{ + pkgs, + self, +}: +let + treefmtWrapped = + (self.wrapperModules.treefmt.apply { + inherit pkgs; + settings = { + formatter.nixfmt = { + command = "nixpkgs-fmt"; + includes = [ "*.nix" ]; + }; + }; + programs = [ pkgs.nixpkgs-fmt ]; + }).wrapper; +in +pkgs.runCommand "treefmt-test" { } '' + "${treefmtWrapped}/bin/treefmt" --version | grep -q "treefmt" + touch $out +'' diff --git a/modules/treefmt/module.nix b/modules/treefmt/module.nix new file mode 100644 index 0000000..178b99d --- /dev/null +++ b/modules/treefmt/module.nix @@ -0,0 +1,96 @@ +{ + config, + lib, + wlib, + ... +}: +let + tomlFmt = config.pkgs.formats.toml { }; +in +{ + _class = "wrapper"; + + options = { + settings = lib.mkOption { + type = lib.types.submodule { + freeformType = tomlFmt.type; + options = { + excludes = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = [ ]; + description = "Global list of paths to exclude. Supports glob."; + }; + formatter = lib.mkOption { + type = lib.types.attrsOf ( + lib.types.submodule { + freeformType = tomlFmt.type; + options = { + command = lib.mkOption { + type = lib.types.str; + description = "Executable name or path obeying the treefmt formatter spec."; + }; + options = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = [ ]; + description = "Arguments to pass to the command."; + }; + includes = lib.mkOption { + type = lib.types.listOf lib.types.str; + description = "File patterns to include for formatting. Supports glob."; + }; + excludes = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = [ ]; + description = "File patterns to exclude from formatting."; + }; + }; + } + ); + default = { }; + description = "Set of formatters to use."; + }; + }; + }; + default = { }; + description = '' + Structured treefmt configuration written to treefmt.toml. + See + ''; + }; + + programs = lib.mkOption { + type = lib.types.listOf lib.types.package; + default = [ ]; + description = '' + Formatter packages to add to PATH. + ''; + }; + + "treefmt.toml" = lib.mkOption { + type = wlib.types.file config.pkgs; + default.path = tomlFmt.generate "treefmt.toml" ( + lib.filterAttrsRecursive (_: v: v != null) config.settings + ); + description = '' + The generated treefmt.toml configuration file. + ''; + }; + }; + + config.package = config.pkgs.treefmt; + + config.flags = { + "--config-file" = toString config."treefmt.toml".path; + }; + + config.extraPackages = config.programs; + + config.meta.maintainers = [ + { + name = "Alexander Kenji Berthold"; + github = "a-kenji"; + githubId = 65275785; + } + ]; + config.meta.platforms = lib.platforms.all; +}