nixfmt icon indicating copy to clipboard operation
nixfmt copied to clipboard

Extend single-line list formatting beyond function arguments

Open dyegoaurelio opened this issue 4 months ago • 4 comments

Previously, single-line list formatting was limited to lists within function arguments. This change extends the behavior to all lists, applying the same rules:

  • Lists with >6 items always: expand
  • Lists with >1 items and at least one non-simple item: expand
  • Otherwise: try to fit on one line

nixpkgs diff is empty because if the brackets are on different lines, we keep them like that (unless we're on--strict mode)

The diff on the test cases were positive IMO.

closes #206

dyegoaurelio avatar Oct 30 '25 14:10 dyegoaurelio

Another heuristic may be that if the list is itself the value of an attribute binding, then it is likely to change over time. Although, this won't catch "foo = [ x y z ] ++ optionals bar [ a b c ], since in that example [ a b c ]is not being bound _directly_ tofoo`.

We're already using the isSimple check, which removes things like sets and functions with a lot arguments.

I think we could do something like this

allowedToContract (Token (LoneAnn (Identifier txt))) = txt  == "false" || txt == "true" -- allow only boolean literals.
allowedToContract (Selection {}) = False -- e.g pkgs.xorg
allowedToContract t = isSimple (Term t)

this prevents changes such as this one

index 063dc21..fd242fb 100644
--- a/test/diff/monsters_3/out-pure.nix
+++ b/test/diff/monsters_3/out-pure.nix
@@ -45,13 +45,7 @@ stdenv.mkDerivation rec {
     wrapGAppsHook4
     glib # for glib-compile-resources
   ];
-  buildInputs = [
-    cairo
-    glib
-    gtk4
-    libadwaita
-    pango
-  ];
+  buildInputs = [ cairo glib gtk4 libadwaita pango ];

while allowing changes such as this one

-  (map (
-    buildAllowCommand "allow" [
-      "snapshot"
-      "mount"
-      "destroy"
-    ]
-  ))
+  (map (buildAllowCommand "allow" [ "snapshot" "mount" "destroy" ]))

dyegoaurelio avatar Oct 30 '25 18:10 dyegoaurelio

We discussed this in today's team meeting, and agree with @MattSturgeon's comments above. We'd like to see either heuristic(s) that handle those cases, or a cli option (as requested in https://github.com/NixOS/nixfmt/issues/206).

It would also be great to discuss this in real time at our next team meeting!

jfly avatar Nov 11 '25 19:11 jfly

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/formatting-team-meeting-2025-11-11/72019/1

nixos-discourse avatar Nov 11 '25 20:11 nixos-discourse