override-utils: Allow wrapping with environment variables

This commit is contained in:
Andrew Kvalheim 2023-08-11 11:46:22 -07:00
parent 4c48c44233
commit b591f3ddb1

View file

@ -2,8 +2,8 @@
let
inherit (builtins) attrNames elemAt filter functionArgs isAttrs isPath length mapAttrs match pathExists removeAttrs toJSON tryEval;
inherit (stable) callPackage fetchFromGitHub;
inherit (stable.lib) attrByPath concatMapStringsSep const findFirst getAttrFromPath hasAttrByPath imap1 info optionalAttrs optionalString recurseIntoAttrs showAttrPath throwIf toList versionAtLeast;
inherit (stable) callPackage fetchFromGitHub makeWrapper symlinkJoin;
inherit (stable.lib) attrByPath concatMapStringsSep concatStringsSep const escapeShellArg findFirst getAttrFromPath hasAttrByPath imap1 info mapAttrsToList optionalAttrs optionalString recurseIntoAttrs showAttrPath throwIf toList versionAtLeast;
# Utilities
composeOverrides = f1: f2: a0: let o1 = f1 a0; o2 = f2 (a0 // o1); in o1 // o2;
@ -45,6 +45,9 @@ let
, overlay ? null
, patch ? null
# Wrapper
, env ? null
# Package input override
, ...
}:
@ -55,6 +58,7 @@ let
# Specification
doOverlay = gappsWrapperArgs != null || overlay != null || patch != null;
doOverride = ! isEmpty override;
doWrapper = env != null;
# Package selection
path = scope ++ [ pname ];
@ -85,14 +89,31 @@ let
if doOverride then package_with_overlay.override override
else package_with_overlay;
# Wrapper
package_with_overlay_with_override_with_wrapper =
if doWrapper then
symlinkJoin
{
name = "${pname}-wrapper";
paths = [ package_with_overlay_with_override ];
buildInputs = [ makeWrapper ];
postBuild = ''
for program in $out/bin/*; do
wrapProgram "$program" \
${concatStringsSep " " (mapAttrsToList (k: v: "--set ${escapeShellArg k} ${escapeShellArg v}") env)}
done
'';
}
else package_with_overlay_with_override;
# Report
summary = "Resolved ${name}" +
(optionalString (version != null) " ${version}") +
(optionalString (release != null) " of NixOS ${release}") +
(optionalString (doOverlay || doOverride) " with override") +
(optionalString (doOverlay || doOverride || doWrapper) " with override") +
(optionalString (condition != null) " meeting condition") +
(optionalString (! isStable repo) " via ${repoName repo}");
unnecessary = isStable repo && !doOverlay && !doOverride;
unnecessary = isStable repo && !doOverlay && !doOverride && !doWrapper;
unnecessarySearches = concatMapStringsSep ", " repoName (filter (r: r._extra > repo._extra or 0) extra);
in
if hasAttrByPath (path ++ [ "overrideScope'" ]) stable then
@ -102,7 +123,7 @@ let
else
throwIf (unnecessarySearches != "") "${name} no longer requires searching ${unnecessarySearches}"
(throwIf unnecessary "${name} no longer requires an override")
(info summary package_with_overlay_with_override)
(info summary package_with_overlay_with_override_with_wrapper)
;
in
{