Merge pull request #27993 from Nadrieril/rsync-run-as-user

rsync service: allow running as user (plus some tweaks)

Changed files
+35 -17
nixos
modules
services
network-filesystems
+35 -17
nixos/modules/services/network-filesystems/rsyncd.nix
···
motdFile = builtins.toFile "rsyncd-motd" cfg.motd;
-
moduleConfig = name:
-
let module = getAttr name cfg.modules; in
-
"[${name}]\n " + (toString (
-
map
-
(key: "${key} = ${toString (getAttr key module)}\n")
-
(attrNames module)
-
));
+
foreach = attrs: f:
+
concatStringsSep "\n" (mapAttrsToList f attrs);
-
cfgFile = builtins.toFile "rsyncd.conf"
-
''
+
cfgFile = ''
${optionalString (cfg.motd != "") "motd file = ${motdFile}"}
${optionalString (cfg.address != "") "address = ${cfg.address}"}
${optionalString (cfg.port != 873) "port = ${toString cfg.port}"}
${cfg.extraConfig}
-
${toString (map moduleConfig (attrNames cfg.modules))}
-
'';
+
${foreach cfg.modules (name: module: ''
+
[${name}]
+
${foreach module (k: v:
+
"${k} = ${v}"
+
)}
+
'')}
+
'';
in
{
···
};
};
+
user = mkOption {
+
type = types.str;
+
default = "root";
+
description = ''
+
The user to run the daemon as.
+
By default the daemon runs as root.
+
'';
+
};
+
+
group = mkOption {
+
type = types.str;
+
default = "root";
+
description = ''
+
The group to run the daemon as.
+
By default the daemon runs as root.
+
'';
+
};
+
};
};
···
config = mkIf cfg.enable {
-
environment.etc = singleton {
-
source = cfgFile;
-
target = "rsyncd.conf";
-
};
+
environment.etc."rsyncd.conf".text = cfgFile;
systemd.services.rsyncd = {
description = "Rsync daemon";
wantedBy = [ "multi-user.target" ];
-
serviceConfig.ExecStart = "${pkgs.rsync}/bin/rsync --daemon --no-detach";
+
restartTriggers = [ config.environment.etc."rsyncd.conf".source ];
+
serviceConfig = {
+
ExecStart = "${pkgs.rsync}/bin/rsync --daemon --no-detach";
+
User = cfg.user;
+
Group = cfg.group;
+
};
};
-
};
}