Skip to content

Commit bc26911

Browse files
nixos/nginx-sso: allow using file-based secrets (NixOS#325838)
2 parents a3e92c3 + 58a1a61 commit bc26911

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

nixos/modules/services/security/nginx-sso.nix

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
{ config, lib, pkgs, ... }:
1+
{ config, lib, pkgs, utils, ... }:
22

33
with lib;
44

55
let
66
cfg = config.services.nginx.sso;
7-
pkg = getBin cfg.package;
8-
configYml = pkgs.writeText "nginx-sso.yml" (builtins.toJSON cfg.configuration);
7+
format = pkgs.formats.yaml { };
8+
configPath = "/var/lib/nginx-sso/config.yaml";
99
in {
1010
options.services.nginx.sso = {
1111
enable = mkEnableOption "nginx-sso service";
1212

1313
package = mkPackageOption pkgs "nginx-sso" { };
1414

1515
configuration = mkOption {
16-
type = types.attrsOf types.unspecified;
16+
type = format.type;
1717
default = {};
1818
example = literalExpression ''
1919
{
2020
listen = { addr = "127.0.0.1"; port = 8080; };
2121
2222
providers.token.tokens = {
23-
myuser = "MyToken";
23+
myuser = {
24+
_secret = "/path/to/secret/token.txt"; # File content should be the secret token
25+
};
2426
};
2527
2628
acl = {
@@ -37,6 +39,11 @@ in {
3739
nginx-sso configuration
3840
([documentation](https://github.com/Luzifer/nginx-sso/wiki/Main-Configuration))
3941
as a Nix attribute set.
42+
43+
Options containing secret data should be set to an attribute set
44+
with the singleton attribute `_secret` - a string value set to the path
45+
to the file containing the secret value which should be used in the
46+
configuration. This file must be readable by `nginx-sso`.
4047
'';
4148
};
4249
};
@@ -47,14 +54,29 @@ in {
4754
after = [ "network.target" ];
4855
wantedBy = [ "multi-user.target" ];
4956
serviceConfig = {
57+
StateDirectory = "nginx-sso";
58+
WorkingDirectory = "/var/lib/nginx-sso";
59+
ExecStartPre = pkgs.writeShellScript "merge-nginx-sso-config" ''
60+
rm -f '${configPath}'
61+
# Relies on YAML being a superset of JSON
62+
${utils.genJqSecretsReplacementSnippet cfg.configuration configPath}
63+
'';
5064
ExecStart = ''
51-
${pkg}/bin/nginx-sso \
52-
--config ${configYml} \
53-
--frontend-dir ${pkg}/share/frontend
65+
${lib.getExe cfg.package} \
66+
--config ${configPath} \
67+
--frontend-dir ${lib.getBin cfg.package}/share/frontend
5468
'';
5569
Restart = "always";
56-
DynamicUser = true;
70+
User = "nginx-sso";
71+
Group = "nginx-sso";
5772
};
5873
};
74+
75+
users.users.nginx-sso = {
76+
isSystemUser = true;
77+
group = "nginx-sso";
78+
};
79+
80+
users.groups.nginx-sso = { };
5981
};
6082
}

nixos/tests/nginx-sso.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import ./make-test-python.nix ({ pkgs, ... }: {
1111
listen = { addr = "127.0.0.1"; port = 8080; };
1212

1313
providers.token.tokens = {
14-
myuser = "MyToken";
14+
myuser = {
15+
_secret = pkgs.writeText "secret-token" "MyToken";
16+
};
1517
};
1618

1719
acl = {

0 commit comments

Comments
 (0)