Skip to content

Commit 8a43f95

Browse files
committed
lib/validateJobs: infer host type if not declared
1 parent 635467a commit 8a43f95

3 files changed

Lines changed: 24 additions & 6 deletions

File tree

flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
nixostest = {
101101
hostname = "nixostest"; # Will be assumed from deployment name if not specified.
102102
output = self.nixosConfigurations.nixostest.config.system.build.toplevel;
103-
type = "nixos";
103+
# type = "nixos"; # Will be inferred based on the output.
104104
user = "root";
105105
};
106106

src/lib.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func runJSON(cmd string, args ...string) ([]byte, error) {
156156
return out, nil
157157
}
158158

159-
func validateJobs(jobs map[string]JobSpec) (map[string]JobSpec, error) {
159+
func validateJobs(jobs map[string]JobSpec, flake string) (map[string]JobSpec, error) {
160160
validated := make(map[string]JobSpec)
161161

162162
for name, spec := range jobs {
@@ -167,12 +167,30 @@ func validateJobs(jobs map[string]JobSpec) (map[string]JobSpec, error) {
167167
}
168168

169169
if spec.Output == "" {
170-
return nil, fmt.Errorf("unsupported system type '%s' for job: %s", spec.Type, name)
171-
170+
return nil, fmt.Errorf("missing 'output' for job: %s", name)
172171
}
173172

174173
if spec.User == "" {
175-
return nil, fmt.Errorf("missing user for job: %s", name)
174+
return nil, fmt.Errorf("missing 'user' for job: %s", name)
175+
}
176+
177+
// Infer Type if missing
178+
if spec.Type == "" {
179+
expr := fmt.Sprintf("%s#nynxDeployments.%s.output.system", flake, name)
180+
data, err := run("nix", "--extra-experimental-features", "nix-command flakes", "eval", "--raw", expr)
181+
if err != nil {
182+
return nil, fmt.Errorf("failed to infer type for job '%s': %w", name, err)
183+
}
184+
system := strings.TrimSpace(string(data))
185+
186+
switch {
187+
case strings.Contains(system, "darwin"):
188+
spec.Type = "darwin"
189+
case strings.Contains(system, "linux"):
190+
spec.Type = "nixos"
191+
default:
192+
return nil, fmt.Errorf("could not infer system type for job '%s' from system '%s'", name, system)
193+
}
176194
}
177195

178196
if spec.Type != "nixos" && spec.Type != "darwin" {

src/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func main() {
5252
fatal("Failed to load deployments: %v", err)
5353
}
5454

55-
validatedJobs, err := validateJobs(jobs)
55+
validatedJobs, err := validateJobs(jobs, flake)
5656
if err != nil {
5757
fatal("Invalid deployments: %v", err)
5858
}

0 commit comments

Comments
 (0)