Files
nixfiles/nixos/modules/pocket-id/default.nix
T
jackos1998andClaude Opus 5.5 eeb1f2d15c
CI / Check, build and cache nixfiles (push) Canceled after 25m17s
Update docs / update (push) Failing after 1m53s
middleman: Replace nginx-sso with Pocket ID SSO
Gate the vhosts without their own sign-in with oauth2-proxy, which
signs in with Pocket ID and allows members of the `admins` group.
Each gated vhost serves its own `/oauth2/` endpoints with a host-only
session cookie, so other `.nul.ie` sites never receive it. Sessions
last a week, and a refresh token lets oauth2-proxy check back with
Pocket ID every hour without a passkey.

Add `my.pocket-id`, which declares Pocket ID groups and OIDC clients
in Nix; `pocket-id-sync` applies them through the API with a static
API key that nginx keeps from outside requests. This lets apps move
to Pocket ID with their client defined next to them, with secrets
from `slop secret generate`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 00:15:06 +00:00

103 lines
3.8 KiB
Nix

{ lib, pkgs, config, ... }:
let
inherit (lib) mkIf mapAttrsToList filterAttrs;
inherit (lib.my) mkOpt' mkBoolOpt';
cfg = config.my.pocket-id;
pocketId = config.services.pocket-id;
groupOpts = { name, ... }: {
options = with lib.types; {
friendlyName = mkOpt' str name "Name shown in Pocket ID's UI.";
};
};
clientOpts = { name, ... }: {
options = with lib.types; {
name = mkOpt' str name "Name shown to users when they sign in.";
callbackURLs = mkOpt' (listOf str) [ ] "Allowed redirect URLs after sign-in.";
logoutCallbackURLs = mkOpt' (listOf str) [ ] "Allowed redirect URLs after sign-out.";
launchURL = mkOpt' (nullOr str) null "URL that Pocket ID's app list links to.";
public = mkBoolOpt' false "Whether this is a public client (no secret).";
pkce = mkBoolOpt' false "Whether to require PKCE.";
allowedGroups = mkOpt' (listOf str) [ ]
"Pocket ID groups (by name) whose members may sign in; any user may when empty.";
secret = {
file = mkOpt' (nullOr path) null "File holding the client secret.";
key = mkOpt' (nullOr str) null
"Read the secret from this key of an env file instead of the whole file.";
};
extraSettings = mkOpt' (attrsOf unspecified) { }
"Extra fields of Pocket ID's client API object (e.g. `skipConsent`).";
};
};
spec = pkgs.writeText "pocket-id-sync.json" (builtins.toJSON {
groups = mapAttrsToList (n: g: { name = n; inherit (g) friendlyName; }) cfg.groups;
clients = mapAttrsToList (id: c: {
inherit id;
groups = c.allowedGroups;
secret = c.secret.file != null;
secretKey = c.secret.key;
settings = {
inherit (c) name callbackURLs logoutCallbackURLs launchURL;
isPublic = c.public;
pkceEnabled = c.pkce;
isGroupRestricted = c.allowedGroups != [ ];
} // c.extraSettings;
}) cfg.clients;
});
sync = pkgs.writers.writePython3 "pocket-id-sync" {
flakeIgnore = [ "E501" ];
} (builtins.readFile ./sync.py);
in
{
options.my.pocket-id = with lib.types; {
enable = mkBoolOpt' false "Whether to manage Pocket ID groups and OIDC clients declaratively.";
apiKeyFile = mkOpt' path null "File holding Pocket ID's static API key (at least 16 characters).";
groups = mkOpt' (attrsOf (submodule groupOpts)) { }
"User groups to create; membership is managed in Pocket ID.";
clients = mkOpt' (attrsOf (submodule clientOpts)) { } "OIDC clients, by client ID.";
};
config = mkIf cfg.enable {
assertions = mapAttrsToList (id: c: {
assertion = c.public == (c.secret.file == null);
message = "my.pocket-id.clients.${id}: a client needs a secret unless it is public.";
}) cfg.clients;
services.pocket-id = {
enable = true;
credentials.STATIC_API_KEY = cfg.apiKeyFile;
};
systemd.services.pocket-id-sync = {
description = "Pocket ID groups and OIDC clients";
requires = [ "pocket-id.service" ];
after = [ "pocket-id.service" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${sync} ${spec} http://127.0.0.1:${toString pocketId.settings.PORT}";
DynamicUser = true;
RuntimeDirectory = "pocket-id-sync";
StateDirectory = "pocket-id-sync";
LoadCredential = [ "api-key:${cfg.apiKeyFile}" ]
++ mapAttrsToList (id: c: "client-${id}:${c.secret.file}")
(filterAttrs (_: c: c.secret.file != null) cfg.clients);
CapabilityBoundingSet = "";
NoNewPrivileges = true;
PrivateDevices = true;
PrivateTmp = true;
ProtectHome = true;
ProtectSystem = "strict";
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
};
};
};
}