[Fix] systemd stuff fr this time

This commit is contained in:
Alois 2026-05-17 22:22:03 +02:00
commit 41deb0d6de
2 changed files with 215 additions and 181 deletions

379
flake.nix
View file

@ -14,8 +14,15 @@
}; };
}; };
outputs = inputs@{ self, nixpkgs, flake-parts, rust-overlay, ttp, ... }: outputs = inputs @ {
flake-parts.lib.mkFlake { inherit inputs; } { self,
nixpkgs,
flake-parts,
rust-overlay,
ttp,
...
}:
flake-parts.lib.mkFlake {inherit inputs;} {
systems = [ systems = [
"x86_64-linux" "x86_64-linux"
"aarch64-linux" "aarch64-linux"
@ -23,203 +30,230 @@
"aarch64-darwin" "aarch64-darwin"
]; ];
perSystem = { self', pkgs, system, ... }: perSystem = {
let self',
rustPkgs = import nixpkgs { pkgs,
inherit system; system,
overlays = [ (import rust-overlay) ]; ...
}; }: let
rustToolchain = rustPkgs.rust-bin.stable.latest.default.override { rustPkgs = import nixpkgs {
extensions = [ "rust-src" "rust-analyzer" "clippy" "rustfmt" ]; inherit system;
}; overlays = [(import rust-overlay)];
in };
{ rustToolchain = rustPkgs.rust-bin.stable.latest.default.override {
packages = { extensions = ["rust-src" "rust-analyzer" "clippy" "rustfmt"];
default = self'.packages.iota; };
iota = pkgs.rustPlatform.buildRustPackage { in {
pname = "iota"; packages = {
version = "0.1.0"; default = self'.packages.iota;
src = ./.; iota = pkgs.rustPlatform.buildRustPackage {
cargoLock = { pname = "iota";
lockFile = ./Cargo.lock; version = "0.1.0";
allowBuiltinFetchGit = true; src = ./.;
}; cargoLock = {
nativeBuildInputs = with pkgs; [ cmake perl pkg-config ]; lockFile = ./Cargo.lock;
buildInputs = with pkgs; [ openssl sqlite ]; allowBuiltinFetchGit = true;
dontUseCmakeConfigure = true;
preConfigure = ''
if [ -d ../cargo-vendor-dir/ttp-core-0.1.0 ]; then
cp ${ttp}/ttp-codec.json ../cargo-vendor-dir/ttp-codec.json
fi
'';
postInstall = ''
mv $out/bin/iota-core $out/bin/iota
for f in $out/bin/*; do
if [ "$(basename "$f")" != "iota" ]; then
rm "$f"
fi
done
'';
passthru.dataDir = "/var/lib/iota";
}; };
}; nativeBuildInputs = with pkgs; [cmake perl pkg-config];
buildInputs = with pkgs; [openssl sqlite];
devShells.default = pkgs.mkShell { dontUseCmakeConfigure = true;
nativeBuildInputs = with pkgs; [ rustToolchain git cmake perl pkg-config ]; preConfigure = ''
buildInputs = with pkgs; [ openssl sqlite ]; if [ -d ../cargo-vendor-dir/ttp-core-0.1.0 ]; then
cp ${ttp}/ttp-codec.json ../cargo-vendor-dir/ttp-codec.json
fi
'';
postInstall = ''
mv $out/bin/iota-core $out/bin/iota
for f in $out/bin/*; do
if [ "$(basename "$f")" != "iota" ]; then
rm "$f"
fi
done
'';
passthru.dataDir = "/var/lib/iota";
}; };
}; };
devShells.default = pkgs.mkShell {
nativeBuildInputs = with pkgs; [rustToolchain git cmake perl pkg-config];
buildInputs = with pkgs; [openssl sqlite];
};
};
flake = { flake = {
nixosModules.default = { config, pkgs, lib, ... }: nixosModules.default = {
let config,
cfg = config.services.iota; pkgs,
defaultPackage = self.packages.${pkgs.stdenv.hostPlatform.system}.default or (throw "iota: no pre-built package for system ${pkgs.stdenv.hostPlatform.system}"); lib,
...
}: let
cfg = config.services.iota;
defaultPackage = self.packages.${pkgs.stdenv.hostPlatform.system}.default or (throw "iota: no pre-built package for system ${pkgs.stdenv.hostPlatform.system}");
configFile = if cfg.settingsFile != null then cfg.settingsFile else configFile =
pkgs.writeText "iota-config.json" (builtins.toJSON cfg.settings); if cfg.settingsFile != null
then cfg.settingsFile
else pkgs.writeText "iota-config.json" (builtins.toJSON cfg.settings);
descriptionText = "Iota Service" descriptionText = "Tensamin Iota";
+ lib.optionalString cfg.useTmux " (attach TUI: tmux -S ${cfg.dataDir}/tmux.sock attach -t iota)"; #+ lib.optionalString cfg.useTmux " (attach TUI: tmux -S ${cfg.dataDir}/tmux.sock attach -t iota)";
in in {
{ options.services.iota = {
options.services.iota = { enable = lib.mkEnableOption "Enable the Iota service.";
enable = lib.mkEnableOption "the Iota service";
dataDir = lib.mkOption { dataDir = lib.mkOption {
type = lib.types.str; type = lib.types.str;
default = cfg.package.passthru.dataDir or "/var/lib/iota"; default = cfg.package.passthru.dataDir or "/var/lib/iota";
defaultText = lib.literalExpression ''config.services.iota.package.passthru.dataDir or "/var/lib/iota"''; defaultText = lib.literalExpression ''config.services.iota.package.passthru.dataDir or "/var/lib/iota"'';
description = "Directory where Iota stores its data, config, and certificates."; description = "Directory where Iota stores its data, config, and certificates.";
};
certFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = "Path to the SSL certificate file (cert.pem).";
};
keyFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = "Path to the SSL private key file (cert.key).";
};
environmentFiles = lib.mkOption {
type = lib.types.listOf lib.types.path;
default = [ ];
description = "Environment files to load for the Iota service.";
};
openFirewall = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Whether to open the firewall for ports used by Iota.";
};
ttpBind = lib.mkOption {
type = lib.types.str;
default = "0.0.0.0";
description = "IP address to bind the TTP/QUIC server to.";
};
bindAddress = lib.mkOption {
type = lib.types.str;
default = "0.0.0.0";
description = "IP address to bind the HTTP server to.";
};
package = lib.mkOption {
type = lib.types.package;
default = defaultPackage;
description = "The Iota package to use.";
};
useTmux = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Whether to run Iota inside a tmux session for shared TUI access.";
};
settings = lib.mkOption {
type = lib.types.attrs;
default = { };
description = "Configuration attributes for Iota, written to config.json.";
};
settingsFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = "Path to an existing config.json file to use instead of generating from settings.";
};
}; };
config = lib.mkIf cfg.enable { certFile = lib.mkOption {
users.users.iota = { type = lib.types.nullOr lib.types.path;
isSystemUser = true; default = null;
group = "iota"; description = "Path to the SSL certificate file (cert.pem).";
home = cfg.dataDir; };
createHome = true;
description = "Iota service user";
};
users.groups.iota = { }; keyFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = "Path to the SSL private key file (cert.key).";
};
systemd.services.iota = { environmentFiles = lib.mkOption {
description = descriptionText; type = lib.types.listOf lib.types.path;
wantedBy = [ "multi-user.target" ]; default = [];
after = [ "network.target" ]; description = "Environment files to load for the Iota service.";
path = [ pkgs.tmux pkgs.bash pkgs.coreutils pkgs.systemd ]; };
serviceConfig = { openFirewall = lib.mkOption {
Type = if cfg.useTmux then "forking" else "simple"; type = lib.types.bool;
default = true;
description = "Whether to open the firewall for ports used by Iota.";
};
ttpBind = lib.mkOption {
type = lib.types.str;
default = "0.0.0.0";
description = "IP address to bind the TTP/QUIC server to.";
};
bindAddress = lib.mkOption {
type = lib.types.str;
default = "0.0.0.0";
description = "IP address to bind the HTTP server to.";
};
package = lib.mkOption {
type = lib.types.package;
default = defaultPackage;
description = "The Iota package to use.";
};
useTmux = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Whether to run Iota inside a tmux session for shared TUI access.";
};
settings = lib.mkOption {
type = lib.types.attrs;
default = {};
description = "Configuration attributes for Iota, written to config.json.";
};
settingsFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = "Path to an existing config.json file to use instead of generating from settings.";
};
};
config = lib.mkIf cfg.enable {
users.users.iota = {
isSystemUser = true;
group = "iota";
home = cfg.dataDir;
createHome = true;
description = "Iota service user";
shell = pkgs.bash;
};
users.groups.iota = {};
systemd.services.iota = let
iotaTmuxCmd = pkgs.writeShellScript "iota-tmux-cmd" ''
mkdir -p ${cfg.dataDir}
echo "[$(date)] Running Iota..."
${cfg.package}/bin/iota
status=$?
echo ""
echo "[$(date)] Iota exited with status: $status"
echo "Press any key to exit..."
read -r -n 1
exit $status
'';
in {
description = descriptionText;
wantedBy = ["multi-user.target"];
after = ["network.target"];
serviceConfig =
{
Type = "simple";
User = "iota"; User = "iota";
Group = "iota"; Group = "iota";
WorkingDirectory = cfg.dataDir; WorkingDirectory = cfg.dataDir;
ExecStart = if cfg.useTmux then ExecStart =
pkgs.writeShellScript "iota-start" '' if cfg.useTmux
${pkgs.tmux}/bin/tmux -S ${cfg.dataDir}/tmux.sock new-session -d -s iota \ then
"${pkgs.bash}/bin/bash -lc 'exec > >(${pkgs.coreutils}/bin/tee -a ${cfg.dataDir}/iota-output.log >(${pkgs.systemd}/bin/systemd-cat -t iota-daemon)) 2>&1; ${cfg.package}/bin/iota; status=$?; printf \"\nProcess exited with status %s. Press any key to close this tmux session...\" \"\$status\"; read -r -n 1; exit \"\$status\"'" pkgs.writeShellScript "iota-start" ''
'' set -e
else export TMUX_TMPDIR=${cfg.dataDir}
"${cfg.package}/bin/iota"; ${pkgs.coreutils}/bin/mkdir -p ${cfg.dataDir}
${pkgs.coreutils}/bin/chown iota:iota ${cfg.dataDir}
ExecStop = if cfg.useTmux then echo "[iota-start] Creating tmux session..."
(pkgs.writeShellScript "iota-stop" '' if ! ${pkgs.tmux}/bin/tmux -S ${cfg.dataDir}/tmux.sock new-session -d -s iota "${iotaTmuxCmd}"; then
${pkgs.tmux}/bin/tmux -S ${cfg.dataDir}/tmux.sock kill-session -t iota 2>/dev/null || true echo "[iota-start] ERROR: tmux new-session failed"
'') exit 1
else fi
null; echo "[iota-start] tmux session created, waiting..."
echo "[iota-start] Run 'tmux -S ${cfg.dataDir}/tmux.sock attach -t iota' to attach to the tmux session."
while ${pkgs.tmux}/bin/tmux -S ${cfg.dataDir}/tmux.sock has-session -t iota 2>/dev/null; do
sleep 2
done
echo "[iota-start] tmux session ended"
''
else "${cfg.package}/bin/iota";
ExecStartPre = [ ExecStartPre = [
("+" + pkgs.writeShellScript "iota-setup" '' ("+"
mkdir -p ${cfg.dataDir}/certs + pkgs.writeShellScript "iota-setup" ''
mkdir -p ${cfg.dataDir}/certs
${lib.optionalString (cfg.certFile != null) "ln -sf ${cfg.certFile} ${cfg.dataDir}/certs/cert.pem"} ${lib.optionalString (cfg.certFile != null) "ln -sf ${cfg.certFile} ${cfg.dataDir}/certs/cert.pem"}
${lib.optionalString (cfg.keyFile != null) "ln -sf ${cfg.keyFile} ${cfg.dataDir}/certs/cert.key"} ${lib.optionalString (cfg.keyFile != null) "ln -sf ${cfg.keyFile} ${cfg.dataDir}/certs/cert.key"}
install -m 644 ${configFile} ${cfg.dataDir}/config.json install -m 644 ${configFile} ${cfg.dataDir}/config.json
chown -R iota:iota ${cfg.dataDir} chown -R iota:iota ${cfg.dataDir}
'')
${lib.optionalString cfg.useTmux ''
echo "Iota started under tmux. Attach with: tmux -S ${cfg.dataDir}/tmux.sock attach -t iota" >&2
''}
'')
]; ];
Restart = "always"; Restart = "always";
RestartSec = "5s"; RestartSec = "5s";
AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ]; AmbientCapabilities = ["CAP_NET_BIND_SERVICE"];
CapabilityBoundingSet = [ "CAP_NET_BIND_SERVICE" ]; CapabilityBoundingSet = ["CAP_NET_BIND_SERVICE"];
ProtectSystem = "strict"; ProtectSystem = "strict";
ProtectHome = true; ProtectHome = true;
PrivateTmp = true; PrivateTmp = true;
NoNewPrivileges = true; NoNewPrivileges = true;
ReadWritePaths = [ cfg.dataDir ]; ReadWritePaths = [cfg.dataDir];
ProtectKernelTunables = true; ProtectKernelTunables = true;
ProtectKernelModules = true; ProtectKernelModules = true;
ProtectControlGroups = true; ProtectControlGroups = true;
@ -231,17 +265,18 @@
"TTP_BIND=${cfg.ttpBind}" "TTP_BIND=${cfg.ttpBind}"
"BIND_ADDRESS=${cfg.bindAddress}" "BIND_ADDRESS=${cfg.bindAddress}"
]; ];
} // lib.optionalAttrs (cfg.environmentFiles != [ ]) { }
// lib.optionalAttrs (cfg.environmentFiles != []) {
EnvironmentFile = cfg.environmentFiles; EnvironmentFile = cfg.environmentFiles;
}; };
}; };
networking.firewall = lib.mkIf cfg.openFirewall { networking.firewall = lib.mkIf cfg.openFirewall {
allowedTCPPorts = [ 1984 ]; allowedTCPPorts = [1984];
allowedUDPPorts = [ 1984 ]; allowedUDPPorts = [1984];
};
}; };
}; };
};
}; };
}; };
} }

View file

@ -149,9 +149,8 @@ pub fn get_children(path: &str) -> Vec<String> {
} }
pub fn get_directory() -> String { pub fn get_directory() -> String {
let exe = std::env::current_exe().unwrap_or_else(|_| PathBuf::from(".")); std::env::current_dir()
exe.parent() .unwrap_or_else(|_| PathBuf::from("."))
.unwrap_or(Path::new("."))
.to_string_lossy() .to_string_lossy()
.to_string() .to_string()
} }