diff options
Diffstat (limited to 'pkgs/simple/su2.nix')
| -rw-r--r-- | pkgs/simple/su2.nix | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/pkgs/simple/su2.nix b/pkgs/simple/su2.nix new file mode 100644 index 00000000..a9a92b10 --- /dev/null +++ b/pkgs/simple/su2.nix @@ -0,0 +1,130 @@ +{ pkgs }: + +pkgs.writeDashBin "su2" '' + # usage: su2 [xauth=auto|disable|trusted|untrusted] user=USER [-- COMMAND [ARGS...]] + + set -efu + + xauth=''${xauth-auto} + while test $# -gt 0; do + case $1 in + xauth=auto|xauth=disable|xauth=trusted|xauth=untrusted) + xauth=''${1#xauth=} + shift + ;; + user=*) + user=''${1#user=} + shift + ;; + --) + shift + break + ;; + *) + echo "$0: error: bad argument $1" >&2 + exit 1 + esac + done + + if test "$xauth" = auto; then + xauth=disable + if test "$user" != root && test -z "''${SSH_CLIENT-}"; then + case ''${DISPLAY-} in + :8) + xauth=untrusted + ;; + :11) + xauth=trusted + ;; + esac + fi + fi + + case $user in + root) + case $xauth in + trusted) + ${pkgs.xauth-user}/bin/xauth-user "$user" --trusted + ;; + untrusted) + ${pkgs.xauth-user}/bin/xauth-user "$user" + ;; + disable) + : + ;; + *) + echo "$0: error: bad xauth value: $xauth" >&2 + exit 2 + esac + if test -n "''${SSH_CLIENT-}"; then + exec /run/wrappers/bin/su - -c "env SSH_CLIENT='$SSH_CLIENT' '$SHELL'" + else + case $TERM in alacritty) + alacritty msg config 'colors.primary.background="#230000"' + alacritty msg config 'colors.primary.foreground="#e0c0c0"' + alacritty msg config 'colors.normal.black="#800000"' + esac + exec /run/wrappers/bin/su - + fi + ;; + *) + INIT=$( + case $xauth in + trusted|untrusted) + XCOOKIE=$( + umask 0077 + tmpfile=$XDG_RUNTIME_DIR/su2.$$.xauth + trusted=$xauth + trap '${pkgs.coreutils}/bin/rm "$tmpfile"' EXIT + ${pkgs.coreutils}/bin/touch "$tmpfile" + ${pkgs.xauth}/bin/xauth -f "$tmpfile" generate "$DISPLAY" . "$trusted" timeout 86400 + ${pkgs.xauth}/bin/xauth -f "$tmpfile" extract - "$DISPLAY" | ${pkgs.coreutils}/bin/base64 + ) + export XCOOKIE + ;; + disable) + : + ;; + *) + echo "$0: error: bad xauth value: $xauth" >&2 + exit 2 + esac + export SU2PID=$$ + ${pkgs.jq}/bin/jq -nr ' + def to_shell_vars(x): + x | to_entries | map( + select(.value != null) | + "\(.key)=\(.value|@sh)" + ) | join(" "); + + env | [ + "export \(to_shell_vars({ + COLORTERM, + DISPLAY, + SSH_CLIENT, + }))", + "\(to_shell_vars({ + SU2PID, + XCOOKIE, + }));" + ] | join(";") + ' + ) + export INIT + + exec machinectl shell -E INIT "$user"@.host ${pkgs.writeDash "login" '' + eval "$INIT" + unset INIT + if test "''${XCOOKIE+x}" = x; then + echo "$XCOOKIE" | ${pkgs.coreutils}/bin/base64 -d | ${pkgs.xauth}/bin/xauth merge - + unset XCOOKIE + fi + if test $# = 0; then + set -- "$SHELL" + fi + # dtach-list finds su2 sessions by this process name. + exec ${pkgs.otherutils}/bin/exec -n "(su2 $SU2PID $1)" "$@" + ''} "$@" + ;; + esac +'' |
