-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
173 lines (150 loc) · 5.27 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
{
description = "A simple DNS proxy server written in rust. Macchiato DNS contains some powerful features that can be used to secure your communications.";
inputs = {
nixpkgs.url = "nixpkgs/nixos-24.05";
nix-std.url = "github:chessai/nix-std";
};
outputs = {self, nixpkgs, nix-std, ...}: let
system = "x86_64-linux";
pname = "macchiato-dns";
std = nix-std.lib;
in {
packages.${system} = let
pkgs = nixpkgs.legacyPackages.${system};
in {
default = pkgs.rustPlatform.buildRustPackage {
name = pname;
version = "0.0.1";
src = ./.;
doCheck = false;
cargoLock.lockFile = ./Cargo.lock;
postInstall = ''
install ./1hosts_pro.txt $out/1hosts_pro.txt
cp -r ./static/ $out/static/
'';
};
};
nixosModules.default = { config , lib , pkgs , ... }: with lib; let
cfg = config.services.macchiato-dns;
configFile = if cfg.configurationPath != null then cfg.configurationPath else builtins.toFile "config.toml"
(std.serde.toTOML {
allowed_domains = cfg.allowedDomains;
auto_update = cfg.autoUpdate;
external = cfg.external;
filters_path = if cfg.filtersPath != null then cfg.filtersPath else (toString cfg.package);
filter_version = cfg.filterVersion;
overrides = cfg.overrides;
small = cfg.small;
verbosity = cfg.verbosity;
web_password = cfg.webPassword;
});
in {
options.services.macchiato-dns = {
enable = lib.mkEnableOption "enable the macchiato-dns service";
package = mkOption {
type = types.package;
default = self.packages.x86_64-linux.default;
description = "macchiato-dns package to use";
};
openFirewall = lib.mkOption {
type = types.bool;
default = false;
description = "Open port UDP 53 in the firewall";
};
openWebFirewall = lib.mkOption {
type = types.bool;
default = false;
description = "Open port TCP 5554 in the firewall";
};
configurationPath = lib.mkOption {
type = types.nullOr types.str;
default = null;
description = "Path to the configuration file. If is set, it overrides most other settings.";
};
allowedDomains = lib.mkOption {
type = types.listOf types.str;
default = [];
description = "List of allowed domains";
};
autoUpdate = lib.mkOption {
type = types.nullOr types.int;
default = null;
description = "Auto update interval in hours";
};
external = lib.mkOption {
type = types.bool;
default = true;
description = "Allow external connections to the server";
};
filtersPath = lib.mkOption {
type = types.nullOr types.str;
default = null;
description = "Path to the filters";
};
filterVersion = lib.mkOption {
type = types.str;
default = "OneHostsLite";
description = "Filter version";
};
overrides = lib.mkOption {
type = types.attrs;
default = {};
description = "List of domain overrides";
};
small = lib.mkOption {
type = types.bool;
default = true;
description = "Small";
};
verbosity = lib.mkOption {
type = types.int;
default = 0;
description = "Verbosity level";
};
webPassword = lib.mkOption {
type = types.nullOr types.str;
default = null;
description = "Web password";
};
};
config = lib.mkIf cfg.enable {
systemd.services.macchiato-dns = {
description = "A simple DNS proxy server written in rust. Macchiato DNS contains some powerful features that can be used to secure your communications.";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
ExecStart = "${cfg.package}/bin/dns --configuration ${configFile}";
Restart = "on-failure";
Type = "exec";
User = "macchiato-dns";
Group = "macchiato-dns";
UMask = "0077";
AmbientCapabilities = "CAP_NET_BIND_SERVICE";
WorkingDirectory = toString cfg.package;
# Security
ProtectHome = "read-only";
NoNewPrivileges = true;
SystemCallArchitectures = "native";
RestrictNamespaces = true;
RestrictRealtime = true;
RestrictSUIDSGID = true;
ProtectControlGroups = true;
ProtectHostname = true;
ProtectKernelLogs = true;
ProtectKernelModules = true;
ProtectKernelTunables = true;
LockPersonality = true;
PrivateTmp = true;
};
};
networking.firewall.allowedUDPPorts = mkIf cfg.openFirewall [ 53 ];
networking.firewall.allowedTCPPorts = mkIf cfg.openWebFirewall [ 5554 ];
users.users.macchiato-dns = {
group = "macchiato-dns";
isNormalUser = true;
};
users.groups.macchiato-dns = {};
};
};
};
}