NixOS / nix

Nix, the purely functional package manager
https://nixos.org/
GNU Lesser General Public License v2.1
12.33k stars 1.49k forks source link

Nix pretty-printer/canonicalizer #832

Open copumpkin opened 8 years ago

copumpkin commented 8 years ago

If we can read in the AST and spit it back out again in a standard (ideally somewhat pretty) format, that'd do a reasonable job at this.

Ideally it would preserve comments and such.

fkz commented 8 years ago

nix-instantiate --parse is doing exactly that (not really pretty printing though and also not preserving comments and canonicalizing paths to absolute paths)

copumpkin commented 8 years ago

How much of a pain would it be to make it preserve comments? It seems like most ASTs other than these auto-formatters will typically discard comments fairly early on, and might also discard whitespace people intended to keep.

For example, the jsonnet canonicalizer will preserve single and double blank lines (under the assumption that the human author added them for a reason), but coalesce 3 or more blank lines into two.

fkz commented 8 years ago

Currently, comments are discarded already in the lexer. I'm currently thinking about changing that for an other purpose (being able to do more reflection inside nix to be able to generate documentation for functions)

copumpkin commented 8 years ago

That sounds awesome for several reasons :+1:

fkz commented 8 years ago

Ah, this is also currently only idempotent if done twice:

nix-instantiate --parse --expr '"1${"2" + "3"}4"'
>("1" + ("2" + "3") + "4")
nix-instantiate --parse --expr '("1" + ("2" + "3") + "4")'
>(("1" + ("2" + "3")) + "4")
nix-instantiate --parse --expr '(("1" + ("2" + "3")) + "4")'
>(("1" + ("2" + "3")) + "4")
domenkozar commented 7 years ago

https://github.com/Gabriel439/nixfmt is incomplete, but does exactly this. See https://github.com/Gabriel439/nixfmt/issues/3 for the current status

fstamour commented 7 years ago

Would be nice to integrate nixfmt with nix-mode (emacs mode) and nixIdea (intellij's idea plugin).

vcunat commented 7 years ago

Related: #1102.

etu commented 5 years ago

Would be nice to integrate nixfmt with nix-mode (emacs mode) and nixIdea (intellij's idea plugin).

What about aiming for the LSP method here? I think there's some works on nix LSP integrations somewhere. That would give benefits for other editors as well.

domenkozar commented 5 years ago

https://github.com/domenkozar/hnix-lsp works, but doesn't support comments (yet)

ghuntley commented 5 years ago

This now exists https://github.com/justinwoo/format-nix/ but it's performance is quite slow..

$ time ./result/bin/format-nix ../nix/packages.nix formatted ../nix/packages.nix.

real 0m6.232s user 0m6.458s sys 0m0.261s

Against https://github.com/digital-asset/daml/blob/master/nix/packages.nix

domenkozar commented 5 years ago
$ \time cat nix/packages.nix | ../result/bin/canonix --pipe >/dev/null
0.00user 0.00system 0:00.00elapsed 100%CPU (0avgtext+0avgdata 3540maxresident)k
0inputs+0outputs (0major+165minor)pagefaults 0swaps

using a bit modified version of https://github.com/hercules-ci/canonix/ that is not really usable yet - might be after ZuriHac.

domenkozar commented 4 years ago

We have https://github.com/nix-community/nixpkgs-fmt now, the main question is if it becomes part of Nix some day.

stale[bot] commented 3 years ago

I marked this as stale due to inactivity. → More info

stale[bot] commented 2 years ago

I closed this issue due to inactivity. → More info

quatquatt commented 2 weeks ago

As there's been much progress on nixfmt being fully adopted by nixpkgs, I think it's important to note an issue I'm having when it comes to simply piping nix eval into nixfmt.

nix eval prints «derivation long/nix/store/path/here» whenever it encounters a package. nixfmt has no clue what to do with this, so it errors.

To reproduce, run nix eval /etc/nixos#nixosConfigurations.MYHOSTNAMEHERE.config.environment.systemPackages | nixfmt.

While I think the eventuall pretty-printing tool should be based on nixfmt, it's not currently able to be used without issue.

Atemu commented 1 week ago

Perhaps nixfmt should learn to deal with such strings inside of «» and accept them as pseudo-literals for the purpose of processing Nix output.