gunk-dev/the-valley

nix.go

source6a6791929c36main's published tip history
1.2 KiB
1package nix
2
3import (
4 "context"
5 "fmt"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "time"
10)
11
12// SetupDevEnvironment checks if flake.nix exists in the given directory
13// and runs 'nix develop --command true' to set up the dev environment.
14// If the nix command fails, a warning is printed to stderr with the
15// combined output.
16func SetupDevEnvironment(dir string) {
17 if _, err := os.Stat(filepath.Join(dir, "flake.nix")); err != nil {
18 return
19 }
20 fmt.Println(" nix project detected, setting up dev environment...")
21 nixCmd := exec.Command("nix", "develop", "--command", "true")
22 nixCmd.Dir = dir
23 output, err := nixCmd.CombinedOutput()
24 if err != nil {
25 fmt.Fprintf(os.Stderr, "warning: nix develop failed: %v\n%s", err, string(output))
26 }
27}
28
29// HasFormatter reports whether the flake in dir has a formatter output for the
30// current system, which nix fmt runs. A missing flake, or one nix can't
31// evaluate, has none.
32func HasFormatter(ctx context.Context, dir string) bool {
33 if _, err := os.Stat(filepath.Join(dir, "flake.nix")); err != nil {
34 return false
35 }
36 // builtins.currentSystem needs --impure.
37 out, err := runNix(ctx, 2*time.Minute, "eval", "--impure", dir+"#formatter",
38 "--apply", "f: builtins.hasAttr builtins.currentSystem f")
39 return err == nil && out == "true"
40}