Flake: HM module

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

View File

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

View File

@@ -15,7 +15,11 @@
ownPkgs = self.packages.${system};
});
in {
devShells = eachSystem ({pkgs, ownPkgs, ...}: {
devShells = eachSystem ({
pkgs,
ownPkgs,
...
}: {
default = pkgs.beam.packages.erlang_26.callPackage ./shell.nix {
inherit ownPkgs;
};
@@ -23,5 +27,45 @@
packages = eachSystem ({pkgs, ...}: {
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 {
name = "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
def init(_) do
{:ok,
%{
keys: %{}
}}
{
:ok,
%{
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
@impl true
@@ -26,10 +35,9 @@ defmodule Frajtano.Agent do
with {:ok, idents} <- Peer.identities(peer),
do: {:ok, {idents, peer}}
end)
# 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,

View File

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

View File

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