Skip to content

Commit ebc3432

Browse files
bacchanaliatm-drtina
authored andcommitted
nixos: add nixpkgs.scopedOverlays option (#254)
so we can add overlays that apply within configurations without effecting the base nixpkgs
1 parent ee29580 commit ebc3432

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

nixos/modules/misc/nixpkgs.nix

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,36 @@ let
101101
;
102102
};
103103

104-
finalPkgs = if opt.pkgs.isDefined then cfg.pkgs.appendOverlays cfg.overlays else defaultPkgs;
104+
overlayedPkgs = if opt.pkgs.isDefined then cfg.pkgs.appendOverlays cfg.overlays else defaultPkgs;
105+
splicedPackages = overlayedPkgs.__splicedPackages;
105106

107+
finalPkgs = if (cfg.scopedOverlays or [ ]) == [ ] then splicedPackages else
108+
let
109+
# cribbed of lib.makeScope
110+
withScope = base: extension:
111+
let
112+
self = packages self // scope;
113+
packages = self: extension self base;
114+
scope = {
115+
inherit extension packages;
116+
117+
newScope = scope: base.newScope (self // scope);
118+
119+
callPackage = self.newScope {};
120+
callPackages = base.callPackagesWith self;
121+
callPackageWith = autoArgs: base.callPackageWith (self // autoArgs);
122+
callPackagesWith = autoArgs: base.callPackagesWith (self // autoArgs);
123+
124+
appendOverlays = fs: withScope (base.appendOverlays fs) extension;
125+
extend = f: self.appendOverlays [f];
126+
127+
overrideScope = f: lib.makeScope base.newScope (lib.extends f packages);
128+
appendOverlaysToScope = fs: withScope base (lib.composeManyExtensions ([extension] ++ fs));
129+
extendScope = f: self.appendOverlaysToScope [f];
130+
};
131+
in base // self;
132+
in
133+
withScope splicedPackages (lib.composeManyExtensions cfg.scopedOverlays);
106134
in
107135

108136
{
@@ -194,6 +222,25 @@ in
194222
'';
195223
};
196224

225+
scopedOverlays = lib.mkOption {
226+
default = [];
227+
example = lib.literalExpression
228+
''
229+
[
230+
(self: super: {
231+
# Log commands run in builds of configuration files
232+
runCommand = name: args: text: super.runCommand name args '''
233+
set +x
234+
''${text}
235+
''';
236+
]
237+
'';
238+
type = lib.types.listOf overlayType;
239+
description = ''
240+
List of overlays to apply in a new scope after `nixpkgs.overlays` is applied accessed trough the `pgks` module argument.
241+
'';
242+
};
243+
197244
hostPlatform = lib.mkOption {
198245
type = lib.types.either lib.types.str lib.types.attrs; # TODO utilize lib.systems.parsedPlatform
199246
example = {
@@ -356,7 +403,7 @@ in
356403
# which is somewhat costly for Nixpkgs. With an explicit priority, we only
357404
# evaluate the wrapper to find out that the priority is lower, and then we
358405
# don't need to evaluate `finalPkgs`.
359-
lib.mkOverride lib.modules.defaultOverridePriority finalPkgs.__splicedPackages;
406+
lib.mkOverride lib.modules.defaultOverridePriority finalPkgs;
360407
};
361408

362409
assertions =

0 commit comments

Comments
 (0)