Skip to content

Commit

Permalink
modules/nixpkgs: restructure nixpkgs.pkgs.isDefined assertion
Browse files Browse the repository at this point in the history
We can throw inline to ensure our error is displayed before any attempt
to use the (non-existent) `pkgs` arg results in a less helpful error.
  • Loading branch information
MattSturgeon committed Oct 19, 2024
1 parent e3239b4 commit b9d17d5
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions modules/top-level/nixpkgs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,28 @@ in
};
};

config = {
# For now we only set this when `nixpkgs.pkgs` is defined
# TODO: construct a default pkgs instance from pkgsPath and cfg options
# https://github.com/nix-community/nixvim/issues/1784
_module.args = lib.optionalAttrs opt.pkgs.isDefined {
config =
let
# TODO: construct a default pkgs instance from pkgsPath and cfg options
# https://github.com/nix-community/nixvim/issues/1784

finalPkgs =
if opt.pkgs.isDefined then
cfg.pkgs
else
# TODO: Remove once pkgs can be constructed internally
throw ''
nixvim: `nixpkgs.pkgs` is not defined. In the future, this option will be optional.
Currently a pkgs instance must be evaluated externally and assigned to `nixpkgs.pkgs` option.
'';
in
{
# We explicitly set the default override priority, so that we do not need
# to evaluate finalPkgs in case an override is placed on `_module.args.pkgs`.
# After all, to determine a definition priority, we need to evaluate `._type`,
# which is somewhat costly for Nixpkgs. With an explicit priority, we only
# evaluate the wrapper to find out that the priority is lower, and then we
# don't need to evaluate `finalPkgs`.
pkgs = lib.mkOverride lib.modules.defaultOverridePriority cfg.pkgs.__splicedPackages;
_module.args.pkgs = lib.mkOverride lib.modules.defaultOverridePriority finalPkgs.__splicedPackages;
};

assertions = [
{
# TODO: Remove or rephrase once pkgs can be constructed internally
assertion = config._module.args ? pkgs;
message = ''
`nixpkgs.pkgs` is not defined. In the future, this option will be optional.
Currently a pkgs instance must be evaluated externally and assigned to `nixpkgs.pkgs` option.
'';
}
];
};
}

0 comments on commit b9d17d5

Please sign in to comment.