Robotnix v1

This commit is contained in:
ari committed 2026-10-23 15:26:04 -04:00
1 parent 0cf504ad27
commit eb6380ddfe
14 files changed
+1642 -1

No files matched your search

+86
View File
@@ -0,0 +1,86 @@
# Shared values for network.nix, robots.nix and the CLI.
{ config, lib }:
let
cfg = config.skyg.nixos.server.robotnix;
# "16G" -> bytes (IEC, same as systemd and truncate).
toBytes =
s:
let
m = builtins.match "([0-9]+)([KMGT])" s;
mult = {
K = 1024;
M = 1024 * 1024;
G = 1024 * 1024 * 1024;
T = 1024 * 1024 * 1024 * 1024;
};
in
lib.toInt (builtins.elemAt m 0) * mult.${builtins.elemAt m 1};
# nft identifiers can't hold "-".
nftName = name: lib.replaceStrings [ "-" ] [ "_" ] name;
# Render a string as a Nix string literal (for generated .nix files).
nixStr = s: lib.replaceStrings [ "\${" ] [ "\\\${" ] (builtins.toJSON s);
# Shared tier + the robot's own tier. Lists are merged, the robot's
# `internet` mode wins when set.
mergeAccess =
r:
let
s = cfg.access.shared;
both = f: lib.unique (f s ++ f r);
in
{
internet =
if r.internet != null then
r.internet
else if s.internet != null then
s.internet
else
"all";
allowLan = s.allowLan || r.allowLan;
allowedDomains = both (t: t.allowedDomains);
allowedCidrs = both (t: t.allowedCidrs);
blockedDomains = both (t: t.blockedDomains);
blockedCidrs = both (t: t.blockedCidrs);
lanAccess = both (t: t.lanAccess);
hostAccess = both (t: t.hostAccess);
};
mkRobot = name: r: {
inherit name;
inherit (r)
id
address
autoStart
privateUsers
cpus
memory
disk
snapshots
seed
user
authorizedKeys
git
;
nft = nftName name;
port = "vb-${name}";
dnsPort = 5300 + r.id;
base = "${cfg.baseDir}/${name}";
run = "/run/robotnix/${name}";
unit = "robotnix-${name}";
access = mergeAccess r.access;
};
robots = lib.mapAttrs mkRobot cfg.robots;
in
{
inherit
toBytes
nftName
nixStr
robots
;
robotList = lib.attrValues robots;
}