1 { config, lib, pkgs, ... }:
6 cfg = config.services.aria2;
8 homeDir = "/var/lib/aria2";
10 settingsDir = "${homeDir}";
11 sessionFile = "${homeDir}/aria2.session";
12 downloadDir = "${homeDir}/Downloads";
14 rangesToStringList = map (x: builtins.toString x.from +"-"+ builtins.toString x.to);
16 settingsFile = pkgs.writeText "aria2.conf"
18 dir=${cfg.downloadDir}
19 listen-port=${concatStringsSep "," (rangesToStringList cfg.listenPortRange)}
20 rpc-listen-port=${toString cfg.rpcListenPort}
21 rpc-secret=${cfg.rpcSecret}
31 description = lib.mdDoc ''
32 Whether or not to enable the headless Aria2 daemon service.
34 Aria2 daemon can be controlled via the RPC interface using
35 one of many WebUI (http://localhost:6800/ by default).
37 Targets are downloaded to ${downloadDir} by default and are
38 accessible to users in the "aria2" group.
41 openPorts = mkOption {
44 description = lib.mdDoc ''
45 Open listen and RPC ports found in listenPortRange and rpcListenPort
46 options in the firewall.
49 downloadDir = mkOption {
51 default = downloadDir;
52 description = lib.mdDoc ''
53 Directory to store downloaded files.
56 listenPortRange = mkOption {
57 type = types.listOf types.attrs;
58 default = [ { from = 6881; to = 6999; } ];
59 description = lib.mdDoc ''
60 Set UDP listening port range used by DHT(IPv4, IPv6) and UDP tracker.
63 rpcListenPort = mkOption {
66 description = lib.mdDoc "Specify a port number for JSON-RPC/XML-RPC server to listen to. Possible Values: 1024-65535";
68 rpcSecret = mkOption {
71 description = lib.mdDoc ''
72 Set RPC secret authorization token.
73 Read https://aria2.github.io/manual/en/html/aria2c.html#rpc-auth to know how this option value is used.
76 extraArguments = mkOption {
77 type = types.separatedString " ";
78 example = "--rpc-listen-all --remote-time=true";
80 description = lib.mdDoc ''
81 Additional arguments to be passed to Aria2.
87 config = mkIf cfg.enable {
89 # Need to open ports for proper functioning
90 networking.firewall = mkIf cfg.openPorts {
91 allowedUDPPortRanges = config.services.aria2.listenPortRange;
92 allowedTCPPorts = [ config.services.aria2.rpcListenPort ];
97 uid = config.ids.uids.aria2;
98 description = "aria2 user";
103 users.groups.aria2.gid = config.ids.gids.aria2;
105 systemd.tmpfiles.rules = [
106 "d '${homeDir}' 0770 aria2 aria2 - -"
107 "d '${config.services.aria2.downloadDir}' 0770 aria2 aria2 - -"
110 systemd.services.aria2 = {
111 description = "aria2 Service";
112 after = [ "network.target" ];
113 wantedBy = [ "multi-user.target" ];
115 if [[ ! -e "${sessionFile}" ]]
117 touch "${sessionFile}"
119 cp -f "${settingsFile}" "${settingsDir}/aria2.conf"
123 Restart = "on-abort";
124 ExecStart = "${pkgs.aria2}/bin/aria2c --enable-rpc --conf-path=${settingsDir}/aria2.conf ${config.services.aria2.extraArguments} --save-session=${sessionFile}";
125 ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";