Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fastapi-dls: init #358647

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions nixos/modules/module-list.nix
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,7 @@
./services/misc/etesync-dav.nix
./services/misc/evdevremapkeys.nix
./services/misc/evremap.nix
./services/misc/fastapi-dls.nix
./services/misc/felix.nix
./services/misc/flaresolverr.nix
./services/misc/forgejo.nix
Expand Down
133 changes: 133 additions & 0 deletions nixos/modules/services/misc/fastapi-dls.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.fastapi-dls;
stateDir = "/var/lib/fastapi-dls";
dls_privkey = "${stateDir}/instance.private.pem";
dls_pubkey = "${stateDir}/instance.public.pem";
https_privkey = "${stateDir}/webserver.key";
https_cert = "${stateDir}/webserver.crt";
in
{
##### interface
options = {
services.fastapi-dls = {
enable = lib.mkEnableOption "fastapi-dls";

package = lib.mkOption {
type = lib.types.package;
default = pkgs.fastapi-dls;
defaultText = lib.literalExpression "pkgs.fastapi-dls";
description = "Which package to use for the fastapi-dls daemon.";
};

listenAddress = lib.mkOption {
type = lib.types.str;
default = "127.0.0.1";
description = "The IP address on which `fastapi-dls` listens.";
};

listenPort = lib.mkOption {
type = lib.types.port;
default = 443;
description = "The port on which `fastapi-dls` listens.";
};

dlsAddress = lib.mkOption {
type = lib.types.str;
default = cfg.listenAddress;
defaultText = lib.literalExpression "services.fastapi-dls.listenAddress";
description = ''
The HTTPS domain name that DLS clients should connect to.
Useful when you put `fastapi-dls` behind a reverse proxy.
'';
};

dlsPort = lib.mkOption {
type = lib.types.port;
default = cfg.listenPort;
defaultText = lib.literalExpression "services.fastapi-dls.listenPort";
description = "The port that DLS clients should connect to.";
};

openFirewall = lib.mkEnableOption "opening the firewall for `fastapi-dls`";
};
};

##### implementation
config =
let
envFile = pkgs.writeText "fastapi-dls.env" ''
# Toggle debug mode
#DEBUG=false

# Where the client can find the DLS server
DLS_URL=${cfg.dlsAddress}
DLS_PORT=${builtins.toString cfg.dlsPort}

# CORS configuration
## comma separated list without spaces
#CORS_ORIGINS="https://${cfg.dlsAddress}:${builtins.toString cfg.dlsPort}"

# Lease expiration in days
LEASE_EXPIRE_DAYS=90
LEASE_RENEWAL_PERIOD=0.2

# Database location
## https://docs.sqlalchemy.org/en/14/core/engines.html
DATABASE=sqlite:///${stateDir}/db.sqlite

# UUIDs for identifying the instance
#SITE_KEY_XID="00000000-0000-0000-0000-000000000000"
#INSTANCE_REF="10000000-0000-0000-0000-000000000001"
#ALLOTMENT_REF="20000000-0000-0000-0000-000000000001"

# Site-wide signing keys
INSTANCE_KEY_RSA=${dls_privkey}
INSTANCE_KEY_PUB=${dls_pubkey}
'';
in
lib.mkIf cfg.enable {
networking.firewall = lib.mkIf cfg.openFirewall {
allowedTCPPorts = [ cfg.listenPort ];
};

systemd.services.fastapi-dls = {
description = "fastapi-dls daemon";
wantedBy = [ "multi-user.target" ];
wants = [ "network-online.target" ];
after = [ "network-online.target" ];
preStart = ''
if [ ! -f "${dls_privkey}" ]; then
${pkgs.openssl}/bin/openssl genrsa -out "${dls_privkey}" 2048
fi
if [ ! -f "${dls_pubkey}" ]; then
${pkgs.openssl}/bin/openssl rsa -in "${dls_privkey}" -outform PEM -pubout -out "${dls_pubkey}"
fi
if [ ! -f "${https_privkey}" ] || [ ! -f "${https_cert}" ]; then
${pkgs.openssl}/bin/openssl req -x509 -nodes \
-days 3650 -newkey rsa:2048 -subj "/CN=fastapi-dls" \
-keyout "${https_privkey}" -out "${https_cert}"
fi
'';
script = ''
${lib.getExe cfg.package} \
--env-file ${envFile} \
--host ${cfg.listenAddress} \
--port ${builtins.toString cfg.listenPort} \
--ssl-keyfile ${https_privkey} \
--ssl-certfile ${https_cert}
'';
serviceConfig = {
DynamicUser = true;
StateDirectory = builtins.baseNameOf stateDir;
};
};
};

meta.maintainers = with lib.maintainers; [ MakiseKurisu ];
}
63 changes: 63 additions & 0 deletions pkgs/by-name/fa/fastapi-dls/package.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
lib,
python3,
fetchFromGitLab,
gitUpdater,
makeWrapper,
}:

let

pythonEnv = python3.withPackages (
packages:
with packages;
[
fastapi
uvicorn
python-jose
pycryptodome
python-dateutil
sqlalchemy
markdown
python-dotenv
]
++ uvicorn.optional-dependencies.standard
);

version = "1.4.1";

in
python3.pkgs.buildPythonApplication {
pname = "fastapi-dls";
inherit version;
pyproject = false;

src = fetchFromGitLab {
domain = "git.collinwebdesigns.de";
owner = "oscar.krause";
repo = "fastapi-dls";
rev = "refs/tags/${version}";
hash = "sha256-H4mtmJ4iQXPZFWQPm12aH/kdg9TAMgHkvkbaHfxfS3I=";
};

postInstall = ''
mkdir -p $out/bin $out/share/fastapi-dls
cp -r README.md app $out/share/fastapi-dls

makeWrapper ${pythonEnv}/bin/uvicorn $out/bin/fastapi-dls \
--add-flags "--app-dir $out/share/fastapi-dls/app" \
--add-flags "--proxy-headers" \
--add-flags "main:app"
'';

passthru.updateScript = gitUpdater { };

meta = with lib; {
homepage = "https://git.collinwebdesigns.de/oscar.krause/fastapi-dls";
license = licenses.unfree;
description = "Minimal Delegated License Service (DLS)";
mainProgram = "fastapi-dls";
platforms = platforms.linux;
maintainers = with maintainers; [ MakiseKurisu ];
};
}