Skip to content

Commit 227fff4

Browse files
committed
fix(sops): render sops.templates in sopsFallbackSecrets hook for non-systemd targets
1 parent 98aedfc commit 227fff4

1 file changed

Lines changed: 37 additions & 6 deletions

File tree

programs/sops.nix

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,46 @@ in
2626

2727
home.activation.sopsFallbackSecrets = lib.hm.dag.entryAfter [ "writeBoundary" ] ''
2828
if [ -f "${cfg.age.keyFile}" ]; then
29-
${lib.concatStringsSep "\n" (lib.mapAttrsToList (name: secret: ''
30-
if [ -n "${secret.path or ""}" ] && [ ! -e "${secret.path}" ]; then
31-
mkdir -p "$(dirname "${secret.path}")"
29+
${lib.concatStringsSep "\n" (lib.mapAttrsToList (name: secret: let
30+
secretPath = if (secret.path != null && secret.path != "")
31+
then secret.path
32+
else "${cfg.defaultSecretsMountPoint}/${name}";
33+
in ''
34+
if [ ! -e "${secretPath}" ]; then
35+
mkdir -p "$(dirname "${secretPath}")"
3236
SOPS_AGE_KEY_FILE="${cfg.age.keyFile}" $DRY_RUN_CMD ${pkgs.sops}/bin/sops --decrypt \
3337
--extract '["${lib.replaceStrings ["/"] ["\"][\""] name}"]' \
34-
${secret.sopsFile} > "${secret.path}" 2>/dev/null || true
35-
chmod ${secret.mode or "0600"} "${secret.path}"
38+
${secret.sopsFile} > "${secretPath}" 2>/dev/null || true
39+
chmod ${secret.mode or "0600"} "${secretPath}"
3640
fi
37-
'') (lib.filterAttrs (_: s: s.path != null && s.path != "") cfg.secrets))}
41+
'') cfg.secrets)}
42+
43+
${lib.concatStringsSep "\n" (lib.mapAttrsToList (tName: template: ''
44+
mkdir -p "$(dirname "${template.path}")"
45+
TMP_FILE=$(mktemp)
46+
cp "${template.file}" "$TMP_FILE"
47+
${lib.concatStringsSep "\n" (lib.mapAttrsToList (sName: secret: let
48+
secretPath = if (secret.path != null && secret.path != "")
49+
then secret.path
50+
else "${cfg.defaultSecretsMountPoint}/${sName}";
51+
placeholder = cfg.placeholder."${sName}";
52+
in ''
53+
if [ -f "${secretPath}" ]; then
54+
SECRET_VAL=$(cat "${secretPath}")
55+
${pkgs.python3}/bin/python3 -c "
56+
import sys
57+
content = open(sys.argv[1]).read()
58+
placeholder = sys.argv[2]
59+
val = sys.argv[3]
60+
new_content = content.replace(placeholder, val)
61+
open(sys.argv[1], 'w').write(new_content)
62+
" "$TMP_FILE" "${placeholder}" "$SECRET_VAL"
63+
fi
64+
'') cfg.secrets)}
65+
$DRY_RUN_CMD cp "$TMP_FILE" "${template.path}"
66+
$DRY_RUN_CMD chmod ${template.mode or "0600"} "${template.path}"
67+
rm -f "$TMP_FILE"
68+
'') cfg.templates)}
3869
fi
3970
'';
4071
}

0 commit comments

Comments
 (0)