Skip to content

Commit 49bbd5d

Browse files
authored
Fix infrec due to filterPlatforms (#113)
* chore: formatting * fix: avoid infrec issues when filtering out packages
1 parent ce2b337 commit 49bbd5d

File tree

1 file changed

+55
-26
lines changed

1 file changed

+55
-26
lines changed

lib/default.nix

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,33 @@ let
1414
flake,
1515
systems,
1616
nixpkgs,
17+
# We need to treat the packages that are being defined in self differently,
18+
# since otherwise we trigger infinite recursion when perSystem is defined in
19+
# terms of the packages defined by self, and self uses perSystem to define
20+
# its packages.
21+
# We run into the infrec when trying to filter out packages based on their
22+
# meta attributes, since that actually requires evaluating the package's derivation
23+
# and can then in turn change the value of perSystem (by removing packages),
24+
# which then requires to evaluate the package again, and so on and so forth.
25+
# To break this cycle, we define perSystem in terms of the filesystem hierarchy,
26+
# and not based on self.packages, and we don't apply any filtering based on
27+
# meta attributes yet.
28+
# The actual self.packages, can then be the filtered set of packages.
29+
unfilteredPackages,
1730
}:
1831
let
19-
2032
# Memoize the args per system
2133
systemArgs = lib.genAttrs systems (
2234
system:
2335
let
2436
# Resolve the packages for each input.
2537
perSystem = lib.mapAttrs (
26-
_: flake: flake.legacyPackages.${system} or { } // flake.packages.${system} or { }
38+
name: flake:
39+
# For self, we need to treat packages differently, see above
40+
if name == "self" then
41+
flake.legacyPackages.${system} or { } // unfilteredPackages.${system}
42+
else
43+
flake.legacyPackages.${system} or { } // flake.packages.${system} or { }
2744
) inputs;
2845

2946
# Handle nixpkgs specially.
@@ -158,6 +175,7 @@ let
158175
flake
159176
nixpkgs
160177
systems
178+
unfilteredPackages
161179
;
162180
})
163181
eachSystem
@@ -336,7 +354,9 @@ let
336354
perSystemModule
337355
path
338356
] ++ mkHomeUsersModule hostName home-manager.nixosModules.default;
339-
specialArgs = specialArgs // { inherit hostName; };
357+
specialArgs = specialArgs // {
358+
inherit hostName;
359+
};
340360
};
341361
};
342362

@@ -354,7 +374,9 @@ let
354374
perSystemModule
355375
path
356376
] ++ mkHomeUsersModule hostName home-manager.darwinModules.default;
357-
specialArgs = specialArgs // { inherit hostName; };
377+
specialArgs = specialArgs // {
378+
inherit hostName;
379+
};
358380
};
359381
};
360382

@@ -372,7 +394,9 @@ let
372394
perSystemSMModule
373395
path
374396
];
375-
extraSpecialArgs = specialArgs // { inherit hostName; };
397+
extraSpecialArgs = specialArgs // {
398+
inherit hostName;
399+
};
376400
};
377401
};
378402

@@ -454,6 +478,30 @@ let
454478
)
455479
)
456480
);
481+
482+
# See the comment in mkEachSystem
483+
unfilteredPackages =
484+
lib.traceIf (builtins.pathExists (src + "/pkgs")) "blueprint: the /pkgs folder is now /packages"
485+
(
486+
let
487+
entries =
488+
(optionalPathAttrs (src + "/packages") (path: importDir path lib.id))
489+
// (optionalPathAttrs (src + "/package.nix") (path: {
490+
default = {
491+
inherit path;
492+
};
493+
}))
494+
// (optionalPathAttrs (src + "/formatter.nix") (path: {
495+
formatter = {
496+
inherit path;
497+
};
498+
}));
499+
in
500+
eachSystem (
501+
{ newScope, system, ... }:
502+
lib.mapAttrs (pname: { path, ... }: newScope { inherit pname; } path { }) entries
503+
)
504+
);
457505
in
458506
# FIXME: maybe there are two layers to this. The blueprint, and then the mapping to flake outputs.
459507
{
@@ -571,27 +619,8 @@ let
571619
defaultNix
572620
];
573621

574-
packages =
575-
lib.traceIf (builtins.pathExists (src + "/pkgs")) "blueprint: the /pkgs folder is now /packages"
576-
(
577-
let
578-
entries =
579-
(optionalPathAttrs (src + "/packages") (path: importDir path lib.id))
580-
// (optionalPathAttrs (src + "/package.nix") (path: {
581-
default = {
582-
inherit path;
583-
};
584-
}))
585-
// (optionalPathAttrs (src + "/formatter.nix") (path: {
586-
formatter = {
587-
inherit path;
588-
};
589-
}));
590-
in
591-
eachSystem (
592-
{ newScope, system, ... }: filterPlatforms system (lib.mapAttrs (pname: { path, ... }: newScope { inherit pname; } path { }) entries)
593-
)
594-
);
622+
# See the comment in mkEachSystem
623+
packages = lib.mapAttrs filterPlatforms unfilteredPackages;
595624

596625
# Defining homeConfigurations under legacyPackages allows the home-manager CLI
597626
# to automatically detect the right output for the current system without

0 commit comments

Comments
 (0)