wezterm: support portable AppImage invocation

https://github.com/AppImage/AppImageKit/issues/368 introduced a way
to maintain configuration alongside an AppImage file stored on a
flash drive:

```
wez@cube-localdomain  /tmp/portable-wezterm
9:06 130 $ ls -al
total 8720
drwxrwxr-x.  4 wez  wez      100 May  6 08:54 ./
drwxrwxrwt. 19 root root     580 May  6 09:05 ../
-rwxr-xr-x.  1 wez  wez  8929256 May  6 08:59 WezTerm-20200505-090057-31c6155f-Fedora31.AppImage*
drwxrwxr-x.  3 wez  wez       60 May  6 08:15 WezTerm-20200505-090057-31c6155f-Fedora31.AppImage.config/
drwxrwxr-x.  2 wez  wez       40 May  6 08:54 WezTerm-20200505-090057-31c6155f-Fedora31.AppImage.home/
```

When launched and these `<IMAGE>.config` and/or `<IMAGE>.home` dirs
exist, the appimage machinery will redirect the HOME and XDG_CONFIG_DIR
env vars to point to those locations instead of the normal place.

We weren't respecting XDG_CONFIG_DIR (fixed in this commit), but we also
need to take care: we don't want to re-export those locations to
children of wezterm so we need to load those values and then clean
up the environment.
This commit is contained in:
Wez Furlong committed 2020-05-06 09:11:13 -07:00
1 parent 31c6155f2b
commit c53ab60940
3 files changed
+61 -21

No files matched your search

+13 -8
View File
@@ -44,10 +44,18 @@ pub use unix::*;
lazy_static! {
pub static ref HOME_DIR: PathBuf = dirs::home_dir().expect("can't find HOME dir");
pub static ref CONFIG_DIR: PathBuf = xdg_config_home();
static ref RUNTIME_DIR: PathBuf = compute_runtime_dir().unwrap();
static ref CONFIG: Configuration = Configuration::new();
}
fn xdg_config_home() -> PathBuf {
match std::env::var_os("XDG_CONFIG_HOME").map(|s| PathBuf::from(s).join("wezterm")) {
Some(p) => p,
None => HOME_DIR.join(".config").join("wezterm"),
}
}
/// Discard the current configuration and replace it with
/// the default configuration
#[allow(dead_code)]
@@ -556,12 +564,10 @@ impl Config {
// specific config directories, but only returns one of them, not
// multiple. In addition, it spawns a lot of subprocesses,
// so we do this bit "by-hand"
let mut paths = vec![
HOME_DIR.join(".config").join("wezterm").join("wezterm.lua"),
HOME_DIR
.join(".config")
.join("wezterm")
.join("wezterm.toml"),
CONFIG_DIR.join("wezterm.lua"),
CONFIG_DIR.join("wezterm.toml"),
HOME_DIR.join(".wezterm.lua"),
HOME_DIR.join(".wezterm.toml"),
];
@@ -710,7 +716,7 @@ impl Config {
fn compute_color_scheme_dirs(&self) -> Vec<PathBuf> {
let mut paths = self.color_scheme_dirs.clone();
paths.push(HOME_DIR.join(".config").join("wezterm").join("colors"));
paths.push(CONFIG_DIR.join("colors"));
if let Ok(exe_name) = std::env::current_exe() {
// If running out of the source tree our executable path will be
@@ -922,8 +928,7 @@ fn compute_runtime_dir() -> Result<PathBuf, Error> {
return Ok(runtime.join("wezterm"));
}
let home = dirs::home_dir().ok_or_else(|| anyhow!("can't find home dir"))?;
Ok(home.join(".local/share/wezterm"))
Ok(HOME_DIR.join(".local/share/wezterm"))
}
pub fn pki_dir() -> anyhow::Result<PathBuf> {
+47 -9
View File
@@ -11,7 +11,7 @@ use std::fs::DirBuilder;
use std::io::{Read, Write};
#[cfg(unix)]
use std::os::unix::fs::DirBuilderExt;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::str::FromStr;
use std::sync::Arc;
@@ -70,7 +70,7 @@ fn wezterm_version() -> &'static str {
version = wezterm_version()
)]
struct Opt {
/// Skip loading ~/.wezterm.toml
/// Skip loading wezterm.lua
#[structopt(short = "n")]
skip_config: bool,
@@ -557,11 +557,10 @@ fn run_terminal_gui(config: config::ConfigHandle, opts: StartCommand) -> anyhow:
if opts.daemonize {
let stdout = config.daemon_options.open_stdout()?;
let stderr = config.daemon_options.open_stderr()?;
let home_dir = dirs::home_dir().ok_or_else(|| anyhow!("can't find home dir"))?;
let mut daemonize = daemonize::Daemonize::new()
.stdout(stdout)
.stderr(stderr)
.working_directory(home_dir.clone());
.working_directory(config::HOME_DIR.clone());
if !running_under_wsl() {
// pid file locking is only partly function when running under
@@ -581,7 +580,7 @@ fn run_terminal_gui(config: config::ConfigHandle, opts: StartCommand) -> anyhow:
bail!("{} {}", err, config.daemon_options.pid_file().display());
}
DaemonizeError::ChangeDirectory => {
bail!("{} {}", err, home_dir.display());
bail!("{} {}", err, config::HOME_DIR.display());
}
_ => return Err(err.into()),
}
@@ -727,7 +726,9 @@ fn run() -> anyhow::Result<()> {
std::env::set_var("WEZTERM_EXECUTABLE", exe);
}
if let Some(_appimage) = std::env::var_os("APPIMAGE") {
if let Some(appimage) = std::env::var_os("APPIMAGE") {
let appimage = std::path::PathBuf::from(appimage);
// We were started via an AppImage, presumably ourselves.
// AppImage exports ARGV0 into the environment and that causes
// everything that was indirectly spawned by us to appear to
@@ -735,6 +736,45 @@ fn run() -> anyhow::Result<()> {
// `WezTerm.AppImage foo`, which is super confusing for everyone!
// Let's just unset that from the environment!
std::env::remove_var("ARGV0");
// This AppImage feature allows redirecting HOME and XDG_CONFIG_HOME
// to live alongside the executable for portable use:
// https://github.com/AppImage/AppImageKit/issues/368
// When we spawn children, we don't want them to inherit this,
// but we do want to respect them for config loading.
// Let's force resolution and cleanup our environment now.
/// Given "/some/path.AppImage" produce "/some/path.AppImageSUFFIX".
/// We only support this for "path.AppImage" that can be converted
/// to UTF-8. Otherwise, we return "/some/path.AppImage" unmodified.
fn append_extra_file_name_suffix(p: &Path, suffix: &str) -> PathBuf {
if let Some(name) = p.file_name().and_then(|o| o.to_str()) {
p.with_file_name(format!("{}{}", name, suffix))
} else {
p.to_path_buf()
}
}
/// Our config stuff exports these env vars to help portable apps locate
/// the correct environment when it is launched via wezterm.
/// However, if we are using the system wezterm to spawn a portable
/// AppImage then we want these to not take effect.
fn clean_wezterm_config_env() {
std::env::remove_var("WEZTERM_CONFIG_FILE");
std::env::remove_var("WEZTERM_CONFIG_DIR");
}
if config::HOME_DIR.starts_with(append_extra_file_name_suffix(&appimage, ".home")) {
// Fixup HOME to point to the user's actual home dir
std::env::remove_var("HOME");
std::env::set_var("HOME", dirs::home_dir().expect("can't resolve HOME dir"));
clean_wezterm_config_env();
}
if config::CONFIG_DIR.starts_with(append_extra_file_name_suffix(&appimage, ".config")) {
std::env::remove_var("XDG_CONFIG_HOME");
clean_wezterm_config_env();
}
}
// This is a bit gross.
@@ -764,9 +804,7 @@ fn run() -> anyhow::Result<()> {
SetStdHandle(STD_ERROR_HANDLE, stderr.into_raw_file_descriptor());
*/
std::env::set_current_dir(
dirs::home_dir().ok_or_else(|| anyhow!("can't find home dir"))?,
)?;
std::env::set_current_dir(config::HOME_DIR)?;
}
};
pretty_env_logger::init_timed();
+1 -4
View File
@@ -79,10 +79,7 @@ pub fn make_lua_context(config_dir: &Path) -> anyhow::Result<Lua> {
}
prefix_path(&mut path_array, &crate::config::HOME_DIR.join(".wezterm"));
prefix_path(
&mut path_array,
&crate::config::HOME_DIR.join(".config").join("wezterm"),
);
prefix_path(&mut path_array, &crate::config::CONFIG_DIR);
if let Ok(exe) = std::env::current_exe() {
if let Some(path) = exe.parent() {
wezterm_mod.set(