1 { config, lib, pkgs, ... }:
6 cfg = config.services.opensnitch;
7 format = pkgs.formats.json {};
9 predefinedRules = flip mapAttrs cfg.rules (name: cfg: {
10 file = pkgs.writeText "rule" (builtins.toJSON cfg);
15 services.opensnitch = {
16 enable = mkEnableOption "Opensnitch application firewall";
20 example = literalExpression ''
26 "duration" = "always";
30 "operand" = "process.path";
31 "data" = "''${lib.getBin pkgs.tor}/bin/tor";
38 Declarative configuration of firewall rules.
39 All rules will be stored in `/var/lib/opensnitch/rules` by default.
40 Rules path can be configured with `settings.Rules.Path`.
41 See [upstream documentation](https://github.com/evilsocket/opensnitch/wiki/Rules)
42 for available options.
45 type = types.submodule {
46 freeformType = format.type;
51 type = types.submodule {
52 freeformType = format.type;
60 Unix socket path (unix:///tmp/osui.sock, the "unix:///" part is
61 mandatory) or TCP socket (192.168.1.100:50051).
68 File to write logs to (use /dev/stdout to write logs to standard
75 DefaultAction = mkOption {
76 type = types.enum [ "allow" "deny" ];
78 Default action whether to block or allow application internet
83 InterceptUnknown = mkOption {
86 Whether to intercept spare connections.
90 ProcMonitorMethod = mkOption {
91 type = types.enum [ "ebpf" "proc" "ftrace" "audit" ];
93 Which process monitoring method to use.
98 type = types.enum [ 0 1 2 3 4 ];
100 Default log level from 0 to 4 (debug, info, important, warning,
105 Firewall = mkOption {
106 type = types.enum [ "iptables" "nftables" ];
108 Which firewall backend to use.
114 MaxEvents = mkOption {
117 Max events to send to the GUI.
121 MaxStats = mkOption {
124 Max stats per item to keep in backlog.
130 Ebpf.ModulesPath = mkOption {
132 default = if cfg.settings.ProcMonitorMethod == "ebpf" then "${config.boot.kernelPackages.opensnitch-ebpf}/etc/opensnitchd" else null;
133 defaultText = literalExpression ''
134 if cfg.settings.ProcMonitorMethod == "ebpf" then
135 "\\$\\{config.boot.kernelPackages.opensnitch-ebpf\\}/etc/opensnitchd"
139 Configure eBPF modules path. Used when
140 `settings.ProcMonitorMethod` is set to `ebpf`.
144 Rules.Path = mkOption {
146 default = "/var/lib/opensnitch/rules";
148 Path to the directory where firewall rules can be found and will
149 get stored by the NixOS module.
156 opensnitchd configuration. Refer to [upstream documentation](https://github.com/evilsocket/opensnitch/wiki/Configurations)
157 for details on supported values.
163 config = mkIf cfg.enable {
165 # pkg.opensnitch is referred to elsewhere in the module so we don't need to worry about it being garbage collected
166 services.opensnitch.settings = mapAttrs (_: v: mkDefault v) (builtins.fromJSON (builtins.unsafeDiscardStringContext (builtins.readFile "${pkgs.opensnitch}/etc/opensnitchd/default-config.json")));
169 packages = [ pkgs.opensnitch ];
170 services.opensnitchd = {
171 wantedBy = [ "multi-user.target" ];
175 "${pkgs.opensnitch}/bin/opensnitchd --config-file ${format.generate "default-config.json" cfg.settings}"
178 preStart = mkIf (cfg.rules != {}) (let
179 rules = flip mapAttrsToList predefinedRules (file: content: {
180 inherit (content) file;
181 local = "${cfg.settings.Rules.Path}/${file}.json";
184 # Remove all firewall rules from rules path (configured with
185 # cfg.settings.Rules.Path) that are symlinks to a store-path, but aren't
186 # declared in `cfg.rules` (i.e. all networks that were "removed" from
188 find ${cfg.settings.Rules.Path} -type l -lname '${builtins.storeDir}/*' ${optionalString (rules != {}) ''
189 -not \( ${concatMapStringsSep " -o " ({ local, ... }:
190 "-name '${baseNameOf local}*'")
193 ${concatMapStrings ({ file, local }: ''
194 ln -sf '${file}' "${local}"
199 "d ${cfg.settings.Rules.Path} 0750 root root - -"
200 "L+ /etc/opensnitchd/system-fw.json - - - - ${pkgs.opensnitch}/etc/opensnitchd/system-fw.json"
206 meta.maintainers = with lib.maintainers; [ onny ];