forked from
lass.pds.lassul.us/superconfig
This repository has no description
2.7 kB
101 lines
1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7let
8
9 shares = {
10 "/home/lass/sync" = [
11 "mors"
12 "green"
13 "coaxmetal"
14 "ignavia"
15 ]; # TODO add aergia
16 "/home/lass/tmp/the_playlist" = [
17 "mors"
18 "radio"
19 ];
20 "/home/lass/decsync" = [
21 "mors"
22 "green"
23 "massulus"
24 "massulus2"
25 ];
26 };
27
28 machine_peers = lib.foldr (
29 host: acc:
30 let
31 syncthing_id_path =
32 config.clan.core.settings.directory + "/vars/per-machine/${host}/syncthing/syncthing.pub/value";
33 in
34 if builtins.pathExists syncthing_id_path then
35 acc
36 // {
37 ${host}.id = lib.removeSuffix "\n" (builtins.readFile syncthing_id_path);
38 }
39 else
40 acc
41 ) { } (lib.attrNames (builtins.readDir ../machines));
42
43 all_peers = machine_peers // {
44 "massulus".id = "R2EGJ5S-PQMETUP-C2UGXQG-A6VP7TB-NGSN3MV-C7OGSWT-SZ34L3X-H6IF6AQ";
45 "massulus2".id = "OTJBJNO-VHBCVDL-QSELUS7-S6JDMYR-7WLE36A-6XSRSLP-VKYOYJJ-AHFGZAG";
46 };
47
48 used_peer_names = lib.unique (
49 lib.filter lib.isString (
50 lib.flatten (lib.mapAttrsToList (_n: v: v.devices) config.services.syncthing.settings.folders)
51 )
52 );
53 used_peers = lib.filterAttrs (n: _v: lib.elem n used_peer_names) all_peers;
54in
55{
56 services.syncthing = {
57 enable = true;
58 group = "syncthing";
59 configDir = "/var/lib/syncthing";
60 key = config.clan.core.vars.generators.syncthing.files."syncthing.key".path;
61 cert = config.clan.core.vars.generators.syncthing.files."syncthing.cert".path;
62 settings.devices = used_peers;
63 settings.folders = lib.mapAttrs (share: devices: {
64 enable = lib.elem config.networking.hostName devices;
65 path = share;
66 ignorePerms = false;
67 versioning = {
68 type = "trashcan";
69 params = {
70 cleanoutDays = "30";
71 };
72 };
73 # Filter devices to only include those that exist in all_peers
74 devices = lib.filter (device: all_peers ? ${device}) devices;
75 }) shares;
76 user = "lass";
77 };
78
79 clan.core.vars.generators.syncthing = {
80 files."syncthing.key" = { };
81 files."syncthing.cert" = { };
82 files."syncthing.pub".secret = false;
83 runtimeInputs = with pkgs; [
84 coreutils
85 gnugrep
86 syncthing
87 ];
88 script = ''
89 syncthing generate --config "$out"
90 mv "$out"/key.pem "$out"/syncthing.key
91 mv "$out"/cert.pem "$out"/syncthing.cert
92 cat "$out"/config.xml | grep -oP '(?<=<device id=")[^"]+' | uniq > "$out"/syncthing.pub
93 '';
94 };
95
96 boot.kernel.sysctl."fs.inotify.max_user_watches" = 524288;
97 networking.firewall = {
98 allowedTCPPorts = [ 22000 ];
99 allowedUDPPorts = [ 21027 ];
100 };
101}