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

doc: add function argument order convention #110060

Merged
merged 2 commits into from Jan 21, 2021

Conversation

deviant
Copy link
Member

@deviant deviant commented Jan 20, 2021

Ordering by usage is the de facto ordering given to arguments. It's
logical, and makes finding argument usage easier. Putting lib first is
common in NixOS modules, so it's reasonable to mirror this in nixpkgs
proper. Additionally, it's not a package as such, has zero dependencies,
and can be found used anywhere in a derivation.

Motivation for this change

Following up on a conversation with @alyssais on IRC:

[...]
<V> stdenv only goes first in a lot of places b/c packages are written as { ... }: stdenv.mkDerivation { ... }, not because stdenv is special
<qyliss> I can see where you're coming from
<V> if we make stdenv be the first arg, then we should make buildGoPackage come before lib as well
<V> (I could go on)
<qyliss> if we're going to change it, we need to update the examples in the manual
<qyliss> if somebody puts together a PR for that I think it's fine
[...]
Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS linux)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Ensured that relevant documentation is up to date
  • Fits CONTRIBUTING.md.
    Ordering by usage is the de facto ordering given to arguments. It's
    logical, and makes finding argument usage easier. Putting lib first is
    common in NixOS modules, so it's reasonable to mirror this in nixpkgs
    proper. Additionally, it's not a package as such, has zero dependencies,
    and can be found used anywhere in a derivation.

Ordering by usage is the de facto ordering given to arguments. It's
logical, and makes finding argument usage easier. Putting lib first is
common in NixOS modules, so it's reasonable to mirror this in nixpkgs
proper. Additionally, it's not a package as such, has zero dependencies,
and can be found used anywhere in a derivation.
@abathur
Copy link
Member

abathur commented Jan 20, 2021

I've wondered a bit about this as I write a few packages. I'm a bit pleased to see order-by-usage suggested--because it should be formatter/linter-enforceable.

On the other hand, it's also common to interleave comments; I'm less sure if a formatter can just optimistically move the preceding comment, if any, any time it reorders arguments? :)

@deviant
Copy link
Member Author

deviant commented Jan 20, 2021

@abathur Although there's a few common patterns comments fall into, they can also get quite convoluted: see ffmpeg for a gnarly example. This will probably be quite a tricky problem to solve correctly (unless we decide to change the format of packages to something more structured).

@abathur
Copy link
Member

abathur commented Jan 20, 2021

Yes. I thought about commenting about semantic groupings, though I waffled since they might actually still group well enough by first use. (Though, no--I hadn't noticed how gnarly ffmpeg is).

I guess it might be easier to address with a linter--since it can just complain that the order is wrong and make a human sort out t he comments--as long as it can respect the semantic-grouping concept.

I see occasional chatter about docstrings--I hope a principled solution could fit there, if nowhere else.

@deviant
Copy link
Member Author

deviant commented Jan 20, 2021

ffmpeg is definitely the exception, not the norm. I'm more accustomed to seeing fairly straightforward examples that would likely stand up to this kind of treatment. Presumably you've had a similar experience.

What I'd like to see is the whole callPackage convention being replaced with a structured package format, with configuration à la Gentoo's USE flags. It's a shame that the proposed config DSL hasn't yet appeared, as I feel it would really help with some of these problems. A structured config format could have these comments instead be docstrings, as you said, allowing them to be queried — again, as on Gentoo:

v@gentoo ~> equery u coreutils
[ Legend : U - final flag setting for installation]
[        : I - package is installed with flag     ]
[ Colors : set, unset                             ]
 * Found these USE flags for sys-apps/coreutils-8.32-r1:
 U I
 + + acl       : Add support for Access Control Lists
 + + caps      : Add Linux capabilities support in output of file utilities (ls, dir, ...) via sys-libs/libcap
 - - gmp       : Add support for dev-libs/gmp (GNU MP library)
 - - hostname  : Build the hostname program
 - - kill      : Build the kill program
 - - multicall : Build all tools into a single `coreutils` program akin to busybox to save space
 + + nls       : Add Native Language Support (using gettext - GNU locale utilities)
 - - static    : !!do not set this during bootstrap!! Causes binaries to be statically linked instead of dynamically
 - - test      : Enable dependencies and/or preparations necessary to run tests (usually controlled by FEATURES=test but can be toggled independently)
 - - vanilla   : Do not add extra patches which change default behaviour; DO NOT USE THIS ON A GLOBAL SCALE as the severity of the meaning changes drastically

@SuperSandro2000
Copy link
Member

Also I am saying how it is but I don't think we can enforce this at all or people are getting mad.

@deviant
Copy link
Member Author

deviant commented Jan 20, 2021

For now, it's a guideline, not a rule. Later, we can have formatting tools apply the reordering for us.

I'm not really sure why you'd think people would be mad at having some actual coding conventions, though. The lack of any is a common complaint.

@deviant
Copy link
Member Author

deviant commented Jan 20, 2021

This is, at its core, a two line change. I've reached my limit with how much bikeshedding I'm willing to deal with at the moment, so I'm leaving it to someone else to carry to the finish line if they want this merged. This kind of feedback is extremely draining to engage with, and seriously demotivates me from contributing.

@deviant deviant closed this Jan 20, 2021
@SuperSandro2000
Copy link
Member

SuperSandro2000 commented Jan 20, 2021

This is, at its core, a two line change. I've reached my limit with how much bikeshedding I'm willing to deal with at the moment, so I'm leaving it to someone else to carry to the finish line if they want this merged. This kind of feedback is extremely draining to engage with, and seriously demotivates me from contributing.

It is not just a two line change. This impacts everyone that contributes to nixpkgs and if we do not have the right tooling first then this kind of guideline for enforceable at all.

If we have a formatter that does this and most people can agree on using it then we can make the formatter a hard requirement.

Right now if I would suggest people to sort their inputs in a PR review I would start a lot of bike sheeting with a lot of people and they would discard my reviews even more.

@grahamc
Copy link
Member

grahamc commented Jan 21, 2021

It is not just a two line change. This impacts everyone that contributes to nixpkgs and if we do not have the right tooling first then this kind of guideline for enforceable at all.

That is okay, writing it down is a good start.

Right now if I would suggest people to sort their inputs in a PR review I would start a lot of bike sheeting with a lot of people and they would discard my reviews even more.

Well, you don't need to require it. 100% compliance doesn't have to be the goal until it is supported by tooling. It could be a request on new packages, or a suggestion on fixups. There is room for discretion.

@SuperSandro2000, we can't wait for perfect tooling or block improvements in cases where 100% compliance isn't feasible.

@grahamc grahamc reopened this Jan 21, 2021
@grahamc grahamc merged commit 7616206 into NixOS:master Jan 21, 2021
@jonringer
Copy link
Contributor

Right now if I would suggest people to sort their inputs in a PR review I would start a lot of bike sheeting with a lot of people and they would discard my reviews even more.

Nixpkgs is huge and broad. If the inputs are getting unwieldy, then you can say to sort if it improves comprehension, but I don't think this is true across the board. Also, you can convey that something is not a blocker (I usually say, I don't feel strongly about something). I think it's important for maintainers to have some "ownership" over the style of code they use.

@SuperSandro2000 I agree it would be nice for some conventions to be well defined, but the nature of nixpkgs is that it's a "living document", in the packages it contains and the conventions it adheres to. Like @grahamc said, this is a step in the right direction, and overall improvement.

Maybe one day we will be like elm, where the style is already determined for everyone, and adherence will be easy because any nix command will auto format your code. Until then, incremental improvement is always a good choice.

@cole-h cole-h mentioned this pull request Jan 21, 2021
10 tasks
@deviant deviant deleted the lib-first-programming branch January 24, 2021 08:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants