fork/home-manager

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

pacienunison: add package option6dfbdc97

master
3.3 KiB133 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.services.unison;
8
9  pairOf = t:
10    let list = types.addCheck (types.listOf t) (l: length l == 2);
11    in list // { description = list.description + " of length 2"; };
12
13  pairOptions = {
14    options = {
15      stateDirectory = mkOption {
16        type = types.path;
17        default = "${config.xdg.dataHome}/unison";
18        defaultText = "$XDG_DATA_HOME/unison";
19        description = ''
20          Unison state directory to use.
21        '';
22      };
23
24      commandOptions = mkOption rec {
25        type = with types; attrsOf (either str (listOf str));
26        apply = mergeAttrs default;
27        default = {
28          repeat = "watch";
29          sshcmd = "${pkgs.openssh}/bin/ssh";
30          ui = "text";
31          auto = "true";
32          batch = "true";
33          log = "false"; # don't log to file, handled by systemd
34        };
35        description = ''
36          Additional command line options as a dictionary to pass to the
37          `unison` program.
38
39          Use a list of strings to declare the same option multiple times.
40
41          See
42          {manpage}`unison(1)`
43          for a list of available options.
44        '';
45      };
46
47      roots = mkOption {
48        type = pairOf types.str;
49        example = literalExpression ''
50          [
51            "/home/user/documents"
52            "ssh://remote/documents"
53          ]
54        '';
55        description = ''
56          Pair of roots to synchronise.
57        '';
58      };
59    };
60  };
61
62  serialiseArg = key: val:
63    concatStringsSep " "
64    (forEach (toList val) (x: escapeShellArg "-${key}=${escape [ "=" ] x}"));
65
66  serialiseArgs = args: concatStringsSep " " (mapAttrsToList serialiseArg args);
67
68  makeDefs = gen:
69    mapAttrs'
70    (name: pairCfg: nameValuePair "unison-pair-${name}" (gen name pairCfg))
71    cfg.pairs;
72
73in {
74  meta.maintainers = with maintainers; [ pacien ];
75
76  options.services.unison = {
77    enable = mkEnableOption "Unison synchronisation";
78
79    package = mkPackageOption pkgs "unison" {
80      example = "pkgs.unison.override { enableX11 = false; }";
81    };
82
83    pairs = mkOption {
84      type = with types; attrsOf (submodule pairOptions);
85      default = { };
86      example = literalExpression ''
87        {
88          "my-documents" = {
89            roots = [
90              "/home/user/documents"
91              "ssh://remote/documents"
92            ];
93          };
94        }
95      '';
96      description = ''
97        Unison root pairs to keep synchronised.
98      '';
99    };
100  };
101
102  config = mkIf cfg.enable {
103    assertions = [
104      (lib.hm.assertions.assertPlatform "services.unison" pkgs
105        lib.platforms.linux)
106    ];
107
108    systemd.user.services = makeDefs (name: pairCfg: {
109      Unit = {
110        Description = "Unison pair sync (${name})";
111        # Retry forever, useful in case of network disruption.
112        StartLimitIntervalSec = 0;
113      };
114
115      Service = {
116        Restart = "always";
117        RestartSec = 60;
118
119        CPUSchedulingPolicy = "idle";
120        IOSchedulingClass = "idle";
121
122        Environment = [ "UNISON='${toString pairCfg.stateDirectory}'" ];
123        ExecStart = ''
124          ${cfg.package}/bin/unison \
125            ${serialiseArgs pairCfg.commandOptions} \
126            ${strings.concatMapStringsSep " " escapeShellArg pairCfg.roots}
127        '';
128      };
129
130      Install = { WantedBy = [ "default.target" ]; };
131    });
132  };
133}