From ff24e73ad7491cb5b26ae1492ada803f6960992b Mon Sep 17 00:00:00 2001 From: Erik Arvstedt Date: Sun, 28 Nov 2021 21:19:20 +0100 Subject: [PATCH] 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. --- modules/onion-addresses.nix | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/modules/onion-addresses.nix b/modules/onion-addresses.nix index 9a5cd47..07466c5 100644 --- a/modules/onion-addresses.nix +++ b/modules/onion-addresses.nix @@ -58,8 +58,20 @@ in { CapabilityBoundingSet = "CAP_CHOWN CAP_FSETID CAP_SETFCAP CAP_DAC_OVERRIDE CAP_DAC_READ_SEARCH CAP_FOWNER CAP_IPC_OWNER"; }; 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 - until [[ -e /var/lib/tor/state ]]; do sleep 0.1; done + waitForFile /var/lib/tor/state cd ${cfg.dataDir} rm -rf * @@ -71,22 +83,20 @@ in { ${concatMapStrings (service: '' onionFile=/var/lib/tor/onion/${service}/hostname - if [[ -e $onionFile ]]; then - cp $onionFile ${user}/${service} - chown ${user} ${user}/${service} - fi + waitForFile $onionFile + cp $onionFile ${user}/${service} + chown ${user} ${user}/${service} '') cfg.access.${user} - } + } '') (builtins.attrNames cfg.access) } ${concatMapStrings (service: '' onionFile=/var/lib/tor/onion/${service}/hostname - if [[ -e $onionFile ]]; then - install -D -o ${config.systemd.services.${service}.serviceConfig.User} -m 400 $onionFile services/${service} - fi + waitForFile $onionFile + install -D -o ${config.systemd.services.${service}.serviceConfig.User} -m 400 $onionFile services/${service} '') cfg.services} ''; };