Skip to content

Commit a3fa239

Browse files
author
Your Name
committed
feat: add service type and environment variables to docker services
1 parent 9b41aef commit a3fa239

14 files changed

Lines changed: 194 additions & 129 deletions

File tree

docker-compose.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ services:
1818
SB_PROJECT_PROFILE: /snowblower/profile
1919
SB_PROJECT_ROOT: /snowblower
2020
SB_PROJECT_STATE: /snowblower/state
21+
SB_SERVICE_TYPE: builder
2122
SB_USER_GID: ${SB_USER_GID}
2223
SB_USER_UID: ${SB_USER_UID}
2324
SB_WORKSPACE_ROOT: /workspace
@@ -40,6 +41,7 @@ services:
4041
SB_PROJECT_PROFILE: /snowblower/profile
4142
SB_PROJECT_ROOT: /snowblower
4243
SB_PROJECT_STATE: /snowblower/state
44+
SB_SERVICE_TYPE: runtime
4345
SB_USER_GID: ${SB_USER_GID}
4446
SB_USER_UID: ${SB_USER_UID}
4547
SB_WORKSPACE_ROOT: /workspace
@@ -63,6 +65,7 @@ services:
6365
SB_PROJECT_PROFILE: /snowblower/profile
6466
SB_PROJECT_ROOT: /snowblower
6567
SB_PROJECT_STATE: /snowblower/state
68+
SB_SERVICE_TYPE: tools
6669
SB_USER_GID: ${SB_USER_GID}
6770
SB_USER_UID: ${SB_USER_UID}
6871
SB_WORKSPACE_ROOT: /workspace

flake-module.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ _localFlake: {...}: {
33
# keep-sorted start
44
./modules/command.nix
55
./modules/docker
6+
./modules/environment-variables.nix
67
./modules/files.nix
78
./modules/hooks.nix
89
./modules/integrations
@@ -11,7 +12,7 @@ _localFlake: {...}: {
1112
./modules/processes.nix
1213
./modules/services
1314
./modules/shell.nix
14-
./modules/snowblower-environment.nix
15+
./modules/snow.nix
1516
./modules/tools
1617
# keep-sorted end
1718
];

lib-bash/snow/down.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ function doSnowDownLogic() {
55
return 1
66
fi
77

8+
# Execute down hooks
9+
doHook__up__post
10+
811
# If it's running, run docker compose down and wait for it to finish then run doDestroySession
912
_iOk "Stopping SnowBlower services..."
1013
runDockerCompose --profile auto-start down --remove-orphans

lib-bash/snow/run.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function runNix() {
2+
$SB_DOCKER_PATH compose -f "$SB_WORKSPACE_ROOT/docker-compose.yml" run --rm builder nix "$@"
3+
}
4+
5+
function runBuilder() {
6+
$SB_DOCKER_PATH compose -f "$SB_WORKSPACE_ROOT/docker-compose.yml" run --rm builder "$@"
7+
}
8+
9+
function runDockerCompose() {
10+
$SB_DOCKER_PATH compose -f "$SB_WORKSPACE_ROOT/docker-compose.yml" "$@"
11+
}
12+
13+
function runDocker() {
14+
$SB_DOCKER_PATH "$@"
15+
}

lib-bash/snow/utils.sh

Lines changed: 0 additions & 15 deletions
This file was deleted.

lib/docker.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
"\${SB_PROJECT_STATE:-/tmp/snowblower/state}:/snowblower/state"
4141
"\${SB_PROJECT_ROOT:-/tmp/snowblower}:/snowblower"
4242
];
43+
environment = {
44+
"SB_SERVICE_TYPE" = "runtime";
45+
};
4346
working_dir = "/workspace";
4447
tty = true;
4548
})

modules/docker/compose.nix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ in {
132132
];
133133
working_dir = "/workspace";
134134
environment = {
135+
"SB_SERVICE_TYPE" = "tools";
135136
"SB_PROJECT_PROFILE" = "/snowblower/profile";
136137
"SB_PROJECT_STATE" = "/snowblower/state";
137138
};
@@ -160,6 +161,9 @@ in {
160161
"\${SB_PROJECT_STATE:-/tmp/snowblower/state}:/snowblower/state"
161162
"\${SB_PROJECT_ROOT:-/tmp/snowblower}:/snowblower"
162163
];
164+
environment = {
165+
"SB_SERVICE_TYPE" = "builder";
166+
};
163167
};
164168
manualStart = true;
165169
};

modules/docker/default.nix

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,24 @@ in {
3535
export SB_CONTAINER_NAME="$CONTAINER_NAME"
3636
export SB_SERVICE_NAME="$SERVICE_NAME"
3737
38+
if [ "$SB_SERVICE_TYPE" == "tools" ]; then
39+
snowblower-hooks tools_pre
40+
elif [ "$SB_SERVICE_TYPE" == "runtime" ]; then
41+
snowblower-hooks runtime_pre
42+
fi
43+
3844
_iNote "Executing: %s" "$*"
3945
expanded_args=()
4046
for arg in "$@"; do
4147
expanded_args+=("$(expand_vars "$arg")")
4248
done
4349
exec "''${expanded_args[@]}"
50+
51+
if [ "$SB_SERVICE_TYPE" == "tools" ]; then
52+
snowblower-hooks tools_post
53+
elif [ "$SB_SERVICE_TYPE" == "runtime" ]; then
54+
snowblower-hooks runtime_post
55+
fi
4456
'';
4557

4658
packages.runtime = [

modules/environment-variables.nix

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{flake-parts-lib, ...}: let
2+
inherit (flake-parts-lib) mkPerSystemOption;
3+
in {
4+
options.perSystem = mkPerSystemOption ({
5+
lib,
6+
config,
7+
pkgs,
8+
...
9+
}: let
10+
inherit (lib) mkOption types;
11+
12+
cfg = config.snowblower;
13+
in {
14+
options.snowblower = {
15+
environmentVariables = mkOption {
16+
default = {};
17+
type = with types;
18+
lazyAttrsOf (oneOf [
19+
str
20+
path
21+
int
22+
float
23+
]);
24+
example = {
25+
EDITOR = "emacs";
26+
};
27+
description = ''
28+
Environment variables to always set.
29+
'';
30+
};
31+
32+
environmentVariablesPackage = mkOption {
33+
type = types.package;
34+
internal = true;
35+
};
36+
};
37+
38+
config = {
39+
snowblower = {
40+
# Provide a file holding all session variables.
41+
environmentVariablesPackage = pkgs.writeTextFile {
42+
name = "sb-session-vars.sh";
43+
text = ''
44+
function doSetupEnvironmentVariables() {
45+
# Only source this once.
46+
if [ -v __SB_SESS_VARS_SOURCED ]; then return; fi
47+
export __SB_SESS_VARS_SOURCED=1
48+
49+
${lib.sbl.shell.exportAll cfg.environmentVariables}
50+
}
51+
52+
doSetupEnvironmentVariables
53+
'';
54+
};
55+
};
56+
};
57+
});
58+
}

modules/hooks.nix

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ in {
2323
name = "Docker Compose Tools Container";
2424
};
2525

26+
runtime = mkHook {
27+
name = "Docker Compose Runtime Container";
28+
};
29+
2630
package = mkOption {
2731
type = types.package;
2832
internal = true;
@@ -35,14 +39,6 @@ in {
3539

3640
config.snowblower = {
3741
hook.package = let
38-
# Generate hook functions for up.pre
39-
upPreHooks = lib.sbl.dag.resolveDag {
40-
name = "snowblower up pre hooks";
41-
dag = config.snowblower.hook.up.pre;
42-
mapResult = result:
43-
lib.concatLines (map (entry: entry.data) result);
44-
};
45-
4642
# Generate hook functions for tools.pre
4743
toolsPreHooks = lib.sbl.dag.resolveDag {
4844
name = "snowblower tools pre hooks";
@@ -58,39 +54,64 @@ in {
5854
mapResult = result:
5955
lib.concatLines (map (entry: entry.data) result);
6056
};
57+
58+
# Generate hook functions for runtime.pre
59+
runtimePreHooks = lib.sbl.dag.resolveDag {
60+
name = "snowblower runtime pre hooks";
61+
dag = config.snowblower.hook.runtime.pre;
62+
mapResult = result:
63+
lib.concatLines (map (entry: entry.data) result);
64+
};
65+
66+
# Generate hook functions for runtime.post
67+
runtimePostHooks = lib.sbl.dag.resolveDag {
68+
name = "snowblower runtime post hooks";
69+
dag = config.snowblower.hook.runtime.post;
70+
mapResult = result:
71+
lib.concatLines (map (entry: entry.data) result);
72+
};
6173
in
6274
pkgs.writeScriptBin "snowblower-hooks" ''
6375
#!/bin/bash
6476
65-
function doHook__up__pre {
66-
echo
67-
${upPreHooks}
68-
}
69-
7077
function doHook__tools__pre {
71-
echo
78+
echo -n
7279
${toolsPreHooks}
7380
}
7481
7582
function doHook__tools__post {
76-
echo
83+
echo -n
7784
${toolsPostHooks}
7885
}
86+
87+
function doHook__runtime__pre {
88+
echo -n
89+
${runtimePreHooks}
90+
}
91+
92+
function doHook__runtime__post {
93+
echo -n
94+
${runtimePostHooks}
95+
}
96+
7997
hook_name="$1"
8098
shift
8199
82100
case "$hook_name" in
83-
up_pre)
84-
doHook__up__pre "$@"
85-
exit 0
86-
;;
87101
tools_pre)
88102
doHook__tools__pre "$@"
89103
exit 0
90104
;;
91105
tools_post)
92106
doHook__tools__post "$@"
93107
;;
108+
runtime_pre)
109+
doHook__runtime__pre "$@"
110+
exit 0
111+
;;
112+
runtime_post)
113+
doHook__runtime__post "$@"
114+
;;
94115
*)
95116
echo "Unknown hook: $hook_name" >&2
96117
exit 1

0 commit comments

Comments
 (0)