Skip to content

Commit 2f580df

Browse files
committed
makeOverridable: set meta.position
1 parent 4ac2eb7 commit 2f580df

2 files changed

Lines changed: 69 additions & 25 deletions

File tree

lib/customisation.nix

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,37 @@ rec {
151151
:::
152152
*/
153153
makeOverridable =
154+
let
155+
positionFrom =
156+
attrs:
157+
if attrs == { } then
158+
null
159+
else
160+
let
161+
loc = unsafeGetAttrPos (head (attrNames attrs)) attrs;
162+
in
163+
if loc == null then null else "${loc.file}:${toString loc.line}";
164+
updateMeta =
165+
position: result:
166+
if position != null && isDerivation result && result ? meta then
167+
extendDerivation true {
168+
meta = result.meta // {
169+
inherit position;
170+
};
171+
172+
# extendDerivation is weird about this and I'm afraid to change it
173+
# with all the TODOs floating around in there. If you do, check
174+
# that
175+
#
176+
# ((pkg.override ...).dev.overrideAttrs ...).outputName == "dev"
177+
#
178+
# for a package with a dev output, which is not true right now
179+
# unless I do this.
180+
${if result ? overrideAttrs then "overrideAttrs" else null} = result.overrideAttrs;
181+
} result
182+
else
183+
result;
184+
in
154185
f:
155186
let
156187
# Creates a functor with the same arguments as f
@@ -178,9 +209,6 @@ rec {
178209
let
179210
result = f origArgs;
180211

181-
# Changes the original arguments with (potentially a function that returns) a set of new attributes
182-
overrideWith = newArgs: origArgs // (if isFunction newArgs then newArgs origArgs else newArgs);
183-
184212
# Re-call the function but with different arguments
185213
overrideArgs = mirrorArgs (
186214
/**
@@ -190,7 +218,13 @@ rec {
190218
191219
This function was provided by `lib.makeOverridable`.
192220
*/
193-
newArgs: makeOverridable f (overrideWith newArgs)
221+
newArgs:
222+
let
223+
overlay = if isFunction newArgs then newArgs origArgs else newArgs;
224+
in
225+
makeOverridable (mirrorArgs (finalArgs: updateMeta (positionFrom overlay) (f finalArgs))) (
226+
origArgs // overlay
227+
)
194228
);
195229
# Change the result of the function call by applying g to it
196230
overrideResult = g: makeOverridable (mirrorArgs (args: g (f args))) origArgs;

pkgs/stdenv/generic/make-derivation.nix

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ let
9696
let
9797
prev = f final;
9898
thisOverlay = overlay final prev;
99+
pos = builtins.unsafeGetAttrPos (head (attrNames thisOverlay)) thisOverlay;
99100
warnForBadVersionOverride = (
100101
prev ? src
101102
&& thisOverlay ? version
@@ -108,28 +109,37 @@ let
108109
);
109110
pname = args.pname or "<unknown name>";
110111
version = args.version or "<unknown version>";
111-
pos = builtins.unsafeGetAttrPos "version" thisOverlay;
112+
versionPos = builtins.unsafeGetAttrPos "version" thisOverlay;
112113
in
113-
lib.warnIf warnForBadVersionOverride ''
114-
${
115-
args.name or "${pname}-${version}"
116-
} was overridden with `version` but not `src` at ${pos.file or "<unknown file>"}:${
117-
toString pos.line or "<unknown line>"
118-
}:${toString pos.column or "<unknown column>"}.
119-
120-
This is most likely not what you want. In order to properly change the version of a package, override
121-
both the `version` and `src` attributes:
122-
123-
hello.overrideAttrs (oldAttrs: rec {
124-
version = "1.0.0";
125-
src = pkgs.fetchurl {
126-
url = "mirror://gnu/hello/hello-''${version}.tar.gz";
127-
hash = "...";
128-
};
129-
})
130-
131-
(To silence this warning, set `__intentionallyOverridingVersion = true` in your `overrideAttrs` call.)
132-
'' (prev // (removeAttrs thisOverlay [ "__intentionallyOverridingVersion" ]))
114+
if thisOverlay == { } then
115+
prev
116+
else
117+
lib.warnIf warnForBadVersionOverride
118+
''
119+
${args.name or "${pname}-${version}"} was overridden with `version` but not `src` at ${
120+
versionPos.file or "<unknown file>"
121+
}:${toString versionPos.line or "<unknown line>"}:${
122+
toString versionPos.column or "<unknown column>"
123+
}.
124+
125+
This is most likely not what you want. In order to properly change the version of a package, override
126+
both the `version` and `src` attributes:
127+
128+
hello.overrideAttrs (oldAttrs: rec {
129+
version = "1.0.0";
130+
src = pkgs.fetchurl {
131+
url = "mirror://gnu/hello/hello-''${version}.tar.gz";
132+
hash = "...";
133+
};
134+
})
135+
136+
(To silence this warning, set `__intentionallyOverridingVersion = true` in your `overrideAttrs` call.)
137+
''
138+
(
139+
prev
140+
// removeAttrs thisOverlay [ "__intentionallyOverridingVersion" ]
141+
// optionalAttrs (pos != null) { inherit pos; }
142+
)
133143
);
134144
in
135145
makeDerivationExtensible (extends' (lib.toExtension f0) rattrs);

0 commit comments

Comments
 (0)