From 0a364342a66bcecfc8a00d2fc38c6c29eee7bc5e Mon Sep 17 00:00:00 2001 From: Rachit Kumar Verma Date: Wed, 26 Aug 2026 18:53:02 +0530 Subject: [PATCH] stylua: init module Stylua is deterministic formatter for Lua 5.1, 5.2, 5.3, 5.4, LuaJIT, Luau and CfxLua/FiveM Lua, built using full-moon. StyLua is inspired by the likes of prettier, it parses your Lua codebase, and prints it back out from scratch, enforcing a consistent code style. --- .../misc/news/2026/08/2026-08-24_17-55-03.nix | 13 ++++ modules/programs/stylua.nix | 72 +++++++++++++++++++ tests/modules/programs/stylua/default.nix | 4 ++ .../modules/programs/stylua/empty-config.nix | 6 ++ .../programs/stylua/example-config.nix | 20 ++++++ .../programs/stylua/expected-config.toml | 7 ++ 6 files changed, 122 insertions(+) create mode 100644 modules/misc/news/2026/08/2026-08-24_17-55-03.nix create mode 100644 modules/programs/stylua.nix create mode 100644 tests/modules/programs/stylua/default.nix create mode 100644 tests/modules/programs/stylua/empty-config.nix create mode 100644 tests/modules/programs/stylua/example-config.nix create mode 100644 tests/modules/programs/stylua/expected-config.toml diff --git a/modules/misc/news/2026/08/2026-08-24_17-55-03.nix b/modules/misc/news/2026/08/2026-08-24_17-55-03.nix new file mode 100644 index 0000000000..95504f6b07 --- /dev/null +++ b/modules/misc/news/2026/08/2026-08-24_17-55-03.nix @@ -0,0 +1,13 @@ +{ + time = "2026-08-24T12:25:03+00:00"; + condition = true; + message = '' + A new module is available: `programs.stylua` + + Stylua is a formatter for lua files. It parses your Lua codebase, and prints + it back from scratch, enforcing a consistent code style. It's inspired by + the likes of prettier. + + See for more details. + ''; +} diff --git a/modules/programs/stylua.nix b/modules/programs/stylua.nix new file mode 100644 index 0000000000..c608a4320a --- /dev/null +++ b/modules/programs/stylua.nix @@ -0,0 +1,72 @@ +{ + lib, + pkgs, + config, + ... +}: +let + cfg = config.programs.stylua; + inherit (lib) + mkOption + mkEnableOption + mkPackageOption + mkIf + types + ; + tomlFormat = pkgs.formats.toml { }; + + styluaPackage = + if cfg.settings != { } then + pkgs.symlinkJoin { + name = "stylua-wrapped"; + paths = [ cfg.package ]; + nativeBuildInputs = [ pkgs.makeWrapper ]; + postBuild = '' + wrapProgram $out/bin/stylua --add-flags '--search-parent-directories' + ''; + } + else + cfg.package; +in +{ + meta.maintainers = [ lib.maintainers.rachitvrma ]; + + options.programs.stylua = { + enable = mkEnableOption "stylua, a formatter for lua"; + package = mkPackageOption pkgs "stylua" { }; + finalPackage = mkOption { + readOnly = true; + visible = false; + type = types.package; + description = "Resulting stylua package"; + }; + settings = mkOption { + inherit (tomlFormat) type; + default = { }; + example = { + call_parentheses = "Always"; + collapse_simple_statement = "Always"; + column_width = 85; + indent_type = "Spaces"; + indent_width = 2; + line_endings = "Unix"; + quote_style = "AutoPreferSingle"; + }; + description = '' + Configuration options that are written to {file}`XDG_CONFIG_HOME/stylua/stylua.toml` + + See for more options. + ''; + }; + }; + + config = mkIf cfg.enable { + home.packages = [ cfg.finalPackage ]; + + programs.stylua.finalPackage = styluaPackage; + + xdg.configFile."stylua/stylua.toml" = mkIf (cfg.settings != { }) { + source = tomlFormat.generate "hm_stylua.toml" cfg.settings; + }; + }; +} diff --git a/tests/modules/programs/stylua/default.nix b/tests/modules/programs/stylua/default.nix new file mode 100644 index 0000000000..698ce4a921 --- /dev/null +++ b/tests/modules/programs/stylua/default.nix @@ -0,0 +1,4 @@ +{ + stylua-empty-config = ./empty-config.nix; + stylua-example-config = ./example-config.nix; +} diff --git a/tests/modules/programs/stylua/empty-config.nix b/tests/modules/programs/stylua/empty-config.nix new file mode 100644 index 0000000000..0a44e627b0 --- /dev/null +++ b/tests/modules/programs/stylua/empty-config.nix @@ -0,0 +1,6 @@ +{ + programs.stylua.enable = true; + nmt.script = '' + assertPathNotExists "home-files/.config/stylua" + ''; +} diff --git a/tests/modules/programs/stylua/example-config.nix b/tests/modules/programs/stylua/example-config.nix new file mode 100644 index 0000000000..91cd36244c --- /dev/null +++ b/tests/modules/programs/stylua/example-config.nix @@ -0,0 +1,20 @@ +{ realPkgs, ... }: { + programs.stylua = { + enable = true; + settings = { + call_parentheses = "Always"; + collapse_simple_statement = "Always"; + column_width = 85; + indent_type = "Spaces"; + indent_width = 2; + line_endings = "Unix"; + quote_style = "AutoPreferSingle"; + }; + }; + test.unstubs = [ (_self: _super: { inherit (realPkgs) stylua; }) ]; + + nmt.script = '' + assertFileContent "home-files/.config/stylua/stylua.toml" ${./expected-config.toml} + assertFileRegex "home-path/bin/stylua" "search-parent-directories" + ''; +} diff --git a/tests/modules/programs/stylua/expected-config.toml b/tests/modules/programs/stylua/expected-config.toml new file mode 100644 index 0000000000..530a54c20a --- /dev/null +++ b/tests/modules/programs/stylua/expected-config.toml @@ -0,0 +1,7 @@ +call_parentheses = "Always" +collapse_simple_statement = "Always" +column_width = 85 +indent_type = "Spaces" +indent_width = 2 +line_endings = "Unix" +quote_style = "AutoPreferSingle"