Environment Variable Fixup Proposal

%3CLINGO-SUB%20id%3D%22lingo-sub-1594494%22%20slang%3D%22en-US%22%3EEnvironment%20Variable%20Fixup%20Proposal%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1594494%22%20slang%3D%22en-US%22%3E%3CP%3ESome%20traditional%20applications%20make%20use%20of%20environment%20variables.%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EThese%20are%20typically%20added%20as%20part%20of%20the%20installation%2Fcustomization.%3C%2FLI%3E%0A%3CLI%3EThey%20may%20be%20declared%20as%20System%20or%20User%20in%20scope.%3C%2FLI%3E%0A%3CLI%3EUltimately%2C%20they%20are%20stored%20in%20the%20registry%20but%20added%20to%20the%20process'%20environment%20block%20inside%20the%20kernel.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EWhile%20Microsoft%20App-V%20had%20support%20for%20the%20package%20declaring%20environment%20variables%20in%20the%20manifest%2C%20MSIX%20does%20not.%26nbsp%3B%20The%20App-V%20support%20turned%20what%20may%20have%20originally%20been%20an%20environment%20variable%20of%20System%20or%20User%20scope%20into%20one%20with%20an%20Application-specific%20scope.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20am%20proposing%20the%20creation%20of%20a%20new%20PSF%20Fixup%20called%20EnvVarFixup.%26nbsp%3B%20When%20included%20in%20the%20package%20this%20would%20allow%20for%20a%20config.json%20definition%20of%20Application-specific%20scope%20environment%20variables.%26nbsp%3B%20The%20configuration%20would%20consist%20of%20an%20array%20of%20tuple%20values%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3Ename%3C%2FLI%3E%0A%3CLI%3Evalue%3C%2FLI%3E%0A%3CLI%3Euseregistry%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EThe%20name%20parameter%20would%20be%20the%20name%20of%20the%20environment%20variable.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20value%20parameter%20would%20be%20the%20value%20specified%20directly%2C%20or%20an%20empty%20string%20if%20useregistry%20is%20specified%20to%20true.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20useregistry%20parameter%20is%20a%20boolean%2C%20and%20when%20set%20to%20true%20it%20indicates%20that%20the%20application%20hive%20registry%20should%20be%20queried%20for%20the%20value.%20In%20this%20case%2C%20the%20current%20user%20portion%20of%20the%20application%20hive%20is%20checked%20first%2C%20followed%20by%20the%20machine%20portion%20if%20necessary.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20fixup%20would%20apply%20to%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EGetEnvironmentVariable()%3C%2FLI%3E%0A%3CLI%3ESetEnvironmentVariable()%20-%20but%20only%20if%20useregistry%20is%20specified.%26nbsp%3B%20Writing%20would%20only%20be%20made%20to%20the%20current%20user%20portion%20of%20the%20application%20hive%20so%20that%20the%20write%20will%20succeed.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EConsideration%20for%26nbsp%3BExpandEnvironmentStrings()%20will%20also%20be%20given%2C%20but%20at%20this%20time%20it%20is%20probably%20out%20of%20scope%20for%20the%20project.%26nbsp%3B%20It%20should%20be%20able%20to%20be%20added%20at%20a%20later%20time%20without%20the%20need%20to%20change%20the%20Json%20configuration.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20am%20working%20on%20the%20prototype%20now%2C%20and%20encourage%20any%20feedback.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1599973%22%20slang%3D%22en-US%22%3ERe%3A%20Environment%20Variable%20Fixup%20Proposal%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1599973%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F146612%22%20target%3D%22_blank%22%3E%40TIMOTHY%20MANGAN%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20like%20that%20idea.%26nbsp%3B%20It%20is%20aweomse.%26nbsp%3B%20I%20thought%20MSIX%20had%20support%20for%20environment%20variables%20though%3F%26nbsp%3B%20Or%20is%20it%20only%20during%20install%20time%20can%20a%20package%20write%20environment%20variables%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1600147%22%20slang%3D%22en-US%22%3ERe%3A%20Environment%20Variable%20Fixup%20Proposal%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1600147%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F475788%22%20target%3D%22_blank%22%3E%40Darren_Hoehna%3C%2FA%3E%26nbsp%3BI%20am%20unaware%20of%20any%20support%20for%20environment%20variables%20in%20MSIX.%20(See%20post%20from%202%20years%20ago%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fmsix-deployment%2Fno-support-for-virtualized-environment-variables%2Fm-p%2F228140%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fmsix-deployment%2Fno-support-for-virtualized-environment-variables%2Fm-p%2F228140%3C%2FA%3E)%3C%2FP%3E%0A%3CP%3ECertainly%2C%20even%20today%20if%20I%20capture%20an%20installer%20that%20sets%20either%20user%20or%20system%20environment%20variables%20with%20the%20MSIX%20Packaging%20Tool%2C%20the%20packaged%20app%20doesn't%20see%20them.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20there%20is%20a%20Manifest%20setting%20needed%20and%20the%20packaging%20tool%20just%20doesn't%20use%20it%20(sort%20of%20like%20Fonts...)%2C%20please%20let%20me%20know.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1604373%22%20slang%3D%22en-US%22%3ERe%3A%20Environment%20Variable%20Fixup%20Proposal%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1604373%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F146612%22%20target%3D%22_blank%22%3E%40TIMOTHY%20MANGAN%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20was%20mistaken.%26nbsp%3B%20I%20thought%20MSIX%20had%20something%20to%20read%20and%20write%20environment%20variables.%26nbsp%3B%20I%20was%20wrong.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBefore%20you%20get%20too%20far%20with%20your%20prototype%20I%20was%20wondering%20if%2C%20instead%20of%20specifying%20what%20Registry%20keys%20the%20user%20needs%20to%20put%20in%20the%20config%2C%20why%20not%20redirect%20all%20registry%20reads%20and%20writes%3F%26nbsp%3B%20That%20way%20the%20user%20can%20use%20this%20fixup%20in%20a%20plug-and-play%20style%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECheers%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EDarren.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1604976%22%20slang%3D%22en-US%22%3ERe%3A%20Environment%20Variable%20Fixup%20Proposal%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1604976%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F475788%22%20target%3D%22_blank%22%3E%40Darren_Hoehna%3C%2FA%3E%26nbsp%3BI'm%20unfamiliar%20with%20the%20%22plug-and-play%22%20scenario%2C%20so%20I'm%20not%20sure%20what%20you%20mean.%26nbsp%3B%20Perhaps%20you%20can%20clarify%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHowever%2C%20I'll%20explain%20what%20I%20have%20so%20far.%26nbsp%3B%20For%20a%20developer%20building%20a%20package%2C%20they%20don't%20need%20the%20registry%20and%20can%20specify%20the%20variables%20using%20the%20fixup%20with%20definitions%20in%20the%20Json.%26nbsp%3B%20The%20registry%20option%20might%20be%20useful%20for%20developers%20if%20they%20want%20the%20app%20to%20be%20able%20to%20modify%20the%20value%20at%20runtime.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20registry%20aspect%20is%20primarily%20intended%20as%20an%20option%20for%20repackaged%20apps%20that%20captured%20an%20installed%20environment%20variable%2C%20using%20the%20registry%20based%20data.%26nbsp%3B%20This%20option%20is%20preferred%2C%20because%20while%20the%20Json%20file%20is%20immutable%2C%20if%20we%20pull%20from%20the%20registry%20the%20app%20at%20runtime%20may%20update%20the%20setting%2C%20allowing%20for%20normal%20MSIX%20redirection%20to%20save%20off%20the%20registry%20changes%20(at%20least%20for%20User%20based%20environment%20variables%20stored%20under%20HKCU).%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1618396%22%20slang%3D%22en-US%22%3ERE%3A%20Environment%20Variable%20Fixup%20Proposal%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1618396%22%20slang%3D%22en-US%22%3EThe%20EnvVarFixup%20that%20was%20described%20here%20is%20now%20up%20in%20the%20Microsoft%20GitHub%20sources%20in%20the%20Develop%20branch.%20For%20those%20interested%2C%20PsfTooling%204.2%20is%20now%20up%20in%20the%20Microsoft%20Store%20and%20includes%20support%20for%20this%20new%20type%20of%20intercept-based%20fixup.%3C%2FLINGO-BODY%3E
MVP

Some traditional applications make use of environment variables. 

  • These are typically added as part of the installation/customization.
  • They may be declared as System or User in scope.
  • Ultimately, they are stored in the registry but added to the process' environment block inside the kernel.

While Microsoft App-V had support for the package declaring environment variables in the manifest, MSIX does not.  The App-V support turned what may have originally been an environment variable of System or User scope into one with an Application-specific scope.

 

I am proposing the creation of a new PSF Fixup called EnvVarFixup.  When included in the package this would allow for a config.json definition of Application-specific scope environment variables.  The configuration would consist of an array of tuple values:

  • name
  • value
  • useregistry

The name parameter would be the name of the environment variable.

 

The value parameter would be the value specified directly, or an empty string if useregistry is specified to true.

 

The useregistry parameter is a boolean, and when set to true it indicates that the application hive registry should be queried for the value. In this case, the current user portion of the application hive is checked first, followed by the machine portion if necessary.

 

The fixup would apply to:

  • GetEnvironmentVariable()
  • SetEnvironmentVariable() - but only if useregistry is specified.  Writing would only be made to the current user portion of the application hive so that the write will succeed.

 

Consideration for ExpandEnvironmentStrings() will also be given, but at this time it is probably out of scope for the project.  It should be able to be added at a later time without the need to change the Json configuration.

 

I am working on the prototype now, and encourage any feedback.

5 Replies

@TIMOTHY MANGAN 

 

I like that idea.  It is aweomse.  I thought MSIX had support for environment variables though?  Or is it only during install time can a package write environment variables?

@Darren_Hoehna I am unaware of any support for environment variables in MSIX. (See post from 2 years ago https://techcommunity.microsoft.com/t5/msix-deployment/no-support-for-virtualized-environment-variab...)

Certainly, even today if I capture an installer that sets either user or system environment variables with the MSIX Packaging Tool, the packaged app doesn't see them. 

 

If there is a Manifest setting needed and the packaging tool just doesn't use it (sort of like Fonts...), please let me know.

@TIMOTHY MANGAN 

 

I was mistaken.  I thought MSIX had something to read and write environment variables.  I was wrong.

 

Before you get too far with your prototype I was wondering if, instead of specifying what Registry keys the user needs to put in the config, why not redirect all registry reads and writes?  That way the user can use this fixup in a plug-and-play style?

 

Cheers,

 

Darren.

@Darren_Hoehna I'm unfamiliar with the "plug-and-play" scenario, so I'm not sure what you mean.  Perhaps you can clarify?

 

However, I'll explain what I have so far.  For a developer building a package, they don't need the registry and can specify the variables using the fixup with definitions in the Json.  The registry option might be useful for developers if they want the app to be able to modify the value at runtime.

 

The registry aspect is primarily intended as an option for repackaged apps that captured an installed environment variable, using the registry based data.  This option is preferred, because while the Json file is immutable, if we pull from the registry the app at runtime may update the setting, allowing for normal MSIX redirection to save off the registry changes (at least for User based environment variables stored under HKCU).

The EnvVarFixup that was described here is now up in the Microsoft GitHub sources in the Develop branch. For those interested, PsfTooling 4.2 is now up in the Microsoft Store and includes support for this new type of intercept-based fixup.