services.xray: pass the settings file with systemd loadCredential

It enables passing a sops-nix secret as a `settingsFile`
@see https://github.com/Mic92/sops-nix/issues/198.

By default sops-nix secrets are accessible by only root. We can change owner to another user, but the xray service is defined with `dynamicUser=true`, which means, there is no user in the compile time.

Systemd `loadCredential` passes the secret file to the service, which is exactly what we need here.
This commit is contained in:
Slava
2025-01-20 15:52:14 +05:00
committed by Slava
parent 3f4d074319
commit 167d6634f8
+4 -1
View File
@@ -80,9 +80,12 @@ with lib;
description = "xray Daemon";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
script = ''
exec "${cfg.package}/bin/xray" -config "$CREDENTIALS_DIRECTORY/config.json"
'';
serviceConfig = {
DynamicUser = true;
ExecStart = "${cfg.package}/bin/xray -config ${settingsFile}";
LoadCredential = "config.json:${settingsFile}";
CapabilityBoundingSet = "CAP_NET_ADMIN CAP_NET_BIND_SERVICE";
AmbientCapabilities = "CAP_NET_ADMIN CAP_NET_BIND_SERVICE";
NoNewPrivileges = true;