{ config, pkgs, lib, ... }: let cfg = config.services.nncp; settingsFormat = pkgs.formats.json { }; in { options.services.nncp = { daemon = { enable = lib.mkEnableOption "NNCP Daemon"; port = lib.mkOption { type = lib.types.port; }; }; caller = { enable = lib.mkEnableOption "NNCP Caller"; }; configFile = lib.mkOption { type = lib.types.path; default = settingsFormat.generate "nncp.hjson" cfg.settings; }; dataDir = lib.mkOption { type = lib.types.path; default = "/var/lib/nncp"; }; user = lib.mkOption { type = lib.types.str; default = "nncp"; }; group = lib.mkOption { type = lib.types.str; default = "nncp"; }; settings = lib.mkOption { }; }; config = { systemd.tmpfiles.rules = lib.mkIf (cfg.daemon.enable || cfg.caller.enable) [ "d '${cfg.dataDir}' 0770 ${cfg.user} ${cfg.group}" "Z '${cfg.dataDir}' 0770 ${cfg.user} ${cfg.group}" "d '/var/spool/nncp' 0770 ${cfg.user} ${cfg.group}" "Z '/var/spool/nncp' 0770 ${cfg.user} ${cfg.group}" ]; systemd.paths."nncp-watcher" = lib.mkIf (cfg.daemon.enable || cfg.caller.enable) { wantedBy = [ "multi-user.target" ]; pathConfig = { PathModified = "/run/keys/nncp"; Unit = "nncp-reloader.service"; }; }; systemd.services."nncp-reloader" = lib.mkIf (cfg.daemon.enable || cfg.caller.enable) { after = [ "networking.target" ]; script = '' exec ${pkgs.nncp}/bin/nncp-cfgdir -cfg ${cfg.configFile} -dump ${cfg.dataDir} exec systemd-tmpfiles --create ''; serviceConfig = { Type = "oneshot"; WorkingDirectory = "${cfg.dataDir}"; }; }; systemd.services.nncp-daemon = lib.mkIf cfg.daemon.enable { # wantedBy = [ "multi-user.target" ]; after = [ "nncp-reloader.service" ]; description = "Node to Node Copy Protocol Daemon"; serviceConfig = { # DynamicUser = true; User = cfg.user; Group = cfg.group; ExecStart = ''${pkgs.nncp}/bin/nncp-daemon -autotoss -noprogress -bind "0.0.0.0:${toString cfg.daemon.port}" -cfg ${cfg.dataDir}''; Restart = "always"; Type = "simple"; RestartSec = 1; StateDirectory = "nncp"; WorkingDirectory = "${cfg.dataDir}"; }; }; systemd.services.nncp-caller = lib.mkIf cfg.caller.enable { # wantedBy = [ "multi-user.target" ]; after = [ "nncp-reloader.service" ]; description = "Node to Node Copy Protocol Caller"; serviceConfig = { # DynamicUser = true; User = cfg.user; Group = cfg.group; ExecStart = ''${pkgs.nncp}/bin/nncp-caller -autotoss -noprogress -cfg ${cfg.dataDir}''; Restart = "always"; Type = "simple"; RestartSec = 1; StateDirectory = "nncp"; WorkingDirectory = "${cfg.dataDir}"; }; }; users.users = lib.mkIf (cfg.user == "nncp") { nncp = { description = "NNCP daemon user"; isSystemUser = true; group = cfg.group; home = cfg.dataDir; }; }; users.groups = lib.mkIf (cfg.group == "nncp") { nncp = { }; }; }; }