Skip to content

Commit a94393c

Browse files
committed
add overrideAttrs
1 parent de01640 commit a94393c

File tree

4 files changed

+107
-83
lines changed

4 files changed

+107
-83
lines changed

doc/content/_index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ https://github.com/viperML/wrapper-manager/issues
203203

204204
## Changelog
205205

206+
- 2024-08-14
207+
- Added `.overrideAttrs` option
208+
206209
- 2023-11-13
207210
- Added `prependFlags`, which maps to `--add-flags`
208211
- Added `appendFlags`, which maps to `--append-flags`

doc/generated.nix

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# This file was generated by nvfetcher, please do not modify it manually.
2-
{ fetchgit, fetchurl, fetchFromGitHub, dockerTools }:
32
{
3+
fetchgit,
4+
fetchurl,
5+
fetchFromGitHub,
6+
dockerTools,
7+
}: {
48
hugo-book = {
59
pname = "hugo-book";
610
version = "d86d5e70c7c0d787675b13d9aee749c1a8b34776";

modules/base.nix

Lines changed: 91 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,19 @@
132132
"nvim" = "custom-nvim";
133133
};
134134
};
135+
136+
overrideAttrs = mkOption {
137+
type = with types; functionTo attrs;
138+
description = ''
139+
Function to override attributes from the final package.
140+
'';
141+
default = lib.id;
142+
example = ''
143+
old: {
144+
pname = "''${old.pname}-wrapped";
145+
}
146+
'';
147+
};
135148
};
136149

137150
config = {
@@ -155,96 +168,92 @@
155168
if config.value == null
156169
then unsetArg
157170
else setArg;
158-
result =
159-
pkgs.symlinkJoin ({
160-
paths = [config.basePackage] ++ config.extraPackages;
161-
nativeBuildInputs = [pkgs.makeWrapper];
162-
postBuild = let
163-
envArgs = lib.mapAttrsToList envToWrapperArg config.env;
164-
# Yes, the arguments are escaped later, yes, this is intended to "double escape",
165-
# so that they are escaped for wrapProgram and for the final binary too.
166-
prependFlagArgs = map (args: ["--add-flags" (lib.escapeShellArg args)]) config.prependFlags;
167-
appendFlagArgs = map (args: ["--append-flags" (lib.escapeShellArg args)]) config.appendFlags;
168-
pathArgs = map (p: ["--prefix" "PATH" ":" "${p}/bin"]) config.pathAdd;
169-
allArgs = lib.flatten (envArgs ++ prependFlagArgs ++ appendFlagArgs ++ pathArgs);
170-
in ''
171-
for file in $out/bin/*; do
172-
echo "Wrapping $file"
173-
wrapProgram \
174-
$file \
175-
${lib.escapeShellArgs allArgs} \
176-
${config.extraWrapperFlags}
177-
done
171+
result = pkgs.symlinkJoin (
172+
{
173+
paths = [config.basePackage] ++ config.extraPackages;
174+
nativeBuildInputs = [pkgs.makeWrapper];
175+
postBuild = let
176+
envArgs = lib.mapAttrsToList envToWrapperArg config.env;
177+
# Yes, the arguments are escaped later, yes, this is intended to "double escape",
178+
# so that they are escaped for wrapProgram and for the final binary too.
179+
prependFlagArgs = map (args: ["--add-flags" (lib.escapeShellArg args)]) config.prependFlags;
180+
appendFlagArgs = map (args: ["--append-flags" (lib.escapeShellArg args)]) config.appendFlags;
181+
pathArgs = map (p: ["--prefix" "PATH" ":" "${p}/bin"]) config.pathAdd;
182+
allArgs = lib.flatten (envArgs ++ prependFlagArgs ++ appendFlagArgs ++ pathArgs);
183+
in ''
184+
for file in $out/bin/*; do
185+
echo "Wrapping $file"
186+
wrapProgram \
187+
$file \
188+
${lib.escapeShellArgs allArgs} \
189+
${config.extraWrapperFlags}
190+
done
178191
179-
# Some derivations have nested symlinks here
180-
if [[ -d $out/share/applications && ! -w $out/share/applications ]]; then
181-
echo "Detected nested symlink, fixing"
182-
temp=$(mktemp -d)
183-
cp -v $out/share/applications/* $temp
184-
rm -vf $out/share/applications
185-
mkdir -pv $out/share/applications
186-
cp -v $temp/* $out/share/applications
187-
fi
192+
# Some derivations have nested symlinks here
193+
if [[ -d $out/share/applications && ! -w $out/share/applications ]]; then
194+
echo "Detected nested symlink, fixing"
195+
temp=$(mktemp -d)
196+
cp -v $out/share/applications/* $temp
197+
rm -vf $out/share/applications
198+
mkdir -pv $out/share/applications
199+
cp -v $temp/* $out/share/applications
200+
fi
188201
189-
cd $out/bin
190-
for exe in *; do
202+
cd $out/bin
203+
for exe in *; do
191204
192-
if false; then
193-
exit 2
194-
${lib.concatStringsSep "\n" (lib.mapAttrsToList (name: value: ''
195-
elif [[ $exe == ${lib.escapeShellArg name} ]]; then
196-
newexe=${lib.escapeShellArg value}
197-
mv -vf $exe $newexe
198-
'')
199-
config.renames)}
200-
else
201-
newexe=$exe
202-
fi
205+
if false; then
206+
exit 2
207+
${lib.concatStringsSep "\n" (lib.mapAttrsToList (name: value: ''
208+
elif [[ $exe == ${lib.escapeShellArg name} ]]; then
209+
newexe=${lib.escapeShellArg value}
210+
mv -vf $exe $newexe
211+
'')
212+
config.renames)}
213+
else
214+
newexe=$exe
215+
fi
203216
204-
# Fix .desktop files
205-
# This list of fixes might not be exhaustive
206-
for file in $out/share/applications/*; do
207-
echo "Fixing file=$file for exe=$exe"
208-
set -x
209-
trap "set +x" ERR
210-
sed -i "s#/nix/store/.*/bin/$exe #$out/bin/$newexe #" "$file"
211-
sed -i -E "s#Exec=$exe([[:space:]]*)#Exec=$out/bin/$newexe\1#g" "$file"
212-
sed -i -E "s#TryExec=$exe([[:space:]]*)#TryExec=$out/bin/$newexe\1#g" "$file"
213-
set +x
214-
done
217+
# Fix .desktop files
218+
# This list of fixes might not be exhaustive
219+
for file in $out/share/applications/*; do
220+
echo "Fixing file=$file for exe=$exe"
221+
set -x
222+
trap "set +x" ERR
223+
sed -i "s#/nix/store/.*/bin/$exe #$out/bin/$newexe #" "$file"
224+
sed -i -E "s#Exec=$exe([[:space:]]*)#Exec=$out/bin/$newexe\1#g" "$file"
225+
sed -i -E "s#TryExec=$exe([[:space:]]*)#TryExec=$out/bin/$newexe\1#g" "$file"
226+
set +x
215227
done
228+
done
216229
217230
218-
# I don't know of a better way to create a multe-output derivation for symlinkJoin
219-
# So if the packages have man, just link them into $out
220-
${
221-
lib.concatMapStringsSep "\n"
222-
(p:
223-
if lib.hasAttr "man" p
224-
then "${pkgs.xorg.lndir}/bin/lndir -silent ${p.man} $out"
225-
else "#")
226-
([config.basePackage] ++ config.extraPackages)
227-
}
228-
'';
229-
passthru =
230-
(config.basePackage.passthru or {})
231-
// {
232-
unwrapped = config.basePackage;
233-
};
234-
}
235-
// lib.getAttrs [
236-
"name"
237-
"meta"
238-
]
239-
config.basePackage)
240-
// (lib.optionalAttrs (lib.hasAttr "pname" config.basePackage) {
241-
inherit (config.basePackage) pname;
242-
})
243-
// (lib.optionalAttrs (lib.hasAttr "version" config.basePackage) {
244-
inherit (config.basePackage) version;
245-
});
231+
# I don't know of a better way to create a multe-output derivation for symlinkJoin
232+
# So if the packages have man, just link them into $out
233+
${
234+
lib.concatMapStringsSep "\n"
235+
(p:
236+
if lib.hasAttr "man" p
237+
then "${pkgs.xorg.lndir}/bin/lndir -silent ${p.man} $out"
238+
else "#")
239+
([config.basePackage] ++ config.extraPackages)
240+
}
241+
'';
242+
passthru =
243+
(config.basePackage.passthru or {})
244+
// {
245+
unwrapped = config.basePackage;
246+
};
247+
}
248+
// {
249+
inherit (config.basePackage) name meta;
250+
}
251+
);
246252
in
247-
lib.recursiveUpdate result {
253+
lib.recursiveUpdate ((result.overrideAttrs config.overrideAttrs).overrideAttrs (old:
254+
lib.optionalAttrs (lib.hasAttr "pname" old) {
255+
pname = lib.warn "wrapper-manager: ${old.name}: symlinkJoin requires a name, not a pname+version" old.pname;
256+
})) {
248257
meta.outputsToInstall = ["out"];
249258
};
250259
};

tests/test-module.nix

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,12 @@
5656
"--disable-gpu"
5757
];
5858
};
59+
60+
wrappers.hello-wrapped = {
61+
basePackage = pkgs.hello;
62+
overrideAttrs = old: {
63+
name = "hello-wrapped";
64+
pname = "hello-wrapped-bad";
65+
};
66+
};
5967
}

0 commit comments

Comments
 (0)