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

Add Nixpkgs invocation helper modules #231940

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

roberth
Copy link
Member

@roberth roberth commented May 14, 2023

Description of changes

This makes a number of well tested and documented additions that lead up to

  • A reusable module that module system applications can use for the purpose of invoking the Nixpkgs function.
  • A much sought after flake-parts module.

... using the supporting additions and improvements:

  • lib.systems.equals function for comparing elaborated system strings.
  • lib.systems.toLosslessStringMaybe which is useful for converting elaborated systems to optional attribute names.
  • The lib.systems tests are now run by ofborg.

This PR contains commits that have been split into a separate PR

Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 23.05 Release Notes (or backporting 22.11 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

@roberth roberth requested a review from Ericson2314 May 14, 2023 22:11
@github-actions github-actions bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: changelog 8.has: documentation labels May 14, 2023
@ofborg ofborg bot added 8.has: package (new) This PR adds a new package 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10 labels May 15, 2023
Copy link
Contributor

@fricklerhandwerk fricklerhandwerk left a comment

Choose a reason for hiding this comment

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

Just a superficial review. TBH the documentation doesn't explain to me who and what this thing is for exactly, and how it's supposed to be used. I could go into option documentation if you like, once the big picture is clear.

@@ -189,6 +189,13 @@ The following attributes can be passed in [`config`](#chap-packageconfig).
<include xmlns="http://www.w3.org/2001/XInclude" href="../doc-support/result/config-options.docbook.xml"/>
```

## Calling Nixpkgs from the Module System {#sec-invoke-nixpkgs-by-module}

For [module system](#module-system) application authors, Nixpkgs provides a reusable module for the purpose of invoking Nixpkgs. Most users should not have to be aware of this. The documentation for its options is meant to be re-exposed in the application's documentation and is therefore not repeated here.
Copy link
Contributor

Choose a reason for hiding this comment

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

By "module system application" you mean things like Home Manager? I'm not sure what you mean here.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's what I meant.
I don't know how to say this without going on a tangent. I think going on a tangent to explain this would distract from the content. Most users will know to skip the clause or skip the paragraph.

Copy link
Contributor

@fricklerhandwerk fricklerhandwerk May 15, 2023

Choose a reason for hiding this comment

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

Suggested change
For [module system](#module-system) application authors, Nixpkgs provides a reusable module for the purpose of invoking Nixpkgs. Most users should not have to be aware of this. The documentation for its options is meant to be re-exposed in the application's documentation and is therefore not repeated here.
For authors of applications based on the [module system](#module-system), Nixpkgs provides a reusable module for the purpose of invoking Nixpkgs. Most users should not have to be aware of this. The documentation for its options is meant to be re-exposed in the application's documentation and is therefore not repeated here.

I can do a documentation review pass when the implementation is done. Unsubscribing for now, ping me if needed.

doc/using/configuration.chapter.md Outdated Show resolved Hide resolved
pkgs/top-level/module/module.nix Outdated Show resolved Hide resolved
pkgs/top-level/module/module.nix Outdated Show resolved Hide resolved
Copy link
Member

@Ericson2314 Ericson2314 left a comment

Choose a reason for hiding this comment

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

I didn't re-review the docs; I trust @fricklerhandwerk on that.

I reviewed the rest, and it looks good modulo these small few things. Excited that we are getting started on the idea behind NixOS/rfcs#78 --- the grand module deduplication!!

@github-actions github-actions bot added the 8.has: module (update) This PR changes an existing module in `nixos/` label May 15, 2023
@roberth roberth changed the title Add Nixpkgs invocation helper module Add Nixpkgs invocation helper modules May 15, 2023
flake.nix Outdated Show resolved Hide resolved
A helper for module system applications.

Naming compatible with NixOS/nix#6257
To be supported by nix flake check in NixOS/nix#8332
Concept is in the spirit of a proposed module based solution to [RFC 0078 System-agnostic configuration file generators](NixOS/rfcs#78).
This hack isn't needed anymore, and would be incompatible with having submodule at `nixpkgs.*`.
A feature flag warning was rightfully picked up by ofborg. This should
solve it, and hopefully help out the next person who tries to render
docs for an optional module.
@blaggacao
Copy link
Contributor

In the outer rings of context to this PR, I have attempted abstractions in a similar problem domain spanning beyond nixpkgs here.

Note that this comment has no bearing to this PR beyond situational awareness for interested parties.

Co-authored-by: David Arnold <dgx.arnold@gmail.com>
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.

Reviewed together with @roberth on a call

{
disabledModules = [
# This replaces the traditional nixpkgs module
../nixpkgs.nix
Copy link
Member

Choose a reason for hiding this comment

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

That module has other imports that would also get removed:

imports = [
./assertions.nix
./meta.nix
(mkRemovedOptionModule [ "nixpkgs" "initialSystem" ] "The NixOS options `nesting.clone` and `nesting.children` have been deleted, and replaced with named specialisation. Therefore `nixpgks.initialSystem` has no effect anymore.")
];

{
disabledModules = [
# This replaces the traditional nixpkgs module
../nixpkgs.nix
Copy link
Member

Choose a reason for hiding this comment

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

Why does this have to be a separate module? How about updating the ../nixpkgs.nix one, and soft deprecating the localSystem and crossSystem options.

{
options = {
hostPlatform = mkOption {
type = types.coercedTo types.str lib.systems.elaborate types.attrs;
Copy link
Member

Choose a reason for hiding this comment

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

We shouldn't use types.attrs, has weird merging behavior, types.raw should be fine

Suggested change
type = types.coercedTo types.str lib.systems.elaborate types.attrs;
type = types.coercedTo types.str lib.systems.elaborate types.raw;

Same for the other options.

{ allowBroken = true; allowUnfree = true; }
'';
# FIXME: use a mergeable type, like NixOS but not a hack
type = types.unique { message = "nixpkgs/pkgs/top-level/module.nix: Merging is not supported for the Nixpkgs config option yet."; } types.attrs;
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
type = types.unique { message = "nixpkgs/pkgs/top-level/module.nix: Merging is not supported for the Nixpkgs config option yet."; } types.attrs;
type = types.unique { message = "nixpkgs/pkgs/top-level/module.nix: Merging is not supported for the Nixpkgs config option yet."; } types.raw;

Or attrsOf raw

Comment on lines +162 to +163
if lib.mapAttrs (_: _: null) (builtins.removeAttrs inputsSet ["hostPlatform"])
!= { hostPlatform = null; }
Copy link
Member

Choose a reason for hiding this comment

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

Seems wrong, but could also be done with builtins.removeAttrs inputsSet [ "hostPlatform" ] != { }

Comment on lines +27 to +28
isCross = config.buildPlatform != config.hostPlatform;
systemArgs =
Copy link
Member

Choose a reason for hiding this comment

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

Let's have a comment here mentioning that this is a bit of a hack and should be moved into the pkgs entrypoint function definition

# key are usable with _memoize implementations that are not aware of them,
# as long as these additions are unused.
inputsSet = lib.filterAttrs
(_: v: v != v.isDefined && v.highestPrio < optDefaultPrio)
Copy link
Member

Choose a reason for hiding this comment

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

This condition needs a bit of work, v != v.isDefined seems wrong (detected by @roberth himself!)

pkgsMemoizer = { pkgs, inputsSet, ... }:
let system = lib.systems.toLosslessStringMaybe inputsSet.hostPlatform.value.system;
in
if lib.attrNames inputsSet == [ "hostPlatform" ] && system != null
Copy link
Member

Choose a reason for hiding this comment

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

See also the other comment

@@ -12,6 +12,20 @@
lib = import ./lib;

forAllSystems = lib.genAttrs lib.systems.flakeExposed;

pkgsMemoizer = { pkgs, inputsSet, ... }:
let system = lib.systems.toLosslessStringMaybe inputsSet.hostPlatform.value.system;
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
let system = lib.systems.toLosslessStringMaybe inputsSet.hostPlatform.value.system;
let system = lib.systems.toLosslessStringMaybe inputsSet.hostPlatform.value.system;

Calling this on system sounds wrong 😆

@wegank wegank added 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 2.status: merge conflict This PR has merge conflicts with the target branch labels Mar 19, 2024
@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Mar 20, 2024
@wegank wegank added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jul 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.status: merge conflict This PR has merge conflicts with the target branch 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: changelog 8.has: documentation 8.has: module (update) This PR changes an existing module in `nixos/` 8.has: package (new) This PR adds a new package 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

6 participants