Last active
November 28, 2024 05:30
-
-
Save kesor/9fef702c04f85b971a298d1690ff317c to your computer and use it in GitHub Desktop.
NIX Environment Variables Wrapper with symlinkJoin to symlink all the original package into the new wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ lib, pkgs, ... }: | |
let | |
# Function to wrap a package with custom environment variables | |
makeWrappedPackage = pkg: envVars: pkgs.symlinkJoin { | |
name = "env-wrap-${pkg.pname or pkg.name}"; | |
paths = [ pkg ]; # Symlink all files form the original | |
buildInputs = [ pkgs.makeWrapper ]; | |
postBuild = '' | |
for exe in ${lib.getExe pkg}; do | |
exeName=$(basename "$exe") | |
rm -f "$out/bin/$exeName" | |
makeWrapper "$exe" "$out/bin/$exeName" \ | |
${lib.concatMapStringsSep " " (kv: "--set ${kv.name} ${kv.value}") (lib.attrsToList envVars)} | |
done | |
''; | |
}; | |
in { | |
wrap = makeWrappedPackage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment