Skip to content

feat: add withConfig & buildConfig from nixpkgs#559

Merged
brianmcgee merged 1 commit intonumtide:mainfrom
MattSturgeon:config-wrappers
Mar 19, 2025
Merged

feat: add withConfig & buildConfig from nixpkgs#559
brianmcgee merged 1 commit intonumtide:mainfrom
MattSturgeon:config-wrappers

Conversation

@MattSturgeon
Copy link
Contributor

@MattSturgeon MattSturgeon commented Mar 19, 2025

Inherit the withConfig and buildConfig wrapper functions added in NixOS/nixpkgs#390147

Fixes #558

If the current nixpkgs revision does not have these attrs, a sane error message is thrown when attempting to use them.

nix-repl> :lf .

nix-repl> packages.x86_64-linux.treefmt.withConfig
error:
       … while evaluating the attribute 'x86_64-linux.treefmt.withConfig'
       … while evaluating a branch condition

       error: Accessing `withConfig` requires a nixpkgs revision that has `treefmt.withConfig`.

I've tested this with and without nix flake lock --override-input nixpkgs 'github:nixos/nixpkgs/nixos-unstable-small'. With the newer nixpkgs revision the wrapper functions seem to work correctly.

The treefmt argument passed is overridden to refer to perSystem.self.treefmt.

I considered using lib.genAttrs to simplify the inheritence, however doing that with // caused the entire passthru to be indented by alejandra. I also considered using inherit (lib.genAttrs), however this didn't seem much of an improvement:

diff --git a/nix/packages/treefmt/default.nix b/nix/packages/treefmt/default.nix
index 4db1258..59032b7 100644
--- a/nix/packages/treefmt/default.nix
+++ b/nix/packages/treefmt/default.nix
@@ -84,8 +84,15 @@ in
         then attr.override {inherit treefmt;}
         else attr;
     in {
-      withConfig = inheritFromNixpkgs "withConfig";
-      buildConfig = inheritFromNixpkgs "buildConfig";
+      inherit
+        (lib.genAttrs [
+            "withConfig"
+            "buildConfig"
+          ]
+          inheritFromNixpkgs)
+        withConfig
+        buildConfig
+        ;
 
       no-vendor-hash = treefmt.overrideAttrs {
         vendorHash = "";

I added some calls to trace to verify that the treefmt package argument is being correctly overridden:

diff --git a/nix/packages/treefmt/default.nix b/nix/packages/treefmt/default.nix
index 4db1258..51d2b59 100644
--- a/nix/packages/treefmt/default.nix
+++ b/nix/packages/treefmt/default.nix
@@ -81,8 +81,8 @@ in
         attr = pkgs.treefmt.${name} or (throw error);
       in
         if attr.override.__functionArgs.treefmt or null != null
-        then attr.override {inherit treefmt;}
-        else attr;
+        then builtins.trace "overriding" attr.override {inherit treefmt;}
+        else builtins.trace "not overriding" attr;
     in {
       withConfig = inheritFromNixpkgs "withConfig";
       buildConfig = inheritFromNixpkgs "buildConfig";

We see that withConfig overrides its treefmt arg, while buildConfig does not depend on treefmt:

nix-repl> :lf .

nix-repl> packages.x86_64-linux.treefmt.withConfig
trace: overriding

nix-repl> packages.x86_64-linux.treefmt.buildConfig
trace: not overriding

Inherit the `withConfig` and `buildConfig` wrapper functions added in
NixOS/nixpkgs#390147

If the current nixpkgs revision does not have these attrs, a sane error
message is thrown when attempting to use them.

The `treefmt` argument passed is overridden to refer to
`perSystem.self.treefmt`.
@brianmcgee
Copy link
Member

This reminds me, we should probably move away from alejandra and start using the official formatter 🤔

@brianmcgee brianmcgee merged commit 3ba936c into numtide:main Mar 19, 2025
27 checks passed
@MattSturgeon MattSturgeon deleted the config-wrappers branch March 19, 2025 18:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inherit simple config wrappers from nixpkgs

2 participants