Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nix pretty-printer/canonicalizer #832

Open
copumpkin opened this issue Mar 3, 2016 · 17 comments
Open

Nix pretty-printer/canonicalizer #832

copumpkin opened this issue Mar 3, 2016 · 17 comments
Assignees

Comments

@copumpkin
Copy link
Member

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
Copy link
Contributor

fkz commented Mar 7, 2016

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

@copumpkin
Copy link
Member Author

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
Copy link
Contributor

fkz commented Mar 7, 2016

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
Copy link
Member Author

That sounds awesome for several reasons 👍

@fkz
Copy link
Contributor

fkz commented Mar 7, 2016

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")

@olejorgenb olejorgenb mentioned this issue Nov 21, 2016
@domenkozar
Copy link
Member

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

@fstamour
Copy link

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

@vcunat
Copy link
Member

vcunat commented Nov 23, 2016

Related: #1102.

@etu
Copy link

etu commented Feb 27, 2019

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
Copy link
Member

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

@ghuntley
Copy link
Member

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
Copy link
Member

$ \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
Copy link
Member

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

@stale
Copy link

stale bot commented Feb 15, 2021

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

@stale stale bot added the stale label Feb 15, 2021
@stale
Copy link

stale bot commented May 1, 2022

I closed this issue due to inactivity. → More info

@stale stale bot closed this as completed May 1, 2022
@thufschmitt thufschmitt reopened this Feb 24, 2023
zolodev pushed a commit to zolodev/nix that referenced this issue Jan 1, 2024
@stale stale bot removed the stale label Sep 22, 2024
@quatquatt
Copy link
Contributor

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
Copy link
Member

Atemu commented Sep 23, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

13 participants