nixos/rsyncd: convert module to an INI generator

This commit is contained in:
Emery Hemingway 2020-08-13 15:31:17 +02:00 committed by ehmry
parent b3f8642587
commit e7d0500cb3
3 changed files with 74 additions and 92 deletions

View file

@ -3,120 +3,76 @@
with lib;
let
cfg = config.services.rsyncd;
motdFile = builtins.toFile "rsyncd-motd" cfg.motd;
foreach = attrs: f:
concatStringsSep "\n" (mapAttrsToList f attrs);
cfgFile = ''
${optionalString (cfg.motd != "") "motd file = ${motdFile}"}
${optionalString (cfg.address != "") "address = ${cfg.address}"}
${optionalString (cfg.port != 873) "port = ${toString cfg.port}"}
${cfg.extraConfig}
${foreach cfg.modules (name: module: ''
[${name}]
${foreach module (k: v:
"${k} = ${v}"
)}
'')}
'';
in
{
settingsFormat = pkgs.formats.ini { };
configFile = settingsFormat.generate "rsyncd.conf" cfg.settings;
in {
options = {
services.rsyncd = {
enable = mkEnableOption "the rsync daemon";
motd = mkOption {
type = types.str;
default = "";
description = ''
Message of the day to display to clients on each connect.
This usually contains site information and any legal notices.
'';
};
port = mkOption {
default = 873;
type = types.int;
type = types.port;
description = "TCP port the daemon will listen on.";
};
address = mkOption {
default = "";
example = "192.168.1.2";
settings = mkOption {
inherit (settingsFormat) type;
default = { };
example = {
global = {
uid = "nobody";
gid = "nobody";
"use chroot" = true;
"max connections" = 4;
};
ftp = {
path = "/var/ftp/./pub";
comment = "whole ftp area";
};
cvs = {
path = "/data/cvs";
comment = "CVS repository (requires authentication)";
"auth users" = [ "tridge" "susan" ];
"secrets file" = "/etc/rsyncd.secrets";
};
};
description = ''
IP address the daemon will listen on; rsyncd will listen on
all addresses if this is not specified.
'';
};
extraConfig = mkOption {
type = types.lines;
default = "";
description = ''
Lines of configuration to add to rsyncd globally.
See <command>man rsyncd.conf</command> for options.
'';
};
modules = mkOption {
default = {};
description = ''
A set describing exported directories.
See <command>man rsyncd.conf</command> for options.
'';
type = types.attrsOf (types.attrsOf types.str);
example = literalExample ''
{ srv =
{ path = "/srv";
"read only" = "yes";
comment = "Public rsync share.";
};
}
'';
};
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.
Configuration for rsyncd. See
<citerefentry><refentrytitle>rsyncd.conf</refentrytitle>
<manvolnum>5</manvolnum></citerefentry>.
'';
};
};
};
###### implementation
imports = (map (option:
mkRemovedOptionModule [ "services" "rsyncd" option ]
"This option was removed in favor of `services.rsyncd.settings`.") [
"address"
"extraConfig"
"motd"
"user"
"group"
]);
config = mkIf cfg.enable {
environment.etc."rsyncd.conf".text = cfgFile;
services.rsyncd.settings.global.port = toString cfg.port;
systemd.services.rsyncd = {
description = "Rsync daemon";
wantedBy = [ "multi-user.target" ];
restartTriggers = [ config.environment.etc."rsyncd.conf".source ];
serviceConfig = {
ExecStart = "${pkgs.rsync}/bin/rsync --daemon --no-detach";
User = cfg.user;
Group = cfg.group;
};
serviceConfig.ExecStart =
"${pkgs.rsync}/bin/rsync --daemon --no-detach --config=${configFile}";
};
};
meta.maintainers = with lib.maintainers; [ ehmry ];
# TODO: socket activated rsyncd
}

25
nixos/tests/rsyncd.nix Normal file
View file

@ -0,0 +1,25 @@
import ./make-test-python.nix ({ pkgs, ... }: {
name = "rsyncd";
meta.maintainers = with pkgs.lib.maintainers; [ ehmry ];
nodes.machine.services.rsyncd = {
enable = true;
settings = {
global = {
"reverse lookup" = false;
"forward lookup" = false;
};
tmp = {
path = "/nix/store";
comment = "test module";
};
};
};
testScript = ''
start_all()
machine.wait_for_unit("rsyncd")
machine.succeed("rsync localhost::")
'';
})

View file

@ -1,7 +1,6 @@
{ stdenv, fetchurl, perl, libiconv, zlib, popt
, enableACLs ? !(stdenv.isDarwin || stdenv.isSunOS || stdenv.isFreeBSD), acl ? null
, enableCopyDevicesPatch ? false
}:
, enableACLs ? !(stdenv.isDarwin || stdenv.isSunOS || stdenv.isFreeBSD)
, acl ? null, enableCopyDevicesPatch ? false, nixosTests }:
assert enableACLs -> acl != null;
@ -23,6 +22,8 @@ stdenv.mkDerivation rec {
configureFlags = ["--with-nobody-group=nogroup"];
passthru.tests = { inherit (nixosTests) rsyncd; };
meta = base.meta // {
description = "A fast incremental file transfer utility";
maintainers = with stdenv.lib.maintainers; [ peti ehmry kampfschlaefer ];