Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions maintainers/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,10 @@
github = "aliaslion";
githubId = "122117018";
};
kuppo = {
name = "Peng Lei";
email = "swk-pl@163.com";
github = "kuppo";
githubId = "17398733";
};
}
95 changes: 95 additions & 0 deletions wrapperModules/s/stylua/check.nix

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test run (success) on x86_64-linux
[jomarm@dell-inspiron7773:~/nix-wrapper-modules]$ nix-build --arg pkgs 'import <nixpkgs> {}' --arg self 'import ./. {}' --arg tlib '(import <nixpkgs> {}).callPackage ./ci/test-lib.nix { self = import ./. {}; }' wrapperModules/s/stylua/check.nix 
these 5 derivations will be built:
  /nix/store/3dn237wch52yf4ldzh3n96dylriyywmz-stylua-2.5.2.drv
  /nix/store/jh5yw0g9zaq09n4z2d625jsm28v74nap-stylua-2.5.2.drv
  /nix/store/v1w0vgp8pm6yf60i74ypf563gb47jmn1-stylua-2.5.2.drv
  /nix/store/w751d89lsqx4r0dgydrzscsla2bn9cis-stylua-2.5.2.drv
  /nix/store/sz12bv8nxlfzqhrzcc2whfnnqmlprl9n-stylua-test.drv
these 9 paths will be fetched (774.3 KiB download, 3.8 MiB unpacked):
  /nix/store/l22fp7vhwvf6zvhk3mf7wmra6llb1pv7-python3.13-colorama-0.4.6
  /nix/store/mclvsxxbggf9v0lh98jpw8n79apb56v8-python3.13-remarshal-1.3.0
  /nix/store/7szlv37z4k1dz4b57h6hjkpv3rgxzbv1-python3.13-rich-argparse-1.7.2
  /nix/store/grx6892wf0s7dw1kjx3librplg1cgw06-python3.13-ruamel-base-1.0.0
  /nix/store/qa0cja0wgm7408k8z4bj84bx12scnpip-python3.13-ruamel-yaml-0.19.1
  /nix/store/jmmdz0vkj4cphlyf6y7gdngm5hfhwisl-python3.13-ruamel-yaml-clib-0.2.15
  /nix/store/07nsvnamhq2xpn0zixw2xbvizs43sj71-python3.13-tomli-2.4.0
  /nix/store/rahjag01h0a9dmi3iljk7y60aal13iyq-python3.13-tomlkit-0.14.0
  /nix/store/12gwm2lik7f59iyan5m7bh5ygpfv8664-python3.13-u-msgpack-python-2.8.0
copying path '/nix/store/l22fp7vhwvf6zvhk3mf7wmra6llb1pv7-python3.13-colorama-0.4.6' from 'https://cache.nixos.org'...
copying path '/nix/store/7szlv37z4k1dz4b57h6hjkpv3rgxzbv1-python3.13-rich-argparse-1.7.2' from 'https://cache.nixos.org'...
copying path '/nix/store/grx6892wf0s7dw1kjx3librplg1cgw06-python3.13-ruamel-base-1.0.0' from 'https://cache.nixos.org'...
copying path '/nix/store/jmmdz0vkj4cphlyf6y7gdngm5hfhwisl-python3.13-ruamel-yaml-clib-0.2.15' from 'https://cache.nixos.org'...
copying path '/nix/store/07nsvnamhq2xpn0zixw2xbvizs43sj71-python3.13-tomli-2.4.0' from 'https://cache.nixos.org'...
copying path '/nix/store/12gwm2lik7f59iyan5m7bh5ygpfv8664-python3.13-u-msgpack-python-2.8.0' from 'https://cache.nixos.org'...
copying path '/nix/store/rahjag01h0a9dmi3iljk7y60aal13iyq-python3.13-tomlkit-0.14.0' from 'https://cache.nixos.org'...
building '/nix/store/jh5yw0g9zaq09n4z2d625jsm28v74nap-stylua-2.5.2.drv'...
copying path '/nix/store/qa0cja0wgm7408k8z4bj84bx12scnpip-python3.13-ruamel-yaml-0.19.1' from 'https://cache.nixos.org'...
/nix/store/8y5ars30x2axzqjwdhg2364sg9gb7kz5-stylua-2.5.2/bin:
stylua: File exists
Patching self-references in specified files...
Running phase: updateAutotoolsGnuConfigScriptsPhase
Running phase: buildPhase
Running phase: installPhase
copying path '/nix/store/mclvsxxbggf9v0lh98jpw8n79apb56v8-python3.13-remarshal-1.3.0' from 'https://cache.nixos.org'...
building '/nix/store/3dn237wch52yf4ldzh3n96dylriyywmz-stylua-2.5.2.drv'...
building '/nix/store/v1w0vgp8pm6yf60i74ypf563gb47jmn1-stylua-2.5.2.drv'...
building '/nix/store/w751d89lsqx4r0dgydrzscsla2bn9cis-stylua-2.5.2.drv'...
/nix/store/8y5ars30x2axzqjwdhg2364sg9gb7kz5-stylua-2.5.2/bin:
stylua: File exists
Patching self-references in specified files...
Running phase: updateAutotoolsGnuConfigScriptsPhase
/nix/store/8y5ars30x2axzqjwdhg2364sg9gb7kz5-stylua-2.5.2/bin:
stylua: File exists
Patching self-references in specified files...
Running phase: updateAutotoolsGnuConfigScriptsPhase
Running phase: buildPhase
Running phase: installPhase
Running phase: buildPhase
/nix/store/8y5ars30x2axzqjwdhg2364sg9gb7kz5-stylua-2.5.2/bin:
stylua: File exists
Patching self-references in specified files...
Running phase: updateAutotoolsGnuConfigScriptsPhase
Running phase: installPhase
Running phase: buildPhase
Running phase: installPhase
building '/nix/store/sz12bv8nxlfzqhrzcc2whfnnqmlprl9n-stylua-test.drv'...
call_parentheses = "Always"
cp_stylua_toml [-h|--help|-i|--add-doc]
        -i|--add-doc    Add the configuration doccumentation to the end of the stylua.toml
cp_stylua_toml [-h|--help|-i|--add-doc]
        -i|--add-doc    Add the configuration doccumentation to the end of the stylua.toml
#    FORMATTING OPTIONS:
#    FORMATTING OPTIONS:
/nix/store/6wyg143nszxb6qxilivfjivmzh84rlqd-stylua-test
Test run (success) on x86_64-darwin
jomarm@Jomars-MBP nix-wrapper-modules % nix-build --arg pkgs 'import <nixpkgs> {}' --arg self 'import ./.
 {}' --arg tlib '(import <nixpkgs> {}).callPackage ./ci/test-lib.nix { self = import ./. {}; }' wrapperModules/s/stylua/check.nix
evaluation warning: Nixpkgs 26.05 will be the last release to support x86_64-darwin; see https://nixos.org/manual/nixpkgs/unstable/release-notes#x86_64-darwin-26.05
these 5 derivations will be built:
  /nix/store/4bfxpm5j6rapxj634p9ya937m0zgdklq-stylua-2.5.2.drv
  /nix/store/bppym7ia8gdcyv9xzrvmrv06hmfzmrgi-stylua-2.5.2.drv
  /nix/store/xqfpkvslzky226lmvkvgx2zqapb9knjz-stylua-2.5.2.drv
  /nix/store/xr1ri10vap1hx07yaajiixvk81zpnwf6-stylua-2.5.2.drv
  /nix/store/va0962j65iapds7s9kdb6lps4ngdlr2d-stylua-test.drv
these 17 paths will be fetched (6.59 MiB download, 30.66 MiB unpacked):
  /nix/store/8h0ylc7kszsqsxi1cvg9k9xxd8lq9i9c-python3.13-cbor2-5.8.0
  /nix/store/6br6kdxlazirwk2xs1y5ym1ihb1jp83f-python3.13-colorama-0.4.6
  /nix/store/p00xl9c8jdik2n139w175wkbqzjwvvwp-python3.13-markdown-it-py-4.0.0
  /nix/store/vr6wlk0aswcy4a5kvmzaxjdaw6ckg6ah-python3.13-pygments-2.20.0
  /nix/store/fhxyi12yhvlihjs4niqgh41nmmvil399-python3.13-python-dateutil-2.9.0.post0
  /nix/store/hilvjx0cz8ly8zc4gh5jdzd7aw1kfdr1-python3.13-pyyaml-6.0.3
  /nix/store/vj7fb77ik2bmd1dmrmikp6yilrjnnd0i-python3.13-remarshal-1.3.0
  /nix/store/6nlw5hwv0fjhvrg9hd9yshnwasl2k76l-python3.13-rich-14.3.3
  /nix/store/dmk31b2yd2lr2dbnlm9kazixdcf3dqg0-python3.13-rich-argparse-1.7.2
  /nix/store/l8f5i34fqrg54r46i0nq38i9xra856r0-python3.13-ruamel-base-1.0.0
  /nix/store/pw3sq3mpx0k9y0wxlad3cshgfldpcw9d-python3.13-ruamel-yaml-0.19.1
  /nix/store/s9nc04k1af75w0psp0bl1878m3388h75-python3.13-ruamel-yaml-clib-0.2.15
  /nix/store/q775apkbdgqpway0ln98c6ldhl97ga72-python3.13-six-1.17.0
  /nix/store/20axdsfn8bkmrhfm4z48hb6vx27bs5vq-python3.13-tomli-2.4.0
  /nix/store/mhwqdpg0qlwaq5ivqhx3wwg4z4jp3frj-python3.13-tomlkit-0.14.0
  /nix/store/1pd3dldgl5l828vlsldv1xh7giblakq9-python3.13-u-msgpack-python-2.8.0
  /nix/store/3gz6bdwx0dzpn0a127c87sp58f02zgm4-stylua-2.5.2
copying path '/nix/store/3gz6bdwx0dzpn0a127c87sp58f02zgm4-stylua-2.5.2' from 'https://cache.nixos.org'...
copying path '/nix/store/8h0ylc7kszsqsxi1cvg9k9xxd8lq9i9c-python3.13-cbor2-5.8.0' from 'https://cache.nixos.org'...
copying path '/nix/store/6br6kdxlazirwk2xs1y5ym1ihb1jp83f-python3.13-colorama-0.4.6' from 'https://cache.nixos.org'...
copying path '/nix/store/p00xl9c8jdik2n139w175wkbqzjwvvwp-python3.13-markdown-it-py-4.0.0' from 'https://cache.nixos.org'...
copying path '/nix/store/vr6wlk0aswcy4a5kvmzaxjdaw6ckg6ah-python3.13-pygments-2.20.0' from 'https://cache.nixos.org'...
copying path '/nix/store/hilvjx0cz8ly8zc4gh5jdzd7aw1kfdr1-python3.13-pyyaml-6.0.3' from 'https://cache.nixos.org'...
copying path '/nix/store/s9nc04k1af75w0psp0bl1878m3388h75-python3.13-ruamel-yaml-clib-0.2.15' from 'https://cache.nixos.org'...
copying path '/nix/store/q775apkbdgqpway0ln98c6ldhl97ga72-python3.13-six-1.17.0' from 'https://cache.nixos.org'...
copying path '/nix/store/1pd3dldgl5l828vlsldv1xh7giblakq9-python3.13-u-msgpack-python-2.8.0' from 'https://cache.nixos.org'...
copying path '/nix/store/mhwqdpg0qlwaq5ivqhx3wwg4z4jp3frj-python3.13-tomlkit-0.14.0' from 'https://cache.nixos.org'...
copying path '/nix/store/l8f5i34fqrg54r46i0nq38i9xra856r0-python3.13-ruamel-base-1.0.0' from 'https://cache.nixos.org'...
copying path '/nix/store/20axdsfn8bkmrhfm4z48hb6vx27bs5vq-python3.13-tomli-2.4.0' from 'https://cache.nixos.org'...
copying path '/nix/store/fhxyi12yhvlihjs4niqgh41nmmvil399-python3.13-python-dateutil-2.9.0.post0' from 'https://cache.nixos.org'...
copying path '/nix/store/pw3sq3mpx0k9y0wxlad3cshgfldpcw9d-python3.13-ruamel-yaml-0.19.1' from 'https://cache.nixos.org'...
building '/nix/store/4bfxpm5j6rapxj634p9ya937m0zgdklq-stylua-2.5.2.drv'...
copying path '/nix/store/6nlw5hwv0fjhvrg9hd9yshnwasl2k76l-python3.13-rich-14.3.3' from 'https://cache.nixos.org'...
copying path '/nix/store/dmk31b2yd2lr2dbnlm9kazixdcf3dqg0-python3.13-rich-argparse-1.7.2' from 'https://cache.nixos.org'...
/nix/store/3gz6bdwx0dzpn0a127c87sp58f02zgm4-stylua-2.5.2/bin:
stylua: File exists
Patching self-references in specified files...
Running phase: buildPhase
Running phase: installPhase
copying path '/nix/store/vj7fb77ik2bmd1dmrmikp6yilrjnnd0i-python3.13-remarshal-1.3.0' from 'https://cache.nixos.org'...
building '/nix/store/bppym7ia8gdcyv9xzrvmrv06hmfzmrgi-stylua-2.5.2.drv'...
/nix/store/3gz6bdwx0dzpn0a127c87sp58f02zgm4-stylua-2.5.2/bin:
stylua: File exists
Patching self-references in specified files...
Running phase: buildPhase
Running phase: installPhase
building '/nix/store/xqfpkvslzky226lmvkvgx2zqapb9knjz-stylua-2.5.2.drv'...
/nix/store/3gz6bdwx0dzpn0a127c87sp58f02zgm4-stylua-2.5.2/bin:
stylua: File exists
Patching self-references in specified files...
Running phase: buildPhase
Running phase: installPhase
building '/nix/store/xr1ri10vap1hx07yaajiixvk81zpnwf6-stylua-2.5.2.drv'...
/nix/store/3gz6bdwx0dzpn0a127c87sp58f02zgm4-stylua-2.5.2/bin:
stylua: File exists
Patching self-references in specified files...
Running phase: buildPhase
Running phase: installPhase
building '/nix/store/va0962j65iapds7s9kdb6lps4ngdlr2d-stylua-test.drv'...
call_parentheses = "Always"
cp_stylua_toml [-h|--help|-i|--add-doc]
        -i|--add-doc    Add the configuration doccumentation to the end of the stylua.toml
cp_stylua_toml [-h|--help|-i|--add-doc]
        -i|--add-doc    Add the configuration doccumentation to the end of the stylua.toml
#    FORMATTING OPTIONS:
#    FORMATTING OPTIONS:
/nix/store/iiwa9f2yd77w6v4fafvccpalqa6s0l2v-stylua-test

I think that it would be good for the behavior of the wrapped program to be checked as well. For example, if you expand the "imperative test process" in my previous comment, you can see how I tested check mode with stylua.

Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{
pkgs,
self,
tlib,
...
}:
let
inherit (tlib)
fileContains
isDirectory
isFile
notIsFile
areEqual
test
;
in
test { wrapper = "stylua"; } {
"stylua wrapper test" =
let
default = self.wrappers.stylua.wrap {
inherit pkgs;
};
styluaWrapper = default.wrap {
customStyle = {
call_parentheses = "Always";
column_width = 100;
};
};
cpScriptWrapper = styluaWrapper.wrap {
generateCpScript = {
enable = true;
};
};
cpScriptNameWrapper = cpScriptWrapper.wrap {
generateCpScript = {
name = "./bin/test_script";
};
};
styluaTomlContent = ''
call_parentheses = "Always"
column_width = 100
'';
in
[
(isDirectory default)
(notIsFile "${default}/styles/stylua.toml")
(notIsFile "${default}/bin/cp_stylua_toml")

(isDirectory styluaWrapper)
(isFile "${styluaWrapper}/styles/stylua.toml")
(fileContains "${styluaWrapper}/styles/stylua.toml" "${styluaTomlContent}")
(notIsFile "${styluaWrapper}/bin/cp_stylua_toml")

(isDirectory cpScriptWrapper)
(isFile "${cpScriptWrapper}/styles/stylua.toml")
(fileContains "${cpScriptWrapper}/styles/stylua.toml" "${styluaTomlContent}")
(isFile "${cpScriptWrapper}/bin/cp_stylua_toml")
(fileContains "${cpScriptWrapper}/bin/cp_stylua_toml" "bin/sh")

(isDirectory cpScriptNameWrapper)
(isFile "${cpScriptNameWrapper}/styles/stylua.toml")
(fileContains "${cpScriptNameWrapper}/styles/stylua.toml" "${styluaTomlContent}")
(isFile "${cpScriptNameWrapper}/bin/test_script")
(fileContains "${cpScriptNameWrapper}/bin/test_script" "bin/sh")

# test the copy script
''
cd /tmp && ${cpScriptNameWrapper}/bin/test_script && \
[[ -e /tmp/stylua.toml ]] && [[ -w /tmp/stylua.toml ]] && \
grep -i "always" /tmp/stylua.toml && rm -f /tmp/stylua.toml
''

''
${cpScriptNameWrapper}/bin/test_script -h |
grep -i "add-doc"
''

''
${cpScriptNameWrapper}/bin/test_script --help |
grep -i "add-doc"
''

''
cd /tmp && ${cpScriptNameWrapper}/bin/test_script -i && \
[[ -e /tmp/stylua.toml ]] && [[ -w /tmp/stylua.toml ]] && \
grep -i "formatting options" /tmp/stylua.toml && rm -f /tmp/stylua.toml
''

''
cd /tmp && ${cpScriptNameWrapper}/bin/test_script --add-doc && \
[[ -e /tmp/stylua.toml ]] && [[ -w /tmp/stylua.toml ]] && \
grep -i "formatting options" /tmp/stylua.toml && rm -f /tmp/stylua.toml
''
];
}
118 changes: 118 additions & 0 deletions wrapperModules/s/stylua/module.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{
wlib,
lib,
config,
pkgs,
...
}:
{
imports = [ wlib.modules.default ];

options = {
customStyle = lib.mkOption {
type = wlib.types.structuredValueWith {
nullable = false;
typeName = "TOML";
};
default = { };
description = ''
nix configuration for the stylua.

Check [StyLua options](https://github.com/JohnnyMorganz/StyLua/blob/main/README.md#options).
'';
example = lib.literalExpression ''
settings = {
call_parentheses = "Always";
column_width = 100;
collapse_simple_statement = "Always";
indent_type = "Spaces";
indent_width = 2;
quote_style = "ForceDouble";
sort_requires.enabled = true;
};
'';
};
generateCpScript = lib.mkOption {
default = { };
description = ''
Options for copy script which help you quickly copy your
settings into `$CWD` for further customization.

The script `cp_stylua_toml` (name is customizable) copys the generated
`stylua.toml` file into '$CWD' in case you want to include it in the
repo or customize it.

With the `-i|--add-doc` option, it will add the configuraiton
documentation to the end of the copied file.
'';
type = lib.types.submodule {
options = {
enable = lib.mkEnableOption ''
generating a copy script.
'';
name = lib.mkOption {
type = lib.types.str;
default = "cp_stylua_toml";
description = ''
Customize the name of the copy script. If the name has `/` in it,
the wrapper will only take the file's base name.
'';
};
};
};
};
};
config = {
package = lib.mkDefault pkgs.stylua;
constructFiles.generatedConfig = lib.mkIf (config.customStyle != { }) {
content = builtins.toJSON config.customStyle;
relPath = "styles/stylua.toml";
builder = ''${pkgs.remarshal}/bin/json2toml "$1" "$2"'';
};
constructFiles."${baseNameOf config.generateCpScript.name}" =
lib.mkIf config.generateCpScript.enable
{
relPath = "bin/${baseNameOf config.generateCpScript.name}";
builder = "cp $1 $2 && chmod +x $2";
content = ''
#!${pkgs.bash}/bin/sh
help=$'cp_stylua_toml [-h|--help|-i|--add-doc]\nCopy stylua files.\nOptions:\n\t-h|--help\tPrint this help\n\t-i|--add-doc\tAdd the configuration doccumentation to the end of the stylua.toml'

target=$(pwd)/stylua.toml

doc=$(${placeholder config.outputName}/bin/stylua --help \
| ${pkgs.gnused}/bin/sed -n \
'/^FORMATTING OPTIONS:$/,$ {1d;s/^[[:space:]]*/ /;s/^[[:space:]]*--/** /;s/^/# /;p}')

if [ "$#" -ne 1 ]; then
cp -f ${placeholder config.outputName}/styles/stylua.toml $(pwd)/ \
&& chmod u+w "$target"
elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
echo "$help"
exit 0
elif [ "$1" == "-i" ] || [ "$1" == "--add-doc" ]; then
cp -f ${placeholder config.outputName}/styles/stylua.toml $(pwd)/ \
&& chmod u+w "$target" && echo >> "$target" && echo "$doc" >> "$target"
fi
'';
};
flags."--config-path" = lib.mkIf (
config.customStyle != { }
) config.constructFiles.generatedConfig.path;
meta = {
maintainers = with wlib.maintainers; [
kuppo
];
description = ''
Wrapper Module for [Stylua](https://github.com/JohnnyMorganz/StyLua).

The wrapper is used to customize the `stylua.toml` file.
You can add you options into `config.customStyle` with pure nix expressions.

The wrapper also provides a script which copys the generated `stylua.toml`
into `$CWD`, in case you want to include the confitugation into the repo or
customize it somehow.
'';
};
};
}
Loading