# Nix formatting team: Treewide Nixpkgs formatting

**URL:** https://discourse.nixos.org/t/nix-formatting-team-treewide-nixpkgs-formatting/56666
**Category:** Announcements
**Created:** [November 27, 2024, 10:53am UTC](https://discourse.nixos.org/t/nix-formatting-team-treewide-nixpkgs-formatting/56666 "2024-11-27T10:53:14Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Infinisil](https://discourse.nixos.org/user_avatar/discourse.nixos.org/infinisil/32/26523_2.png) [@Infinisil](https://discourse.nixos.org/u/Infinisil)
#### Post date: [November 27, 2024, 10:53am UTC](https://discourse.nixos.org/t/nix-formatting-team-treewide-nixpkgs-formatting/56666/1 "2024-11-27T10:53:14Z")

</div>

Hi everyone,

After preparing for it throughout the year\[1\], we as the [Nix formatting team](https://nixos.org/community/teams/formatting/) are proud to announce the soon to be merged (almost) full reformat of nixpkgs with the following primary PR:

> <https://github.com/NixOS/nixpkgs/pull/322537>
>
> \> \[!Note\]
> \> Enforcement of formatting has been split into this separate PR: htt…ps://github.com/NixOS/nixpkgs/pull/326407
> 
> \## Description of changes
> 
> This is the next step towards accepted NixOS/rfcs#166 after #322512, see also https://github.com/NixOS/nixpkgs/issues/322520 and https://github.com/NixOS/nixfmt/issues/153 :tada:.
> 
> The @NixOS/nix-formatting will approve/merge this PR.
> 
> This PR formats all "inactive" files using nixfmt-rfc-style, where "inactive" means that there are no open PRs with activity in the last ~2 months that touch those files. 
> 
> Doing it this way makes ensures that we don't cause any conflicts for recently active PRs that would be ready to merge. Only once those PRs get updated, CI will kick in and require the files to be formatted.
> 
> A bunch later, we can do another pass to get the rest.
> 
> Furthermore, this makes sure that we can merge this PR without having to constantly rebase it!
> 
> \#### Verification
> 
> It's impossible to review this, but that's not necessary because you can review the code that generated it instead, which can be found \[here\](https://github.com/infinisil/treewide-nixpkgs-reformat-script/blob/5439e16eda66856aed747db3b45ed595835de7c6/default.nix). At the same time this can be used to verify the contents of this PR by:
> 1. Checking out this PR in your Nixpkgs clone (with the GitHub CLI here):
> \`\`\`
> cd ~/src/nixpkgs
> gh co 322537
> \`\`\`
> 2. Building the verification script:
> \`\`\`
> nix-build https://github.com/infinisil/treewide-nixpkgs-reformat-script/archive/df3821ae0ab1203adbd43a533493a7e1c50b9dc7.tar.gz \\
> --argstr baseRev c8da2f827c5e20822811f3a6b7432a50017293b3
> \`\`\`
> 3. Running it on your clone (no worries if your tree is dirty, it uses a separate worktree and cleans up behind itself):
> \`\`\`
> result/bin/apply-formatting .
> \`\`\`
> 4. Verifying that the diff only contains the revision in \`.git-blame-ignore-revs\` (which changes because the date of the commit it creates changes)
> 
> \## Things done
> \- \[\] Merge and propagate the nixfmt fixes in https://github.com/NixOS/nixfmt/pull/215
> - \[x\] Merge the fixes
> - \[x\] Update nixfmt in Nixpkgs: https://github.com/NixOS/nixpkgs/pull/326622
> - \[x\] Update the \[pinned Nixpkgs for CI/shell.nix\](https://github.com/NixOS/nixpkgs/tree/master/ci#pinned-nixpkgs): https://github.com/NixOS/nixpkgs/pull/328186
> \- \[\] Confirm that others are getting the same result with the script
> \- \[x\] Confirm that there are (almost) no rebuilds (See https://github.com/NixOS/nixpkgs/issues/301014)
> - Can be done separately, non-blocking in https://github.com/NixOS/nixpkgs/pull/326430
> \- \[x\] Confirm that \`nix-instantiate --parse\` doesn't change
> - Discovered some (non-blocking) inconsistencies with \`nix-instantiate --parse\` from this:
> - https://github.com/NixOS/nix/pull/11120
> - https://github.com/NixOS/nix/pull/11123
> \- \[\] Consider fixing \[this problem\](https://github.com/NixOS/nixpkgs/pull/326430#discussion\_r1679889039) first
> - https://github.com/NixOS/nixfmt/pull/224
> \- \[\] Make a Discourse post
> \- \[\] Prepare the same PR for release and staging branches, so that they can be merged at the same time. This ensures that there aren't any problems when backporting PRs or merging staging
> - https://github.com/NixOS/nixpkgs/pull/327796
> 
> 
> \---
> 
> This work is sponsored by \[Antithesis\](https://antithesis.com/) :sparkles:
> 
> Add a :+1: \[reaction\] to \[pull requests you find important\].
> 
> \[reaction\]: https://github.blog/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/
> \[pull requests you find important\]: https://github.com/NixOS/nixpkgs/pulls?q=is%3Aopen+sort%3Areactions-%2B1-desc

This PR will format all files without recent\[2\] activity to minimise merge conflicts on active PRs. Once files are formatted, CI will enforce them to stay formatted going forward.

We are coordinating asynchronously [on Matrix](https://matrix.to/#/#nix-formatting:nixos.org) and will hold a final review and merge party 🎉 at Invalid date in [Jitsi](https://meet.zrh.init7.net/nix-formatting), feel free to join both!

Once merged, we will monitor for potential issues before going ahead with formatting the remaining files in the new year.

If you have any questions, concerns or problems with the reformatting process, let us know here as a reply or open a [nixfmt issue](https://github.com/NixOS/nixfmt/issues) or a [Nixpkgs issue](https://github.com/NixOS/nixpkgs/issues) while pinging `@NixOS/nix-formatting`. 🙂

* * *

If you are interested in following the development of the tooling, feel free to join the Matrix room and the meetings, which are currently happening [every second Tuesday](https://calendar.google.com/calendar/u/0/embed?src=b9o52fobqjak8oq8lfkhg3t0qg@group.calendar.google.com) with the next meeting at 2024-12-10T19:00:00Z. The [nixfmt repository](https://github.com/NixOS/nixfmt) Readme is also worth mentioning, it contains a lot of instructions for configuring tools and editors (additions welcome!). We also appreciate more help with development.

Note that `nixfmt-rfc-style` is a not a Nixpkgs specific tool, you’re welcome to use it on your own projects.

Finally we would like to thank everybody involved in making this happen, starting from people involved in [RFC 101](https://github.com/NixOS/rfcs/pull/101) and the other formatter efforts such as [alejandra](https://github.com/kamadorueda/alejandra), [nixpkgs-fmt](https://github.com/nix-community/nixpkgs-fmt) and the [classic nixfmt](https://github.com/serokell/nixfmt/tree/v0.6.0), to everyone giving us feedback in the accepted [RFC 166](https://github.com/NixOS/rfcs/pull/166).

* * *

1. Preparation work such as fixing all the critical formatter issues that were brought up, and soon updating the formatter on both `master` and the release branches and update CI tooling to use the new version. 

2. Specifically, files that are touched by a PR with activity in the last ~2 months.

---

<div class="post-metadata">

### Author: ![Sandro](https://discourse.nixos.org/user_avatar/discourse.nixos.org/sandro/32/2865_2.png) [@Sandro](https://discourse.nixos.org/u/Sandro)
#### Post date: [December 1, 2024, 11:24am UTC](https://discourse.nixos.org/t/nix-formatting-team-treewide-nixpkgs-formatting/56666/2 "2024-12-01T11:24:41Z")

</div>

The problem described in this issue [Appending an optional string / array causes the entire expression to be reformatted · Issue #228 · NixOS/nixfmt · GitHub](https://github.com/NixOS/nixfmt/issues/228) got already reported multiple times by different people over the last months and keeps coming up as a major bug that should be fixed before this.

---

<div class="post-metadata">

### Author: ![Atemu](https://discourse.nixos.org/user_avatar/discourse.nixos.org/atemu/32/10383_2.png) [@Atemu](https://discourse.nixos.org/u/Atemu)
#### Post date: [December 1, 2024, 5:13pm UTC](https://discourse.nixos.org/t/nix-formatting-team-treewide-nixpkgs-formatting/56666/3 "2024-12-01T17:13:40Z")

</div>

That’s not a bug, that’s intended behaviour in accordance with the RFC.

It’s a trade-off between never absorbing anything and diff noise when absorbability changes. Diff noise is the lesser evil here; you do not want non-absorbed expressions everywhere, that’s insanely ugly and wasteful w.r.t. vertical space.

The diff noise isn’t _that_ bad either as it’s pretty easy to tell what happened and easy to verify nothing else changed at a glance.

To prevent it in situation where you think it’s likely that it’ll happen, I think we’ll see patterns like

```Nix
foo = 
  []
  ++ [...];

```

or

```Nix
foo = 
  #
  [...];

```

emerge. I don’t like this either but, again, this is by far the lesser evil.

---

<div class="post-metadata">

### Author: ![waffle8946](https://discourse.nixos.org/user_avatar/discourse.nixos.org/waffle8946/32/27221_2.png) [@waffle8946](https://discourse.nixos.org/u/waffle8946)
#### Post date: [December 1, 2024, 9:30pm UTC](https://discourse.nixos.org/t/nix-formatting-team-treewide-nixpkgs-formatting/56666/4 "2024-12-01T21:30:18Z")

</div>

You can also hide whitespace changes in the github web interface when reviewing; I find indentation changes to be a non-issue.

---

<div class="post-metadata">

### Author: ![das\_j](https://discourse.nixos.org/user_avatar/discourse.nixos.org/das_j/32/5144_2.png) [@das\_j](https://discourse.nixos.org/u/das_j)
#### Post date: [December 2, 2024, 8:37am UTC](https://discourse.nixos.org/t/nix-formatting-team-treewide-nixpkgs-formatting/56666/5 "2024-12-02T08:37:13Z")

</div>

Correct me if I’m wrong but in the last meeting we (the team) decided that while this is an issue that has not been addressed, it’s not blocking for an initial reformat and can be fixed at a later point.

---

<div class="post-metadata">

### Author: ![Sandro](https://discourse.nixos.org/user_avatar/discourse.nixos.org/sandro/32/2865_2.png) [@Sandro](https://discourse.nixos.org/u/Sandro)
#### Post date: [December 8, 2024, 1:23pm UTC](https://discourse.nixos.org/t/nix-formatting-team-treewide-nixpkgs-formatting/56666/6 "2024-12-08T13:23:07Z")

</div>

I thought reducing diff noise was a goal and fixing it down the line will cause a lot of noise commits again.

---

<div class="post-metadata">

### Author: ![Atemu](https://discourse.nixos.org/user_avatar/discourse.nixos.org/atemu/32/10383_2.png) [@Atemu](https://discourse.nixos.org/u/Atemu)
#### Post date: [December 8, 2024, 1:52pm UTC](https://discourse.nixos.org/t/nix-formatting-team-treewide-nixpkgs-formatting/56666/7 "2024-12-08T13:52:19Z")

</div>

I may not have been clear enough in my previous reply but this is a _trade-off_; a compromise between two suboptimal options.

You get to choose between this:

```Nix
{
  foo =
    [value];
  bar = 
    { attr = value; };
  empty = 
    [];
  moreData =
    {
      foo = bar;
      baz = quux;
    };
}

```

and this

```Nix
{
  foo = [value];
  bar = {
    attr = value;
  };
  empty = [];
  moreData = {
    foo = bar;
    baz = quux;
  };
}

```

where the latter has the diff noise issue and the former doesn’t.

---

<div class="post-metadata">

### Author: ![ThePhoenix](https://discourse.nixos.org/user_avatar/discourse.nixos.org/thephoenix/32/38925_2.png) [@ThePhoenix](https://discourse.nixos.org/u/ThePhoenix)
#### Post date: [December 8, 2024, 4:29pm UTC](https://discourse.nixos.org/t/nix-formatting-team-treewide-nixpkgs-formatting/56666/8 "2024-12-08T16:29:34Z")

</div>

Could I ask why one causes more noise than the other?

Just as I never knew that layout/formatting could increase or decrease “noise”.

Also, given both are sub-optimal options, what would an optimal solution look like?

---

<div class="post-metadata">

### Author: ![Atemu](https://discourse.nixos.org/user_avatar/discourse.nixos.org/atemu/32/10383_2.png) [@Atemu](https://discourse.nixos.org/u/Atemu)
#### Post date: [December 8, 2024, 4:47pm UTC](https://discourse.nixos.org/t/nix-formatting-team-treewide-nixpkgs-formatting/56666/9 "2024-12-08T16:47:53Z")

</div>

> [@ThePhoenix](#):
>
> Could I ask why one causes more noise than the other?

See the issue linked in [Nix formatting team: Treewide Nixpkgs formatting - #2 by Sandro](https://discourse.nixos.org/t/nix-formatting-team-treewide-nixpkgs-formatting/56666/2).

> [@ThePhoenix](#):
>
> Just as I never knew that layout/formatting could increase or decrease “noise”.

Of course it can. If the formatter reformats lines that are not part of the actual diff, that adds noise to the diff; it’s no longer just the lines that changed but other lines were “changed” that didn’t actually meaningfully change and only had to be changed to fit the formatting rules.

> [@ThePhoenix](#):
>
> Also, given both are sub-optimal options, what would an optimal solution look like?

There is none.

The best we could theoretically do would be to allow the user to specify whether they want the binding to be absorbed/compact or not so that we as humans can pre-emptively prevent diff noise where we think it is likely to happen.

That is imperfect and has its own additional issues though and I think the workaround patterns I described are preferable, particularly the no-op append.

---

<div class="post-metadata">

### Author: ![ThePhoenix](https://discourse.nixos.org/user_avatar/discourse.nixos.org/thephoenix/32/38925_2.png) [@ThePhoenix](https://discourse.nixos.org/u/ThePhoenix)
#### Post date: [December 8, 2024, 5:12pm UTC](https://discourse.nixos.org/t/nix-formatting-team-treewide-nixpkgs-formatting/56666/10 "2024-12-08T17:12:50Z")

</div>

The reason it didn’t initially occur to me, was as I’ve never actually used git for anything except storing my nix config.

So as a result, I’ve never touched a formatting tool before - however, I can now see the pros and cons of using one!

Thanks so much for taking the time to explain 🙏

---

<div class="post-metadata">

### Author: ![Infinisil](https://discourse.nixos.org/user_avatar/discourse.nixos.org/infinisil/32/26523_2.png) [@Infinisil](https://discourse.nixos.org/u/Infinisil)
#### Post date: [December 10, 2024, 4:07pm UTC](https://discourse.nixos.org/t/nix-formatting-team-treewide-nixpkgs-formatting/56666/11 "2024-12-10T16:07:16Z")

</div>

Reminder that the merge party is in 3 hours, at 2024-12-10T19:00:00Z in [this Jitsi room](https://meet.zrh.init7.net/nix-formatting) 🙂

---

<div class="post-metadata">

### Author: ![Infinisil](https://discourse.nixos.org/user_avatar/discourse.nixos.org/infinisil/32/26523_2.png) [@Infinisil](https://discourse.nixos.org/u/Infinisil)
#### Post date: [December 10, 2024, 8:19pm UTC](https://discourse.nixos.org/t/nix-formatting-team-treewide-nixpkgs-formatting/56666/12 "2024-12-10T20:19:22Z")

</div>

Thank y’all for joining, we successfully completed the treewide reformat! Please enjoy [this commit](https://github.com/NixOS/nixpkgs/commit/4f0dadbf38ee4cf4cc38cbc232b7708fddf965bc) that changes 21293 files 🎉

A note on merge conflicts: Some automated backport PRs created after the reformat for PRs that were merged before the reformat might fail. Should not be many, but feel free to let me know if anybody needs help with that.

Before we proceed with the full treewide reformat (probably in a month or so), I’ll try to get [the rebase script](https://github.com/NixOS/nixpkgs/pull/363759) ready, so we can more easily deal with merge conflicts caused by reformatting.

Finally, I want to give a big shout-out to [Tweag](https://www.tweag.io/) and [Antithesis](https://antithesis.com/) for funding me to work on such Nixpkgs improvements, I’m very grateful for that! ✨

---

<div class="post-metadata">

### Author: ![Atemu](https://discourse.nixos.org/user_avatar/discourse.nixos.org/atemu/32/10383_2.png) [@Atemu](https://discourse.nixos.org/u/Atemu)
#### Post date: [January 7, 2025, 8:31pm UTC](https://discourse.nixos.org/t/nix-formatting-team-treewide-nixpkgs-formatting/56666/13 "2025-01-07T20:31:45Z")

</div>

> [@Atemu](#):
>
> You get to choose between this:
> 
> ```auto
> {
> foo =
> [value];
> bar = 
> { attr = value; };
> empty = 
> [];
> moreData =
> {
> foo = bar;
> baz = quux;
> };
> }
> 
> ```
> 
> and this

Actually, this is wrong. Single-line expressions could and should still be absorbed. It’d be like this:

```auto
{
  foo = [value];
  bar = { attr = value; };
  empty = [];
  moreData =
    {
      foo = bar;
      baz = quux;
    };
}

```

Only the multi-line one would not be absorbed.

---

<div class="post-metadata">

### Author: ![Infinisil](https://discourse.nixos.org/user_avatar/discourse.nixos.org/infinisil/32/26523_2.png) [@Infinisil](https://discourse.nixos.org/u/Infinisil)
#### Post date: [March 18, 2025, 8:00pm UTC](https://discourse.nixos.org/t/nix-formatting-team-treewide-nixpkgs-formatting/56666/14 "2025-03-18T20:00:56Z")

</div>

> [@Nix formatting team: Full Nixpkgs reformat](https://discourse.nixos.org/t/nix-formatting-team-full-nixpkgs-reformat/61867):
>
> After the [previous partial Nixpkgs reformat](https://discourse.nixos.org/t/nix-formatting-team-treewide-nixpkgs-formatting/56666), the [Nix formatting team](https://github.com/NixOS/nixfmt/tree/master/team) is now ready for follow-up: To fully standardise formatting in Nixpkgs! rocket Highlights: All files will be formatted using a pinned version the [official Nix formatter](https://github.com/NixOS/nixfmt), aka pkgs.nixpkgs-rfc-format. CI will enforce this going forward, ensuring that all files continue to be formatted consistently. Inside Nixpkgs’ nix-shell/nix develop, you will be able to run [treefmt](https://treefmt.com/) to get the right formatting on all files. You can also …
