Files
allmende/nix/module.nix
T
nilsandClaude Sonnet 5.5 20ea9b32f7 Add e2e script to the repo, Dockerfile, and a Nix flake with a NixOS module
- scripts/e2e.sh (+ scripts/e2e/*.ts): the two-node end-to-end test, now
  self-contained and cleaning up after itself; `make e2e`, part of `make check`
- Dockerfile/.dockerignore: static Go build in a non-root alpine image, data in
  /data, defaults to region u1j; `make docker`
- flake.nix, nix/: package (buildGoModule), dev shell, NixOS module
  services.allmende (hardened systemd unit that waits for `allmende-admin init`),
  and a NixOS VM test run by `nix flake check`
- README: running a node with Docker and NixOS

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-17 21:21:21 +01:00

161 lines
4.5 KiB
Nix

self:
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.allmende;
exe = lib.getExe cfg.package;
# `allmende init` and `recover` run as the service's user and need a terminal
# (they show and check the recovery phrase), so they are not part of the unit.
admin = pkgs.writeShellScriptBin "allmende-admin" ''
exec sudo -u allmende env ALLMENDE_DATA=${lib.escapeShellArg cfg.dataDir} ${exe} "$@"
'';
args =
[
"run"
"--http"
cfg.httpAddress
"--listen"
"/ip4/0.0.0.0/tcp/${toString cfg.p2pPort}"
]
++ lib.concatMap (r: [
"--region"
r
]) cfg.regions
++ lib.concatMap (b: [
"--bootstrap"
b
]) cfg.bootstrap
++ cfg.extraArgs;
in
{
options.services.allmende = {
enable = lib.mkEnableOption "an Allmende node (a free, distributed marketplace for homes)";
package = lib.mkOption {
type = lib.types.package;
default = self.packages.${pkgs.stdenv.hostPlatform.system}.default;
defaultText = lib.literalExpression "allmende.packages.\${system}.default";
description = "The allmende package to run.";
};
regions = lib.mkOption {
type = lib.types.listOf lib.types.str;
example = [ "u1j" ];
description = ''
Regions to replicate: 3-character geohashes, each about 150 km wide.
`u1j` is Bonn, `u1h` Cologne, `u1x` Hamburg.
'';
};
bootstrap = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
example = [ "/ip4/203.0.113.5/tcp/4001/p2p/12D3KooW..." ];
description = "Known peers to connect to, as multiaddrs ending in /p2p/<peer id>.";
};
httpAddress = lib.mkOption {
type = lib.types.str;
default = "127.0.0.1:8080";
description = ''
Where the web UI and API listen. The same port serves the owner API,
which the admin token protects, so keep it on loopback and put a TLS
reverse proxy in front for public use.
'';
};
p2pPort = lib.mkOption {
type = lib.types.port;
default = 4001;
description = "TCP port other nodes connect to.";
};
openFirewall = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Open the peer-to-peer port ({option}`p2pPort`) in the firewall.";
};
dataDir = lib.mkOption {
type = lib.types.path;
default = "/var/lib/allmende";
description = "Where keys, the admin token and the database live.";
};
extraArgs = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
example = [ "--no-mdns" ];
description = "Further arguments for `allmende run`.";
};
};
config = lib.mkIf cfg.enable {
assertions = [
{
assertion = cfg.regions != [ ];
message = "services.allmende.regions needs at least one region, for example [ \"u1j\" ].";
}
];
users.users.allmende = {
isSystemUser = true;
group = "allmende";
home = cfg.dataDir;
description = "Allmende node";
};
users.groups.allmende = { };
systemd.tmpfiles.rules = [ "d ${cfg.dataDir} 0700 allmende allmende -" ];
# One-time setup, because it shows the recovery phrase:
# sudo allmende-admin init && sudo systemctl start allmende
# The unit is skipped (not failed) until that has happened.
systemd.services.allmende = {
description = "Allmende node";
wantedBy = [ "multi-user.target" ];
wants = [ "network-online.target" ];
after = [ "network-online.target" ];
unitConfig.ConditionPathExists = "${cfg.dataDir}/admin.token";
environment.ALLMENDE_DATA = cfg.dataDir;
serviceConfig = {
ExecStart = "${exe} ${lib.escapeShellArgs args}";
User = "allmende";
Group = "allmende";
Restart = "on-failure";
RestartSec = "5s";
NoNewPrivileges = true;
PrivateTmp = true;
PrivateDevices = true;
ProtectSystem = "strict";
ProtectHome = true;
ReadWritePaths = [ cfg.dataDir ];
ProtectKernelTunables = true;
ProtectKernelModules = true;
ProtectControlGroups = true;
RestrictSUIDSGID = true;
LockPersonality = true;
# libp2p reads the network interfaces over netlink to find its addresses.
RestrictAddressFamilies = [
"AF_INET"
"AF_INET6"
"AF_UNIX"
"AF_NETLINK"
];
};
};
networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [ cfg.p2pPort ];
environment.systemPackages = [ admin ];
};
}