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