11 cfg = config.services.unit;
13 configFile = pkgs.writeText "unit.json" cfg.config;
19 enable = mkEnableOption "Unit App Server";
20 package = mkPackageOption pkgs "unit" { };
24 description = "User account under which unit runs.";
29 description = "Group account under which unit runs.";
33 default = "/var/spool/unit";
34 description = "Unit data directory.";
38 default = "/var/log/unit";
39 description = "Unit log directory.";
53 "application": "example-php-72"
65 "file": "/etc/php.d/default.ini",
67 "max_execution_time": "30",
68 "max_input_time": "30",
69 "display_errors": "off",
70 "display_startup_errors": "off",
71 "open_basedir": "/dev/urandom:/proc/cpuinfo:/proc/meminfo:/etc/ssl/certs:/var/www",
72 "disable_functions": "exec,passthru,shell_exec,system"
79 description = "Unit configuration in JSON format. More details here https://unit.nginx.org/configuration";
84 config = mkIf cfg.enable {
86 environment.systemPackages = [ cfg.package ];
88 systemd.tmpfiles.rules = [
89 "d '${cfg.stateDir}' 0750 ${cfg.user} ${cfg.group} - -"
90 "d '${cfg.logDir}' 0750 ${cfg.user} ${cfg.group} - -"
93 systemd.services.unit = {
94 description = "Unit App Server";
95 after = [ "network.target" ];
96 wantedBy = [ "multi-user.target" ];
98 [ ! -e '${cfg.stateDir}/conf.json' ] || rm -f '${cfg.stateDir}/conf.json'
101 ${pkgs.curl}/bin/curl -X PUT --data-binary '@${configFile}' --unix-socket '/run/unit/control.unit.sock' 'http://localhost/config'
105 PIDFile = "/run/unit/unit.pid";
107 ${cfg.package}/bin/unitd --control 'unix:/run/unit/control.unit.sock' --pid '/run/unit/unit.pid' \
108 --log '${cfg.logDir}/unit.log' --statedir '${cfg.stateDir}' --tmpdir '/tmp' \
109 --user ${cfg.user} --group ${cfg.group}
112 ${pkgs.curl}/bin/curl -X DELETE --unix-socket '/run/unit/control.unit.sock' 'http://localhost/config'
114 # Runtime directory and mode
115 RuntimeDirectory = "unit";
116 RuntimeDirectoryMode = "0750";
117 # Access write directories
123 NoNewPrivileges = true;
125 ProtectSystem = "strict";
128 PrivateDevices = true;
129 PrivateUsers = false;
130 ProtectHostname = true;
132 ProtectKernelTunables = true;
133 ProtectKernelModules = true;
134 ProtectKernelLogs = true;
135 ProtectControlGroups = true;
136 RestrictAddressFamilies = [
141 LockPersonality = true;
142 MemoryDenyWriteExecute = true;
143 RestrictRealtime = true;
144 RestrictSUIDSGID = true;
145 PrivateMounts = true;
146 # System Call Filtering
147 SystemCallArchitectures = "native";
151 users.users = optionalAttrs (cfg.user == "unit") {
158 users.groups = optionalAttrs (cfg.group == "unit") {