fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge branch 'literalExpression' [#2371]4fa1ba72

master
2.3 KiB82 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.services.dropbox;
8  baseDir = ".dropbox-hm";
9  dropboxCmd = "${pkgs.dropbox-cli}/bin/dropbox";
10  homeBaseDir = "${config.home.homeDirectory}/${baseDir}";
11
12in {
13  meta.maintainers = [ maintainers.eyjhb ];
14
15  options = {
16    services.dropbox = {
17      enable = mkEnableOption "Dropbox daemon";
18
19      path = mkOption {
20        type = types.path;
21        default = "${config.home.homeDirectory}/Dropbox";
22        defaultText =
23          literalExpression ''"''${config.home.homeDirectory}/Dropbox"'';
24        apply = toString; # Prevent copies to Nix store.
25        description = "Where to put the Dropbox directory.";
26      };
27    };
28  };
29
30  config = mkIf cfg.enable {
31    assertions = [
32      (lib.hm.assertions.assertPlatform "services.dropbox" pkgs
33        lib.platforms.linux)
34    ];
35
36    home.packages = [ pkgs.dropbox-cli ];
37
38    systemd.user.services.dropbox = {
39      Unit = { Description = "dropbox"; };
40
41      Install = { WantedBy = [ "default.target" ]; };
42
43      Service = {
44        Environment = [ "HOME=${homeBaseDir}" "DISPLAY=" ];
45
46        Type = "forking";
47        PIDFile = "${homeBaseDir}/.dropbox/dropbox.pid";
48
49        Restart = "on-failure";
50        PrivateTmp = true;
51        ProtectSystem = "full";
52        Nice = 10;
53
54        ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
55        ExecStop = "${dropboxCmd} stop";
56        ExecStart = toString (pkgs.writeShellScript "dropbox-start" ''
57          # ensure we have the dirs we need
58          $DRY_RUN_CMD ${pkgs.coreutils}/bin/mkdir $VERBOSE_ARG -p \
59            ${homeBaseDir}/{.dropbox,.dropbox-dist,Dropbox}
60
61          # symlink them as needed
62          if [[ ! -d ${config.home.homeDirectory}/.dropbox ]]; then
63            $DRY_RUN_CMD ${pkgs.coreutils}/bin/ln $VERBOSE_ARG -s \
64              ${homeBaseDir}/.dropbox ${config.home.homeDirectory}/.dropbox
65          fi
66
67          if [[ ! -d ${escapeShellArg cfg.path} ]]; then
68            $DRY_RUN_CMD ${pkgs.coreutils}/bin/ln $VERBOSE_ARG -s \
69              ${homeBaseDir}/Dropbox ${escapeShellArg cfg.path}
70          fi
71
72          # get the dropbox bins if needed
73          if [[ ! -f $HOME/.dropbox-dist/VERSION ]]; then
74            ${pkgs.coreutils}/bin/yes | ${dropboxCmd} update
75          fi
76
77          ${dropboxCmd} start
78        '');
79      };
80    };
81  };
82}