Create a gist now

Instantly share code, notes, and snippets.

@timbertson /gnome-shell.nix Secret
Last active Jan 30, 2016

What would you like to do?
Run nixpkgs' gnome shell inside a `xephyr` window
# NOTE: unfortunately `nixpkgs` xephyr doesn't nest properly in my testing.
# So you need to have a system Xephyr.
#
# This script will create a ./scratch directory in the current directory,
# where it'll store temporary files and a makeshift $HOME dir.
#
# To build the script:
# nix-build --out-link gnome-shell.sh gnome-shell.nix
#
# Then run it with:
# ./gnome-shell.sh
{pkgs ? import <nixpkgs> {}, display ? ":1", extraEnv ? "", xephyr ? "/usr/bin/Xephyr" }:
with pkgs;
let dbus-services = with gnome3; [
dconf
gnome_settings_daemon
]; in
writeScript "gnome-shell-wrapper" ''#!${bash}/bin/bash
set -eux
here="$(pwd)"
scratch="$here/scratch"
echo "Making scratch dir in $scratch ..."
mkdir -p "$scratch/home"
me="$(id -un)"
function kill_pid () {
pid="$1"
desc="$2"
if [ -n "$pid" ]; then
echo "Killing $desc ($pid)" >&2
# wait "$pid"
# set -x
kill -INT "$pid" || true
while kill -0 "$pid" 2>/dev/null; do
sleep 0.2
done
fi
}
dbus_pid=""
xephyr_pid=""
function cleanup () {
kill_pid "$dbus_pid" DBus
kill_pid "$xephyr_pid" Xephyr
}
trap cleanup EXIT
exec 3> "$scratch/dbus-pid"
exec 4> "$scratch/dbus-address"
exec 5> "$scratch/xephyr-display"
env XDG_DATA_DIRS="${
lib.concatStringsSep ":" (map (x: "${x}/share") dbus-services)
}''${XDG_DATA_DIRS:+:$XDG_DATA_DIRS}" \
HOME="$here/scratch/home" \
${dbus_daemon}/bin/dbus-daemon --session --fork \
--print-pid=3 --print-address=4
dbus_pid="$(cat "$scratch/dbus-pid")"
export DBUS_SESSION_BUS_ADDRESS="$(cat "$scratch/dbus-address")"
${xephyr} -ac -displayfd 5 -terminate "${display}" &
xephyr_pid="$!"
exec 5>&-
while [ -z "$(cat scratch/xephyr-display)" ]; do
sleep 1
echo "awaiting xephyr..."
done
exec 3>&-
exec 4>&-
exec 5>&-
# use sudo just to clear out envvars
sudo -u "$me" \
env \
LIBGL_DRIVERS_PATH="${mesa_drivers}/lib/dri:${mesa_noglu}/lib" \
DBUS_SESSION_BUS_ADDRESS="$(cat "$scratch/dbus-address")" \
LD_LIBRARY_PATH="''${LD_LIBRARY_PATH:-}:${mesa_noglu}/lib" \
HOME="$here/scratch/home" \
DISPLAY="${display}" \
XDG_DATA_DIRS="$scratch/home/.local/share:$HOME/.local/share:/usr/local/share:/usr/share" \
GIO_EXTRA_MODULES="${gnome3.dconf}/lib/gio/modules" \
PATH="${gnome3.gnome_shell}/bin:${gnome3.gnome-tweak-tool}/bin:$PATH" \
${extraEnv} \
"${gnome3.gnome_shell}/bin/gnome-shell" \
"$@"
''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment