-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
211 lines (185 loc) · 7.75 KB
/
Copy pathflake.nix
File metadata and controls
211 lines (185 loc) · 7.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
{
description = "Shared flake infrastructure for systemic-engineering projects";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, flake-utils, rust-overlay }:
{
# System-independent outputs
nixosModules.actor = import ./actor.nix;
} //
flake-utils.lib.eachDefaultSystem (system:
let
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs {
inherit system overlays;
};
beam = import ./beam.nix { inherit pkgs system; };
rust = import ./rust.nix {
inherit pkgs system;
rustOverlay = pkgs;
inherit (beam) worktreeGuard glueConnect;
};
nif = import ./nif.nix {
inherit pkgs system;
inherit (rust) rustTools;
};
conversation = import ./conversation.nix {
inherit pkgs system;
inherit (rust) rustTools cargoHook;
};
crate = import ./crate.nix {
inherit pkgs rust;
};
# ── Base tools ───────────────────────────────────────────────────────
baseTools = [
pkgs.git
pkgs.just
pkgs.jq
pkgs.curl
pkgs.dhall
pkgs.dhall-json
];
baseShellHook = ''
export LANG=en_US.UTF-8
'';
# ── Deploy script ────────────────────────────────────────────────────
# Builds body release on the VM (source available via virtiofs),
# copies to /opt/body, and restarts via systemd.
#
# Usage: nix run .#deploy-body [-- --strategy hot|cold]
# Set BODY_DIR to override the default body source path.
# Called by OBC pipeline's BEAM.Reload cascade.
deploy-body = pkgs.writeShellApplication {
name = "deploy-body";
runtimeInputs = [ pkgs.openssh ];
text = ''
set -euo pipefail
STRATEGY="cold"
while [[ $# -gt 0 ]]; do
case "$1" in
--strategy) STRATEGY="$2"; shift 2 ;;
*) echo "Unknown arg: $1" >&2; exit 1 ;;
esac
done
if [[ "$STRATEGY" != "hot" && "$STRATEGY" != "cold" ]]; then
echo "Invalid strategy: $STRATEGY (must be hot or cold)" >&2
exit 1
fi
# ── Find VM ──────────────────────────────────────────────────
VM_IP=""
for ip in $(arp -an 2>/dev/null | grep -oE '192\.168\.64\.[0-9]+' | grep -v '\.1$\|\.255$') \
$(seq 2 30 | xargs -I{} echo "192.168.64.{}"); do
if ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
-o ConnectTimeout=2 -o BatchMode=yes -o LogLevel=ERROR \
"reed@$ip" true 2>/dev/null; then
VM_IP="$ip"
break
fi
done
if [[ -z "$VM_IP" ]]; then
echo "VM not reachable." >&2
exit 1
fi
echo "VM at reed@$VM_IP"
SSH="ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR"
# ── Build ────────────────────────────────────────────────────
echo "Building body release on VM..."
BODY_DIR="''${BODY_DIR:-/Users/alexwolf/dev/projects/body}"
$SSH "reed@$VM_IP" BODY_DIR="$BODY_DIR" bash -l << 'BUILD'
set -euo pipefail
cd "$BODY_DIR"
export MIX_ENV=prod
export MIX_BUILD_ROOT=$PWD/_build-aarch64-linux
export MIX_HOME=$PWD/.nix-mix
export HEX_HOME=$PWD/.nix-hex
export PATH=$MIX_HOME/bin:$HEX_HOME/bin:$PATH
mix local.hex --force --if-missing
mix local.rebar --force --if-missing
mix deps.get --only prod
mix deps.compile
mix compile
mix release glue --overwrite
# Atomic deploy: copy to staging, then swap
rm -rf /opt/body-staging
cp -r _build-aarch64-linux/prod/rel/glue /opt/body-staging
if [ -d /opt/body ]; then
rm -rf /opt/body-old
mv /opt/body /opt/body-old
fi
mv /opt/body-staging /opt/body
rm -rf /opt/body-old
echo "Release deployed to /opt/body"
BUILD
# ── Reload ───────────────────────────────────────────────────
if [[ "$STRATEGY" == "cold" ]]; then
echo "Cold reload: restarting body via systemd..."
$SSH "root@$VM_IP" systemctl restart body
else
echo "Hot reload: signaling body to upgrade..."
$SSH "reed@$VM_IP" /opt/body/bin/glue rpc \
"IO.puts(\"Hot reload not yet implemented — restart manually\")"
fi
echo "Done. strategy=$STRATEGY"
'';
};
in {
# Export beam.nix and rust.nix for consumption by project flakes
lib.beam = beam;
lib.rust = rust;
lib.nif = nif;
lib.conversation = conversation;
lib.crate = crate;
devShells = {
# Minimal: git, just, jq, dhall.
default = pkgs.mkShell {
buildInputs = baseTools;
shellHook = baseShellHook;
};
# Elixir 1.18 / OTP 27 + base tools.
elixir = pkgs.mkShell {
buildInputs = baseTools ++ beam.elixirTools;
shellHook = baseShellHook + beam.elixirHook;
};
# Gleam + OTP 27 + base tools.
gleam = pkgs.mkShell {
buildInputs = baseTools ++ beam.gleamTools;
shellHook = baseShellHook + beam.gleamHook;
};
# Full BEAM: Elixir + Gleam + base tools.
beam = pkgs.mkShell {
buildInputs = baseTools ++ beam.elixirTools ++ beam.gleamTools;
shellHook = baseShellHook + beam.beamHook;
};
# Rust: cargo isolation + worktree guard + glue.
rust = pkgs.mkShell {
buildInputs = baseTools ++ rust.rustTools;
shellHook = baseShellHook + rust.rustHook;
};
# Gleam + Rust: for projects with Rust NIFs.
gleam-rust = pkgs.mkShell {
buildInputs = baseTools ++ beam.gleamTools ++ rust.rustTools;
shellHook = baseShellHook + beam.gleamHook + rust.cargoHook;
};
# Elixir + NIF: for Elixir projects with fragmentation Rustler NIFs.
elixir-nif = pkgs.mkShell {
buildInputs = baseTools ++ beam.elixirTools ++ nif.nifTools;
shellHook = baseShellHook + beam.elixirHook + nif.nifHook;
};
# Conversation: .conv package development.
conversation = pkgs.mkShell {
buildInputs = baseTools ++ conversation.conversationTools;
shellHook = baseShellHook + conversation.conversationHook;
};
};
apps.deploy-body = {
type = "app";
program = "${deploy-body}/bin/deploy-body";
};
});
}