modules/nixpkgs: construct an instance of nixpkgs.source

This commit is contained in:
Matt Sturgeon 2024-10-19 22:59:48 +01:00
parent 8dc8fa38b0
commit 912841c1a7
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299
3 changed files with 81 additions and 37 deletions

View file

@ -54,17 +54,7 @@ in
description = '' description = ''
If set, the `pkgs` argument to all Nixvim modules is the value of this option. If set, the `pkgs` argument to all Nixvim modules is the value of this option.
<!-- TODO: remove --> If unset, the `pkgs` argument is determined by importing `nixpkgs.source`.
If unset, an assertion will trigger. In the future a `pkgs` instance will be constructed.
<!--
TODO:
If unset, the pkgs argument is determined as shown in the default value for this option.
TODO:
The default value imports the Nixpkgs input specified in Nixvim's `flake.lock`.
The `config`, `overlays`, `localSystem`, and `crossSystem` come from this option's siblings.
-->
This option can be used by external applications to increase the performance of evaluation, This option can be used by external applications to increase the performance of evaluation,
or to create packages that depend on a container that should be built with the exact same or to create packages that depend on a container that should be built with the exact same
@ -160,15 +150,8 @@ in
For details, see the [Overlays chapter in the Nixpkgs manual](https://nixos.org/manual/nixpkgs/stable/#chap-overlays). For details, see the [Overlays chapter in the Nixpkgs manual](https://nixos.org/manual/nixpkgs/stable/#chap-overlays).
<!-- TODO: Remove --> If the {option}`nixpkgs.pkgs` option is set, overlays specified using `nixpkgs.overlays`
Overlays specified using the {option}`nixpkgs.overlays` option will be will be applied after the overlays that were already included in `nixpkgs.pkgs`.
applied after the overlays that were already included in `nixpkgs.pkgs`.
<!--
TODO:
If the {option}`nixpkgs.pkgs` option is set, overlays specified using `nixpkgs.overlays`
will be applied after the overlays that were already included in `nixpkgs.pkgs`.
-->
''; '';
}; };
@ -228,26 +211,33 @@ in
Ignored when `nixpkgs.pkgs` is set. Ignored when `nixpkgs.pkgs` is set.
''; '';
# FIXME: This is a stub option for now
internal = true;
}; };
}; };
config = config =
let let
# TODO: construct a default pkgs instance from pkgsPath and cfg options
# https://github.com/nix-community/nixvim/issues/1784
finalPkgs = finalPkgs =
if opt.pkgs.isDefined then if opt.pkgs.isDefined then
cfg.pkgs.appendOverlays cfg.overlays cfg.pkgs.appendOverlays cfg.overlays
else else
# TODO: Remove once pkgs can be constructed internally let
throw '' args = {
nixvim: `nixpkgs.pkgs` is not defined. In the future, this option will be optional. inherit (cfg) config overlays;
Currently a pkgs instance must be evaluated externally and assigned to `nixpkgs.pkgs` option. };
'';
# Configure `localSystem` and `crossSystem` as required
systemArgs =
if cfg.buildPlatform == cfg.hostPlatform then
{
localSystem = cfg.hostPlatform;
}
else
{
localSystem = cfg.buildPlatform;
crossSystem = cfg.hostPlatform;
};
in
import cfg.source (args // systemArgs);
in in
{ {
# We explicitly set the default override priority, so that we do not need # We explicitly set the default override priority, so that we do not need
@ -258,11 +248,6 @@ in
# don't need to evaluate `finalPkgs`. # don't need to evaluate `finalPkgs`.
_module.args.pkgs = lib.mkOverride lib.modules.defaultOverridePriority finalPkgs.__splicedPackages; _module.args.pkgs = lib.mkOverride lib.modules.defaultOverridePriority finalPkgs.__splicedPackages;
# FIXME: This is a stub option for now
warnings = lib.optional (
opt.source.isDefined && opt.source.highestPrio < (lib.mkOptionDefault null).priority
) "Defining the option `nixpkgs.source` currently has no effect";
assertions = [ assertions = [
{ {
assertion = opt.pkgs.isDefined -> cfg.config == { }; assertion = opt.pkgs.isDefined -> cfg.config == { };

15
tests/nixpkgs-mock.nix Normal file
View file

@ -0,0 +1,15 @@
# This mock nixpkgs can be used as `nixpkgs.source` in nixpkgs-module-test
# if we want/need to avoid importing & instantiating a real nixpkgs
{
config ? { },
...
}:
let
pkgs = {
_type = "pkgs";
__splicedPackages = pkgs;
inherit config pkgs;
mock = true;
};
in
pkgs

View file

@ -36,7 +36,51 @@ let
in in
linkFarmFromDrvs "nixpkgs-module-test" [ linkFarmFromDrvs "nixpkgs-module-test" [
# TODO: expect not setting `nixpkgs.pkgs` to throw # Test that pkgs-config is affected by `nixpkgs.config`
(testModule "nixpkgs-config" (
{ pkgs, ... }:
{
nixpkgs.config = {
permittedInsecurePackages = [
"foobar123"
];
};
nixpkgs.hostPlatform = {
inherit (stdenv.hostPlatform) system;
};
assertions = [
{
assertion = pkgs.config.permittedInsecurePackages == [ "foobar123" ];
message = ''
Expected `pkgs.config.permittedInsecurePackages` to match [ "foobar123" ], but found:
${lib.generators.toPretty { } pkgs.config.permittedInsecurePackages}'';
}
];
}
))
# Test that a nixpkgs revision can be specified using `nixpkgs.source`
(testModule "nixpkgs-source" (
{ pkgs, ... }:
{
nixpkgs.source = ./nixpkgs-mock.nix;
nixpkgs.hostPlatform = {
inherit (stdenv.hostPlatform) system;
};
assertions = [
{
assertion = pkgs.mock or false;
message = "Expected `pkgs.mock` to be true, but ${
if pkgs ? mock then "found " + lib.generators.toPretty { } pkgs.mock else "isn't present"
}";
}
];
}
))
(testModule "nixpkgs-overlays" ( (testModule "nixpkgs-overlays" (
{ pkgs, ... }: { pkgs, ... }: