blob: 13535cb745ca8f55351c317255171b7423c85b2c (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
{
lib,
config,
nixosConfig,
sysLib,
pkgs,
...
}: let
unisonPath = "${config.xdg.dataHome}/unison";
# These are only used for the script
unisonOptions = {
sshcmd = "ssh";
ui = "text";
auto = "true";
# This is useless, with hm links
links = "false";
};
paths_to_keep = [
"~/.local/state/mpv"
"~/.local/state/nvim"
"~/.local/share"
"~/.local/.Trash-1000"
"~/.mozilla/.Trash-1000"
"~/.mozilla/firefox"
"~/media"
"~/school"
"~/repos"
];
paths_to_ignore = [
# already synchronized by the taskserver
"~/.local/share/task"
# Should not be synchronized
"~/.local/share/unison"
];
hostName = let
hn = nixosConfig.networking.hostName;
in
if hn == "tiamat"
then "apzu"
else if hn == "apzu"
then "tiamat"
else builtins.throw "Host (${hn}) not yet covered in the unison host mapping.";
unitName = name: builtins.replaceStrings ["/"] ["-"] name;
mkPath = path:
if lib.strings.hasPrefix "~" path
then "${builtins.elemAt (builtins.attrNames config.home.persistence)
0}${lib.strings.removePrefix "~" path}"
else
builtins.throw
"Every pathname needs to start with a '~'";
mkPair = pathname: let
path = mkPath pathname;
in {
name = unitName "${pathname}";
value = {
stateDirectory = unisonPath;
roots = [
"${path}"
"ssh://${config.home.username}@${hostName}.fritz.box/${path}"
];
};
};
getIgnoredSingle = path: path_to_ignore: let
clean_path_to_ignore = mkPath path_to_ignore;
commonPath = builtins.substring 0 (builtins.stringLength path) clean_path_to_ignore;
in
if commonPath == path
then let
preFinalPath =
builtins.substring (builtins.stringLength commonPath)
(builtins.stringLength clean_path_to_ignore)
clean_path_to_ignore;
finalPath =
if lib.strings.hasPrefix "/" preFinalPath
then lib.strings.removePrefix "/" preFinalPath
else preFinalPath;
in "BelowPath ${finalPath}"
else null;
getIgnored = paths_to_ignore: path:
serialiseArgs {
ignore =
builtins.filter (x: x != null) (builtins.map (getIgnoredSingle path) paths_to_ignore);
};
serialiseArg = key: val:
if builtins.typeOf val == "string"
then lib.strings.escapeShellArg "-${key}=${lib.strings.escape ["="] val}"
else if builtins.typeOf val == "list"
then lib.strings.concatStringsSep " " (builtins.map (serialiseArg key) val)
else builtins.throw "Unsupported type: ${builtins.typeOf val}";
serialiseArgs = args:
lib.strings.concatStringsSep " " (
lib.attrsets.mapAttrsToList
serialiseArg
args
);
esa = a: lib.strings.escapeShellArg a;
mkScriptLine = pathname: let
path =
mkPath pathname;
in
lib.strings.concatStringsSep " " [
"unison"
"${serialiseArgs unisonOptions}"
"$EXTRA_OPTIONS"
"${getIgnored paths_to_ignore path}"
"${esa path}"
(esa "ssh://${config.home.username}@${hostName}.fritz.box/${path}")
];
script = lib.strings.concatStringsSep "\n" (builtins.map mkScriptLine paths_to_keep);
pairs = builtins.listToAttrs (builtins.map mkPair paths_to_keep);
in {
home.sessionVariables = {
UNISON = unisonPath;
};
home.packages = [
pkgs.unison
(sysLib.writeShellScript {
name = "unison-sync";
src = builtins.toFile "unison-backup" (''
#!/usr/bin/env dash
# shellcheck source=/dev/null
SHELL_LIBRARY_VERSION="2.1.1" . %SHELL_LIBRARY_PATH
export UNISON=${esa unisonPath};
if [ "$1" = "links" ]; then
EXTRA_OPTIONS="-links=true";
fi
EXTRA_OPTIONS="$EXTRA_OPTIONS $*"
''
+ script);
dependencies = with pkgs; [unison openssh];
})
];
services.unison = {
enable = false;
inherit pairs;
};
}
|