summaryrefslogtreecommitdiff
path: root/overlays/sshpass-secret.nix
blob: 2f3d6169302acd20f128f619c1ec8281f8a995bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
final: prev: {
  sshpassSecret = prev.writeScriptBin "sshpass-secret" ''
    #!${final.zsh}/bin/zsh -e

    typeset -a cmd
    cmd=()

    while
      if [[ "$#" -le 0 ]]; then
        echo "Insufficient arguments" >&2
        exit 2
      fi
      arg="''${1}"
      shift
    do
      [[ "''${arg}" == '--' ]] && break
      user=''${arg%@*}
      host=''${arg#*@}
      exec {fdnum}<<<$(${final.libsecret}/bin/secret-tool lookup service sshpass host "''${host}" user "''${user}")
      cmd+=(${final.sshpass}/bin/sshpass -d ''${fdnum} -P "''${user}@''${host}'s password:")
    done

    cmd+=($@)
    exec -a ''${cmd[1]} -- $cmd
  '';
}