1{ config, pkgs, lib, ... }:
2
3with lib;
4
5let
6
7 cfg = config.services.yandex-disk;
8
9 dir = "/var/lib/yandex-disk";
10
11 u = if cfg.user != null then cfg.user else "yandexdisk";
12
13in
14
15{
16
17 ###### interface
18
19 options = {
20
21 services.yandex-disk = {
22
23 enable = mkOption {
24 default = false;
25 description = "
26 Whether to enable Yandex-disk client. See https://disk.yandex.ru/
27 ";
28 };
29
30 username = mkOption {
31 default = "";
32 type = types.string;
33 description = ''
34 Your yandex.com login name.
35 '';
36 };
37
38 password = mkOption {
39 default = "";
40 type = types.string;
41 description = ''
42 Your yandex.com password. Warning: it will be world-readable in /nix/store.
43 '';
44 };
45
46 user = mkOption {
47 default = null;
48 description = ''
49 The user the yandex-disk daemon should run as.
50 '';
51 };
52
53 directory = mkOption {
54 default = "/home/Yandex.Disk";
55 description = "The directory to use for Yandex.Disk storage";
56 };
57
58 excludes = mkOption {
59 default = "";
60 type = types.string;
61 example = "data,backup";
62 description = ''
63 Comma-separated list of directories which are excluded from synchronization.
64 '';
65 };
66
67 };
68
69 };
70
71
72 ###### implementation
73
74 config = mkIf cfg.enable {
75
76 users.users = mkIf (cfg.user == null) [ {
77 name = u;
78 uid = config.ids.uids.yandexdisk;
79 group = "nogroup";
80 home = dir;
81 } ];
82
83 systemd.services.yandex-disk = {
84 description = "Yandex-disk server";
85
86 after = [ "network.target" ];
87
88 wantedBy = [ "multi-user.target" ];
89
90 # FIXME: have to specify ${directory} here as well
91 unitConfig.RequiresMountsFor = dir;
92
93 script = ''
94 mkdir -p -m 700 ${dir}
95 chown ${u} ${dir}
96
97 if ! test -d "${cfg.directory}" ; then
98 (mkdir -p -m 755 ${cfg.directory} && chown ${u} ${cfg.directory}) ||
99 exit 1
100 fi
101
102 ${pkgs.su}/bin/su -s ${pkgs.runtimeShell} ${u} \
103 -c '${pkgs.yandex-disk}/bin/yandex-disk token -p ${cfg.password} ${cfg.username} ${dir}/token'
104
105 ${pkgs.su}/bin/su -s ${pkgs.runtimeShell} ${u} \
106 -c '${pkgs.yandex-disk}/bin/yandex-disk start --no-daemon -a ${dir}/token -d ${cfg.directory} --exclude-dirs=${cfg.excludes}'
107 '';
108
109 };
110 };
111
112}
113