@@ -52,28 +52,35 @@ func (n *NixConfigurator) InstallNix(
52
52
host string ,
53
53
sshKeyPath string ,
54
54
) error {
55
- // Esperar a que SSH esté disponible
55
+ // Wait for SSH to be available
56
56
fmt .Printf ("⏳ Waiting for SSH to be available on %s...\n " , host )
57
- for i := 0 ; i < 30 ; i ++ {
57
+ for i := 0 ; i < 60 ; i ++ {
58
58
checkCmd := exec .Command ("ssh" ,
59
59
"-i" , sshKeyPath ,
60
60
"-o" , "StrictHostKeyChecking=no" ,
61
- "-o" , "ConnectTimeout=5" ,
61
+ "-o" , "ConnectTimeout=10" ,
62
+ "-o" , "ServerAliveInterval=5" ,
63
+ "-o" , "ServerAliveCountMax=3" ,
62
64
fmt .Sprintf ("root@%s" , host ),
63
- "echo 'SSH is ready'" )
65
+ "echo 'SSH is ready' && test -w /root " )
64
66
65
67
if err := checkCmd .Run (); err == nil {
68
+ fmt .Printf ("✅ SSH connection established to %s\n " , host )
66
69
break
67
70
}
68
71
69
- if i == 29 {
72
+ if i == 59 {
70
73
return fmt .Errorf ("timeout waiting for SSH to be ready" )
71
74
}
72
75
73
- fmt .Printf (" Retrying in 10 seconds... (%d/30 )\n " , i + 1 )
76
+ fmt .Printf (" Retrying in 10 seconds... (%d/60 )\n " , i + 1 )
74
77
time .Sleep (10 * time .Second )
75
78
}
76
79
80
+ // Give the system a moment to stabilize
81
+ fmt .Println ("⏳ Waiting for system to stabilize..." )
82
+ time .Sleep (15 * time .Second )
83
+
77
84
// Verificar si Nix ya está instalado
78
85
checkCmd := exec .Command ("ssh" ,
79
86
"-i" , sshKeyPath ,
@@ -88,7 +95,31 @@ func (n *NixConfigurator) InstallNix(
88
95
89
96
fmt .Printf ("🔧 Installing Nix on %s...\n " , host )
90
97
91
- // Descargar e instalar Nix directamente
98
+ // Clean up any previous Nix installation files
99
+ cleanupCmd := exec .Command ("ssh" ,
100
+ "-i" , sshKeyPath ,
101
+ "-o" , "StrictHostKeyChecking=no" ,
102
+ fmt .Sprintf ("root@%s" , host ),
103
+ `rm -f /etc/bash.bashrc.backup-before-nix \
104
+ /etc/profile.backup-before-nix \
105
+ /etc/zshrc.backup-before-nix \
106
+ /etc/bashrc.backup-before-nix \
107
+ /etc/profile.d/nix.sh.backup-before-nix \
108
+ /etc/profile.d/nix.sh \
109
+ && \
110
+ rm -rf /nix /etc/nix ~/.nix* /root/.nix* && \
111
+ mkdir -p /etc/profile.d && \
112
+ touch /etc/profile.d/nix.sh && \
113
+ systemctl daemon-reload` )
114
+
115
+ cleanupCmd .Stdout = os .Stdout
116
+ cleanupCmd .Stderr = os .Stderr
117
+
118
+ if err := cleanupCmd .Run (); err != nil {
119
+ fmt .Printf ("⚠️ Warning: Failed to clean up previous installation: %v\n " , err )
120
+ }
121
+
122
+ // Download and install Nix directly
92
123
cmd := exec .Command ("ssh" ,
93
124
"-i" , sshKeyPath ,
94
125
"-o" , "StrictHostKeyChecking=no" ,
@@ -102,7 +133,7 @@ func (n *NixConfigurator) InstallNix(
102
133
return fmt .Errorf ("failed to install Nix: %v" , err )
103
134
}
104
135
105
- // Configurar Nix
136
+ // Configure Nix
106
137
fmt .Println ("⚙️ Configuring Nix..." )
107
138
configCmd := exec .Command ("ssh" ,
108
139
"-i" , sshKeyPath ,
@@ -123,7 +154,7 @@ func (n *NixConfigurator) InstallNix(
123
154
return nil
124
155
}
125
156
126
- // ApplyConfiguration applies the Nix configuration to a host
157
+ // Apply Configuration applies the Nix configuration to a host
127
158
func (n * NixConfigurator ) ApplyConfiguration (
128
159
host ,
129
160
sshKeyPath ,
@@ -166,7 +197,29 @@ func (n *NixConfigurator) ApplyConfiguration(
166
197
"-i" , resolvedPath ,
167
198
"-o" , "StrictHostKeyChecking=no" ,
168
199
fmt .Sprintf ("root@%s" , host ),
169
- "nixos-rebuild test && nixos-rebuild switch" ) // test primero para validar
200
+ `source /etc/profile && \
201
+ export PATH=$PATH:/nix/var/nix/profiles/default/bin && \
202
+ export NIX_PATH=nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/etc/nixos/configuration.nix && \
203
+ nix-collect-garbage -d && \
204
+ rm -rf /nix/var/nix/profiles && \
205
+ mkdir -p /nix/var/nix/profiles && \
206
+ nix-env -iA nixos.nixos-rebuild && \
207
+ export PATH=$PATH:/root/.nix-profile/bin && \
208
+ free -h && \
209
+ TMPDIR=/tmp nixos-rebuild switch \
210
+ --option sandbox false \
211
+ --option cores 1 \
212
+ --max-jobs 1 \
213
+ --option binary-caches https://cache.nixos.org \
214
+ --option trusted-binary-caches https://cache.nixos.org \
215
+ --option binary-cache-public-keys cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= \
216
+ --option use-substitutes true \
217
+ --option build-use-substitutes true \
218
+ --option enforce-substitutes true \
219
+ --option build-cores 1 \
220
+ --option system-features "" \
221
+ --no-build-output \
222
+ --show-trace` )
170
223
171
224
applyCmd .Stdout = os .Stdout
172
225
applyCmd .Stderr = os .Stderr
@@ -181,31 +234,12 @@ func (n *NixConfigurator) PrepareNixOS(
181
234
) error {
182
235
fmt .Printf ("🔧 Preparing NixOS on %s...\n " , host )
183
236
184
- // Instalar nixos-install
185
- fmt .Println ("📦 Installing NixOS tools..." )
186
- installCmd := exec .Command ("ssh" ,
187
- "-i" , sshKeyPath ,
188
- "-o" , "StrictHostKeyChecking=no" ,
189
- fmt .Sprintf ("root@%s" , host ),
190
- `source /etc/profile && \
191
- nix-env -iA \
192
- nixos.nixos-install-tools \
193
- nixos.nixos-rebuild \
194
- nixos.nix` )
195
-
196
- installCmd .Stdout = os .Stdout
197
- installCmd .Stderr = os .Stderr
198
-
199
- if err := installCmd .Run (); err != nil {
200
- return fmt .Errorf ("failed to install NixOS tools: %v" , err )
201
- }
202
-
203
- // Crear directorios necesarios
237
+ // Create necessary directories first
204
238
createDirsCmd := exec .Command ("ssh" ,
205
239
"-i" , sshKeyPath ,
206
240
"-o" , "StrictHostKeyChecking=no" ,
207
241
fmt .Sprintf ("root@%s" , host ),
208
- `mkdir -p /etc/nixos && \
242
+ `mkdir -p /etc/nixos/cloud && \
209
243
touch /etc/nixos/configuration.nix && \
210
244
chmod 644 /etc/nixos/configuration.nix` )
211
245
@@ -216,44 +250,67 @@ func (n *NixConfigurator) PrepareNixOS(
216
250
return fmt .Errorf ("failed to create NixOS directories: %v" , err )
217
251
}
218
252
219
- // Crear archivo base.nix
220
- baseNixCmd := exec .Command ("ssh" ,
253
+ // Copy NixOS configuration files
254
+ fmt .Println ("📁 Copying NixOS configuration files..." )
255
+ copyFilesCmd := exec .Command ("ssh" ,
221
256
"-i" , sshKeyPath ,
222
257
"-o" , "StrictHostKeyChecking=no" ,
223
258
fmt .Sprintf ("root@%s" , host ),
224
- `cat > /etc/nixos/base.nix << 'EOL'
225
- { config, pkgs, ... }:
226
- {
227
- imports = [ ];
228
-
229
- boot.loader.grub.enable = true;
230
- boot.loader.grub.version = 2;
231
-
232
- networking.useDHCP = true;
233
-
234
- services.openssh.enable = true;
235
- services.openssh.permitRootLogin = "yes";
236
-
237
- users.users.root.openssh.authorizedKeys.keys = [
238
- "$(cat ~/.ssh/authorized_keys)"
239
- ];
240
-
241
- system.stateVersion = "23.11";
242
- }
243
- EOL` )
259
+ `mkdir -p /etc/nixos/cloud` )
244
260
245
- baseNixCmd .Stdout = os .Stdout
246
- baseNixCmd .Stderr = os .Stderr
261
+ if err := copyFilesCmd .Run (); err != nil {
262
+ return fmt .Errorf ("failed to create cloud directory: %v" , err )
263
+ }
247
264
248
- if err := baseNixCmd .Run (); err != nil {
249
- return fmt .Errorf ("failed to create base.nix: %v" , err )
265
+ // Copy configuration files to their respective locations
266
+ copyFilesCmd = exec .Command ("scp" ,
267
+ "-i" , sshKeyPath ,
268
+ "-o" , "StrictHostKeyChecking=no" ,
269
+ "-r" ,
270
+ "nix/base.nix" ,
271
+ fmt .Sprintf ("root@%s:/etc/nixos/base.nix" , host ))
272
+
273
+ if err := copyFilesCmd .Run (); err != nil {
274
+ return fmt .Errorf ("failed to copy base.nix: %v" , err )
275
+ }
276
+
277
+ copyFilesCmd = exec .Command ("scp" ,
278
+ "-i" , sshKeyPath ,
279
+ "-o" , "StrictHostKeyChecking=no" ,
280
+ "-r" ,
281
+ "nix/cloud/digitalocean.nix" ,
282
+ fmt .Sprintf ("root@%s:/etc/nixos/cloud/digitalocean.nix" , host ))
283
+
284
+ copyFilesCmd .Stdout = os .Stdout
285
+ copyFilesCmd .Stderr = os .Stderr
286
+
287
+ if err := copyFilesCmd .Run (); err != nil {
288
+ return fmt .Errorf ("failed to copy NixOS configuration files: %v" , err )
289
+ }
290
+
291
+ // Install NixOS tools first
292
+ fmt .Println ("📦 Installing NixOS tools..." )
293
+ nixosToolsCmd := exec .Command ("ssh" ,
294
+ "-i" , sshKeyPath ,
295
+ "-o" , "StrictHostKeyChecking=no" ,
296
+ fmt .Sprintf ("root@%s" , host ),
297
+ `source /etc/profile && \
298
+ nix-channel --add https://nixos.org/channels/nixos-unstable nixos && \
299
+ nix-channel --update && \
300
+ nix-env -iA nixos.nixos-install nixos.nixos-rebuild` )
301
+
302
+ nixosToolsCmd .Stdout = os .Stdout
303
+ nixosToolsCmd .Stderr = os .Stderr
304
+
305
+ if err := nixosToolsCmd .Run (); err != nil {
306
+ return fmt .Errorf ("failed to install NixOS tools: %v" , err )
250
307
}
251
308
252
309
fmt .Printf ("✅ NixOS preparation completed on %s\n " , host )
253
310
return nil
254
311
}
255
312
256
- // InstallNixOS performs the NixOS installation
313
+ // Install NixOS performs the NixOS installation
257
314
func (n * NixConfigurator ) InstallNixOS (
258
315
host string ,
259
316
sshKeyPath string ,
0 commit comments