Skip to content

Instantly share code, notes, and snippets.

@kesor
Last active November 28, 2024 05:30
Show Gist options
  • Save kesor/9fef702c04f85b971a298d1690ff317c to your computer and use it in GitHub Desktop.
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
{ 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