Flake: HM module
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
|
||||
@@ -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 "$@"
|
||||
;;
|
||||
|
||||
47
flake.nix
47
flake.nix
@@ -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,46 @@
|
||||
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 {
|
||||
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;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
20
lib/agent.ex
20
lib/agent.ex
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user