tv alacritty: add support for multiple profiles

This commit is contained in:
tv 2023-01-11 17:14:19 +01:00
parent ab8139704a
commit 48f91cbeb1

View File

@ -6,7 +6,7 @@ let
program = "${pkgs.font-size-alacritty}/bin/font-size-alacritty"; program = "${pkgs.font-size-alacritty}/bin/font-size-alacritty";
args = [arg]; args = [arg];
}; };
config = { configs.default = {
bell.animation = "EaseOut"; bell.animation = "EaseOut";
bell.duration = 50; bell.duration = 50;
bell.color = "#ff00ff"; bell.color = "#ff00ff";
@ -50,16 +50,31 @@ let
]; ];
scrolling.multiplier = 8; scrolling.multiplier = 8;
}; };
config-file = pkgs.writeJSON "alacritty-tv.json" config; writeProfile = name: config: let
profile = pkgs.writeText "alacritty-tv.profile" /* sh */ '' config-file =
assert lib.types.filename.check name;
pkgs.writeJSON "alacritty-tv-${name}.json" config;
in pkgs.writeText "alacritty-tv-${name}.profile" /* sh */ ''
# Use home so Alacritty can find the configuration without arguments. # Use home so Alacritty can find the configuration without arguments.
# HOME will be reset once in Alacritty. # HOME will be reset once in Alacritty.
HOME=$TMPDIR/Alacritty HOME=$TMPDIR/Alacritty-${name}
export HOME export HOME
# Tell Alacritty via XDG_RUNTIME_DIR where to create sockets.
# XDG_RUNTIME_DIR needs to be reset manually.
export ALACRITTY_XDG_RUNTIME_DIR="$XDG_RUNTIME_DIR"
export BASH_EXTRA_INIT=${pkgs.writeDash "alacritty-tv.cleanup.sh" ''
XDG_RUNTIME_DIR=$ALACRITTY_XDG_RUNTIME_DIR
unset ALACRITTY_XDG_RUNTIME_DIR
unset BASH_EXTRA_INIT
''}
export XDG_RUNTIME_DIR="$HOME"
# Install stored configuration if it has changed. # Install stored configuration if it has changed.
# This allows for both declarative updates and runtime modifications. # This allows for both declarative updates and runtime modifications.
${pkgs.coreutils}/bin/mkdir -p "$HOME" # rust-xdg requires XDG_RUNTIME_DIR to be secure:
# https://docs.rs/xdg/2.4.1/src/xdg/lib.rs.html#311
${pkgs.coreutils}/bin/mkdir -m 0700 -p "$HOME"
if test "$(${pkgs.coreutils}/bin/cat "$HOME"/ref)" != ${config-file}; then if test "$(${pkgs.coreutils}/bin/cat "$HOME"/ref)" != ${config-file}; then
echo ${config-file} > "$HOME"/ref echo ${config-file} > "$HOME"/ref
${pkgs.coreutils}/bin/cp ${config-file} "$HOME"/.alacritty.yml ${pkgs.coreutils}/bin/cp ${config-file} "$HOME"/.alacritty.yml
@ -72,20 +87,35 @@ pkgs.symlinkJoin {
paths = [ paths = [
(pkgs.writeDashBin "alacritty" '' (pkgs.writeDashBin "alacritty" ''
# usage: # usage:
# alacritty [--singleton] [ARGS...] # alacritty [--profile=PROFILE] [--singleton] [ARGS...]
# where
# PROFILE one of ${lib.toJSON (lib.attrNames configs)}
set -efu set -efu
case ''${1-} in
${lib.concatMapStringsSep "\n" (name: /* sh */ ''
--${lib.shell.escape name}|--profile=${lib.shell.escape name})
shift
profile=${writeProfile name configs.${name}}
;;
'') (lib.attrNames configs)}
*)
profile=${writeProfile "default" configs.default}
;;
esac
case ''${1-} in case ''${1-} in
--singleton) --singleton)
shift shift
if ! ${pkgs.alacritty}/bin/alacritty msg create-window "$@"; then if ! ${pkgs.alacritty}/bin/alacritty msg create-window "$@"; then
. ${profile} . "$profile"
${pkgs.alacritty}/bin/alacritty "$@" & ${pkgs.alacritty}/bin/alacritty "$@" &
fi fi
;; ;;
*) *)
. ${profile} . "$profile"
exec ${pkgs.alacritty}/bin/alacritty "$@" exec ${pkgs.alacritty}/bin/alacritty "$@"
;; ;;
esac esac