summaryrefslogtreecommitdiff
path: root/overlays/sshpass-secret.nix
blob: 388471cb9723506d91d837f7ef752c88419c57be (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
  '';
}