In Cloud Foundry, user-defined env variables can be set using "cf set-env" command.
Can the same be set from buildpack programmatically?
Are you asking about creating a custom buildpack, or using one of the available CF buildpacks?
A buildpack sets the command that is used to launch an application, so if you are creating your own buildpack or customizing an existing one you can set env vars as part of that startup command. For example a startup command could like like this:
VAR1=VALUE1 VAR2=VALUE2 some-command