Bug 490432 - ~/.config/plasma-workspace/env startup scripts cannot set an environment variable as empty
Summary: ~/.config/plasma-workspace/env startup scripts cannot set an environment vari...
Status: RESOLVED FIXED
Alias: None
Product: plasmashell
Classification: Plasma
Component: Startup process (other bugs)
Version First Reported In: 6.0.5
Platform: NixOS Linux
: NOR normal
Target Milestone: 1.0
Assignee: Plasma Bugs List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-07-18 03:58 UTC by hexclover
Modified: 2024-07-23 12:28 UTC (History)
2 users (show)

See Also:
Latest Commit:
Version Fixed/Implemented In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description hexclover 2024-07-18 03:58:43 UTC
SUMMARY
Plasma workspace allows the user to set environment variables in .sh files located in ~/.config/plasma-workspace/env. However, if one sets a variable to the empty string, it does not get exported into the plasma process.

STEPS TO REPRODUCE
Put these into ~/.config/plasma-workspace/env/foo.sh and give it +x permission:

```bash
export SHOULD_BE_ONE=1
export SHOULD_BE_NONE=
```

Then boot KDE.

OBSERVED RESULT
SHOULD_BE_ONE is 1 but SHOULD_BE_NONE is unset.

EXPECTED RESULT
SHOULD_BE_NONE should be set to the empty string (should appear in the output of `env`).

SOFTWARE/OS VERSIONS
Operating System: NixOS 24.05
KDE Plasma Version: 6.0.5
KDE Frameworks Version: 6.2.0
Qt Version: 6.7.2
Graphics Platform: Wayland

ADDITIONAL INFORMATION

To fix the problem, arguably we should fix this function in `startplasma.cpp`:

```cpp
void setEnvironmentVariable(const char *name, QByteArrayView value)
{
    if (qgetenv(name) != value) {
        qputenv(name, value);
    }
}
```

`qgetenv` is documented to return the an empty byte array if the variable is unset, so `gputenv` is not called.

This behavior is not always problematic, but consider the following situation. Suppose some env var `X` is set in the DBus environment already, but the user would like to use the value they specified. `startplasma-wayland.cpp` does three things in order:

1. Source `env/*.sh` and import the env vars by calling `runEnvironmentScripts()`
2. Sync environment to DBus by calling `syncDBusEnvironment()`
3. Import systemd env vars by calling `importSystemdEnvrionment()`

If in the .sh file `X` is set, it should be imported into the plasma process in step 1, then exported to DBus in step 2, so step 3 just re-import that exported value, which is set by the user. However, if the user sets `X` to be empty in the script, then it won't get pass step 1, so the value from systemd will be used after step 3.
Comment 1 Bug Janitor Service 2024-07-18 13:04:33 UTC
A possibly relevant merge request was started @ https://invent.kde.org/plasma/plasma-workspace/-/merge_requests/4537
Comment 2 Kai Uwe Broulik 2024-07-23 12:14:03 UTC
Git commit f469e2f22d8137e578fe5a7293287e64eb1dce94 by Kai Uwe Broulik.
Committed on 23/07/2024 at 11:03.
Pushed by broulik into branch 'master'.

startplasma: Also set environment variable when it is not currently set

qgetenv returns a null QByteArray when there is currently no such variable.
A null QByteArray is distinct from an empty one. However, operator!= will
treat them the same. Therefore it's impossible to set an environment variable
to an empty value.

Explicitly check for null and also qputenv it when this is the case.

M  +2    -1    startkde/startplasma.cpp

https://invent.kde.org/plasma/plasma-workspace/-/commit/f469e2f22d8137e578fe5a7293287e64eb1dce94
Comment 3 Kai Uwe Broulik 2024-07-23 12:28:11 UTC
Git commit a413e7d985bcd0b54dea247556aa21328ffcd43b by Kai Uwe Broulik.
Committed on 23/07/2024 at 12:14.
Pushed by broulik into branch 'Plasma/6.1'.

startplasma: Also set environment variable when it is not currently set

qgetenv returns a null QByteArray when there is currently no such variable.
A null QByteArray is distinct from an empty one. However, operator!= will
treat them the same. Therefore it's impossible to set an environment variable
to an empty value.

Explicitly check for null and also qputenv it when this is the case.


(cherry picked from commit f469e2f22d8137e578fe5a7293287e64eb1dce94)

Co-authored-by: Kai Uwe Broulik <kde@privat.broulik.de>

M  +2    -1    startkde/startplasma.cpp

https://invent.kde.org/plasma/plasma-workspace/-/commit/a413e7d985bcd0b54dea247556aa21328ffcd43b