Skip to content

Commit

Permalink
Merge pull request #249 from shyim/add-hostsctl
Browse files Browse the repository at this point in the history
hostctl: init module
  • Loading branch information
domenkozar authored Jan 17, 2023
2 parents 818abea + 23f28be commit 167b533
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 23 deletions.
1 change: 0 additions & 1 deletion src/devenv-devShell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ pkgs.writeScriptBin "devenv" ''
case $command in
up)
procfilescript=${config.procfileScript}
cat $procfilescript
if [ "$(cat $procfilescript|tail -n +2)" = "" ]; then
echo "No 'processes' option defined: https://devenv.sh/processes/"
exit 1
Expand Down
1 change: 0 additions & 1 deletion src/devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ pkgs.writeScriptBin "devenv" ''
shell
eval "$env"
procfilescript=$($CUSTOM_NIX/bin/nix $NIX_FLAGS build --no-link --print-out-paths --impure '.#procfileScript')
cat $procfilescript
if [ "$(cat $procfilescript|tail -n +2)" = "" ]; then
echo "No 'processes' option defined: https://devenv.sh/processes/"
exit 1
Expand Down
47 changes: 47 additions & 0 deletions src/modules/integrations/hostctl.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{ pkgs, lib, config, ... }:

let
entries = lib.mapAttrsToList (hostname: ip: { inherit hostname ip; }) config.hosts;
entriesByIp = builtins.groupBy ({ ip, ... }: ip) entries;
hostnamesByIp = builtins.mapAttrs (hostname: entries: builtins.map ({ hostname, ... }: hostname) entries) entriesByIp;
lines = lib.mapAttrsToList (ip: hostnames: "${ip} ${lib.concatStringsSep " " hostnames}") hostnamesByIp;
hostContent = lib.concatStringsSep "\n" lines;
hostHash = builtins.hashString "sha256" hostContent;
file = pkgs.writeText "hosts" ''
# ${hostHash}
${hostContent}
'';
in
{
options = {
hostsProfileName = lib.mkOption {
type = lib.types.str;
default = "devenv-${builtins.hashString "sha256" config.env.DEVENV_ROOT}";
description = "Profile name to use.";
};

hosts = lib.mkOption {
type = lib.types.attrsOf lib.types.str;
default = { };
description = "List of hosts entries.";
example = {
"example.com" = "127.0.0.1";
};
};
};

config = lib.mkIf (hostContent != "") {
process.before = ''
if [[ ! -f "$DEVENV_STATE/hostctl" || "$(cat "$DEVENV_STATE/hostctl")" != "${hostHash}" ]]; then
sudo ${pkgs.hostctl}/bin/hostctl replace ${config.hostsProfileName} --from ${file}
mkdir -p "$DEVENV_STATE"
echo "${hostHash}" > "$DEVENV_STATE/hostctl"
fi
'';

process.after = ''
rm -f "$DEVENV_STATE/hostctl"
sudo ${pkgs.hostctl}/bin/hostctl remove ${config.hostsProfileName}
'';
};
}
77 changes: 56 additions & 21 deletions src/modules/processes.nix
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ let
builtins.removeAttrs config.env [ "DEVENV_PROFILE" ]
else
config.env);

procfileScripts = {
honcho = ''
${pkgs.honcho}/bin/honcho start -f ${config.procfile} --env ${config.procfileEnv} &
'';

overmind = ''
OVERMIND_ENV=${config.procfileEnv} ${pkgs.overmind}/bin/overmind start --procfile ${config.procfile} &
'';

process-compose = ''
${pkgs.process-compose}/bin/process-compose --config ${config.procfile} \
--port $PC_HTTP_PORT \
--tui=$PC_TUI_ENABLED &
'';

hivemind = ''
${pkgs.hivemind}/bin/hivemind --print-timestamps ${config.procfile} &
'';
};
in
{
options = {
Expand Down Expand Up @@ -77,6 +97,18 @@ in
log_level = "fatal";
};
};

before = lib.mkOption {
type = types.lines;
description = "Bash code to execute before starting processes.";
default = "";
};

after = lib.mkOption {
type = types.lines;
description = "Bash code to execute after stopping processes.";
default = "";
};
};

# INTERNAL
Expand Down Expand Up @@ -117,27 +149,30 @@ in
procfileEnv =
pkgs.writeText "procfile-env" (lib.concatStringsSep "\n" envList);

procfileScript = {
honcho = pkgs.writeShellScript "honcho-up" ''
echo "Starting processes ..." 1>&2
echo "" 1>&2
${pkgs.honcho}/bin/honcho start -f ${config.procfile} --env ${config.procfileEnv}
'';

overmind = pkgs.writeShellScript "overmind-up" ''
OVERMIND_ENV=${config.procfileEnv} ${pkgs.overmind}/bin/overmind start --procfile ${config.procfile}
'';

process-compose = pkgs.writeShellScript "process-compose-up" ''
${pkgs.process-compose}/bin/process-compose --config ${config.procfile} \
--port $PC_HTTP_PORT \
--tui=$PC_TUI_ENABLED
'';

hivemind = pkgs.writeShellScript "hivemind-up" ''
${pkgs.hivemind}/bin/hivemind --print-timestamps ${config.procfile}
'';
}.${implementation};
procfileScript = pkgs.writeShellScript "devenv-up" ''
${config.process.before}
${procfileScripts.${implementation}}
if [[ ! -d "$DEVENV_STATE" ]]; then
mkdir -p "$DEVENV_STATE"
fi
stop_up() {
echo "Stopping processes..."
kill -TERM $(cat "$DEVENV_STATE/devenv.pid")
rm "$DEVENV_STATE/devenv.pid"
wait
${config.process.after}
echo "Processes stopped."
}
trap stop_up SIGINT SIGTERM
echo $! > "$DEVENV_STATE/devenv.pid"
wait
'';

ci = [ config.procfileScript ];

Expand Down

0 comments on commit 167b533

Please sign in to comment.