nixos/ntfy: add environmentFile option for secrets (#441304)

This commit is contained in:
Franz Pletz
2025-09-09 11:50:41 +02:00
committed by GitHub
2 changed files with 46 additions and 19 deletions
+13
View File
@@ -61,6 +61,18 @@ in
Configuration for ntfy.sh, supported values are [here](https://ntfy.sh/docs/config/#config-options).
'';
};
environmentFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
example = "/run/secrets/ntfy";
description = ''
Path to a file containing extra ntfy environment variables in the systemd `EnvironmentFile`
format. Refer to the [documentation](https://docs.ntfy.sh/config/) for config options.
This can be used to pass secrets such as creating declarative users or token without putting them in the Nix store.
'';
};
};
config =
@@ -109,6 +121,7 @@ in
MemoryDenyWriteExecute = true;
# Upstream Recommendation
LimitNOFILE = 20500;
EnvironmentFile = lib.mkIf (cfg.environmentFile != null) cfg.environmentFile;
};
};
+33 -19
View File
@@ -1,28 +1,42 @@
import ./make-test-python.nix {
name = "ntfy-sh";
import ./make-test-python.nix (
{ pkgs, ... }:
{
name = "ntfy-sh";
nodes.machine =
{ ... }:
{
services.ntfy-sh.enable = true;
services.ntfy-sh.settings.base-url = "http://localhost:2586";
};
nodes.machine =
{ ... }:
{
services.ntfy-sh.enable = true;
services.ntfy-sh.settings.base-url = "http://localhost:2586";
testScript = ''
import json
# Create a user with user:123
services.ntfy-sh.environmentFile = pkgs.writeText "ntfy.env" ''
NTFY_AUTH_DEFAULT_ACCESS='deny-all'
NTFY_AUTH_USERS='user:$2a$12$W2v7IQhkayvJOYRpg6YEruxj.jUO3R2xQOU7s1vC3HzLLB9gSKJ9.:user'
NTFY_AUTH_ACCESS='user:test:rw'
'';
};
msg = "Test notification"
testScript = ''
import json
machine.wait_for_unit("multi-user.target")
msg = "Test notification"
machine.wait_for_open_port(2586)
machine.wait_for_unit("multi-user.target")
machine.succeed(f"curl -d '{msg}' localhost:2586/test")
machine.wait_for_open_port(2586)
notif = json.loads(machine.succeed("curl -s localhost:2586/test/json?poll=1"))
machine.succeed(f"curl -u user:1234 -d '{msg}' localhost:2586/test")
assert msg == notif["message"], "Wrong message"
# If we have a user, receive a message
notif = json.loads(machine.succeed("curl -u user:1234 -s localhost:2586/test/json?poll=1"))
assert msg == notif["message"], "Wrong message"
machine.succeed("ntfy user list")
'';
}
# If we have no user, we should get forbidden, making sure the default access config works
notif = json.loads(machine.succeed("curl -s localhost:2586/test/json?poll=1"))
assert 403 == notif["http"], f"Should return 403, got {notif["http"]}"
machine.succeed("ntfy user list")
'';
}
)