Skip to content

Commit

Permalink
add overrideAttrs
Browse files Browse the repository at this point in the history
  • Loading branch information
viperML committed Aug 14, 2024
1 parent e1584a2 commit 7b5d480
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 83 deletions.
3 changes: 3 additions & 0 deletions doc/content/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ https://github.com/viperML/wrapper-manager/issues

## Changelog

- 2024-08-14
- Added `.overrideAttrs` option

- 2023-11-13
- Added `prependFlags`, which maps to `--add-flags`
- Added `appendFlags`, which maps to `--append-flags`
Expand Down
6 changes: 5 additions & 1 deletion doc/generated.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# This file was generated by nvfetcher, please do not modify it manually.
{ fetchgit, fetchurl, fetchFromGitHub, dockerTools }:
{
fetchgit,
fetchurl,
fetchFromGitHub,
dockerTools,
}: {
hugo-book = {
pname = "hugo-book";
version = "d86d5e70c7c0d787675b13d9aee749c1a8b34776";
Expand Down
173 changes: 91 additions & 82 deletions modules/base.nix
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,19 @@
"nvim" = "custom-nvim";
};
};

overrideAttrs = mkOption {
type = with types; functionTo attrs;
description = ''
Function to override attributes from the final package.
'';
default = lib.id;
example = ''
old: {
pname = "''${old.pname}-wrapped";
}
'';
};
};

config = {
Expand All @@ -155,96 +168,92 @@
if config.value == null
then unsetArg
else setArg;
result =
pkgs.symlinkJoin ({
paths = [config.basePackage] ++ config.extraPackages;
nativeBuildInputs = [pkgs.makeWrapper];
postBuild = let
envArgs = lib.mapAttrsToList envToWrapperArg config.env;
# Yes, the arguments are escaped later, yes, this is intended to "double escape",
# so that they are escaped for wrapProgram and for the final binary too.
prependFlagArgs = map (args: ["--add-flags" (lib.escapeShellArg args)]) config.prependFlags;
appendFlagArgs = map (args: ["--append-flags" (lib.escapeShellArg args)]) config.appendFlags;
pathArgs = map (p: ["--prefix" "PATH" ":" "${p}/bin"]) config.pathAdd;
allArgs = lib.flatten (envArgs ++ prependFlagArgs ++ appendFlagArgs ++ pathArgs);
in ''
for file in $out/bin/*; do
echo "Wrapping $file"
wrapProgram \
$file \
${lib.escapeShellArgs allArgs} \
${config.extraWrapperFlags}
done
result = pkgs.symlinkJoin (
{
paths = [config.basePackage] ++ config.extraPackages;
nativeBuildInputs = [pkgs.makeWrapper];
postBuild = let
envArgs = lib.mapAttrsToList envToWrapperArg config.env;
# Yes, the arguments are escaped later, yes, this is intended to "double escape",
# so that they are escaped for wrapProgram and for the final binary too.
prependFlagArgs = map (args: ["--add-flags" (lib.escapeShellArg args)]) config.prependFlags;
appendFlagArgs = map (args: ["--append-flags" (lib.escapeShellArg args)]) config.appendFlags;
pathArgs = map (p: ["--prefix" "PATH" ":" "${p}/bin"]) config.pathAdd;
allArgs = lib.flatten (envArgs ++ prependFlagArgs ++ appendFlagArgs ++ pathArgs);
in ''
for file in $out/bin/*; do
echo "Wrapping $file"
wrapProgram \
$file \
${lib.escapeShellArgs allArgs} \
${config.extraWrapperFlags}
done
# Some derivations have nested symlinks here
if [[ -d $out/share/applications && ! -w $out/share/applications ]]; then
echo "Detected nested symlink, fixing"
temp=$(mktemp -d)
cp -v $out/share/applications/* $temp
rm -vf $out/share/applications
mkdir -pv $out/share/applications
cp -v $temp/* $out/share/applications
fi
# Some derivations have nested symlinks here
if [[ -d $out/share/applications && ! -w $out/share/applications ]]; then
echo "Detected nested symlink, fixing"
temp=$(mktemp -d)
cp -v $out/share/applications/* $temp
rm -vf $out/share/applications
mkdir -pv $out/share/applications
cp -v $temp/* $out/share/applications
fi
cd $out/bin
for exe in *; do
cd $out/bin
for exe in *; do
if false; then
exit 2
${lib.concatStringsSep "\n" (lib.mapAttrsToList (name: value: ''
elif [[ $exe == ${lib.escapeShellArg name} ]]; then
newexe=${lib.escapeShellArg value}
mv -vf $exe $newexe
'')
config.renames)}
else
newexe=$exe
fi
if false; then
exit 2
${lib.concatStringsSep "\n" (lib.mapAttrsToList (name: value: ''
elif [[ $exe == ${lib.escapeShellArg name} ]]; then
newexe=${lib.escapeShellArg value}
mv -vf $exe $newexe
'')
config.renames)}
else
newexe=$exe
fi
# Fix .desktop files
# This list of fixes might not be exhaustive
for file in $out/share/applications/*; do
echo "Fixing file=$file for exe=$exe"
set -x
trap "set +x" ERR
sed -i "s#/nix/store/.*/bin/$exe #$out/bin/$newexe #" "$file"
sed -i -E "s#Exec=$exe([[:space:]]*)#Exec=$out/bin/$newexe\1#g" "$file"
sed -i -E "s#TryExec=$exe([[:space:]]*)#TryExec=$out/bin/$newexe\1#g" "$file"
set +x
done
# Fix .desktop files
# This list of fixes might not be exhaustive
for file in $out/share/applications/*; do
echo "Fixing file=$file for exe=$exe"
set -x
trap "set +x" ERR
sed -i "s#/nix/store/.*/bin/$exe #$out/bin/$newexe #" "$file"
sed -i -E "s#Exec=$exe([[:space:]]*)#Exec=$out/bin/$newexe\1#g" "$file"
sed -i -E "s#TryExec=$exe([[:space:]]*)#TryExec=$out/bin/$newexe\1#g" "$file"
set +x
done
done
# I don't know of a better way to create a multe-output derivation for symlinkJoin
# So if the packages have man, just link them into $out
${
lib.concatMapStringsSep "\n"
(p:
if lib.hasAttr "man" p
then "${pkgs.xorg.lndir}/bin/lndir -silent ${p.man} $out"
else "#")
([config.basePackage] ++ config.extraPackages)
}
'';
passthru =
(config.basePackage.passthru or {})
// {
unwrapped = config.basePackage;
};
}
// lib.getAttrs [
"name"
"meta"
]
config.basePackage)
// (lib.optionalAttrs (lib.hasAttr "pname" config.basePackage) {
inherit (config.basePackage) pname;
})
// (lib.optionalAttrs (lib.hasAttr "version" config.basePackage) {
inherit (config.basePackage) version;
});
# I don't know of a better way to create a multe-output derivation for symlinkJoin
# So if the packages have man, just link them into $out
${
lib.concatMapStringsSep "\n"
(p:
if lib.hasAttr "man" p
then "${pkgs.xorg.lndir}/bin/lndir -silent ${p.man} $out"
else "#")
([config.basePackage] ++ config.extraPackages)
}
'';
passthru =
(config.basePackage.passthru or {})
// {
unwrapped = config.basePackage;
};
}
// {
inherit (config.basePackage) name meta;
}
);
in
lib.recursiveUpdate result {
lib.recursiveUpdate ((result.overrideAttrs config.overrideAttrs).overrideAttrs (old:
lib.optionalAttrs (lib.hasAttr "pname" old) {
pname = lib.warn "wrapper-manager: ${old.name}: symlinkJoin requires a name, not a pname+version" old.pname;
})) {
meta.outputsToInstall = ["out"];
};
};
Expand Down
8 changes: 8 additions & 0 deletions tests/test-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,12 @@
"--disable-gpu"
];
};

wrappers.hello-wrapped = {
basePackage = pkgs.hello;
overrideAttrs = old: {
name = "hello-wrapped";
pname = "hello-wrapped-bad";
};
};
}

0 comments on commit 7b5d480

Please sign in to comment.