Skip to content

Commit

Permalink
nixos/modules/clash: refactor rules under services.clash (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc authored Dec 27, 2023
2 parents 5b83370 + 51a6ed0 commit 013cae3
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 93 deletions.
2 changes: 1 addition & 1 deletion nixos/configurations/adrastea/networking.nix
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ in

services.clash = {
enable = true;
configPath = config.sops.templates."clash-config.yaml".path;
rule.enable = true;
};
}
2 changes: 1 addition & 1 deletion nixos/configurations/aplaz/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

services.clash = {
enable = true;
configPath = config.sops.templates."clash-config.yaml".path;
rule.enable = true;
};

services.resolved.enable = true;
Expand Down
3 changes: 2 additions & 1 deletion nixos/configurations/metis/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

services.clash = {
enable = true;
configPath = config.sops.templates."clash-config.yaml".path;
rule.enable = true;
rule.enableTUN = true;
};

inclyc.user.enable = true;
Expand Down
115 changes: 76 additions & 39 deletions nixos/modules/clash/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
with lib;
let
cfg = config.services.clash;
defaultUser = "clash";
in
{
options.services.clash = {
Expand All @@ -26,59 +25,97 @@ in
configPath = mkOption {
type = types.path;
};
user = mkOption {
default = defaultUser;
example = "john";
type = types.str;
};

group = mkOption {
default = defaultUser;
example = "users";
type = types.str;
rule = {
enable = mkEnableOption "clash rule generation";
enableTUN = mkEnableOption "TUN interface";
};
};
config =
mkIf cfg.enable {

users.users = optionalAttrs (cfg.user == defaultUser) {
${defaultUser} =
{
description = "clash user";
group = defaultUser;
uid = config.ids.uids.znc;
isSystemUser = true;
};
};

users.groups = optionalAttrs (cfg.user == defaultUser) {
${defaultUser} =
{
gid = config.ids.gids.znc;
members = [ defaultUser ];
};
};

config = lib.mkMerge [
(mkIf cfg.enable {
systemd.services.clash = {

wantedBy = [ "multi-user.target" ];
after = [ "systemd-networkd-wait-online.service" ];
description = "Clash Daemon";

serviceConfig = rec {
Type = "simple";
User = cfg.user;
Group = cfg.group;
PrivateTmp = true;
DynamicUser = "yes";
LoadCredential = "config.yaml:${cfg.configPath}";
WorkingDirectory = "${cfg.workingDirectory}";
ExecStartPre = "${pkgs.coreutils}/bin/ln -s ${pkgs.clash-geoip}/etc/clash/Country.mmdb ${cfg.configDirectory}";
ExecStart = "${lib.getExe cfg.package}"
+ " -d ${cfg.configDirectory}"
+ " -f ${cfg.configPath}";
+ " -f %d/config.yaml";
Restart = "on-failure";
CapabilityBoundingSet = [ "CAP_NET_ADMIN" "CAP_NET_RAW" "CAP_NET_BIND_SERVICE" ];
AmbientCapabilities = CapabilityBoundingSet;
ProtectSystem = "strict";
ProtectHome = "yes";
PrivateDevices = "yes";
PrivateUsers = "yes";
ProtectHostname = "yes";
ProtectClock = "yes";
ProtectKernelTunables = "yes";
ProtectKernelModules = "yes";
ProtectKernelLogs = "yes";
ProtectControlGroups = "yes";
ProtectProc = "yes";
LockPersonality = "yes";
};
};
};
})
(mkIf cfg.rule.enable (
let
generate_204 = "http://www.gstatic.com/generate_204";
providers = [ "dler" "mielink" "bywave" ];
proxyProviders = lib.genAttrs providers (name: {
type = "http";
path = "./${name}.yaml";
url = "${config.sops.placeholder."clash-provider/${name}"}";
interval = 3600;
health-check = {
enable = true;
url = generate_204;
interval = 300;
};
});
proxyGroups = [
{
name = "Proxy";
type = "select";
use = providers;
proxies = [
"Auto"
"DIRECT"
];
}
{
name = "Auto";
type = "url-test";
use = providers;
proxies = [ "DIRECT" ];
url = generate_204;
interval = "3600";
}
] ++ builtins.fromJSON (builtins.readFile ./proxy-groups.json);
in
{
services.clash.configPath = lib.mkDefault config.sops.templates."clash-config.yaml".path;
sops.secrets = lib.attrsets.mergeAttrsList (map
(name: {
"clash-provider/${name}" = { };
})
providers);
sops.templates."clash-config.yaml".content = builtins.readFile ./rule.yaml + ''
proxy-groups: ${builtins.toJSON proxyGroups}
proxy-providers: ${builtins.toJSON proxyProviders}
'' + (lib.optionalString cfg.rule.enableTUN ''
tun:
enable: true
stack: system
auto-route: true
auto-detect-interface: true
'');
}
))
];
}
50 changes: 0 additions & 50 deletions nixos/modules/clash/rule.nix

This file was deleted.

1 change: 0 additions & 1 deletion nixos/modules/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
imports = [
inputs.home-manager.nixosModules.home-manager
./clash
./clash/rule.nix
./nix.nix
./ddns.nix
./nix-ld.nix
Expand Down

0 comments on commit 013cae3

Please sign in to comment.