diff --git a/.gitignore b/.gitignore index 1f19321..804e0e2 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,4 @@ frajtano-*.tar /.nix-hex /.nix-mix +/result diff --git a/config/runtime.exs b/config/runtime.exs new file mode 100644 index 0000000..f763b8c --- /dev/null +++ b/config/runtime.exs @@ -0,0 +1,2 @@ +import Config +config :frajtano, listen_path: Path.expand(System.get_env("FRAJTANO_DIR")) |> Path.join("agent.sock") diff --git a/default.nix b/default.nix index 4d863e4..e34944f 100644 --- a/default.nix +++ b/default.nix @@ -1,24 +1,40 @@ { + lib, + pkgs, mixRelease, - fetchMixDeps, elixir, -}: -mixRelease rec { +}: let pname = "frajtano"; - version = "0.0.1"; + pkg = mixRelease { + inherit pname; + version = "0.0.1"; - inherit elixir; - src = builtins.path { - path = ./.; - name = "${pname}-source"; - filter = path: _type: baseNameOf path != "flake.nix" && baseNameOf path != "flake.lock"; - }; - mixFodDeps = fetchMixDeps { - pname = "mix-deps-${pname}"; - inherit version src; - sha256 = "sha256-lknkq6b1yDOLgTpR/tPEk9UKqpqQT6H1N5g15M60nCA="; - }; + inherit elixir; + src = builtins.path { + path = ./.; + name = "${pname}-source"; + filter = path: _type: baseNameOf path != "flake.nix" && baseNameOf path != "flake.lock"; + }; - ELIXIR_MAKE_CACHE_DIR = "/tmp/.elixir-make-cache"; - meta.mainProgram = "${pname}"; -} + ELIXIR_MAKE_CACHE_DIR = "/tmp/.elixir-make-cache"; + meta.mainProgram = pname; + }; + script = pkgs.writeShellScriptBin pname '' + set -eu + file="$FRAJTANO_DIR/cookie" + (umask 077; [ -f "$file" ] || ${pkgs.coreutils}/bin/head -c 128 /dev/urandom | ${pkgs.coreutils}/bin/base64 -w0 > "$file") + export RELEASE_COOKIE=$(${pkgs.coreutils}/bin/cat "$file") + run() { + exec ${lib.getExe pkg} "$@" + } + + case $1 in + assimilate) + run rpc ":ok = \"$(echo -n "$2" | ${pkgs.coreutils}/bin/base64)\" |> Base.decode64!() |> Frajtano.Agent.add_peer()" + ;; + *) + run "$@" + ;; + esac + ''; +in pkgs.symlinkJoin { name = pname; paths = [ script pkg ]; meta.mainProgram = pname; } diff --git a/lib/agent.ex b/lib/agent.ex index 1685ba2..a3bc661 100644 --- a/lib/agent.ex +++ b/lib/agent.ex @@ -49,6 +49,7 @@ defmodule Frajtano.Agent do @impl true def handle_call({:add_peer, path}, _from, state) do + # TODO: deduplicate peers by socket path case Peer.start(path) do {:ok, _} -> {:reply, :ok, state} {:error, error} -> {:reply, {:error, error}, state} diff --git a/lib/frajtano.ex b/lib/frajtano.ex index 867418a..88d1233 100644 --- a/lib/frajtano.ex +++ b/lib/frajtano.ex @@ -18,7 +18,7 @@ defmodule Frajtano.Supervisor do def init(:ok) do children = [ Frajtano.Agent, - {Frajtano.Listener, ["/tmp/frajtano1"]}, + {Frajtano.Listener, [Application.fetch_env!(:frajtano, :listen_path)]}, {Task.Supervisor, name: Frajtano.ClientSupervisor}, {DynamicSupervisor, name: Frajtano.Peer} ] diff --git a/mix.exs b/mix.exs index ff47929..d9b6c2e 100644 --- a/mix.exs +++ b/mix.exs @@ -14,6 +14,7 @@ defmodule Frajtano.MixProject do def application do [ extra_applications: [:logger], + env: [listen_path: nil], mod: {Frajtano, []} ] end