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