2021-09-22 15:38:15 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.opensnitch;
|
2022-03-05 16:20:37 +00:00
|
|
|
format = pkgs.formats.json {};
|
2022-12-02 08:20:57 +00:00
|
|
|
|
|
|
|
predefinedRules = flip mapAttrs cfg.rules (name: cfg: {
|
|
|
|
file = pkgs.writeText "rule" (builtins.toJSON cfg);
|
|
|
|
});
|
|
|
|
|
2021-09-22 15:38:15 +00:00
|
|
|
in {
|
|
|
|
options = {
|
|
|
|
services.opensnitch = {
|
2024-04-21 15:54:59 +00:00
|
|
|
enable = mkEnableOption "Opensnitch application firewall";
|
2022-12-02 08:20:57 +00:00
|
|
|
|
|
|
|
rules = mkOption {
|
|
|
|
default = {};
|
|
|
|
example = literalExpression ''
|
|
|
|
{
|
|
|
|
"tor" = {
|
|
|
|
"name" = "tor";
|
|
|
|
"enabled" = true;
|
|
|
|
"action" = "allow";
|
|
|
|
"duration" = "always";
|
|
|
|
"operator" = {
|
|
|
|
"type" ="simple";
|
|
|
|
"sensitive" = false;
|
|
|
|
"operand" = "process.path";
|
|
|
|
"data" = "''${lib.getBin pkgs.tor}/bin/tor";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
'';
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-12-02 08:20:57 +00:00
|
|
|
Declarative configuration of firewall rules.
|
2024-02-29 20:09:43 +00:00
|
|
|
All rules will be stored in `/var/lib/opensnitch/rules` by default.
|
|
|
|
Rules path can be configured with `settings.Rules.Path`.
|
2022-12-02 08:20:57 +00:00
|
|
|
See [upstream documentation](https://github.com/evilsocket/opensnitch/wiki/Rules)
|
|
|
|
for available options.
|
|
|
|
'';
|
|
|
|
|
|
|
|
type = types.submodule {
|
|
|
|
freeformType = format.type;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-03-05 16:20:37 +00:00
|
|
|
settings = mkOption {
|
|
|
|
type = types.submodule {
|
|
|
|
freeformType = format.type;
|
|
|
|
|
|
|
|
options = {
|
|
|
|
Server = {
|
|
|
|
|
|
|
|
Address = mkOption {
|
|
|
|
type = types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-03-05 16:20:37 +00:00
|
|
|
Unix socket path (unix:///tmp/osui.sock, the "unix:///" part is
|
|
|
|
mandatory) or TCP socket (192.168.1.100:50051).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
LogFile = mkOption {
|
|
|
|
type = types.path;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-03-05 16:20:37 +00:00
|
|
|
File to write logs to (use /dev/stdout to write logs to standard
|
|
|
|
output).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
DefaultAction = mkOption {
|
|
|
|
type = types.enum [ "allow" "deny" ];
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-03-05 16:20:37 +00:00
|
|
|
Default action whether to block or allow application internet
|
|
|
|
access.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
InterceptUnknown = mkOption {
|
|
|
|
type = types.bool;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-12-17 10:02:37 +00:00
|
|
|
Whether to intercept spare connections.
|
2022-03-05 16:20:37 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
ProcMonitorMethod = mkOption {
|
|
|
|
type = types.enum [ "ebpf" "proc" "ftrace" "audit" ];
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-03-05 16:20:37 +00:00
|
|
|
Which process monitoring method to use.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
LogLevel = mkOption {
|
|
|
|
type = types.enum [ 0 1 2 3 4 ];
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-03-05 16:20:37 +00:00
|
|
|
Default log level from 0 to 4 (debug, info, important, warning,
|
|
|
|
error).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
Firewall = mkOption {
|
|
|
|
type = types.enum [ "iptables" "nftables" ];
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-03-05 16:20:37 +00:00
|
|
|
Which firewall backend to use.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
Stats = {
|
|
|
|
|
|
|
|
MaxEvents = mkOption {
|
|
|
|
type = types.int;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-03-05 16:20:37 +00:00
|
|
|
Max events to send to the GUI.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
MaxStats = mkOption {
|
|
|
|
type = types.int;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-03-05 16:20:37 +00:00
|
|
|
Max stats per item to keep in backlog.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
2024-02-29 20:09:43 +00:00
|
|
|
|
|
|
|
Ebpf.ModulesPath = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = if cfg.settings.ProcMonitorMethod == "ebpf" then "${config.boot.kernelPackages.opensnitch-ebpf}/etc/opensnitchd" else null;
|
|
|
|
defaultText = literalExpression ''
|
|
|
|
if cfg.settings.ProcMonitorMethod == "ebpf" then
|
|
|
|
"\\$\\{config.boot.kernelPackages.opensnitch-ebpf\\}/etc/opensnitchd"
|
|
|
|
else null;
|
|
|
|
'';
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2024-02-29 20:09:43 +00:00
|
|
|
Configure eBPF modules path. Used when
|
|
|
|
`settings.ProcMonitorMethod` is set to `ebpf`.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
Rules.Path = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/var/lib/opensnitch/rules";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2024-02-29 20:09:43 +00:00
|
|
|
Path to the directory where firewall rules can be found and will
|
|
|
|
get stored by the NixOS module.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-03-05 16:20:37 +00:00
|
|
|
};
|
|
|
|
};
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-12-02 08:20:57 +00:00
|
|
|
opensnitchd configuration. Refer to [upstream documentation](https://github.com/evilsocket/opensnitch/wiki/Configurations)
|
2022-03-05 16:20:37 +00:00
|
|
|
for details on supported values.
|
|
|
|
'';
|
|
|
|
};
|
2021-09-22 15:38:15 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
2022-03-05 16:20:37 +00:00
|
|
|
# pkg.opensnitch is referred to elsewhere in the module so we don't need to worry about it being garbage collected
|
2023-08-22 20:05:09 +00:00
|
|
|
services.opensnitch.settings = mapAttrs (_: v: mkDefault v) (builtins.fromJSON (builtins.unsafeDiscardStringContext (builtins.readFile "${pkgs.opensnitch}/etc/opensnitchd/default-config.json")));
|
2022-03-05 16:20:37 +00:00
|
|
|
|
2021-09-22 15:38:15 +00:00
|
|
|
systemd = {
|
|
|
|
packages = [ pkgs.opensnitch ];
|
2024-02-29 20:09:43 +00:00
|
|
|
services.opensnitchd = {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = [
|
|
|
|
""
|
|
|
|
"${pkgs.opensnitch}/bin/opensnitchd --config-file ${format.generate "default-config.json" cfg.settings}"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
preStart = mkIf (cfg.rules != {}) (let
|
|
|
|
rules = flip mapAttrsToList predefinedRules (file: content: {
|
|
|
|
inherit (content) file;
|
|
|
|
local = "${cfg.settings.Rules.Path}/${file}.json";
|
|
|
|
});
|
|
|
|
in ''
|
|
|
|
# Remove all firewall rules from rules path (configured with
|
|
|
|
# cfg.settings.Rules.Path) that are symlinks to a store-path, but aren't
|
|
|
|
# declared in `cfg.rules` (i.e. all networks that were "removed" from
|
|
|
|
# `cfg.rules`).
|
|
|
|
find ${cfg.settings.Rules.Path} -type l -lname '${builtins.storeDir}/*' ${optionalString (rules != {}) ''
|
|
|
|
-not \( ${concatMapStringsSep " -o " ({ local, ... }:
|
|
|
|
"-name '${baseNameOf local}*'")
|
|
|
|
rules} \) \
|
|
|
|
''} -delete
|
|
|
|
${concatMapStrings ({ file, local }: ''
|
|
|
|
ln -sf '${file}' "${local}"
|
|
|
|
'') rules}
|
|
|
|
'');
|
|
|
|
};
|
|
|
|
tmpfiles.rules = [
|
|
|
|
"d ${cfg.settings.Rules.Path} 0750 root root - -"
|
|
|
|
"L+ /etc/opensnitchd/system-fw.json - - - - ${pkgs.opensnitch}/etc/opensnitchd/system-fw.json"
|
|
|
|
];
|
2021-09-22 15:38:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
2024-02-29 20:09:43 +00:00
|
|
|
|
|
|
|
meta.maintainers = with lib.maintainers; [ onny ];
|
2021-09-22 15:38:15 +00:00
|
|
|
}
|
|
|
|
|