flatpak: Add --print-updated-env option to print environment

This is the environment needed to use flatpaks; a following commit will
hook this up to the systemd environment generator.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
This commit is contained in:
Philip Withnall
2020-11-15 15:54:55 +00:00
committed by Alexander Larsson
parent ba612b243e
commit 9411fe2bca
4 changed files with 92 additions and 1 deletions

View File

@@ -50,6 +50,8 @@ static gboolean opt_default_arch;
static gboolean opt_supported_arches;
static gboolean opt_gl_drivers;
static gboolean opt_list_installations;
static gboolean opt_print_updated_env;
static gboolean opt_print_system_only;
static gboolean opt_user;
static gboolean opt_system;
static char **opt_installations;
@@ -168,6 +170,8 @@ static GOptionEntry empty_entries[] = {
{ "supported-arches", 0, 0, G_OPTION_ARG_NONE, &opt_supported_arches, N_("Print supported arches and exit"), NULL },
{ "gl-drivers", 0, 0, G_OPTION_ARG_NONE, &opt_gl_drivers, N_("Print active gl drivers and exit"), NULL },
{ "installations", 0, 0, G_OPTION_ARG_NONE, &opt_list_installations, N_("Print paths for system installations and exit"), NULL },
{ "print-updated-env", 0, 0, G_OPTION_ARG_NONE, &opt_print_updated_env, N_("Print the updated environment needed to run flatpaks"), NULL },
{ "print-system-only", 0, 0, G_OPTION_ARG_NONE, &opt_print_system_only, N_("Only include the system installation with --print-updated-env"), NULL },
{ NULL }
};
@@ -733,6 +737,59 @@ flatpak_run (int argc,
exit (EXIT_SUCCESS);
}
}
/* systemd environment generator for system and user installations.
* Intended to be used only from the scripts in env.d/.
* See `man systemd.environment-generator`. */
if (opt_print_updated_env)
{
g_autoptr(GPtrArray) installations = g_ptr_array_new_with_free_func (g_free); /* (element-type GFile) */
GPtrArray *system_installation_locations; /* (element-type GFile) */
const gchar * const *xdg_data_dirs = g_get_system_data_dirs ();
g_autoptr(GPtrArray) new_dirs = g_ptr_array_new_with_free_func (g_free); /* (element-type filename) */
g_autofree gchar *new_dirs_joined = NULL;
/* Work out the set of installations we want in the environment. */
if (!opt_print_system_only)
{
g_autoptr(GFile) home_installation_location = flatpak_get_user_base_dir_location ();
g_ptr_array_add (installations, g_file_get_path (home_installation_location));
}
system_installation_locations = flatpak_get_system_base_dir_locations (NULL, &local_error);
if (local_error != NULL)
{
g_printerr ("%s\n", local_error->message);
exit (1);
}
for (gsize i = 0; i < system_installation_locations->len; i++)
g_ptr_array_add (installations, g_file_get_path (system_installation_locations->pdata[i]));
/* Get the export path for each installation, and filter out
* ones which are already listed in @xdg_data_dirs. */
for (gsize i = 0; i < installations->len; i++)
{
g_autofree gchar *share_path = g_build_filename (installations->pdata[i], "exports", "share", NULL);
g_autofree gchar *share_path_with_slash = g_strconcat (share_path, "/", NULL);
if (g_strv_contains (xdg_data_dirs, share_path) || g_strv_contains (xdg_data_dirs, share_path_with_slash))
continue;
g_ptr_array_add (new_dirs, g_steal_pointer (&share_path));
}
/* Add the rest of the existing @xdg_data_dirs to the new list. */
for (gsize i = 0; xdg_data_dirs[i] != NULL; i++)
g_ptr_array_add (new_dirs, g_strdup (xdg_data_dirs[i]));
g_ptr_array_add (new_dirs, NULL);
/* Print in a format suitable for a system environment generator. */
new_dirs_joined = g_strjoinv (":", (gchar **) new_dirs->pdata);
g_print ("XDG_DATA_DIRS=%s\n", new_dirs_joined);
exit (EXIT_SUCCESS);
}
}
if (local_error)

View File

@@ -178,6 +178,28 @@
</para></listitem>
</varlistentry>
<varlistentry>
<term><option>--print-system-only</option></term>
<listitem><para>
When the <command>flatpak --print-updated-env</command>
command is run, only print the environment for system
flatpak installations, not including the users home
installation.
</para></listitem>
</varlistentry>
<varlistentry>
<term><option>--print-updated-env</option></term>
<listitem><para>
Print the set of environment variables needed to use
flatpaks, amending the current set of environment variables.
This is intended to be used in a systemd environment
generator, and should not need to be run manually.
</para></listitem>
</varlistentry>
</variablelist>
</refsect1>

View File

@@ -24,7 +24,7 @@ set -euo pipefail
# This test looks for specific localized strings.
export LC_ALL=C
echo "1..10"
echo "1..11"
${FLATPAK} --version > version_out
@@ -47,6 +47,16 @@ assert_streq `head -1 arches` `cat arch`
ok "default arch"
${FLATPAK} --print-updated-env > updated_env
${FLATPAK} --print-updated-env --print-system-only > updated_env_system
assert_file_has_content updated_env "exports/share"
assert_file_has_content updated_env "^XDG_DATA_DIRS="
assert_file_has_content updated_env_system "exports/share"
assert_file_has_content updated_env_system "^XDG_DATA_DIRS="
ok "print updated env"
${FLATPAK} --gl-drivers > drivers
assert_file_has_content drivers "^default$";

View File

@@ -69,6 +69,8 @@ ${FLATPAK} complete "flatpak --" 10 "--" | sort > complete_out
--installation=
--installations
--ostree-verbose
--print-system-only
--print-updated-env
--supported-arches
--system
--user