syncthing: Support remote declarative sync

This commit is contained in:
Geoffrey Frogeye 2025-01-27 00:15:16 +01:00
parent 460ab8938f
commit aceb44293f
Signed by: geoffrey
GPG key ID: C72403E7F82E6AD8
3 changed files with 64 additions and 4 deletions

View file

@ -213,6 +213,10 @@
system = "x86_64-linux";
modules = [ ./abavorana/standin.nix ];
};
nixosConfigurations.ludwig = lib.nixosSystem {
system = "x86_64-linux";
modules = [ ./ludwig/standin.nix ];
};
nixosConfigurations.sprinkles = lib.nixosSystem {
system = "aarch64-linux";
modules = [ ./sprinkles/standin.nix ];

10
ludwig/standin.nix Normal file
View file

@ -0,0 +1,10 @@
{ ... }:
{
config = {
frogeye = {
name = "ludwig";
storageSize = "big";
syncthing.name = "Ludwig";
};
};
}

View file

@ -24,7 +24,9 @@ let
);
allDevices = nixosDevices;
syncingDevices = builtins.filter (device: device.syncthing.id != null) allDevices;
peerDevices = builtins.filter (device: device.name != config.frogeye.name) syncingDevices;
peerDevices = builtins.filter (
device: device.syncthing.id != config.frogeye.syncthing.id
) syncingDevices;
# Can't use the module's folders enable option, as it still requests things somehow
allFolders = builtins.attrValues config.frogeye.folders;
@ -34,12 +36,58 @@ let
folder: device:
(lib.hasAttrByPath [ folder.name ] device.folders)
&& device.folders.${folder.name}.syncthing.enable;
folderDeviceEntry = folder: device: { deviceID = device.syncthing.id; };
enable = (builtins.length syncedFolders) > 0;
in
{
config = {
# Allow to export configuration to other systems
system.build.syncthingConfig = {
folders = lib.trivial.pipe syncedFolders [
(builtins.map (folder: {
name = folder.name;
value = folder;
}))
builtins.listToAttrs
(lib.attrsets.mapAttrs (
folderName: folder:
(lib.attrsets.filterAttrs (
k: v:
builtins.elem k [
"label"
"path"
"syncthing"
"user"
]
))
folder
))
];
devices = lib.trivial.pipe syncingDevices [
(builtins.map (device: {
name = device.name;
value = device;
}))
builtins.listToAttrs
(lib.attrsets.mapAttrs (
deviceName: device:
{
folders = lib.trivial.pipe device.folders [
(lib.attrsets.filterAttrs (folderName: folder: folder.syncthing.enable))
(lib.attrsets.mapAttrs (folderName: folder: { syncthing.enable = true; }))
];
}
//
(lib.attrsets.filterAttrs (
k: v:
builtins.elem k [
"syncthing"
]
))
device
))
];
};
services.${service} = {
inherit enable;
openDefaultPorts = true;
@ -63,8 +111,6 @@ in
value = {
label = "${capitalizeFirstLetter folder.user} ${folder.label}";
path = "${config.users.users.${folder.user}.home}/${folder.path}";
# Despite further in the code indicating this is possible, it is, actually not
# devices = builtins.map (folderDeviceEntry folder) (builtins.filter (folderShouldSyncWith folder) peerDevices);
devices = builtins.map (device: device.name) (
builtins.filter (folderShouldSyncWith folder) peerDevices
);