Commit fd362457a9c4

Vincent Demeester <vincent@sbr.pm>
2025-06-12 01:23:06
home: more flexible syncthing configuration in progress
This adds a bunch of function and globals value so that the syncthing configuration is more dynamic. It does work for folders, we now need to set up the devices configurations, complete globals definitions *and* have a system wide syncthing configuration as well. Signed-off-by: Vincent Demeester <vincent@sbr.pm>
1 parent eac3a70
Changed files (3)
home/common/services/syncthing.nix
@@ -1,42 +1,10 @@
-_:
-let
-  # Folders list
-  folders = [
-    {
-      label = "sync";
-      id = "7dshg-r8zr6";
-    } # TODO maybe deprecate for documents
-    {
-      label = "documents";
-      id = "oftdb-t5anv";
-    }
-    {
-      label = "org";
-      id = "sjpsr-xfwdu";
-    }
-    {
-      label = "screenshots";
-      id = "prpsz-azlz9";
-    }
-    {
-      label = "wallpapers";
-      id = "wpiah-ydwwx";
-    }
-    {
-      label = "photos";
-      id = "uetya-ypa3d";
-    }
-    {
-      label = "music";
-      id = "kcyrf-mugzt";
-    }
-  ];
-  # outputs.nixosConfigurations.$.syncthingFolders will contains the folders for a host
-  # FIXME: we could use another file, and a "configuration" so that I don't import it ? or in the flake, but not in makeHost
-
-  # non-nixos syncthing machines
-in
 {
+  globals,
+  hostname,
+  ...
+}:
+{
+  # warning = [ "${ttt}" ];
   services.syncthing = {
     enable = true;
     extraOptions = [ "--no-default-folder" ];
@@ -88,63 +56,66 @@ in
           ];
         };
       };
-      folders = {
-        "/home/vincent/sync" = {
-          label = "sync";
-          id = "7dshg-r8zr6";
-          devices = [
-            "aomi"
-            "aion"
-            "shikoku"
-            "sakhalin"
-          ];
-          rescanIntervalS = 3600 * 6;
-        };
-        "/home/vincent/desktop/org" = {
-          label = "org";
-          id = "sjpsr-xfwdu";
-          devices = [
-            "aomi"
-            "aion"
-            "shikoku"
-            "sakhalin"
-          ];
-          rescanIntervalS = 3600 * 6;
-        };
-        "/home/vincent/desktop/documents" = {
-          label = "documents";
-          id = "oftdb-t5anv";
-          devices = [
-            "aomi"
-            "aion"
-            "shikoku"
-            "sakhalin"
-          ];
-          rescanIntervalS = 3600 * 6;
-        };
-        "/home/vincent/desktop/pictures/screenshots" = {
-          label = "screenshots";
-          id = "prpsz-azlz9";
-          devices = [
-            "aomi"
-            "aion"
-            "shikoku"
-            "sakhalin"
-          ];
-          rescanIntervalS = 3600 * 6;
-        };
-        "/home/vincent/desktop/pictures/wallpapers" = {
-          label = "wallpapers";
-          id = "wpiah-ydwwx";
-          devices = [
-            "aomi"
-            "aion"
-            "shikoku"
-            "sakhalin"
-          ];
-          rescanIntervalS = 3600 * 6;
-        };
-      };
+      folders =
+        globals.fn.generateSyncthingFolders globals.machines."${hostname}" globals.machines
+          globals.syncthingFolders;
+      # folders = {
+      #   "/home/vincent/sync" = {
+      #     label = "sync";
+      #     id = "7dshg-r8zr6";
+      #     devices = [
+      #       "aomi"
+      #       "aion"
+      #       "shikoku"
+      #       "sakhalin"
+      #     ];
+      #     rescanIntervalS = 3600 * 6;
+      #   };
+      #   "/home/vincent/desktop/org" = {
+      #     label = "org";
+      #     id = "sjpsr-xfwdu";
+      #     devices = [
+      #       "aomi"
+      #       "aion"
+      #       "shikoku"
+      #       "sakhalin"
+      #     ];
+      #     rescanIntervalS = 3600 * 6;
+      #   };
+      #   "/home/vincent/desktop/documents" = {
+      #     label = "documents";
+      #     id = "oftdb-t5anv";
+      #     devices = [
+      #       "aomi"
+      #       "aion"
+      #       "shikoku"
+      #       "sakhalin"
+      #     ];
+      #     rescanIntervalS = 3600 * 6;
+      #   };
+      #   "/home/vincent/desktop/pictures/screenshots" = {
+      #     label = "screenshots";
+      #     id = "prpsz-azlz9";
+      #     devices = [
+      #       "aomi"
+      #       "aion"
+      #       "shikoku"
+      #       "sakhalin"
+      #     ];
+      #     rescanIntervalS = 3600 * 6;
+      #   };
+      #   "/home/vincent/desktop/pictures/wallpapers" = {
+      #     label = "wallpapers";
+      #     id = "wpiah-ydwwx";
+      #     devices = [
+      #       "aomi"
+      #       "aion"
+      #       "shikoku"
+      #       "sakhalin"
+      #     ];
+      #     rescanIntervalS = 3600 * 6;
+      #   };
+      # };
     };
   };
 }
lib/default.nix
@@ -15,7 +15,10 @@
       system ? "x86_64-linux",
     }:
     let
-      globals = import ../globals.nix { inherit (inputs.nixpkgs) lib; };
+      globals = import ../globals.nix {
+        inherit (inputs.nixpkgs) lib;
+        inherit hostname;
+      };
     in
     inputs.home-manager.lib.homeManagerConfiguration {
       pkgs = inputs.nixpkgs.legacyPackages.${system};
@@ -47,7 +50,10 @@
       homeInput ? inputs.home-manager,
     }:
     let
-      globals = import ../globals.nix { inherit (pkgsInput) lib; };
+      globals = import ../globals.nix {
+        inherit (pkgsInput) lib;
+        inherit hostname;
+      };
       specialArgs = {
         inherit
           self
globals.nix
@@ -1,4 +1,32 @@
-{ lib, ... }:
+{ hostname, lib, ... }:
+let
+
+  isCurrentHost = n: n == hostname;
+  /**
+      Return true if the given host has a list of Syncthing folder configured.
+    *
+  */
+  hasSyncthingFolders =
+    host:
+    builtins.hasAttr "syncthing" host
+    && builtins.hasAttr "folders" host.syncthing
+    && (builtins.length (lib.attrsets.attrValues host.syncthing.folders)) > 0;
+
+  # Get the path for the given folder, either using the host specified path or the default one
+  syncthingFolderPath =
+    name: folder: folders:
+    lib.attrsets.attrByPath [ "path" ] folders."${name}".path folder;
+
+  # Filter machine with the given syncthing folder
+  syncthingMachinesWithFolder =
+    folderName: machines:
+    lib.attrsets.filterAttrs (
+      name: value:
+      hasSyncthingFolders value
+      && !(isCurrentHost name)
+      && (builtins.hasAttr folderName value.syncthing.folders)
+    ) machines;
+in
 {
   ssh = {
     vincent = [
@@ -8,6 +36,36 @@
       "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBBFzxC16VqwTgWDQfw2YCiOw2JzpH3z9XgHtKoHhBdHi2i9m9XUc7fIUeEIIf7P8ARRNd8q5bjvl8JY7LtPkNCU="
     ];
   };
+  syncthingFolders = {
+    sync = {
+      id = "7dshg-r8zr6";
+      path = "/home/vincent/sync";
+    };
+    documents = {
+      id = "oftdb-t5anv";
+      path = "/home/vincent/desktop/documents";
+    };
+    org = {
+      id = "sjpsr-xfwdu";
+      path = "/home/vincent/desktop/org";
+    };
+    screenshots = {
+      id = "prpsz-azlz9";
+      path = "/home/vincent/desktop/pictures/screenshots";
+    };
+    wallpapers = {
+      id = "wpiah-ydwwx";
+      path = "/home/vincent/desktop/pictures/wallpapers";
+    };
+    photos = {
+      id = "uetya-ypa3d";
+      path = "/home/vincent/desktop/pictures/photos";
+    };
+    music = {
+      id = "kcyrf-mugzt";
+      path = "/home/vincent/desktop/music";
+    };
+  };
   net = {
     dns = {
       cacheNetworks = [
@@ -65,6 +123,11 @@
           pubkey = "RWqH7RdIXg+YE9U1nlsNiOC7jH8eWjWQmikqBVDGSXU=";
           ips = [ "10.100.0.83" ];
         };
+        names = [
+          "ahena.home"
+          "athena.vpn"
+          "athena.sbr.pm"
+        ];
       };
       ssh = {
         root = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFQVlSrUKU0xlM9E+sJ8qgdgqCW6ePctEBD2Yf+OnyME root@aomi";
@@ -78,6 +141,11 @@
           pubkey = "/bBh4gvDty/AA2qIiHc7K0OHoOXWmj2SFFXdDq8nsUU=";
           ips = [ "10.100.0.82" ];
         };
+        names = [
+          "demeter.home"
+          "demeter.vpn"
+          "demeter.sbr.pm"
+        ];
       };
     };
     aix = {
@@ -87,6 +155,12 @@
           ips = [ "10.100.0.89" ];
         };
       };
+      # syncthing = {
+      # 	folders = {
+      # 		sync = {
+      #       type = "receiveonly";};
+      # 	};
+      # };
     };
     kyushu = {
       net = {
@@ -100,20 +174,22 @@
         };
       };
       syncthing = {
+        id = "SBLRZF4-NOMC7QO-S6UW7OH-VK7KHQS-LZCESY6-USBJ5Z5-RIVIRII-XS7DGQS";
         folders = {
           org = { };
           documents = { };
           sync = { };
           screenshots = { };
           wallpapers = { };
-          photos = {
-            type = "receiveonly";
-            paused = true; # TODO: implement this, start as paused
-          };
-          music = {
-            type = "receiveonly";
-            paused = true; # TODO: implement this, start as paused
-          };
+          # TODO: implement paused or filter theses
+          # photos = {
+          #   type = "receiveonly";
+          #   paused = true; # TODO: implement this, start as paused
+          # };
+          # music = {
+          #   type = "receiveonly";
+          #   paused = true; # TODO: implement this, start as paused
+          # };
         };
       };
     };
@@ -125,6 +201,20 @@
           ips = [ "10.100.0.17" ];
         };
       };
+      syncthing = {
+        id = "XCR6WWB-OZUDGFB-LQPFW73-MV5SPJK-4IGOMA4-IAXON3I-C6OFETL-TPK5FQS";
+        folders = {
+          org = { };
+          documents = { };
+          sync = { };
+          screenshots = { };
+          wallpapers = { };
+          photos = {
+            type = "receiveonly";
+            paused = true; # TODO: implement this, start as paused
+          };
+        };
+      };
     };
     shikoku = {
       net = {
@@ -134,13 +224,32 @@
           ips = [ "10.100.0.2" ];
         };
       };
+      syncthing = {
+        id = "";
+        folders = {
+          org = { };
+        };
+      };
       ssh = {
         vincent = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGxstR3xEf87leVVDS3GVPx8Ap9+eP+OfkSvM26V54XP vincent@shikoku";
       };
     };
+    kerkouane = {
+      net = { };
+      syncthing = {
+        id = "";
+      };
+    };
+    sakhalin = {
+      net = { };
+      syncthing = {
+        id = "";
+      };
+    };
   };
   # FIXME Maybe I should move this elsewhere, in ./lib maybe ?
   fn = {
+    inherit syncthingFolderPath hasSyncthingFolders syncthingMachinesWithFolder;
     /**
          Return a list of wireguard ips from a list of ips.
 
@@ -155,14 +264,18 @@
     #   machines: user:
     #   lib.attrsets.mapAttrsToList (_name: value: value) (lib.attrsets.filterAttrs hasSSHAttr machines);
 
-    /**
-        Return true if the given host has a list of Syncthing folder configured.
-      *
-    */
-    hasSyncthingFolders =
-      host:
-      builtins.hasAttr "syncthing" host
-      && builtins.hasAttr "folders" host.syncthing
-      && (builtins.length (lib.attrsets.attrValues host.syncthing.folders)) > 0;
+    # SYNCTHING
+
+    generateSyncthingFolders =
+      machine: machines: folders:
+      lib.attrsets.mapAttrs' (
+        name: value:
+        lib.attrsets.nameValuePair (syncthingFolderPath name value folders) {
+          inherit (folders."${name}") id;
+          label = name;
+          devices = lib.attrsets.mapAttrsToList (n: _v: n) (syncthingMachinesWithFolder name machines);
+          rescanIntervalS = 3600 * 6; # TODO: make it configurable
+        }
+      ) (lib.attrsets.attrByPath [ "syncthing" "folders" ] { } machine);
   };
 }