Skip to content

Collapse single-definitions or Group multiple definitions #328

Closed as not planned

Description

@gauthsvenkat

Description

Collapsing single-definition attr sets using dot notions. For e.g.

Small example input

services = {
  openssh = {
    enable = true;
  };
}

Expected output

services.openssh.enable = true;

Actual output

services = {
  openssh = {
    enable = true;
  };
}

Similarly, grouping multiple definitions with common prefixes into a common attr set

Small example input

services.openssh.enable = true;
services.tailscale.enable = true;

Expected output

services = {
  openssh.enable = true;
  tailscale.enable = true;
};

Actual output

services.openssh.enable = true;
services.tailscale.enable = true;

This is quite an opinionated change and I can't imagine everyone would prefer this. Maybe this can be opt-in in some way?

Activity

  1. MattSturgeon commented on Aug 21, 2025

    @MattSturgeon
    Contributor

    I believe we would consider this a "syntax transformation" similar to #321 (comment)

    Generally speaking, these are out of scope for nixfmt. Although there are nix linters out there, some of which may suggest these kinda changes.

  2. piegamesde commented on Aug 22, 2025

    @piegamesde
    Member

    Even ignoring our policy of not doing syntax normalization, this also is a non-starter because of the Nix language syntax. Your examples of expected output are trivial, but real code contains comments and unrelated attributes between attributes to be merged, and such a feature would expose users to spooky action at a distance when formatting. Even worse, in Nix, attribute merging is a complete utter mess, and touching the attributes in any non-trivial way would require Nixfmt to re-implement the cursed syntax in a bug-compatible manner (and failure to correctly do so would result in accidental breaking semantic changes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions