Skip to content

lib.makeScope: allow overriding callPackage#500752

Merged
philiptaron merged 2 commits intoNixOS:masterfrom
quantenzitrone:makeScope-override-callPackage
Mar 22, 2026
Merged

lib.makeScope: allow overriding callPackage#500752
philiptaron merged 2 commits intoNixOS:masterfrom
quantenzitrone:makeScope-override-callPackage

Conversation

@quantenzitrone
Copy link
Copy Markdown
Contributor

@quantenzitrone quantenzitrone commented Mar 17, 2026

this is probably needed for NixOS/nixpkgs-vet#206

Things done

  • Built on platform:
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • Tested, as applicable:
  • Ran nixpkgs-review on this PR. See nixpkgs-review usage.
  • Tested basic functionality of all binary files, usually in ./result/bin/.
  • Nixpkgs Release Notes
    • Package update: when the change is major or breaking.
  • NixOS Release Notes
    • Module addition: when adding a new NixOS module.
    • Module update: when the change is significant.
  • Fits CONTRIBUTING.md, pkgs/README.md, maintainers/README.md and other READMEs.

@quantenzitrone quantenzitrone force-pushed the makeScope-override-callPackage branch from 93c6b91 to f08c835 Compare March 17, 2026 15:30
@nixpkgs-ci nixpkgs-ci bot added 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 6.topic: lib The Nixpkgs function library labels Mar 17, 2026
@quantenzitrone
Copy link
Copy Markdown
Contributor Author

rebuilds are

{
  "rebuildsByKernel": {
    "darwin": [
      "nixpkgs-manual",
      "tests.lib-tests"
    ],
    "linux": [
      "extra-container",
      "nixos-container",
      "nixos-install-tools",
      "nixpkgs-manual",
      "tests.lib-tests",
      "tests.nixos-functions.nixos-test"
    ]
  }
}

@quantenzitrone quantenzitrone marked this pull request as ready for review March 17, 2026 15:41
@nixpkgs-ci nixpkgs-ci bot requested review from a team and alyssais March 17, 2026 15:45
Copy link
Copy Markdown
Contributor

@philiptaron philiptaron left a comment

Choose a reason for hiding this comment

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

Lgtm; @ShamrockLee or @hsjobeki what think you?

@nixpkgs-ci nixpkgs-ci bot added the 12.approvals: 1 This PR was reviewed and approved by one person. label Mar 18, 2026
@ShamrockLee
Copy link
Copy Markdown
Contributor

The diff seems reasonable.

I haven't got a chance to test it. Is it possible to guard this feature with some test cases?

Pinging Nixpkgs library experts: @infinisil @roberth @emilazy

@hsjobeki
Copy link
Copy Markdown
Contributor

Lgtm; @ShamrockLee or @hsjobeki what think you?

Yes. A little bit out of my comfort zone here. I would ask robert or infinisil if they see problems here. Unfortunately didn't have enough time to dig into this area deep enough.

Test that:
- makeScope provides a default callPackage
- the scope function can override callPackage
- the override persists through overrideScope
@philiptaron
Copy link
Copy Markdown
Contributor

philiptaron commented Mar 19, 2026

I pushed a commit with three tests for the new behavior:

  1. The default callPackage still works when the scope function doesn't override it.
  2. The scope function can provide its own callPackage and it takes effect.
  3. The overridden callPackage persists through overrideScope.

I also verified that test 2 fails with the old makeScope, as the right-hand // clobbers the override, so you just get the default callPackage back. Thanks @ShamrockLee for flagging the need for tests.

@nixpkgs-ci nixpkgs-ci bot requested review from hsjobeki and infinisil March 19, 2026 21:17
@quantenzitrone quantenzitrone changed the title lib.newScope: allow overriding callPackage lib.makeScope: allow overriding callPackage Mar 19, 2026
@quantenzitrone
Copy link
Copy Markdown
Contributor Author

quantenzitrone commented Mar 19, 2026

do we need release notes for this?
because

lib.makeScope lib.callPackageWith (self: {
  inherit (self) callPackage;
})

now throws an infinite recursion error

i mean inheriting from self in a fixed point attrset is generally stupid but there was one case in nixpkgs (octave packages) so it might break stuff for people.

Also thanks for adding the tests.

@philiptaron
Copy link
Copy Markdown
Contributor

i mean inheriting from self in a fixed point attrset is generally stupid but there was one case in nixpkgs (octave packages) so it might break stuff for people.

This is how I feel... but the release note seem so in the weeds that I hesitate to even draft it. I think I just want to merge this.

@philiptaron philiptaron added this pull request to the merge queue Mar 22, 2026
Merged via the queue into NixOS:master with commit 9ff9393 Mar 22, 2026
29 of 31 checks passed
@quantenzitrone quantenzitrone deleted the makeScope-override-callPackage branch March 22, 2026 19:36
@9999years
Copy link
Copy Markdown
Contributor

Hello this PR has made my makeScope invocation fail:

let
  pkgs = import ./. { };
in
pkgs.lib.makeScope pkgs.newScope (final: {
  inherit (final) callPackage newScope;
})

Here I am using this invocation in a project of mine: https://github.com/9999years/npingler/blob/570a77e2e7831ec42a84c29e9908b729e2fd91e7/lib/default.nix

I thought this was the 'normal' way to use makeScope, but unfortunately the docs are quite light. How should I fix this error?

I see similar patterns around GitHub:

https://github.com/michaelvanstraten/templates/blob/9c8c6389e26c2415c74f65d5348197413a89ea0e/templates/generic/lib/default.nix#L8

It seems like callPackage = self.newScope { }; is slightly more common, but the docs and tests don't have any relevant examples.

@quantenzitrone
Copy link
Copy Markdown
Contributor Author

quantenzitrone commented Mar 23, 2026

callPackage and newScope are automatically added to the resulting attrset
you don't need to inherit it
to shorten final.callPackage to callPackage you can do what https://github.com/michaelvanstraten/templates/blob/9c8c6389e26c2415c74f65d5348197413a89ea0e/templates/generic/lib/default.nix#L8 is doing and inherit it in a let in block

tldr

let
  pkgs = import ./. { };
in
pkgs.lib.makeScope pkgs.newScope (final: {
})

9999years added a commit to 9999years/npingler that referenced this pull request Mar 23, 2026
I'm not sure this was ever needed, but it causes problems with the
lastest Nixpkgs.

See: NixOS/nixpkgs#500752 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: lib The Nixpkgs function library 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. 12.approvals: 1 This PR was reviewed and approved by one person.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants