[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

107
flake.nix
View file

@ -14,7 +14,14 @@
};
};
outputs = inputs@{ self, nixpkgs, flake-parts, rust-overlay, ttp, ... }:
outputs = inputs @ {
self,
nixpkgs,
flake-parts,
rust-overlay,
ttp,
...
}:
flake-parts.lib.mkFlake {inherit inputs;} {
systems = [
"x86_64-linux"
@ -23,8 +30,12 @@
"aarch64-darwin"
];
perSystem = { self', pkgs, system, ... }:
let
perSystem = {
self',
pkgs,
system,
...
}: let
rustPkgs = import nixpkgs {
inherit system;
overlays = [(import rust-overlay)];
@ -32,8 +43,7 @@
rustToolchain = rustPkgs.rust-bin.stable.latest.default.override {
extensions = ["rust-src" "rust-analyzer" "clippy" "rustfmt"];
};
in
{
in {
packages = {
default = self'.packages.iota;
iota = pkgs.rustPlatform.buildRustPackage {
@ -71,20 +81,25 @@
};
flake = {
nixosModules.default = { config, pkgs, lib, ... }:
let
nixosModules.default = {
config,
pkgs,
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
pkgs.writeText "iota-config.json" (builtins.toJSON cfg.settings);
configFile =
if cfg.settingsFile != null
then cfg.settingsFile
else pkgs.writeText "iota-config.json" (builtins.toJSON cfg.settings);
descriptionText = "Iota Service"
+ lib.optionalString cfg.useTmux " (attach TUI: tmux -S ${cfg.dataDir}/tmux.sock attach -t iota)";
in
{
descriptionText = "Tensamin Iota";
#+ lib.optionalString cfg.useTmux " (attach TUI: tmux -S ${cfg.dataDir}/tmux.sock attach -t iota)";
in {
options.services.iota = {
enable = lib.mkEnableOption "the Iota service";
enable = lib.mkEnableOption "Enable the Iota service.";
dataDir = lib.mkOption {
type = lib.types.str;
@ -161,39 +176,62 @@
home = cfg.dataDir;
createHome = true;
description = "Iota service user";
shell = pkgs.bash;
};
users.groups.iota = {};
systemd.services.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"];
path = [ pkgs.tmux pkgs.bash pkgs.coreutils pkgs.systemd ];
serviceConfig = {
Type = if cfg.useTmux then "forking" else "simple";
serviceConfig =
{
Type = "simple";
User = "iota";
Group = "iota";
WorkingDirectory = cfg.dataDir;
ExecStart = if cfg.useTmux then
ExecStart =
if cfg.useTmux
then
pkgs.writeShellScript "iota-start" ''
${pkgs.tmux}/bin/tmux -S ${cfg.dataDir}/tmux.sock new-session -d -s iota \
"${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\"'"
''
else
"${cfg.package}/bin/iota";
set -e
export TMUX_TMPDIR=${cfg.dataDir}
${pkgs.coreutils}/bin/mkdir -p ${cfg.dataDir}
${pkgs.coreutils}/bin/chown iota:iota ${cfg.dataDir}
ExecStop = if cfg.useTmux then
(pkgs.writeShellScript "iota-stop" ''
${pkgs.tmux}/bin/tmux -S ${cfg.dataDir}/tmux.sock kill-session -t iota 2>/dev/null || true
'')
else
null;
echo "[iota-start] Creating tmux session..."
if ! ${pkgs.tmux}/bin/tmux -S ${cfg.dataDir}/tmux.sock new-session -d -s iota "${iotaTmuxCmd}"; then
echo "[iota-start] ERROR: tmux new-session failed"
exit 1
fi
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 = [
("+" + pkgs.writeShellScript "iota-setup" ''
("+"
+ pkgs.writeShellScript "iota-setup" ''
mkdir -p ${cfg.dataDir}/certs
${lib.optionalString (cfg.certFile != null) "ln -sf ${cfg.certFile} ${cfg.dataDir}/certs/cert.pem"}
@ -202,10 +240,6 @@
install -m 644 ${configFile} ${cfg.dataDir}/config.json
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
''}
'')
];
@ -231,7 +265,8 @@
"TTP_BIND=${cfg.ttpBind}"
"BIND_ADDRESS=${cfg.bindAddress}"
];
} // lib.optionalAttrs (cfg.environmentFiles != [ ]) {
}
// lib.optionalAttrs (cfg.environmentFiles != []) {
EnvironmentFile = cfg.environmentFiles;
};
};

View file

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