onion-addresses: fix files not being copied

When NixOS is already running and Tor is restarted due to config
changes, `/var/lib/tor/state` may be present even when Tor has not
yet finished setting up onion services.
This caused the previous version of `onion-addresses` to not wait for
Tor and to skip not yet present onion service files.

`onion-addresses` now waits until each required onion service file
has appeared.
This commit is contained in:
Erik Arvstedt 2021-11-28 21:19:20 +01:00
parent c6fe017aeb
commit ff24e73ad7
No known key found for this signature in database
GPG Key ID: 33312B944DD97846

View File

@ -58,8 +58,20 @@ in {
CapabilityBoundingSet = "CAP_CHOWN CAP_FSETID CAP_SETFCAP CAP_DAC_OVERRIDE CAP_DAC_READ_SEARCH CAP_FOWNER CAP_IPC_OWNER"; CapabilityBoundingSet = "CAP_CHOWN CAP_FSETID CAP_SETFCAP CAP_DAC_OVERRIDE CAP_DAC_READ_SEARCH CAP_FOWNER CAP_IPC_OWNER";
}; };
script = '' script = ''
waitForFile() {
file=$1
for ((i=0; i<300; i++)); do
if [[ -e $file ]]; then
return;
fi
sleep 0.1
done
echo "Error: File $file did not appear after 30 sec."
exit 1
}
# Wait until tor is up # Wait until tor is up
until [[ -e /var/lib/tor/state ]]; do sleep 0.1; done waitForFile /var/lib/tor/state
cd ${cfg.dataDir} cd ${cfg.dataDir}
rm -rf * rm -rf *
@ -71,22 +83,20 @@ in {
${concatMapStrings ${concatMapStrings
(service: '' (service: ''
onionFile=/var/lib/tor/onion/${service}/hostname onionFile=/var/lib/tor/onion/${service}/hostname
if [[ -e $onionFile ]]; then waitForFile $onionFile
cp $onionFile ${user}/${service} cp $onionFile ${user}/${service}
chown ${user} ${user}/${service} chown ${user} ${user}/${service}
fi
'') '')
cfg.access.${user} cfg.access.${user}
} }
'') '')
(builtins.attrNames cfg.access) (builtins.attrNames cfg.access)
} }
${concatMapStrings (service: '' ${concatMapStrings (service: ''
onionFile=/var/lib/tor/onion/${service}/hostname onionFile=/var/lib/tor/onion/${service}/hostname
if [[ -e $onionFile ]]; then waitForFile $onionFile
install -D -o ${config.systemd.services.${service}.serviceConfig.User} -m 400 $onionFile services/${service} install -D -o ${config.systemd.services.${service}.serviceConfig.User} -m 400 $onionFile services/${service}
fi
'') cfg.services} '') cfg.services}
''; '';
}; };