mirror of
https://github.com/oddlama/nix-config.git
synced 2025-10-10 23:00:39 +02:00
feat: cache decrypted repository "secrets"
This commit is contained in:
parent
3111408f7d
commit
8947434a1e
2 changed files with 37 additions and 1 deletions
|
@ -22,8 +22,11 @@
|
|||
in
|
||||
lenContent >= lenSuffix && builtins.substring (lenContent - lenSuffix) lenContent content == suffix;
|
||||
in {
|
||||
# Instead of calling rage directly here, we call a wrapper script that will cache the output
|
||||
# in a predictable path in /tmp, which allows us to only require the password for each encrypted
|
||||
# file once.
|
||||
rageImportEncrypted = identities: nixFile:
|
||||
assert assertMsg (builtins.isPath nixFile) "The file to decrypt must be given as a path to prevent impurity.";
|
||||
assert assertMsg (hasSuffix ".nix.age" nixFile) "The content of the decrypted file must be a nix expression and should therefore end in .nix.age";
|
||||
exec (["rage" "-d"] ++ (builtins.concatMap (x: ["-i" x]) identities) ++ [nixFile]);
|
||||
exec ([./rage-decrypt.sh nixFile] ++ identities);
|
||||
}
|
||||
|
|
33
nix/rage-decrypt.sh
Executable file
33
nix/rage-decrypt.sh
Executable file
|
@ -0,0 +1,33 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
file="$1"
|
||||
[[ "$file" == "/nix/store/"* ]] || { echo "Input must be a store path!"; exit 1; }
|
||||
shift
|
||||
identities=("$@")
|
||||
|
||||
# Strip .age suffix and store path prefix
|
||||
basename="${file%".age"}"
|
||||
basename="${basename#*"-"}"
|
||||
|
||||
# Calculate a unique content-based identifier (relocations of
|
||||
# the source file in the nix store should not affect caching)
|
||||
new_name="$(sha512sum "$file")"
|
||||
new_name="${new_name:0:32}-${basename//"/"/"%"}"
|
||||
|
||||
# Derive the path where the decrypted file will be stored
|
||||
out="/tmp/nix-import-encrypted/$new_name"
|
||||
mkdir -p "$(dirname "$out")"
|
||||
|
||||
# Decrypt only if necessary
|
||||
if [[ ! -e "$out" ]]; then
|
||||
args=()
|
||||
for i in "${identities[@]}"; do
|
||||
args+=("-i" "$i")
|
||||
done
|
||||
rage -d "${args[@]}" -o "$out" "$file"
|
||||
fi
|
||||
|
||||
# Print decrypted content
|
||||
cat "$out"
|
Loading…
Add table
Add a link
Reference in a new issue