From 7b5d4802db9f454b7e9b7b98fd0c159ace6dc8c2 Mon Sep 17 00:00:00 2001 From: Fernando Ayats Date: Wed, 14 Aug 2024 23:20:58 +0200 Subject: [PATCH] add overrideAttrs --- doc/content/_index.md | 3 + doc/generated.nix | 6 +- modules/base.nix | 173 ++++++++++++++++++++++-------------------- tests/test-module.nix | 8 ++ 4 files changed, 107 insertions(+), 83 deletions(-) diff --git a/doc/content/_index.md b/doc/content/_index.md index bfbcc94..cff39cd 100644 --- a/doc/content/_index.md +++ b/doc/content/_index.md @@ -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` diff --git a/doc/generated.nix b/doc/generated.nix index 2c64f56..4949299 100644 --- a/doc/generated.nix +++ b/doc/generated.nix @@ -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"; diff --git a/modules/base.nix b/modules/base.nix index c9d501f..1ebea63 100644 --- a/modules/base.nix +++ b/modules/base.nix @@ -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 = { @@ -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"]; }; }; diff --git a/tests/test-module.nix b/tests/test-module.nix index a379f0e..1ed5f98 100644 --- a/tests/test-module.nix +++ b/tests/test-module.nix @@ -56,4 +56,12 @@ "--disable-gpu" ]; }; + + wrappers.hello-wrapped = { + basePackage = pkgs.hello; + overrideAttrs = old: { + name = "hello-wrapped"; + pname = "hello-wrapped-bad"; + }; + }; }