From ab31842e585a122e8d54229feb4babdfa31dbbe1 Mon Sep 17 00:00:00 2001 From: hektor Date: Tue, 3 Feb 2026 12:11:25 +0100 Subject: [PATCH] feat: set up syncthing (with e-reader) --- flake.lock | 24 +++++----- hosts/andromache/default.nix | 41 +++++++++-------- hosts/astyanax/default.nix | 36 +++++++++------ modules/syncthing/default.nix | 85 +++++++++++++++++++++++++++++++++++ modules/syncthing/devices.nix | 5 +++ 5 files changed, 147 insertions(+), 44 deletions(-) create mode 100644 modules/syncthing/default.nix create mode 100644 modules/syncthing/devices.nix diff --git a/flake.lock b/flake.lock index 1fe5ac3..43444ac 100644 --- a/flake.lock +++ b/flake.lock @@ -53,11 +53,11 @@ }, "locked": { "dir": "pkgs/firefox-addons", - "lastModified": 1769745834, - "narHash": "sha256-/7EzWXFrHiF2LiuQPFUD1jhICcRa30QVa/uQCPu1Q/g=", + "lastModified": 1770091431, + "narHash": "sha256-9Sqq/hxq8ZDLRSzu+edn0OfWG+FAPWFpwMKaJobeLec=", "owner": "rycee", "repo": "nur-expressions", - "rev": "ec30ecfdee4b0df2325c2672db21684e806f4b69", + "rev": "4f827ff035c6ddc58d04c45abe5b777d356b926a", "type": "gitlab" }, "original": { @@ -145,11 +145,11 @@ ] }, "locked": { - "lastModified": 1769776025, - "narHash": "sha256-70a1kVC08AMTvPc7iqQsJbbD4Y1fukakMVudz4oY9SM=", + "lastModified": 1769978395, + "narHash": "sha256-gj1yP3spUb1vGtaF5qPhshd2j0cg4xf51pklDsIm19Q=", "owner": "nix-community", "repo": "home-manager", - "rev": "0fba737f8d5571d41467f3d99a878e11b8c0f0f0", + "rev": "984708c34d3495a518e6ab6b8633469bbca2f77a", "type": "github" }, "original": { @@ -321,11 +321,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1769461804, - "narHash": "sha256-msG8SU5WsBUfVVa/9RPLaymvi5bI8edTavbIq3vRlhI=", + "lastModified": 1770019141, + "narHash": "sha256-VKS4ZLNx4PNrABoB0L8KUpc1fE7CLpQXQs985tGfaCU=", "owner": "nixos", "repo": "nixpkgs", - "rev": "bfc1b8a4574108ceef22f02bafcf6611380c100d", + "rev": "cb369ef2efd432b3cdf8622b0ffc0a97a02f3137", "type": "github" }, "original": { @@ -612,11 +612,11 @@ ] }, "locked": { - "lastModified": 1769469829, - "narHash": "sha256-wFcr32ZqspCxk4+FvIxIL0AZktRs6DuF8oOsLt59YBU=", + "lastModified": 1769921679, + "narHash": "sha256-twBMKGQvaztZQxFxbZnkg7y/50BW9yjtCBWwdjtOZew=", "owner": "Mic92", "repo": "sops-nix", - "rev": "c5eebd4eb2e3372fe12a8d70a248a6ee9dd02eff", + "rev": "1e89149dcfc229e7e2ae24a8030f124a31e4f24f", "type": "github" }, "original": { diff --git a/hosts/andromache/default.nix b/hosts/andromache/default.nix index b7b67a4..afd758c 100644 --- a/hosts/andromache/default.nix +++ b/hosts/andromache/default.nix @@ -36,6 +36,7 @@ in ../../modules/ssh/hardened-openssh.nix (import ../../modules/secrets { inherit lib inputs config; }) ../../modules/docker + ../../modules/syncthing ]; home-manager.users.${username} = import ../../home/hosts/andromache { @@ -96,7 +97,6 @@ in inputs.colmena.packages.${pkgs.system}.colmena ]; - services = { xserver = { videoDrivers = [ "nvidia" ]; @@ -106,30 +106,33 @@ in enable = true; harden = true; }; - - syncthing = { - enable = true; - openDefaultPorts = true; - settings = { - devices = { - # "device1" = { - # id = "DEVICE-ID-GOES-HERE"; - # }; - }; - folders = { - "/home/${username}/sync" = { - id = "sync"; - devices = [ ]; - }; - }; - }; - }; locate = { enable = true; package = pkgs.plocate; }; }; + my.syncthing = { + enable = true; + deviceNames = [ + "boox" + "astyanax" + ]; + folders = { + readings = { + path = "/home/h/doc/readings"; + id = "readings"; + devices = [ + { + device = "boox"; + type = "receiveonly"; + } + "astyanax" + ]; + }; + }; + }; + networking = { # TODO: generate unique hostId on actual host with: head -c 8 /etc/machine-id hostId = "80eef97e"; diff --git a/hosts/astyanax/default.nix b/hosts/astyanax/default.nix index 9bb9303..93490d8 100644 --- a/hosts/astyanax/default.nix +++ b/hosts/astyanax/default.nix @@ -37,6 +37,7 @@ in # ../../modules/vpn/wireguard.nix (import ../../modules/secrets { inherit lib inputs config; }) ../../modules/docker + ../../modules/syncthing ]; home-manager.users.${username} = import ../../home/hosts/astyanax { @@ -99,21 +100,30 @@ in enable = true; harden = true; }; - syncthing = { - enable = true; - openDefaultPorts = true; - folders = { - "/home/h/sync" = { - id = "sync"; - devices = [ ]; - }; - }; - devices = { - # "device1" = { - # id = "DEVICE-ID-GOES-HERE"; - # }; + }; + + my.syncthing = { + enable = true; + deviceNames = [ + "boox" + "andromache" + ]; + folders = { + readings = { + path = "/home/h/doc/readings"; + id = "readings"; + devices = [ + { + device = "boox"; + type = "receiveonly"; + } + "andromache" + ]; }; }; + }; + + services = { locate = { enable = true; package = pkgs.plocate; diff --git a/modules/syncthing/default.nix b/modules/syncthing/default.nix new file mode 100644 index 0000000..ed8ce77 --- /dev/null +++ b/modules/syncthing/default.nix @@ -0,0 +1,85 @@ +{ + lib, + config, + ... +}: + +with lib; + +let + cfg = config.my.syncthing; + allDevices = import ./devices.nix; +in +{ + options.my.syncthing = { + enable = mkEnableOption "Syncthing file synchronization"; + username = mkOption { + type = types.str; + default = "h"; + }; + deviceNames = mkOption { + type = types.listOf types.str; + default = [ ]; + }; + folders = mkOption { + type = types.attrsOf ( + types.submodule { + options = { + path = mkOption { type = types.path; }; + id = mkOption { type = types.str; }; + devices = mkOption { + type = types.listOf ( + types.either types.str ( + types.submodule { + options = { + device = mkOption { type = types.str; }; + type = mkOption { + type = types.str; + default = "sendreceive"; + }; + }; + } + ) + ); + default = cfg.deviceNames; + }; + }; + } + ); + default = { }; + }; + }; + + config = mkIf cfg.enable { + users.groups.${cfg.username} = { }; + + services.syncthing = { + enable = true; + user = cfg.username; + group = cfg.username; + configDir = "/home/${cfg.username}/.local/state/syncthing"; + openDefaultPorts = true; + settings = { + options = { + localAnnounceEnabled = true; + globalAnnounceEnabled = true; + relaysEnabled = true; + urAccepted = -1; + }; + devices = mapAttrs (name: id: { inherit id; }) ( + filterAttrs (name: _: elem name cfg.deviceNames) allDevices + ); + folders = mapAttrs (name: folder: { + inherit (folder) id path; + devices = map ( + device: + if isString device then + allDevices.${device} + else + device // { deviceID = allDevices.${device.device}; } + ) folder.devices; + }) cfg.folders; + }; + }; + }; +} diff --git a/modules/syncthing/devices.nix b/modules/syncthing/devices.nix new file mode 100644 index 0000000..569e682 --- /dev/null +++ b/modules/syncthing/devices.nix @@ -0,0 +1,5 @@ +{ + boox = "7B5E6PT-HWRQ3WH-OLOSWAZ-T3WNHND-VKUREIJ-AD5I4EA-UJR4M5E-OR5COA7"; + andromache = "QNVYMOC-GAPIIF7-PKYBN22-PPNAWOF-UJWNUFD-6DXRAJA-AZSMPOS-ULMZWAU"; + astyanax = "CSSFKEO-2T5PAO5-HX43E4E-UKARH4I-6FVHP36-6YC5WXX-VNMHXD3-WY3QKA7"; +}