Skip to content

Commit b5e81d5

Browse files
committed
syncthing: handle encryptionPassword secret
Rewrite the syncthing config update script to embed secrets into the json request. Specifically, we handle the `encryptionPassword` secret. With this code, the user can embed path to the encrpyption password for a given device the folder is shared with, and have it loaded in, without touching the nix store.
1 parent d413e71 commit b5e81d5

File tree

1 file changed

+60
-3
lines changed

1 file changed

+60
-3
lines changed

nixos/modules/services/networking/syncthing.nix

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,66 @@ let
103103
# don't exist in the array given. That's why we use here `POST`, and
104104
# only if s.override == true then we DELETE the relevant folders
105105
# afterwards.
106-
(map (new_cfg: ''
107-
curl -d ${lib.escapeShellArg (builtins.toJSON new_cfg)} -X POST ${s.baseAddress}
108-
''))
106+
(map (new_cfg:
107+
let
108+
isSecret = attr: value: builtins.isString value && attr == "encryptionPassword";
109+
110+
resolveSecrets = attr: value:
111+
if builtins.isAttrs value then
112+
# Attribute set: process each attribute
113+
builtins.mapAttrs (name: val: resolveSecrets name val) value
114+
else if builtins.isList value then
115+
# List: process each element
116+
map (item: resolveSecrets "" item) value
117+
else if isSecret attr value then
118+
# String that looks like a path: replace with placeholder
119+
let
120+
varName = "secret_${builtins.hashString "sha256" value}";
121+
in
122+
"\${${varName}}"
123+
else
124+
# Other types: return as is
125+
value;
126+
127+
# Function to collect all file paths from the configuration
128+
collectPaths = attr: value:
129+
if builtins.isAttrs value then
130+
concatMap (name: collectPaths name value.${name}) (builtins.attrNames value)
131+
else if builtins.isList value then
132+
concatMap (name: collectPaths "" name) value
133+
else if isSecret attr value then
134+
[ value ]
135+
else
136+
[];
137+
138+
# Function to generate variable assignments for the secrets
139+
generateSecretVars = paths:
140+
concatStringsSep "\n" (map (path:
141+
let
142+
varName = "secret_${builtins.hashString "sha256" path}";
143+
in
144+
''
145+
if [ ! -r ${path} ]; then
146+
echo "${path} does not exist"
147+
exit 1
148+
fi
149+
${varName}=$(<${path})
150+
''
151+
) paths);
152+
153+
resolved_cfg = resolveSecrets "" new_cfg;
154+
secretPaths = collectPaths "" new_cfg;
155+
secretVarsScript = generateSecretVars secretPaths;
156+
157+
jsonString = builtins.toJSON resolved_cfg;
158+
escapedJson = builtins.replaceStrings ["\""] ["\\\""] jsonString;
159+
in
160+
''
161+
${secretVarsScript}
162+
163+
curl -d "${escapedJson}" -X POST ${s.baseAddress}
164+
''
165+
))
109166
(lib.concatStringsSep "\n")
110167
]
111168
/* If we need to override devices/folders, we iterate all currently configured

0 commit comments

Comments
 (0)