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

RFC 101/166 style #118

Merged
merged 128 commits into from
Mar 14, 2024
Merged

RFC 101/166 style #118

merged 128 commits into from
Mar 14, 2024

Conversation

piegamesde
Copy link
Member

This is the experimental branch where we try to implement currently discussed formatting decisions, to get a better feeling of how they affect real-world code.

To make more clear that one formats and the other one only checks
Tests mostly imported from Alejandra
- Disallow single-line if statements
- Handle multi-line conditions differently (indent instead of absorb)
- The (from) case needs to be improved
- Somehow it indents the comments *after* the statement now, no idea why or how to fix
The file was identical to diff/idioms_nixos_1 and therefore redundant.
Moreover, we need to also keep a look on the *really* big packages in there,
and Mozilla is a prime example.
Except when that item is another list or attribute set
This is nixos/modules/services/web-apps/nextcloud.nix, so that we have a
big NixOS module in our suite
@infinisil
Copy link
Member

There's a bug in this PR that indents comments too much:

Input

let
  a = null;
  # b
in
null

Output

let
  a = null;
    # b
in
null

@piegamesde
Copy link
Member Author

No it's not a bug in this PR, it's #32 / #117. However, the style changes in this PR make the issue a lot more prominent. I currently ignore all comment formatting for now until #32 is fixed.

@@ -0,0 +1 @@
use_nix
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this added? Should it be up to the user whether or not they would want to use this specific default Nix shell? Committing this to the repo means anyone with direnv installed is going to get that big red consent message after cd-ing into the project for the first time. This also makes this hard to extend if users have other environment needs & now have to deal with potentially accidentally committing those changes as this personal environment file is now checked into the repository.

I would prefer this be moved to .envrc.example but given it is a single line & those with direnv already know how to use it, a line could be added to the README or flake that says enabling is echo "use flake" > .envrc && direnv allow.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about direnv deny? Also works with direnv disallow, direnv block, and direnv revoke

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly don't understand your point. Committing the .envrc file is the point of direnv, to make the developer experience smoother. I love it when projects do that. The shell is additive and not pure, so it will take the outside environment into account.

Copy link
Contributor

@toastal toastal Mar 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

direnv deny

This isn’t the experience a user wants. They want to enable direnv, but only after they have a) had time to inspect the .envrc file & b) made modifications for themselves. Current state throws up ANSI red prompt just for cd-ing into the directory. Allow should happen after everything is ready.

the point

Scenario: You and I have different setup needs. I need different environment variables & scripts specific to me. The only way to get them for my personal setup is to edit the .envrc. Now that I have edited the .envrc, git status is saying I have made modification even tho they should be specific to me. I might also accidentally commit those changes as a result since it’s a tracked file. I believe the point is to set up your environment for you, not for everyone as not everyone has the same environment. The point of an .envrc.example is the place for a suggested setup for the average user. cp .envrc.example .envrc is one tiny step. Many projects have .env in their .gitignore because they don’t want to a) check in sensitive env info b) prescribe everyone have the same setup. This is as weird as committing your personal text editor settings… they are your environment settings, not everyone else’s.

love it

I hate it. If I just need to edit a README, I don’t want a direnv prompt, & certainly don’t want to copy down another version of Nixpkgs with tooling I won’t be needing for my task, and definitely not automatically running arbritrary executables I don’t yet understand because I just cloned the repo. I do like it when they offer an example I can copy, or if there is nothing interesting but use flake or use nix, just stick it in the docs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn’t the experience a user wants.

As a user, I expect that when I install a tool (direnv) and a repo supports it (nixfmt), then the tool should be used. Don't want direnv? Don't install it, then.

I need different environment variables & scripts specific to me

Great, direnv has support for that: $XDG_CONFIG_HOME/direnv/direnvrc and $XDG_CONFIG_HOME/direnv/lib/*.sh. We could also do this but that has security implications.

To be honest, I have never heard from any developer who has direnv installed that they don't want to use it (why did they install it then?). To me it's as simple as:

  • Only editing the readme once? Ignore the warning
  • Doing something in the repo often? direnv deny it once. It's quick and easy
  • Doing something in the repo often but you are like me? Ignore the warning every time

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please don't block this PR on that, I think everyone can handle whatever outcome we end up with 🙈 Thank you for your work!! :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I have never heard from any developer who has direnv installed that they don't want to use it (why did they install it then?). To me it's as simple as:

  • Only editing the readme once? Ignore the warning
  • Doing something in the repo often? direnv deny it once. It's quick and easy
  • Doing something in the repo often but you are like me? Ignore the warning every time

@dasJ Late to the party, but I just wanted to say I strongly discourage adding .envrc to a git repo (or any other VCS), and it's not about having direnv installed and not wanting to use it btw :) Here are some counter arguments:

  • I want to have my own nix shell and env vars when working on this repo (or any other), for which I would like to be in control of my own .envrc file.
    • Specifically on this project, what if I want to use a different Haskell LSP server version? or a different Cabal version? Or if I need some other package in this environment that the project assumes it's globally available?
  • I want to be able to source other local files relevant to my environment on that .envrc, e.g. I use fish shell and it requires some tweaks on certain repos, other folks use zsh or bash, etc.

Having this file committed to the repo removes this flexibility, but it's also easier to argue in its favor for an open source public repo. For private repos there's even more counter arguments, for instance, we have this at work:

  • Need to set AWS secrets on a daily basis (lasts 24 hours).
  • Need to set specific secrets that last a year (can be sourced from a local file).
  • Still need specific Nix shells at times (every project is different).
  • Need project-specific env vars that need to change depending on what we are testing and on which environment.

What I suggest instead

Let the users decide what they want available in their environments while still leveraging direnv, be a good citizen and add .envrc to your global .gitignore or add it to the project's .gitignore to avoid anyone adding it back.

Copy link

@adrian-gierakowski adrian-gierakowski Aug 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or if I need some other package in this environment that the project assumes it's globally available?

a good project shell would assume no such things

Regarding credentials: these should not be set in .envrc, but project shell should provide tools (scripts, commands) to manage these

Still need specific Nix shells at times (every project is different).

These can be entered ad-hoc with nix shell\develop

Need project-specific env vars that need to change depending on what we are testing and on which environment.

A command (provided by default dev shell) should be provided to switch between envs

.envrc IMHO should never be edited by devs, it should simply setup all tools needed to work on given project. If customisation of env vars is needed this could be loaded from optional, gitignored file(s)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a good project shell would assume no such things

This project assumes I need a specific HLS version

Regarding credentials: these should not be set in .envrc, but project shell should provide tools (scripts, commands) to manage these

Agree to disagree. The methods vary across different projects when working on a big organization. A lot of flows are very complex and unfortunately can't be automated, e.g. sign in via SSO behind a VPN, approving the sign in attempt via Okta / Duo, etc. It barely works on Linux, let alone trying to automate these things.

It's much easier on open source public projects, I'll give you that, but private organizations are a completely different story.

These can be entered ad-hoc with nix shell\develop

It doesn't make it easier to pin specific pkgs, add overlays, etc.

A command (provided by default dev shell) should be provided to switch between envs

This isn't bad actually, might work well in some cases, will give it a try :) Certainly not in cases where switching envs requires going through the complex authorization flows, though.

If customisation of env vars is needed this could be loaded from optional, gitignored file(s)

How would you do this if you don't control the content of .envrc?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've had this topic at least thrice already. Not sure having this discussion yet another time is a productive use of anybody's time

@piegamesde piegamesde marked this pull request as ready for review March 6, 2024 14:36
@DavHau
Copy link
Member

DavHau commented Mar 7, 2024

Found a little weirdness here, while formatting my code base:

in:

(import ./default.nix).devShells.${builtins.currentSystem}.default
or (throw "dev-shell not defined. Cannot find flake attribute devShell.${builtins.currentSystem}.default")

out:

(import ./default.nix).devShells.${builtins.currentSystem}.default
  or (throw "dev-shell not defined. Cannot find flake attribute devShell.${builtins.currentSystem}.default"
  )

@piegamesde
Copy link
Member Author

@DavHau can you please confirm that you are running on the latest commit? This bug should have been fixed in 6bd07b5, and I cannot reproduce it anymore

@DavHau
Copy link
Member

DavHau commented Mar 7, 2024

Oops, most likely this was the case. Never mind then.

And thanks to the rfc team for doing this great work. I imagine formatting discussions to be quite tiring. What you are doing is very valuable and will support large amounts of developers not having to waste their time on formatting related discussions anymore or reading badly formatted code. Thanks for pushing this through!

You are awesome!

@terlar
Copy link

terlar commented Mar 8, 2024

I was just testing out this from nixpkgs together with treefmt.

«derivation /nix/store/qkgjk776s6pb7y7lzh7xxfjp86mn1v93-nixfmt-unstable-2024-03-01.drv»

Seems to be a regression where nixfmt modifies the timestamp of the files every time and thus causing treefmt to fail when run with --fail-on-change.
https://numtide.github.io/treefmt/formatters-spec/#2-write-to-changed-files

$ sha256sum flake.nix 
c4f47d1cdb986ea08343ee18791842e0969f82feaeaf8b94f5afd6ab51c994ee  flake.nix
$ ls -la flake.nix
-rw-r--r-- 1 terje terje 2829 mar  8 16:23 flake.nix
$ nix run nixpkgs#nixfmt-rfc-style flake.nix
$ sha256sum flake.nix
c4f47d1cdb986ea08343ee18791842e0969f82feaeaf8b94f5afd6ab51c994ee  flake.nix
$ ls -la flake.nix
-rw-r--r-- 1 terje terje 2829 mar  8 16:29 flake.nix

@piegamesde
Copy link
Member Author

@terlar this is fixed on master but not here yet unfortunately: #88

Copy link
Member

@infinisil infinisil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this is looking okay, so instead of trying to review this in-depth, let's just merge it and iterate with further PRs!

There's still more work to do before the first official release though, see #153 for the overall progress.

@infinisil
Copy link
Member

There's a test failure after the master merge, could you check that out @piegamesde?

❯ test/test.sh
nixfmt v0.6.0
[OK] test/correct/commented-list.nix
[OK] test/correct/dollars-before-interpolation.nix
[OK] test/correct/final-comments-in-sets.nix
[OK] test/correct/float-below-one.nix
[OK] test/correct/if-with-comments.nix
[OK] test/correct/paths-with-interpolations.nix
[OK] test/correct/quotes-in-inherit-2.nix
[OK] test/correct/quotes-in-inherit.nix
[OK] test/correct/short-inherit-from.nix
[OK] test/correct/standalone-comments.nix
[OK] test/correct/string-with-single-quote-at-end.nix
[OK] test/invalid/interpolation-in-env-path.nix
[OK] test/invalid/interpolation-in-inherit-1.nix
[OK] test/invalid/interpolation-in-inherit-2.nix
[OK] test/invalid/naked-interpolation.nix
[OK] test/invalid/path-starting-with-interpolation.nix
[OK] test/invalid/path-with-escaped-interpolation.nix
[OK] test/invalid/path-with-interpolation-before-slash.nix
[OK] test/invalid/smiley.nix
Checking test/diff/apply/in.nix …
--- test/diff/apply/out.nix	2024-03-14 00:41:10.594172158 +0100
+++ /dev/fd/63	2024-03-14 01:01:43.945198577 +0100
@@ -316,8 +316,7 @@
     foo2 =
       {
         # A lot of values here
-      }
-      .overrideAttrs
+      }.overrideAttrs
         (prevAttrs: {
           # stuff here
         });
[ERROR] (run with --update-diff to update the diff)

@infinisil
Copy link
Member

Tests now pass, and I can also confirm that the formatting of Nixpkgs has not changed one bit after the master merge, so this is good to go now!

@infinisil infinisil merged commit 91b200a into NixOS:master Mar 14, 2024
1 check passed
@infinisil infinisil mentioned this pull request Mar 14, 2024
cq2n-iwym pushed a commit to Hogeyama/nix-config that referenced this pull request Apr 5, 2024
This reverts commit 5303938.
[これ](NixOS/nixfmt#118
入るまでは待つことにする。
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

Successfully merging this pull request may close these issues.

None yet