Flake: HM module

This commit is contained in:
bluepython508
2024-09-20 15:08:27 +01:00
parent 6aaf8ff8e5
commit dbe6fae810
6 changed files with 70 additions and 11 deletions

View File

@@ -1,2 +1,6 @@
import Config import Config
config :frajtano, listen_path: Path.expand(System.get_env("FRAJTANO_DIR")) |> Path.join("agent.sock") config :frajtano,
listen_path: Path.expand(System.get_env("FRAJTANO_DIR")) |> Path.join("agent.sock"),
initial_peers: System.get_env("FRAJTANO_PEERS") |> String.split(":") |> Enum.map(&Path.expand/1)

View File

@@ -32,6 +32,9 @@
assimilate) assimilate)
run rpc ":ok = \"$(echo -n "$2" | ${pkgs.coreutils}/bin/base64)\" |> Base.decode64!() |> Frajtano.Agent.add_peer()" run rpc ":ok = \"$(echo -n "$2" | ${pkgs.coreutils}/bin/base64)\" |> Base.decode64!() |> Frajtano.Agent.add_peer()"
;; ;;
socket)
echo $FRAJTANO_DIR/agent.sock
;;
*) *)
run "$@" run "$@"
;; ;;

View File

@@ -15,7 +15,11 @@
ownPkgs = self.packages.${system}; ownPkgs = self.packages.${system};
}); });
in { in {
devShells = eachSystem ({pkgs, ownPkgs, ...}: { devShells = eachSystem ({
pkgs,
ownPkgs,
...
}: {
default = pkgs.beam.packages.erlang_26.callPackage ./shell.nix { default = pkgs.beam.packages.erlang_26.callPackage ./shell.nix {
inherit ownPkgs; inherit ownPkgs;
}; };
@@ -23,5 +27,46 @@
packages = eachSystem ({pkgs, ...}: { packages = eachSystem ({pkgs, ...}: {
default = pkgs.beam.packages.erlang_26.callPackage ./default.nix {}; default = pkgs.beam.packages.erlang_26.callPackage ./default.nix {};
}); });
homeModules.default = {
config,
lib,
pkgs,
...
}: let
cfg = config.bluepython508.frajtano;
in {
options.bluepython508.frajtano = {
enable = lib.mkEnableOption "frajtano";
dir = lib.mkOption {
description = "directory in which to place the listening socket";
default = "${config.home.homeDirectory}/.ssh/frajtano";
};
};
config = lib.mkIf cfg.enable {
home.sessionVariables.FRAJTANO_DIR = cfg.dir;
home.packages = [self.packages.${pkgs.system}.default];
systemd.user.services.frajtano = {
Unit.Description = "frajtano";
Unit.After = ["default.target"];
Service.Environment = "'FRAJTANO_DIR=${cfg.dir}'";
Service.ExecStart = "${self.packages.${pkgs.system}.default} start";
Install.WantedBy = ["default.target"];
};
launchd.agents.frajtano = {
enable = true;
config = {
EnvironmentVariables = {
FRAJTANO_DIR = cfg.dir;
};
ProgramArguments = ["${lib.getExe self.packages.${pkgs.system}.default}" "start"];
RunAtLoad = true;
KeepAlive = true;
};
};
};
};
}; };
} }

View File

@@ -8,10 +8,19 @@ defmodule Frajtano.Agent do
@impl true @impl true
def init(_) do def init(_) do
{:ok, {
:ok,
%{ %{
keys: %{} keys: %{}
}} },
{:continue, :init_peers}
}
end
@impl true
def handle_continue(:init_peers, state) do
for peer <- Application.fetch_env!(:frajtano, :initial_peers), do: {:ok, _} = Peer.start(peer)
{:noreply, state}
end end
@impl true @impl true
@@ -27,9 +36,8 @@ defmodule Frajtano.Agent do
do: {:ok, {idents, peer}} do: {:ok, {idents, peer}}
end) end)
# Double :ok-wrapping because of Task.async_stream # Double :ok-wrapping because of Task.async_stream
idents = (for {:ok, {:ok, {idents, peer}}} <- idents, do: {idents, peer}) idents = for {:ok, {:ok, {idents, peer}}} <- idents, do: {idents, peer}
{ {
:reply, :reply,

View File

@@ -17,10 +17,10 @@ defmodule Frajtano.Supervisor do
@impl true @impl true
def init(:ok) do def init(:ok) do
children = [ children = [
{DynamicSupervisor, name: Frajtano.Peer},
Frajtano.Agent, Frajtano.Agent,
{Frajtano.Listener, [Application.fetch_env!(:frajtano, :listen_path)]},
{Task.Supervisor, name: Frajtano.ClientSupervisor}, {Task.Supervisor, name: Frajtano.ClientSupervisor},
{DynamicSupervisor, name: Frajtano.Peer} {Frajtano.Listener, [Application.fetch_env!(:frajtano, :listen_path)]},
] ]
Supervisor.init(children, strategy: :one_for_one) Supervisor.init(children, strategy: :one_for_one)

View File

@@ -14,7 +14,6 @@ defmodule Frajtano.MixProject do
def application do def application do
[ [
extra_applications: [:logger], extra_applications: [:logger],
env: [listen_path: nil],
mod: {Frajtano, []} mod: {Frajtano, []}
] ]
end end