summary refs log tree commit diff stats
path: root/system/services/nix-sync/default.nix
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--system/services/nix-sync/default.nix43
1 files changed, 28 insertions, 15 deletions
diff --git a/system/services/nix-sync/default.nix b/system/services/nix-sync/default.nix
index c8f6c44..ed0a4fe 100644
--- a/system/services/nix-sync/default.nix
+++ b/system/services/nix-sync/default.nix
@@ -6,6 +6,15 @@
 }: let
   cfg = config.services.nix-sync;
 
+  mkTimer = name: repo: {
+    description = "Nix sync ${name} timer";
+    wantedBy = ["timers.target"];
+    timerConfig = {
+      OnActiveSec = repo.interval;
+    };
+    after = ["network-online.target"];
+  };
+
   mkUnit = name: repo: let
     esa = lib.strings.escapeShellArg;
     parents = path: let
@@ -23,23 +32,20 @@
       export XDG_CACHE_HOME="$CACHE_DIRECTORY";
       cd ${esa repoCachePath};
 
-      while true; do
-        origin="$(git rev-parse @{u})";
-        branch="$(git rev-parse @)";
+      origin="$(git rev-parse @{u})";
+      branch="$(git rev-parse @)";
 
-        if ! [ "$origin" = "$branch" ]; then
-          git pull;
+      if ! [ "$origin" = "$branch" ]; then
+        git pull;
 
-          out_paths=$(mktemp);
-          nix build . --print-out-paths --experimental-features 'nix-command flakes' > "$out_paths";
-          [ "$(wc -l < "$out_paths")" -gt 1 ] && (echo "To many out-paths"; exit 1)
-          out_path="$(cat "$out_paths")";
-          rm -r ${esa repo.path};
-          ln -s "$out_path" ${esa repo.path};
-          rm "$out-paths";
-        fi
-        sleep ${esa repo.interval};
-      done
+        out_paths=$(mktemp);
+        nix build . --print-out-paths --experimental-features 'nix-command flakes' > "$out_paths";
+        [ "$(wc -l < "$out_paths")" -gt 1 ] && (echo "To many out-paths"; exit 1)
+        out_path="$(cat "$out_paths")";
+        rm -r ${esa repo.path};
+        ln -s "$out_path" ${esa repo.path};
+        rm "$out-paths";
+      fi
     '';
     execStartPreScript = ''
       export XDG_CACHE_HOME="$CACHE_DIRECTORY";
@@ -120,6 +126,12 @@
       value = mkUnit name repo;
     })
     cfg.repositories;
+  timers =
+    lib.mapAttrs' (name: repo: {
+      name = "nix-sync-${name}";
+      value = mkTimer name repo;
+    })
+    cfg.repositories;
 
   repositoryType = lib.types.submodule ({name, ...}: {
     options = {
@@ -211,6 +223,7 @@ in {
     environment.etc."nginx/websites/.keep".text = "keep this directory";
 
     systemd.services = services;
+    systemd.timers = timers;
     users.users =
       if cfg.user == "nix-sync"
       then {