flatpak-context: Implement --env-expanded for in-sandbox env expansion#6336
flatpak-context: Implement --env-expanded for in-sandbox env expansion#6336ryanabx wants to merge 1 commit into
--env-expanded for in-sandbox env expansion#6336Conversation
|
Some issues were raised in previous attempt that may need to be addressed here as well, see #2555 (comment) and below. |
smcv
left a comment
There was a problem hiding this comment.
What @Erick555 said. Before anyone spends time on reviewing the implementation, I think we'll need a summary of the design that explains how it addresses the concerns from previous attempts at this, and I would not want to merge this without approval from @alexlarsson.
|
How would the compatibility work with older version of a flatpak? If a package maintainer put this in finish-args and the user use a flatpak version that doesn't understand it. Given how long some distro version that never update flatpak (because they are hostile to it) are being used, this is likely to be unusable. |
That's another valid question. Conversely, if a pre-existing app has a literal |
The fact that this specific case doesn't work is good. Because it's not supposed to work. binaries in $HOME are not supposed to work inside the sandbox. |
I do have an answer for that, which is the same as every other new sandboxing parameter: if this feature was added, then the app maintainer should either not use it until the necessary Flatpak version is ubiquitous (a timescale of years), or set their app's minimum version to a Flatpak that has this feature and accept that it will not be installable on oldLTS distros. |
Which mean not on flathub. This totally defeat the whole purpose. |
Ah, I didn't realize there was a previous implementation, and I probably should have checked to see if it had been attempted before 😅 From Alex in the other PR:
And from @smcv:
I agree that if we want to move forward with this, maybe we do want to separate this into a new flag I would also have to have a set order, alphabetical seems fine to me, as long as we document it. Handling escaped $ might be as simple as evaluating
We start to get more in the weeds with stuff like this, but I see now why this feature doesn't exist yet in flatpak |
Actually, I envisioned this to be more useful for local overrides that a user might set, I hadn't even considered it being set in a flatpak manifest, but it makes sense that developers might want to do that. |
c40ad15 to
696da9f
Compare
d98a447 to
12a0a57
Compare
|
Should be ready for an initial review now. Decided to go with a new flag I'll address what I ended up going with regarding #2555 (comment) :
I have not made any escapes for
I don't have any special would evaluate to which I don't actually know what that will do 🤷
This one was pretty easy. I just made expansion happen before applying all the expansions in envp. We also avoid the idea of recursive expansion, simplifying it even more. Let me know your thoughts! |
--env values--env-expanded for in-sandbox env expansion
af0a128 to
ad4ab25
Compare
This is how it is specified in POSIX:
I tested Bash and it seems conformant: $ cat greet
#!/usr/bin/env bash
echo hello
$ greet
bash: greet: command not found
$ PATH=":$PATH"
$ greet
helloI'm not sure how it works for other |
ad4ab25 to
bce9fc0
Compare
bce9fc0 to
28c73a1
Compare
28c73a1 to
67e8be8
Compare
67e8be8 to
087cf0f
Compare
|
If anyone is interested in this patch, some testing would be nice. From what I recall, this worked on my VM test setup, but edge cases could be different. 🤷 |
Resolves: flatpak#1415 Resolves: flatpak#6333 When using `--env-expanded=KEY=$OTHER_KEY`, `$OTHER_KEY` will be evaluated as the incoming `$OTHER_KEY` value into the bwrap environment. This is useful for adding locations to the PATH, among other things. This environment expansion is subject to some limitations. 1. The environment expansion only expands existing environment variables. For example, `flatpak run --env-expanded=A=VALUE --env-expanded=B=$A` will result in A being `VALUE` and B being an empty string. This is to avoid the unnecessary complexity of recursing environment variables and the potential nondeterminism of resolving environment variables in differing orders on each execution. If you need to expand B to equal A, you can easily perform the expansion yourself prior to running the `flatpak run` command. 2. For backwards compatibility with existing overrides, command invocations, and build args, only environment variables in `--env-expanded` will be expanded. 3. `--env-expanded` environment variables are applied after `--env`, to allow modifying things like `XDG_DATA_DIRS` for example. 4. You may escape environment variable expansion in `--env-expanded` by using a leading backslash before your environment expansion. i.e. `--env-expanded=A=\$VALUE` will set A to the literal `$VALUE` Note: In most shell environments, you will already have to escape with a leading backslash when entering in the values as arguments or else you will expand the variable on the host.
087cf0f to
5c0623e
Compare

Resolves: #1415
Resolves: #6333
When using
--env-expanded=KEY=$OTHER_KEY,$OTHER_KEYwill beevaluated as the incoming
$OTHER_KEYvalue into the bwrap environment.This is useful for adding locations to the PATH, among other things.
This environment expansion is subject to some limitations.
variables. For example,
flatpak run --env-expanded=A=VALUE --env-expanded=B=$Awill result in A being
VALUEand B being an empty string. This is toavoid the unnecessary complexity of recursing environment variables and
the potential nondeterminism of resolving environment variables in
differing orders on each execution. If you need to expand B to equal
A, you can easily perform the expansion yourself prior to running the
flatpak runcommand.For backwards compatibility with existing overrides, command
invocations, and build args, only environment variables in
--env-expandedwill be expanded.--env-expandedenvironment variables are applied after--env, toallow modifying things like
XDG_DATA_DIRSfor example.You may escape environment variable expansion in
--env-expandedbyusing a leading backslash before your environment expansion.
i.e.
--env-expanded=A=\$VALUEwill set A to the literal$VALUENote: In most shell environments, you will already have to escape with a
leading backslash when entering in the values as arguments or else you
will expand the variable on the host.
I haven't done a lot of C and GLib programming before, so let me know if this is a sane implementation. It makes sense to me though. If the function needs to be moved above or below other functions in
flatpak-bwrap, let me know.Also, currently if an environment variable is not found in the bwrap environment, it will behave like a shell would, and just evaluate to nothing. Let me know if we want to change that.
I also modified the test-exports and test-override tests to have some coverage for --env-expanded.