Skip to content
Merged
8 changes: 8 additions & 0 deletions nixos/doc/manual/release-notes/rl-1903.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@
make sure to update your configuration if you want to keep <literal>proglodyte-wasm</literal>
</para>
</listitem>
<listitem>
<para>
When the <literal>nixpkgs.pkgs</literal> option is set, NixOS will no
longer ignore the <literal>nixpkgs.overlays</literal> option. The old
behavior can be recovered by setting <literal>nixpkgs.overlays =
lib.mkForce [];</literal>.
</para>
</listitem>
<listitem>
<para>
OpenSMTPD has been upgraded to version 6.4.0p1. This release makes
Expand Down
58 changes: 45 additions & 13 deletions nixos/modules/misc/nixpkgs.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{ config, lib, pkgs, ... }:
{ config, options, lib, pkgs, ... }:

with lib;

let
cfg = config.nixpkgs;
opt = options.nixpkgs;

isConfig = x:
builtins.isAttrs x || lib.isFunction x;
Expand Down Expand Up @@ -54,28 +55,38 @@ let
check = builtins.isAttrs;
};

defaultPkgs = import ../../../pkgs/top-level/default.nix {
inherit (cfg) config overlays localSystem crossSystem;
};

finalPkgs = if opt.pkgs.isDefined then cfg.pkgs.appendOverlays cfg.overlays else defaultPkgs;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not so sure about this. While I see the use case of setting pkgs and overlays, there's no reason you can't set overlays yourself when you init pkgs. Since we don't do this for config, crossSystem, or localSystem, it seems a little unexpected. We probably should have an assertion for those so that you don't set pkgs and something that has no effect.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to add the assertion when I ran into #49765. I actually wrote the assertion yesterday, but forgot to push it here. I don't think it is necessary to add the other assertion about the host system.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding whether we should, this elegantly enables a use case pointed out by aszlig for nixosTest where one of the machines has an extra overlay. So that is one reason why you can't set overlays yourself when you init pkgs.

We could support the other options but I can't tell at this point how well that would work.


in

{
options.nixpkgs = {

pkgs = mkOption {
defaultText = literalExample
''import "''${nixos}/.." {
''import "''${nixos}/../pkgs/top-level" {
inherit (cfg) config overlays localSystem crossSystem;
}
'';
default = import ../../.. {
inherit (cfg) config overlays localSystem crossSystem;
};
type = pkgsType;
example = literalExample ''import <nixpkgs> {}'';
description = ''
This is the evaluation of Nixpkgs that will be provided to
all NixOS modules. Defining this option has the effect of
ignoring the other options that would otherwise be used to
evaluate Nixpkgs, because those are arguments to the default
value. The default value imports the Nixpkgs source files
If set, the pkgs argument to all NixOS modules is the value of
this option, extended with <code>nixpkgs.overlays</code>, if
that is also set. Either <code>nixpkgs.crossSystem</code> or
<code>nixpkgs.localSystem</code> will be used in an assertion
to check that the NixOS and Nixpkgs architectures match. Any
other options in <code>nixpkgs.*</code>, notably <code>config</code>,
will be ignored.

If unset, the pkgs argument to all NixOS modules is determined
as shown in the default value for this option.

The default value imports the Nixpkgs source files
relative to the location of this NixOS module, because
NixOS and Nixpkgs are distributed together for consistency,
so the <code>nixos</code> in the default value is in fact a
Expand Down Expand Up @@ -128,12 +139,14 @@ in
description = ''
List of overlays to use with the Nix Packages collection.
(For details, see the Nixpkgs documentation.) It allows
you to override packages globally. This is a function that
you to override packages globally. Each function in the list
takes as an argument the <emphasis>original</emphasis> Nixpkgs.
The first argument should be used for finding dependencies, and
the second should be used for overriding recipes.

Ignored when <code>nixpkgs.pkgs</code> is set.
If <code>nixpkgs.pkgs</code> is set, overlays specified here
will be applied after the overlays that were already present
in <code>nixpkgs.pkgs</code>.
'';
};

Expand Down Expand Up @@ -207,7 +220,26 @@ in

config = {
_module.args = {
pkgs = cfg.pkgs;
pkgs = finalPkgs;
};

assertions = [
(
let
nixosExpectedSystem =
if config.nixpkgs.crossSystem != null
then config.nixpkgs.crossSystem.system
else config.nixpkgs.localSystem.system;
nixosOption =
if config.nixpkgs.crossSystem != null
then "nixpkgs.crossSystem"
else "nixpkgs.localSystem";
pkgsSystem = finalPkgs.stdenv.targetPlatform.system;
in {
assertion = nixosExpectedSystem == pkgsSystem;
message = "The NixOS nixpkgs.pkgs option was set to a Nixpkgs invocation that compiles to target system ${pkgsSystem} but NixOS was configured for system ${nixosExpectedSystem} via NixOS option ${nixosOption}. The NixOS system settings must match the Nixpkgs target system.";
}
)
];
};
}
2 changes: 1 addition & 1 deletion pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22142,7 +22142,7 @@ with pkgs;
in
myOS.run-nginx

Unlike in plain NixOS, the nixpkgs.config, nixpkgs.overlays and
Unlike in plain NixOS, the nixpkgs.config and
nixpkgs.system options will be ignored by default. Instead,
nixpkgs.pkgs will have the default value of pkgs as it was
constructed right after invoking the nixpkgs function (e.g. the
Expand Down
4 changes: 3 additions & 1 deletion pkgs/top-level/stage.nix
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ let
# preexisting overlays. Prefer to initialize with the right overlays
# in one go when calling Nixpkgs, for performance and simplicity.
appendOverlays = extraOverlays:
import ./stage.nix (args // { overlays = args.overlays ++ extraOverlays; });
if extraOverlays == []
then self
else import ./stage.nix (args // { overlays = args.overlays ++ extraOverlays; });

# Extend the package set with a single overlay. This preserves
# preexisting overlays. Prefer to initialize with the right overlays
Expand Down