feat(nix): port over libs

This commit is contained in:
Mohammad Rafiq 2025-07-09 01:33:28 +08:00
parent 03bac12a2d
commit 96b5aa3fef
No known key found for this signature in database
3 changed files with 104 additions and 1 deletions

13
nix/lib/lists.nix Normal file
View file

@ -0,0 +1,13 @@
let
inherit (builtins) length tail;
in
{
flake.lib.lists = rec {
shortenList =
count: list:
let
len = length list;
in
if len <= count then list else (shortenList count (tail list));
};
}

View file

@ -1,7 +1,13 @@
{ lib, ... }: { lib, ... }:
let let
inherit (lib.options) mkOption; inherit (lib.options) mkOption;
inherit (lib.types) str; inherit (lib.types)
str
path
int
port
attrs
;
in in
{ {
flake.lib.options = { flake.lib.options = {
@ -11,5 +17,29 @@ in
inherit default; inherit default;
type = str; type = str;
}; };
mkAttrOption =
default:
mkOption {
inherit default;
type = attrs;
};
mkIntOption =
default:
mkOption {
inherit default;
type = int;
};
mkPortOption =
default:
mkOption {
type = port;
inherit default;
};
mkPathOption =
default:
mkOption {
type = path;
inherit default;
};
}; };
} }

60
nix/lib/services.nix Normal file
View file

@ -0,0 +1,60 @@
{ config, lib, ... }:
let
inherit (builtins) length concatStringsSep;
inherit (lib.options) mkEnableOption;
inherit (lib.strings) splitString;
inherit (lib.lists) singleton;
inherit (lib.modules) mkMerge mkIf;
inherit (cfg.lib.options) mkStrOption mkPortOption mkAttrOption;
inherit (cfg.lib.lists) shortenList;
cfg = config.flake;
in
{
flake.lib.services = rec {
splitDomain = domain: splitString "." domain;
isRootDomain = domain: length (splitDomain domain) <= 2;
mkRootDomain = domain: concatStringsSep "." (shortenList 2 (splitDomain domain));
mkWildcardDomain = rootDomain: concatStringsSep "." ((singleton "*") ++ (splitDomain rootDomain));
mkHost = domain: if isRootDomain domain then domain else mkWildcardDomain (mkRootDomain domain);
mkWebApp =
{
name,
defaultPort,
persistDirs ? [ ],
serviceConfig ? { },
extraOptions ? { },
}:
let
cfg = config.server.web-apps.${name};
in
{
options.server.web-apps.${name} = {
enable = mkEnableOption "";
port = mkPortOption defaultPort;
domain = mkStrOption "";
openFirewall = mkEnableOption "";
extraCfg = mkAttrOption { };
} // extraOptions;
config = mkIf cfg.enable (mkMerge [
{
inherit persistDirs;
networking.firewall = mkIf cfg.openFirewall { allowedTCPPorts = singleton cfg.port; };
}
(mkIf (cfg.domain != "") {
assertions = singleton {
assertion = config.server.web-servers.nginx.enable;
message = "You must enable a web server if you want to set server.web-apps.${name}.domain.";
};
server.networking.ddns.domains = singleton (mkRootDomain cfg.domain);
server.web-servers.nginx.proxies = singleton {
source = cfg.domain;
target = "http://${config.hostname}:${toString cfg.port}";
};
})
serviceConfig
cfg.extraCfg
]);
};
};
}