summary refs log tree commit diff stats
path: root/system/services/nix-sync
diff options
context:
space:
mode:
authorSoispha <soispha@vhack.eu>2023-07-04 22:13:28 +0200
committerSoispha <soispha@vhack.eu>2023-07-04 22:14:52 +0200
commit42d44c6ad84fff9b41624625634a524a69974333 (patch)
tree668d959a3621eb15718719c4e47db889832f85b1 /system/services/nix-sync
parentFix(system/services/nginx): Set the correct acme webRoot (diff)
downloadnixos-server-42d44c6ad84fff9b41624625634a524a69974333.tar.gz
nixos-server-42d44c6ad84fff9b41624625634a524a69974333.zip
Feat(system/services/nix-sync): Split unit into a timer and unit
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 {