summary refs log tree commit diff
path: root/nixos/modules/services/networking/onedrive.nix
blob: 0256a6a41115293cc25cef19f6b8af1941d77ed5 (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
{ config, lib, pkgs, ... }:
let
  cfg = config.services.onedrive;

  onedriveLauncher =  pkgs.writeShellScriptBin
    "onedrive-launcher"
    ''
      # XDG_CONFIG_HOME is not recognized in the environment here.
      if [ -f $HOME/.config/onedrive-launcher ]
      then
        # Hopefully using underscore boundary helps locate variables
        for _onedrive_config_dirname_ in $(cat $HOME/.config/onedrive-launcher | grep -v '[ \t]*#' )
        do
          systemctl --user start onedrive@$_onedrive_config_dirname_
        done
      else
        systemctl --user start onedrive@onedrive
      fi
    ''
  ;

in {
  ### Documentation
  # meta.doc = ./onedrive.xml;

  ### Interface

  options.services.onedrive = {
    enable = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = "Enable OneDrive service";
    };

     package = lib.mkOption {
       type = lib.types.package;
       default = pkgs.onedrive;
       defaultText = lib.literalExpression "pkgs.onedrive";
       description = ''
         OneDrive package to use.
       '';
     };
  };
### Implementation

  config = lib.mkIf cfg.enable {
    environment.systemPackages = [ cfg.package ];

    systemd.user.services."onedrive@" = {
      description = "Onedrive sync service";

      serviceConfig = {
        Type = "simple";
        ExecStart = ''
          ${cfg.package}/bin/onedrive --monitor --confdir=%h/.config/%i
        '';
        Restart="on-failure";
        RestartSec=3;
        RestartPreventExitStatus=3;
      };
    };

    systemd.user.services.onedrive-launcher = {
      wantedBy = [ "default.target" ];
      serviceConfig = {
        Type = "oneshot";
        ExecStart = "${onedriveLauncher}/bin/onedrive-launcher";
      };
    };
  };
}