forked from
lass.pds.lassul.us/superconfig
This repository has no description
5.7 kB
147 lines
1{ self, inputs, ... }:
2{
3 perSystem =
4 {
5 pkgs,
6 system,
7 ...
8 }:
9 {
10 packages.pass = inputs.wrappers.lib.wrapPackage {
11 pkgs = pkgs;
12 package = pkgs.passage;
13 runtimeInputs = [
14 pkgs.age-plugin-fido2-hmac
15 pkgs.age-plugin-yubikey
16 pkgs.age-plugin-tpm
17 self.packages.${system}.pass-otp
18 self.packages.${system}.age-detect
19 ]
20 ++ (pkgs.lib.optionals pkgs.stdenv.isLinux [
21 self.packages.${system}.pinentry-rofi-age
22 ])
23 ++ (pkgs.lib.optionals pkgs.stdenv.isDarwin [
24 pkgs.age-plugin-se
25 ])
26 ++ (pkgs.lib.optionals (!pkgs.stdenv.isDarwin) [
27 # Dummy age-plugin-se for Linux (encryption only, no Secure Enclave hardware)
28 (pkgs.writeShellScriptBin "age-plugin-se" ''
29 exec env PYTHONUNBUFFERED=1 ${
30 pkgs.python3.withPackages (ps: [ ps.cryptography ])
31 }/bin/python3 ${./age-plugin-se-dummy.py} "$@"
32 '')
33 ]);
34 aliases = [ "pass" ];
35 wrapper =
36 {
37 exePath,
38 envString,
39 preHook,
40 ...
41 }:
42 ''
43 ${envString}
44 ${preHook}
45
46 # Check if bulk key file should be used for decryption
47 if [[ -n "''${PASS_BULK_KEY_FILE:-}" ]]; then
48 if [[ ! -f "$PASS_BULK_KEY_FILE" ]] || [[ ! -s "$PASS_BULK_KEY_FILE" ]]; then
49 mkdir -p "$(dirname "$PASS_BULK_KEY_FILE")"
50 ${exePath} show bulk-operations/age-key > "$PASS_BULK_KEY_FILE" 2>/dev/null || true
51 fi
52 fi
53
54 # Set up identities for age decryption
55 if [[ -n "''${PASS_BULK_KEY_FILE:-}" ]] && [[ -f "$PASS_BULK_KEY_FILE" ]] && [[ -s "$PASS_BULK_KEY_FILE" ]]; then
56 # Use bulk key if available
57 export PASSAGE_IDENTITIES_FILE="$PASS_BULK_KEY_FILE"
58 else
59 # Detect available age keys
60 export AGE_DETECT_KEYS_DIR="${self}/keys"
61 eval "$(age-detect)"
62
63 # Set up identity file if detected
64 if [[ -n "''${IDENTITY_FILE:-}" ]]; then
65 export PASSAGE_IDENTITIES_FILE="$IDENTITY_FILE"
66 trap 'rm -f "$IDENTITY_FILE"' EXIT
67 fi
68 fi
69
70 ${
71 if pkgs.stdenv.isLinux then
72 ''
73 maybe_set_tpm_pin() {
74 local target="''${1:-<stdin>}"
75 if [[ "''${KEY_TYPE:-}" != "tpm" ]]; then
76 return 0
77 fi
78 if [[ -n "''${AGE_TPM_PIN:-}" ]]; then
79 return 0
80 fi
81 AGE_TPM_PIN="$(${self.packages.${system}.pinentry-rofi-age}/bin/pinentry-rofi-age "$target")"
82 export AGE_TPM_PIN
83 }
84
85 decrypt_target=""
86 if [[ "''${1:-}" == "show" ]]; then
87 decrypt_target="''${2:-}"
88 elif [[ -n "''${1:-}" ]] && [[ "''${1:-}" != -* ]] && [[ "''${1:-}" != "otp" ]]; then
89 # Skip passage subcommands that don't need decryption
90 case "''${1:-}" in
91 ls|find|git|insert|generate|rm|mv|cp|help|version|init)
92 ;;
93 edit|grep|reencrypt)
94 # These commands do decrypt, prompt with command as target
95 decrypt_target="''${2:-''${1:-}}"
96 ;;
97 *)
98 # Bare pass-name (implicit show)
99 decrypt_target="''${1:-}"
100 ;;
101 esac
102 fi
103
104 if [[ -n "$decrypt_target" ]]; then
105 maybe_set_tpm_pin "$decrypt_target"
106 fi
107 ''
108 else
109 ''
110 # No TPM on Darwin, define no-op
111 maybe_set_tpm_pin() { :; }
112 ''
113 }
114
115 # Handle OTP commands by delegating to pass-otp
116 if [[ "''${1:-}" == "otp" ]]; then
117 shift
118 # Extract pass name and options
119 clip_arg=""
120 pass_name=""
121 for arg in "$@"; do
122 case "$arg" in
123 "--clip"|"-c")
124 clip_arg="--clip"
125 ;;
126 *)
127 pass_name="$arg"
128 ;;
129 esac
130 done
131
132 if [[ -z "$pass_name" ]]; then
133 echo "Usage: pass otp [--clip,-c] pass-name"
134 exit 1
135 fi
136
137 # Show pass and pipe to OTP (identities already set up by age-detect)
138 maybe_set_tpm_pin "$pass_name"
139 ${exePath} show "$pass_name" | pass-otp $clip_arg
140 else
141 # Pass all other commands directly to passage (identities already set up)
142 ${exePath} "$@"
143 fi
144 '';
145 };
146 };
147}