Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] a hacky implementation of services #35

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions devshell.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,11 @@ help = "golang linter"
name = "golangci-lint"
package = "golangci-lint"
category = "linters"

[[commands]]
name = "hello-loop"
command = "while true; do hello; sleep 1; done"

[[services]]
name = "hello-printer"
command = "hello-loop"
42 changes: 41 additions & 1 deletion mkDevShell/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ let
inherit (config)
bash
commands
services
env
motd
name
Expand All @@ -57,9 +58,41 @@ let
(writeShellScriptBin name (toString command))
];
in
(builtins.concatMap op commands) ++ packages;
(builtins.concatMap op commands) ++ packages ++ [ serviceViewer ];
};

sessionName = "${name}-sessions";
serviceRunner = let
makeService = service: ''
echo "Starting service ${service.name}"
${pkgs.tmux}/bin/tmux new-window -n '${service.name}' -t '${sessionName}' '${service.command}'
'';
in pkgs.writeShellScript "service-runner" ''
# In case user is running multiple terminals with the same devshell
${pkgs.tmux}/bin/tmux list-sessions | grep '^${sessionName}: ' && exit 0

${pkgs.tmux}/bin/tmux new-session -d -s '${sessionName}'
${lib.strings.concatStringsSep
"\n"
(map
makeService
services
)
}
'';
serviceViewer = writeShellScriptBin "service-viewer" ''
serviceName="$1"
case "$serviceName" in
${lib.strings.concatStrings (map (service: "${service.name})\n;;\n") services)}
*)
echo "Invalid service name. Choose one of ${lib.strings.concatStringsSep "\n" (map (service: service.name) services)}"
exit 1
;;
esac

${pkgs.tmux}/bin/tmux select-window -t '${sessionName}':"$serviceName" \; a -t '${sessionName}'
'';

# write a bash profile to load
bashrc = writeText "${name}-bashrc" ''
# Set all the passed environment variables
Expand All @@ -73,6 +106,13 @@ let
# Expose the path to nixpkgs
export NIXPKGS_PATH=${toString pkgs.path}

function stopServices {
echo "Cleaning up services"
${pkgs.tmux}/bin/tmux kill-session -t '${sessionName}'
}
trap stopServices EXIT
${serviceRunner}

# Load installed profiles
for file in "$DEVSHELL_DIR/etc/profile.d/"*.sh; do
# If that folder doesn't exist, bash loves to return the whole glob
Expand Down
14 changes: 14 additions & 0 deletions mkDevShell/options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ let
'';
};
};

serviceOptions = {
name = mkOption {
type = types.str;
};
command = mkOption {
type = types.str;
};
};
in
{
options = {
Expand Down Expand Up @@ -160,6 +169,11 @@ in
'';
};

services = mkOption {
type = types.listOf (types.submodule { options = serviceOptions; });
default = [ ];
};

bash = mkOption {
type = types.submodule {
options = {
Expand Down