How to set variable at runtime

I’m trying to implement this in the existing Nixpkgs package. That means I need to change the executable so that it starts by running a few lines to set a variable before actually running the underlying binary:

''
  AVALONIA_GLOBAL_SCALE_FACTOR="$(${glib}/bin/gsettings get org.gnome.desktop.interface scaling-factor | cut --delimiter=' ' --fields=2)"
  AVALONIA_GLOBAL_SCALE_FACTOR="$${AVALONIA_GLOBAL_SCALE_FACTOR:-1}"
  export AVALONIA_GLOBAL_SCALE_FACTOR
''

How can I achieve this?