Skip to content

Commit 3e985b4

Browse files
committed
services/pocket-id: init
- add service for pocket-id (https://pocket-id.org) - basic example - exemple with caddy used as reverse proxy
1 parent c570189 commit 3e985b4

File tree

5 files changed

+175
-0
lines changed

5 files changed

+175
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
set -euxo pipefail
3+
4+
env | grep UNIX_SOCKET
5+
env | grep TRUST_PROXY
6+
curl -sf "http://localhost/setup"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{ pkgs, config, ... }:
2+
{
3+
packages = [ pkgs.curl ]; # used for the test
4+
5+
services = {
6+
pocket-id = {
7+
enable = true;
8+
package = pkgs.pocket-id;
9+
10+
disable_analytics = true;
11+
12+
# Use caddy to expose pocket-id to the network
13+
app_url = "http://localhost";
14+
use_unix_socket = true;
15+
};
16+
17+
caddy = {
18+
enable = true;
19+
virtualHosts = {
20+
"localhost:80".extraConfig = ''
21+
reverse_proxy unix/${config.env.DEVENV_RUNTIME}/pocket-id.sock
22+
'';
23+
};
24+
25+
};
26+
};
27+
}

examples/pocket-id/.test.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
set -euxo pipefail
3+
4+
env | grep ANALYTICS_DISABLED
5+
6+
wait_for_port 1234
7+
curl -sf "http://127.0.0.1:1234/setup"

examples/pocket-id/devenv.nix

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{ pkgs, config, ... }:
2+
{
3+
packages = [ pkgs.curl ]; # used for the test
4+
5+
services = {
6+
pocket-id = {
7+
enable = true;
8+
package = pkgs.pocket-id;
9+
10+
disable_analytics = true;
11+
env.PORT = "1234";
12+
};
13+
};
14+
}

src/modules/services/pocket-id.nix

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
{ pkgs, config, lib, ... }:
2+
let
3+
cfg = config.services.pocket-id;
4+
types = lib.types;
5+
pocket-id-storage = config.env.DEVENV_STATE + "/pocket-id";
6+
in
7+
{
8+
options.services.pocket-id = {
9+
enable = lib.mkEnableOption "Pocket ID server, an OIDC provider. [pocket-id.org](https://pocket-id.org)";
10+
11+
package = lib.mkOption {
12+
type = types.package;
13+
default = pkgs.pocket-id;
14+
defaultText = lib.literalExpression "pkgs.pocket-id";
15+
description = "The pocket-id package to use.";
16+
};
17+
18+
app_url = lib.mkOption {
19+
type = types.str;
20+
default = "http://localhost:1411";
21+
description = ''
22+
Specifies the connection string used to connect to the database.
23+
24+
This will set the environment variable `APP_URL`.
25+
'';
26+
};
27+
28+
disable_analytics = lib.mkOption {
29+
type = types.bool;
30+
description = ''
31+
Disable heartbeat that gets sent every 24 hours to count how many Pocket ID instances are running.
32+
33+
See [docs page](https://pocket-id.org/docs/configuration/analytics/).
34+
35+
This will set the environment variable `ANALYTICS_DISABLED`.
36+
'';
37+
default = false;
38+
};
39+
40+
disable_geolite = lib.mkOption {
41+
type = types.bool;
42+
default = false;
43+
description = ''
44+
Disable usage of GeoLite by setting the download URL for the GeoLite database to an empty string.
45+
46+
This will set the environment variable `GEOLITE_DB_URL` with an empty string.
47+
'';
48+
};
49+
50+
reverse_proxy = lib.mkOption {
51+
type = types.bool;
52+
default = false;
53+
description = ''
54+
Whether the app is behind a reverse proxy.
55+
56+
This will set the environment variable `TRUST_PROXY`.
57+
'';
58+
};
59+
60+
use_unix_socket = lib.mkOption {
61+
type = types.bool;
62+
default = false;
63+
description = ''
64+
Make pocket-id listen to a UNIX socket instead of TCP. The socket will be located at `$DEVENV_RUNTIME/pocket-id.sock`.
65+
66+
This will set the `UNIX_SOCKET` environment variable with the socket location. Pocket ID will ignore the environment variables `HOST` and `PORT`.
67+
68+
Additionally, the option `reverse_proxy` will be set to `true`.
69+
'';
70+
};
71+
72+
disable_ui_configuration = lib.mkOption {
73+
type = types.bool;
74+
default = false;
75+
description = ''
76+
Disable the ability to configure the UI through the web client. Customization will be done exclusively through environment variables.
77+
78+
This will set the environment variable `UI_CONFIG_DISABLED`.
79+
'';
80+
};
81+
82+
env = lib.mkOption {
83+
type = types.attrsOf types.str;
84+
default = { };
85+
description = ''
86+
Additional environment variables for pocket-id.
87+
88+
See [list of all variables](https://pocket-id.org/docs/configuration/environment-variables).
89+
'';
90+
};
91+
92+
};
93+
94+
config = lib.mkIf cfg.enable {
95+
packages = [ cfg.package ];
96+
97+
env = {
98+
ANALYTICS_DISABLED = if cfg.disable_analytics then "true" else null;
99+
100+
APP_URL = cfg.app_url;
101+
102+
DB_CONNECTION_STRING = "file:${pocket-id-storage}/pocket-id.db";
103+
UPLOAD_PATH = "${pocket-id-storage}/uploads";
104+
KEYS_PATH = "${pocket-id-storage}/keys";
105+
GEOLITE_DB_PATH = "${pocket-id-storage}/GeoLite2-City.mmdb";
106+
107+
UNIX_SOCKET = if cfg.use_unix_socket then "${config.env.DEVENV_RUNTIME}/pocket-id.sock" else null;
108+
TRUST_PROXY = if cfg.use_unix_socket or cfg.reverse_proxy then "true" else null;
109+
110+
GEOLITE_DB_URL = if cfg.disable_geolite then "" else null;
111+
112+
UI_CONFIG_DISABLED = if cfg.disable_ui_configuration then "true" else null;
113+
} // cfg.env;
114+
115+
processes.pocket-id.exec = ''
116+
mkdir -p ${pocket-id-storage}
117+
exec "${cfg.package}/bin/pocket-id"
118+
'';
119+
};
120+
121+
}

0 commit comments

Comments
 (0)