From 691ac99e4d6a95d599766b8c2d5a42e5b9da5067 Mon Sep 17 00:00:00 2001 From: Connor Prussin Date: Fri, 27 Jan 2023 13:43:12 -0700 Subject: [PATCH] Use nixops implementation of key services --- src/nix/hive/modules.nix | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/nix/hive/modules.nix b/src/nix/hive/modules.nix index 25f2e16..2dbe1a8 100644 --- a/src/nix/hive/modules.nix +++ b/src/nix/hive/modules.nix @@ -78,19 +78,30 @@ with builtins; { systemd.services = lib.mapAttrs' (name: val: { name = "${name}-key"; value = { - bindsTo = [ "${name}-key.path" ]; + enable = true; serviceConfig = { - Restart = "on-failure"; + TimeoutStartSec = "infinity"; + Restart = "always"; + RestartSec = "100ms"; }; path = [ pkgs.inotify-tools ]; - script = '' - if [[ ! -e "${val.path}" ]]; then - >&2 echo "${val.path} does not exist" + preStart = '' + (while read f; do if [ "$f" = "${val.name}" ]; then break; fi; done \ + < <(inotifywait -qm --format '%f' -e create,move ${val.destDir}) ) & + if [[ -e "${val.path}" ]]; then + echo 'flapped down' + kill %1 exit 0 fi - - inotifywait -qq -e delete_self "${val.path}" - >&2 echo "${val.path} disappeared" + wait %1 + ''; + script = '' + inotifywait -qq -e delete_self "${val.path}" & + if [[ ! -e "${val.path}" ]]; then + echo 'flapped up' + exit 0 + fi + wait %1 ''; }; }) config.deployment.keys;