fork/home-manager

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

Robert HelgessonMerge PR #421575790441

master
4.6 KiB165 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.services.syncthing;
8
9  defaultSyncthingArgs = [
10    "${pkgs.syncthing}/bin/syncthing"
11    "-no-browser"
12    "-no-restart"
13    "-logflags=0"
14  ];
15
16  syncthingArgs = defaultSyncthingArgs ++ cfg.extraOptions;
17
18in {
19  meta.maintainers = [ maintainers.rycee ];
20
21  options = {
22    services.syncthing = {
23      enable = mkEnableOption "Syncthing continuous file synchronization";
24
25      extraOptions = mkOption {
26        type = types.listOf types.str;
27        default = [ ];
28        example = [ "--gui-apikey=apiKey" ];
29        description = ''
30          Extra command-line arguments to pass to {command}`syncthing`.
31        '';
32      };
33
34      tray = mkOption {
35        type = with types;
36          either bool (submodule {
37            options = {
38              enable = mkOption {
39                type = types.bool;
40                default = false;
41                description = "Whether to enable a syncthing tray service.";
42              };
43
44              command = mkOption {
45                type = types.str;
46                default = "syncthingtray";
47                defaultText = literalExpression "syncthingtray";
48                example = literalExpression "qsyncthingtray";
49                description = "Syncthing tray command to use.";
50              };
51
52              package = mkOption {
53                type = types.package;
54                default = pkgs.syncthingtray-minimal;
55                defaultText = literalExpression "pkgs.syncthingtray-minimal";
56                example = literalExpression "pkgs.qsyncthingtray";
57                description = "Syncthing tray package to use.";
58              };
59            };
60          });
61        default = { enable = false; };
62        description = "Syncthing tray service configuration.";
63      };
64    };
65  };
66
67  config = mkMerge [
68    (mkIf cfg.enable {
69      home.packages = [ (getOutput "man" pkgs.syncthing) ];
70
71      systemd.user.services = {
72        syncthing = {
73          Unit = {
74            Description =
75              "Syncthing - Open Source Continuous File Synchronization";
76            Documentation = "man:syncthing(1)";
77            After = [ "network.target" ];
78          };
79
80          Service = {
81            ExecStart = escapeShellArgs syncthingArgs;
82            Restart = "on-failure";
83            SuccessExitStatus = [ 3 4 ];
84            RestartForceExitStatus = [ 3 4 ];
85
86            # Sandboxing.
87            LockPersonality = true;
88            MemoryDenyWriteExecute = true;
89            NoNewPrivileges = true;
90            PrivateUsers = true;
91            RestrictNamespaces = true;
92            SystemCallArchitectures = "native";
93            SystemCallFilter = "@system-service";
94          };
95
96          Install = { WantedBy = [ "default.target" ]; };
97        };
98      };
99
100      launchd.agents.syncthing = {
101        enable = true;
102        config = {
103          ProgramArguments = syncthingArgs;
104          KeepAlive = {
105            Crashed = true;
106            SuccessfulExit = false;
107          };
108          ProcessType = "Background";
109        };
110      };
111    })
112
113    (mkIf (isAttrs cfg.tray && cfg.tray.enable) {
114      assertions = [
115        (hm.assertions.assertPlatform "services.syncthing.tray" pkgs
116          platforms.linux)
117      ];
118
119      systemd.user.services = {
120        ${cfg.tray.package.pname} = {
121          Unit = {
122            Description = cfg.tray.package.pname;
123            Requires = [ "tray.target" ];
124            After = [ "graphical-session-pre.target" "tray.target" ];
125            PartOf = [ "graphical-session.target" ];
126          };
127
128          Service = {
129            ExecStart = "${cfg.tray.package}/bin/${cfg.tray.command}";
130          };
131
132          Install = { WantedBy = [ "graphical-session.target" ]; };
133        };
134      };
135    })
136
137    # deprecated
138    (mkIf (isBool cfg.tray && cfg.tray) {
139      assertions = [
140        (hm.assertions.assertPlatform "services.syncthing.tray" pkgs
141          platforms.linux)
142      ];
143
144      systemd.user.services = {
145        "syncthingtray" = {
146          Unit = {
147            Description = "syncthingtray";
148            Requires = [ "tray.target" ];
149            After = [ "graphical-session-pre.target" "tray.target" ];
150            PartOf = [ "graphical-session.target" ];
151          };
152
153          Service = {
154            ExecStart = "${pkgs.syncthingtray-minimal}/bin/syncthingtray";
155          };
156
157          Install = { WantedBy = [ "graphical-session.target" ]; };
158        };
159      };
160      warnings = [
161        "Specifying 'services.syncthing.tray' as a boolean is deprecated, set 'services.syncthing.tray.enable' instead. See https://github.com/nix-community/home-manager/pull/1257."
162      ];
163    })
164  ];
165}