···11#!/usr/bin/env bash
22# SPDX-License-Identifier: MPL-2.0
33# Sets EDITOR and related variables to use VS Code instead of firing up nano (or worse vi),
44-# based on env var detection of VSCODE_* vars.
44+# based on env var detection of VSCODE_* vars. Utilized by git-commit and similar git commands
55+# that fire up an editor.
5667# Handle verbose logging
88+log() {
99+ echo "[detect-vscode-for-git] $*"
1010+}
1111+712if [[ -n $DEBUG ]]; then
813 set -x
914fi
10151116if [[ "$VSCODE_GIT_IPC_HANDLE" != "" && "$VSCODE_GIT_ASKPASS_NODE" != "" && "$TERM_PROGRAM" == "vscode" ]]; then
1217 __VSCODE_BASE_PATH="$(dirname "${VSCODE_GIT_ASKPASS_NODE}")"
1818+ log "Detected VS Code environment. Base path: ${__VSCODE_BASE_PATH}"
13191420 # Check if the path is in ~/.vscode/cli/servers (for Remote Tunnels), ~/.vscode-server/cli
1521 # (for Remote SSH) or not
1622 if [[ $__VSCODE_BASE_PATH =~ ^"$HOME"/.vscode/cli/servers ]] || [[ $__VSCODE_BASE_PATH =~ ^"$HOME"/.vscode-server/cli ]]; then
1723 # If it is, set the path to the correct location
1818- VSCODE_CLI_PATH="${__VSCODE_BASE_PATH}/bin/remote-cli/code"
2424+ __VSCODE_STABLE_CLI_PATH="${__VSCODE_BASE_PATH}/bin/remote-cli/code"
2525+ __VSCODE_INSIDERS_CLI_PATH="${__VSCODE_BASE_PATH}/bin/remote-cli/code-insiders"
2626+2727+ # Check if the stable or insiders version exists
2828+ if [[ -f "$__VSCODE_INSIDERS_CLI_PATH" ]]; then
2929+ VSCODE_CLI_PATH="$__VSCODE_INSIDERS_CLI_PATH"
3030+ elif [[ -f "$__VSCODE_STABLE_CLI_PATH" ]]; then
3131+ VSCODE_CLI_PATH="$__VSCODE_STABLE_CLI_PATH"
3232+ fi
1933 else
2034 # otherwise just use plain cli/desktop app CLI
2121- VSCODE_CLI_PATH="$(command -v code)"
3535+ __VSCODE_STABLE_CLI_PATH="$(command -v code)"
3636+ __VSCODE_INSIDERS_CLI_PATH="$(command -v code-insiders)"
3737+3838+ # Same with earlier
3939+ if [[ -f "$__VSCODE_INSIDERS_CLI_PATH" ]]; then
4040+ VSCODE_CLI_PATH="$__VSCODE_INSIDERS_CLI_PATH"
4141+ elif [[ -f "$__VSCODE_STABLE_CLI_PATH" ]]; then
4242+ VSCODE_CLI_PATH="$__VSCODE_STABLE_CLI_PATH"
4343+ fi
2244 fi
4545+ log "Using VS Code CLI path: ${VSCODE_CLI_PATH}"
23462447 export GIT_EDITOR="${VSCODE_CLI_PATH} --wait" EDITOR="${VSCODE_CLI_PATH} --wait" VISUAL="${VSCODE_CLI_PATH} --wait" VSCODE_CLI_PATH
2548fi
+20-6
misc/bash/lib/ssh-agent-loader
···11#!/usr/bin/env bash
22# shellcheck disable=SC2034
33# SPDX-License-Identifier: MPL-2.0
44+# Reliably detect and switch between SSH agents like keychain, yubikey-agent, 1Password, etc.
4556if [[ $DEBUG != "" ]]; then
67 set -x
···3031if [[ -z "${XDG_RUNTIME_DIR}" ]]; then
3132 logOps warn "XDG_RUNTIME_DIR is possibly undefined, see https://github.com/swaywm/sway/issues/7202"
3233 logOps warn "for context and https://wiki.archlinux.org/title/XDG_Base_Directory for docs"
3333- logOps warn "setting it up for you using the default '/run/user/$(id -u)' value in 3s..."
3434+ logOps warn "Temporarily using '/run/user/$(id -u)' for XDG_RUNTIME_DIR in 3s..."
3435 sleep 3
3536 XDG_RUNTIME_DIR="/run/user/$(id -u)"
3637 export XDG_RUNTIME_DIR
···4647try_keychain_ssh_agent() {
4748 if [[ $FF_KEYCHAIN == "1" ]]; then
4849 logOps info "attempting to use keychain for SSH agents" keychain
4949- [[ "$SSH_AGENT_LOADER_SLIENT" == "" ]] && eval "$(keychain --eval --agents ssh,gpg)" || eval "$(keychain --eval --agents ssh,gpg --quiet)"
5050+ if [[ "$SSH_AGENT_LOADER_SLIENT" == "" ]]; then
5151+ eval "$(keychain --ssh-spawn-gpg --ssh-allow-gpg)"
5252+ else
5353+ eval "$(keychain --eval --ssh-spawn-gpg --ssh-allow-gpg --quiet)"
5454+ fi
5055 else
5156 logOps warn "keychain is not in PATH yet" keychain
5257 return 1
···55605661# Ripped off NixOS-generated set-environment on my laptop for yubikey-agent setup
5762try_yubikey_agent() {
5858- if [[ -f "${XDG_RUNTIME_DIR}/yubikey-agent/yubikey-agent.sock" ]]; then
5959- logOps info "using YubiKey SSH Agent via socket at XDG_RUNTIME_DIR" yubikey-agent
6060- export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/yubikey-agent/yubikey-agent.sock"
6363+ YUBIKEY_AGENT_AUTH_SOCK="${XDG_RUNTIME_DIR}/yubikey-agent/yubikey-agent.sock"
6464+ if [[ -f "${YUBIKEY_AGENT_AUTH_SOCK}" ]]; then
6565+ logOps info "attempting to use Yubikey SSH agent" yubikey-agent
6666+ if ! SSH_AUTH_SOCK=${YUBIKEY_AGENT_AUTH_SOCK} ssh-add -l >> /dev/null 2>&1; then
6767+ logOps warn "something went wrong while checking for Yubikey SSH agent availability" yubikey-agent
6868+ logOps warn "is your Yubikey plugged in properly?" yubikey-agent
6969+ return 1
7070+ fi
7171+ else
7272+ logOps error "Yubikey SSH agent seems to be not available on this host" yubikey-agent
7373+ return 1
6174 fi
6275}
6376···8194ssh-agent-loader() {
8295 if [[ $1 == "" || $1 == "auto" ]]; then
8396 if [[ $SSH_CONNECTION != "" ]] && [[ $VSCODE_IPC_HOOK_CLI != "" ]]; then
8484- logOps info "automatic detection is disabled while you're in a VS Code Remote SSH session"
9797+ logOps info "automatic detection is disabled while you're in a VS Code Remote SSH/Tunnels session"
9898+ logOps info "to enable SSH agent, please manually invoke the shell function with desired agent"
8599 return
86100 fi
87101
+9
pkgs/README.md
···11+# Nix flakes-packages utilities
22+33+## What's included
44+55+To install them, run `nix profile add github:andreijiroh-dev/nixops-config#<package-name>` or
66+add them into your NixOS/home-manager package lists after importing the flake.
77+88+* [`detect-vscode-for-git`](./detect-vscode-for-git.nix)
99+* [`ssh-agent-loader`](./ssh-agent-loader.nix)
+25
pkgs/detect-vscode-for-git.nix
···11+{ stdenv, lib, ... }:
22+33+stdenv.mkDerivation {
44+ pname = "detect-vscode-for-git";
55+ version = "0.1.0";
66+77+ src = ../misc/bash/lib/detect-vscode-for-git;
88+99+ dontUnpack = true;
1010+1111+ installPhase = ''
1212+ runHook preInstall
1313+1414+ install -D -m755 $src $out/bin/detect-vscode-for-git
1515+1616+ runHook postInstall
1717+ '';
1818+1919+ meta = with lib; {
2020+ description = "A script to detect and set VS Code as the git editor.";
2121+ license = licenses.mpl20;
2222+ platforms = platforms.all;
2323+ maintainers = with maintainers; [ ajhalili2006 ];
2424+ };
2525+}
+18
pkgs/maintainer-metadata.nix
···11+# pkgs/maintainer-metadata.nix
22+#
33+# This file contains maintainer information for packages in this flake.
44+# It's intended to be imported by package definitions.
55+{
66+ ajhalili2006 = {
77+ name = "Andrei Jiroh Halili";
88+ github = "ajhalili2006";
99+ githubId = 34998342;
1010+ email = "ajhalili2006@andreijiroh.dev";
1111+ matrix = "@ajhalili2006@envs.net";
1212+ keys = [
1313+ {
1414+ fingerprint = "4D5E 6317 58CB 9CC4 5941 B1CE 67BF C91B 3DA1 2BE8"
1515+ }
1616+ ];
1717+ };
1818+}
+25
pkgs/ssh-agent-loader.nix
···11+{ stdenv, lib, ... }:
22+33+stdenv.mkDerivation {
44+ pname = "ssh-agent-loader";
55+ version = "0.1.0";
66+77+ src = ../misc/bash/lib/ssh-agent-loader;
88+99+ dontUnpack = true;
1010+1111+ installPhase = ''
1212+ runHook preInstall
1313+1414+ install -D -m755 $src $out/bin/ssh-agent-loader
1515+1616+ runHook postInstall
1717+ '';
1818+1919+ meta = with lib; {
2020+ description = "A script to reliably detect and switch between SSH agents.";
2121+ license = licenses.mpl20;
2222+ platforms = platforms.all;
2323+ maintainers = with maintainers; [ ajhalili2006 ];
2424+ };
2525+}
+9-4
shared/home-manager/shell.nix
···11-{ lib, ... }: {
11+{ lib, pkgs, ... }: {
22+ home.packages = [
33+ pkgs.detect-vscode-for-git
44+ pkgs.ssh-agent-loader
55+ ];
66+27 # taken from https://github.com/andreijiroh-dev/dotfiles/blob/main/.config/aliases
38 home.shellAliases = {
49 signoff = "git commit --signoff";
···6368 fi
64696570 # source our ssh-agent-loader first
6666- FF_SKIP_AUTO_SSH_AGENT_LOADER=true . ${../../misc/bash/lib/ssh-agent-loader}
7171+ FF_SKIP_AUTO_SSH_AGENT_LOADER=true . ${pkgs.ssh-agent-loader}/bin/ssh-agent-loader
67726873 # try to use keychain in this situation
6974 SSH_AGENT_LOADER_SLIENT=1 ssh-agent-loader keychain
···7681 '';
7782 bashrcExtra = ''
7883 # detect if we are inside VS Code
7979- source ${../../misc/bash/lib/detect-vscode-for-git}
8484+ source ${pkgs.detect-vscode-for-git}/bin/detect-vscode-for-git
80858186 # HACK: https://github.com/akinomyoga/ble.sh/wiki/Manual-A1-Installation#user-content-nixpkgs
8287 export BLESH_PATH=$(blesh-share)
···8792 export GPG_TTY=$(tty)
88938994 # source our ssh-agent-loader first
9090- source ${../../misc/bash/lib/ssh-agent-loader}
9595+ source ${pkgs.ssh-agent-loader}/bin/ssh-agent-loader
91969297 # hack around for 1Password CLI when 1Password desktop app is up
9398 if [[ $$FF_USE_OP_CLI_PLUGINS == "true" ]]; then