1{ config, lib, pkgs, ... }:
2let
3 cfg = config.services.onedrive;
4
5 onedriveLauncher = pkgs.writeShellScriptBin
6 "onedrive-launcher"
7 ''
8 # XDG_CONFIG_HOME is not recognized in the environment here.
9 if [ -f $HOME/.config/onedrive-launcher ]
10 then
11 # Hopefully using underscore boundary helps locate variables
12 for _onedrive_config_dirname_ in $(cat $HOME/.config/onedrive-launcher | grep -v '[ \t]*#' )
13 do
14 systemctl --user start onedrive@$_onedrive_config_dirname_
15 done
16 else
17 systemctl --user start onedrive@onedrive
18 fi
19 ''
20 ;
21
22in {
23 ### Documentation
24 # meta.doc = ./onedrive.xml;
25
26 ### Interface
27
28 options.services.onedrive = {
29 enable = lib.mkOption {
30 type = lib.types.bool;
31 default = false;
32 description = "Enable OneDrive service";
33 };
34
35 package = lib.mkOption {
36 type = lib.types.package;
37 default = pkgs.onedrive;
38 defaultText = "pkgs.onedrive";
39 example = lib.literalExample "pkgs.onedrive";
40 description = ''
41 OneDrive package to use.
42 '';
43 };
44 };
45### Implementation
46
47 config = lib.mkIf cfg.enable {
48 environment.systemPackages = [ cfg.package ];
49
50 systemd.user.services."onedrive@" = {
51 description = "Onedrive sync service";
52
53 serviceConfig = {
54 Type = "simple";
55 ExecStart = ''
56 ${cfg.package}/bin/onedrive --monitor --confdir=%h/.config/%i
57 '';
58 Restart="on-failure";
59 RestartSec=3;
60 RestartPreventExitStatus=3;
61 };
62 };
63
64 systemd.user.services.onedrive-launcher = {
65 wantedBy = [ "default.target" ];
66 serviceConfig = {
67 Type = "oneshot";
68 ExecStart = "${onedriveLauncher}/bin/onedrive-launcher";
69 };
70 };
71 };
72}