← Back to Pull requests

lib/customisation: add overrideVariant to preserve API in by-name migrations, fix formatting in docstring #537454

Open
guylamar2006 opened this pull request with 108 lines diff across 3 files
View on GitHub
guylamar2006 guylamar2006 commented on Jul 01, 2026 14:53 UTC
Author

This pull request introduces a new utility function to simplify package variant overrides and migrates two packages to use this approach. The main focus is to facilitate the "by-name" migration for package variants, making it easier to override and extend packages without losing the ability to chain .override calls. Additionally, the documentation for related functions has been improved for clarity.

New utility for package overrides:

Added the overrideVariant function to lib/customisation.nix, allowing clean and flexible overriding of base packages with specific attributes and passthrough arguments. This is designed to support the "by-name" migration and preserve .override chaining for package variants.

Migration to new override utility:

Updated pkgs/by-name/ja/jabcode-reader/package.nix and pkgs/by-name/ja/jabcode-writer/package.nix to use lib.customisation.overrideVariant instead of manual override logic, improving maintainability and consistency. [[1]](diffhunk://#diff-f8e7031be16152262f7fb3f3c12046ce899b1390aac24828f120afa3bfebb1e2R2-R9) [[2]](diffhunk://#diff-38f23ebbe7f428cc9054f58b758faad5094e5ca98c661a58250d0be4c950bdc0R2-R9)

Documentation improvements:

Enhanced the documentation for callPackageWith in lib/customisation.nix with clearer examples and explanations for easier understanding and usage.

Code cleanup:

Commented out an unused line related to function argument inheritance in lib/customisation.nix to avoid confusion and maintain code clarity.

Abstract overrideAttrs logic for issue #537188
<!--
^ Please summarise the changes you have done and explain why they are necessary here ^

For package updates please link to a changelog or describe changes, this helps your fellow maintainers discover breaking updates.
For new packages please briefly describe the package or provide a link to its homepage.
-->

Things done

<!-- Please check what applies. Note that these are not hard requirements but merely serve as information for reviewers. -->

  • Built on platform:

  • - [x] x86_64-linux
    - [ ] aarch64-linux
    - [ ] aarch64-darwin
  • Tested, as applicable:

  • - [ ] [NixOS tests] in [nixos/tests].
    - [ ] [Package tests] at passthru.tests.
    - [ ] Tests in [lib/tests] or [pkgs/test] for functions and "core" functionality.
  • 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.

  • Follows the [automation/AI policy].
  • [NixOS tests]: https://nixos.org/manual/nixos/unstable/index.html#sec-nixos-tests
    [Package tests]: https://github.com/NixOS/nixpkgs/blob/master/pkgs/README.md#package-tests
    [nixpkgs-review usage]: https://github.com/Mic92/nixpkgs-review#usage

    [CONTRIBUTING.md]: https://github.com/NixOS/nixpkgs/blob/master/CONTRIBUTING.md
    [automation/AI policy]: https://github.com/NixOS/nixpkgs/blob/master/CONTRIBUTING.md#automationai-policy
    [lib/tests]: https://github.com/NixOS/nixpkgs/blob/master/lib/tests
    [maintainers/README.md]: https://github.com/NixOS/nixpkgs/blob/master/maintainers/README.md
    [nixos/tests]: https://github.com/NixOS/nixpkgs/blob/master/nixos/tests
    [pkgs/README.md]: https://github.com/NixOS/nixpkgs/blob/master/pkgs/README.md
    [pkgs/test]: https://github.com/NixOS/nixpkgs/blob/master/pkgs/test

    Diff: 108 lines (+80 / -28) across 3 files Reviewer: SandaruKasa Review duration: 222430m
    All Vouch review confidence checks passed
    Residual risk 0.07 = Change Risk (0.64) × (1 − Review Confidence (0.89)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.64 · 108 lines diff across 3 files
    0.64
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.89 · 5 substantive comment(s)
    0.89
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    88%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.281
    Sensitive Paths sensitive_paths
    0.064
    Real PR Conversation Timeline
    8 items
    guylamar2006 guylamar2006 commented on Jul 01, 2026 15:01 UTC
    Author

    :white_check_mark: nixpkgs-review result

    Generated using nixpkgs-review-gha (da739d6)

    Command: nixpkgs-review pr 537454
    Commit: 05ae943c9645d9a0256f30ef22c76a1a67914797 (subsequent changes)
    Merge: 962ecb728ce325df9fbd4442714bb68b9d155992

    Logs: https://github.com/guylamar2006/nixpkgs-review-gha/actions/runs/28526580304/attempts/1

    ---

    x86_64-linux


    <details>
    <summary>:fast_forward: 2 packages blacklisted:</summary>
    <ul>
    <li>nixos-install-tools</li>
    <li>tests.nixos-functions.nixos-test</li>
    </ul>
    </details>
    <details>
    <summary>:white_check_mark: 4 packages built:</summary>
    <ul>
    <li>extra-container</li>
    <li>nixos-container</li>
    <li>nixpkgs-manual</li>
    <li>tests.lib-tests</li>
    </ul>
    </details>

    ---

    aarch64-linux


    <details>
    <summary>:fast_forward: 2 packages blacklisted:</summary>
    <ul>
    <li>nixos-install-tools</li>
    <li>tests.nixos-functions.nixos-test</li>
    </ul>
    </details>
    <details>
    <summary>:white_check_mark: 4 packages built:</summary>
    <ul>
    <li>extra-container</li>
    <li>nixos-container</li>
    <li>nixpkgs-manual</li>
    <li>tests.lib-tests</li>
    </ul>
    </details>

    ---

    aarch64-darwin (sandbox = relaxed)


    <details>
    <summary>:white_check_mark: 2 packages built:</summary>
    <ul>
    <li>nixpkgs-manual</li>
    <li>tests.lib-tests</li>
    </ul>
    </details>

    SandaruKasa SandaruKasa commented on code on Jul 01, 2026 22:02 UTC
    Code Review
    lib/customisation.nix:1
    @@ -877,7 +917,7 @@ rec {
             # Inherit the __functionArgs from the base build helper
             optionalAttrs inheritFunctionArgs (removeAttrs (functionArgs constructDrv) excludeDrvArgNames)
             # Recover the __functionArgs from the derived build helper
    -        // functionArgs (extendDrvArgs { })
    +        # // functionArgs (extendDrvArgs { })

    Accidental? Intentional?

    SandaruKasa SandaruKasa commented on Jul 01, 2026 22:03 UTC
    Reviewer

    I generally like the idea, but haven't reviewed the implementation too closely

    guylamar2006 guylamar2006 commented on code on Jul 01, 2026 22:18 UTC
    Code Review
    lib/customisation.nix:1
    @@ -877,7 +917,7 @@ rec {
             # Inherit the __functionArgs from the base build helper
             optionalAttrs inheritFunctionArgs (removeAttrs (functionArgs constructDrv) excludeDrvArgNames)
             # Recover the __functionArgs from the derived build helper
    -        // functionArgs (extendDrvArgs { })
    +        # // functionArgs (extendDrvArgs { })

    Accidental! My AI assistant slipped a comment token in there while formatting the file, and I didn't catch it before pushing. I am reverting it now so we don't break callPackage reflection for custom builder arguments.

    GraysonTinker GraysonTinker commented on code on Jul 02, 2026 02:59 UTC
    Code Review
    lib/customisation.nix:1
    @@ -215,6 +215,42 @@ rec {
         else
           mirrorArgs f';
     
    +  /**
    +      Overrides a base package with specific attributes, while cleanly passing
    +      through any additional arguments provided by `callPackage`.
    +
    +      This is specifically designed to facilitate the `by-name` migration for
    +      package variants (e.g., migrating `package-gui` without losing the
    +      user's ability to chain `.override` calls).
    +
    +      # Inputs
    +
    +      `basePackage`
    +
    +      : 1\. Function argument (The base derivation to override)
    +
    +      `excludeArgs`
    +
    +      : 2\. Function argument (A list of argument strings to remove from the passthrough)
    +
    +      `variantOverrides`
    +
    +      : 3\. Function argument (The attributes to override)
    +
    +      `variantArgs`
    +
    +      : 4\. Function argument (The arguments passed to the variant by `callPackage`)
    +
    +      # Type
    +
    +      ```
    +      overrideVariant :: Derivation -> [String] -> AttrSet -> AttrSet -> Derivation
    +      ```
    +  */
    +  overrideVariant =

    Could mkOverrideVariant be a more intuitive name?

    GraysonTinker GraysonTinker commented on code on Jul 02, 2026 03:03 UTC
    Code Review
    lib/customisation.nix:1
    @@ -215,6 +215,42 @@ rec {
         else
           mirrorArgs f';
     
    +  /**
    +      Overrides a base package with specific attributes, while cleanly passing
    +      through any additional arguments provided by `callPackage`.
    +
    +      This is specifically designed to facilitate the `by-name` migration for
    +      package variants (e.g., migrating `package-gui` without losing the
    +      user's ability to chain `.override` calls).
    +
    +      # Inputs
    +
    +      `basePackage`
    +
    +      : 1\. Function argument (The base derivation to override)
    +
    +      `excludeArgs`
    +
    +      : 2\. Function argument (A list of argument strings to remove from the passthrough)
    +
    +      `variantOverrides`
    +
    +      : 3\. Function argument (The attributes to override)
    +
    +      `variantArgs`
    +
    +      : 4\. Function argument (The arguments passed to the variant by `callPackage`)
    +
    +      # Type
    +
    +      ```
    +      overrideVariant :: Derivation -> [String] -> AttrSet -> AttrSet -> Derivation
    +      ```
    +  */
    +  overrideVariant =
    +    basePackage: excludeArgs: variantOverrides: variantArgs:
    +    basePackage.override (variantOverrides // removeAttrs variantArgs (excludeArgs ++ [ "lib" ]));

    I wonder if automatically adding basePackage to the exclusion list could be a good idea. I imagine in a lot of cases that would be the only thing being explicitly removed. (It may make calls of the function less understandable, though.. is there a way to not require the empty list?) Maybe that could break packages which use themselves in tests though... but I think finalAttrs.finalPackage is the idiomatic way to do that now.

    GraysonTinker GraysonTinker commented on Jul 02, 2026 03:06 UTC
    Reviewer

    Thanks for taking this up. Just a few thoughts from a quick lookthrough, but I'm no lib expert, so take them with a grain of salt.

    Reviewers
    Assignees
    guylamar2006 guylamar2006
    Labels
    risk: low confidence: 88.8% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 64.2% (MEDIUM) · Higher = larger blast radius
    64.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 88.8% · Lower = rushed or shallow review
    88.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 7.2% · Safe to merge
    7.2%
    Repository