Skip to content

nixos/system-path: convert environment.systemPackages to an attrset#255086

Draft
r-vdp wants to merge 6 commits intoNixOS:masterfrom
r-vdp:system_packages_as_attr_set
Draft

nixos/system-path: convert environment.systemPackages to an attrset#255086
r-vdp wants to merge 6 commits intoNixOS:masterfrom
r-vdp:system_packages_as_attr_set

Conversation

@r-vdp
Copy link
Contributor

@r-vdp r-vdp commented Sep 14, 2023

Description of changes

Lists are pretty bad for overriding, this PR proposes to use an attribute set for environment.systemPackages so that we can more easily disable packages or override the derivation for a package.

In order not to break the current API, we use the coercedTo type so that we can still accept a list of derivations and we convert this list into an attribute set. This should therefore be a non-breaking change.

We also allow for a shorthand syntax:

environment.systemPackages = {
  inherit (pkgs) acl;
};

Which gets coerced into

environment.systemPackages = {
  acl = {
    enable = true;
    package = pkgs.acl;
    outputs = {
      bin = true;
      man = true;
    };
  };
};

where the outputs were taken from meta.outputsToInstall and can be individually disabled (hence the attrset that maps every output to a boolean).

The nixos test gives a good overview of what the syntax would look like.

@roberth @infinisil @nikstur : I'd love to get your thoughts on this.

@github-actions github-actions bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` 6.topic: module system About "NixOS" module system internals 6.topic: lib The Nixpkgs function library labels Sep 14, 2023
@r-vdp r-vdp force-pushed the system_packages_as_attr_set branch from 74a0c32 to 02edf48 Compare September 14, 2023 10:04
@infinisil infinisil added the 1.severity: significant Novel ideas, large API changes, notable refactorings, issues with RFC potential, etc. label Sep 14, 2023
@r-vdp r-vdp force-pushed the system_packages_as_attr_set branch from 02edf48 to f0ce34f Compare September 14, 2023 10:18
@ofborg ofborg bot added 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. labels Sep 14, 2023
@nikstur
Copy link
Contributor

nikstur commented Sep 14, 2023

You should mark this as a draft if you're still working on it.

@r-vdp r-vdp changed the title [WIP] nixos/system-path: convert environment.systemPackages to an attrset nixos/system-path: convert environment.systemPackages to an attrset Sep 14, 2023
@r-vdp r-vdp marked this pull request as draft September 14, 2023 12:44
@r-vdp
Copy link
Contributor Author

r-vdp commented Sep 14, 2023

Right, I removed the WIP label and converted to draft for now.

I'd want to do a bit more tidying up before merging, but before spending more time on this I'd first like to know that people agree with the approach.

@nikstur
Copy link
Contributor

nikstur commented Sep 14, 2023

I think the approach is really good. I've been discussing with some people that the systemPackages should be an overridable attrset but never acted on it, so I'm happy that someone else picked it up.

The attrset way of declaring a package is a little verbose, but I'd assume that most people are just using the list way, so its fine.

@r-vdp r-vdp force-pushed the system_packages_as_attr_set branch 2 times, most recently from a2d8e5d to 176d3ca Compare September 14, 2023 17:05
@r-vdp
Copy link
Contributor Author

r-vdp commented Sep 14, 2023

@nikstur after implementing the suggestions from @infinisil, the way to declare a package using the attrset approach became a lot more convenient, as you can see in the test case. We can deduce the outputs automatically now.

@r-vdp r-vdp requested review from infinisil and nikstur September 14, 2023 22:46
@r-vdp r-vdp force-pushed the system_packages_as_attr_set branch from 4ab1693 to c1d6dfe Compare September 14, 2023 22:51
@roberth
Copy link
Member

roberth commented Sep 20, 2023

I think the following definition leads to an unfortunate error message:

systemPackages = pkgs.hello;

hello is an attrset, so its package attributes will be treated as packages instead. I think we should detect this mistake in the type and report that it should be systemPackages.hello = <pkg>.

@github-actions github-actions bot removed 6.topic: module system About "NixOS" module system internals 6.topic: lib The Nixpkgs function library labels Sep 21, 2023
@roberth
Copy link
Member

roberth commented Sep 21, 2023

This also needs a release note and an update to the option description to clarify what the attribute name is for.

@r-vdp r-vdp force-pushed the system_packages_as_attr_set branch from e8d72ef to 6c86b56 Compare January 12, 2024 09:14
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.

This PR currently adds 4 non-trivial, untested and undocumented lib functions. Either those should be done in smaller PRs (see also the new lib PR guidelines), or we shouldn't expose them publicly, as hinted by Robert.

@r-vdp r-vdp force-pushed the system_packages_as_attr_set branch from 6c86b56 to e119b86 Compare January 15, 2024 23:04
Copy link
Member

@roberth roberth left a comment

Choose a reason for hiding this comment

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

Some clarifications needed

Copy link
Member

Choose a reason for hiding this comment

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

I checked several of the rebuilds and they are due to the ordering of the paths in the resulting list. This was in definition order before, and is in alphabetical order now, due to attrsets being sorted.

I believe this is important enough to be added to the release note.
While the definition order is sufficiently "unpredictable" that it should not be relied upon, that doesn't exclude the possibility that some configurations happen to rely on it by accident.
Could you add an item to describe this breaking aspect, in the Backward Incompatibilities section below?

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
This change is backwards compatible, but we might deprecate the list syntax in a future release.
This change is backwards compatible, unless the configuration relies on a specific ordering of packages. We might deprecate the list syntax in a future release.

Suppose I want util-linux for obvious reasons, but I also want my own custom mount executable from a custom package. How do I achieve that? After reading this, it
d looks like I'm stuck, or I'd have to meddle with the util-linux definition. I think this also needs to reference the priority option.

Please describe an alternative to list ordering here and in the option doc.

Copy link
Member

Choose a reason for hiding this comment

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

Could you describe the effect of priority?
E.g. what does a large value do? Do packages without a priority have a default priority?
I think you could link to https://nixos.org/manual/nixpkgs/stable/#var-meta-priority, but that doc isn't much better.
Maybe this? https://nixos.org/manual/nix/stable/command-ref/nix-env/install.html?highlight=priority#description

I think it'd be ok to copy some of this information, because

  • NixOS/nixpkgs duplicates this logic anyway
  • It's not going to change in Nix, because that'd break profiles for no good reason

@SuperSandro2000
Copy link
Member

I am sorry if that was already answered in some thread, I didn't read all of them but what happens to packages that where before duplicated by name in the list but not by the files they contain?

@r-vdp
Copy link
Contributor Author

r-vdp commented Jan 16, 2024

I am sorry if that was already answered in some thread, I didn't read all of them but what happens to packages that where before duplicated by name in the list but not by the files they contain?

What do you mean exactly by "the files they contain"? Packages added through the list syntax, end up in the attrset with their drv path as key, so unless they come from the same derivation, they would end up under different keys. If they do come from the same derivation, then their outputs will be combined in the list of outputs to install.

Does that answer your question?

@SuperSandro2000
Copy link
Member

Yeah, I first thought they would be using the package variable as a key which isn't unique but the drv path must be unique. 👍🏼

@r-vdp r-vdp force-pushed the system_packages_as_attr_set branch 2 times, most recently from b350003 to 209683e Compare January 19, 2024 08:55
@r-vdp r-vdp force-pushed the system_packages_as_attr_set branch from 209683e to 4c10a51 Compare January 25, 2024 11:48
@r-vdp
Copy link
Contributor Author

r-vdp commented Jan 25, 2024

There's currently still an issue with bash being selected instead of bashInteractive, I need to find the time to figure out why that is.

@wegank wegank added the 2.status: merge conflict This PR has merge conflicts with the target branch label Mar 20, 2024
@nixos-discourse
Copy link

@nyabinary
Copy link
Contributor

There's currently still an issue with bash being selected instead of bashInteractive, I need to find the time to figure out why that is.

Any update on this? :3

@wegank wegank added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jan 2, 2025
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/best-practices-with-with/58857/20

@RazYang
Copy link

RazYang commented Apr 1, 2025

Any progress?

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Apr 1, 2025
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/breaking-changes-announcement-for-unstable/17574/82

@drupol drupol marked this pull request as draft May 14, 2025 05:45
@nixpkgs-ci nixpkgs-ci bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Oct 28, 2025
@nixos-discourse
Copy link

@nixpkgs-ci nixpkgs-ci bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jan 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

1.severity: significant Novel ideas, large API changes, notable refactorings, issues with RFC potential, etc. 2.status: merge conflict This PR has merge conflicts with the target branch 6.topic: lib The Nixpkgs function library 6.topic: module system About "NixOS" module system internals 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: changelog This PR adds or changes release notes 8.has: documentation This PR adds or changes documentation 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux.

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.