Home

nixfig @main - refs - log -
-
https://git.jolheiser.com/nixfig.git
Nix as a Config
tree log patch
feat: add fmt Signed-off-by: jolheiser <john.olheiser@gmail.com>
Signature
-----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEgqEQpE3xoo1QwJO/uFOtpdp7v3oFAmZzhoIACgkQuFOtpdp7 v3pDBA/+OdOXpJIse5quJUo3SZtMf47cfzTD9nmEvzwYutXTYkQiz1FJJ5w9S/bG yva50YLOOwR0jnpzsRXUn9BVNLZNIoskrROESR+/ux6rEZUoIS8/6jxANpeRnC15 EYs1kWO2wEpOi1Mp1vLcecgRl0luqPyYulpQSgh4ik+M3vRTZcWadX9JccoW9thU fJckkMXFkpbWhVAdf6VYbLL3Vfs4nhMQBuIuDbccgArORGDdw41VO2LJqaSDvxu4 3XxS5MeTbMpQzSz7dcue8QZBUjpmhWQdUIFfSxSC23cvvvFMnR7K2/1GN1JUDzTL htPe/RVvyUX59nxkK3z38BjcY7OeDe0W1hjNy5gYKUHEPBwSGmNnlx5PCyg2VYnJ TPcLVRItfIzLrkeZzac/Q5LcRMjqLPcGZjcWYsq1M03yXGlR/KkGtChu37vuK4Ox ccCYVaBsl6+E5I4nQ8RvSudbo+0vOh2KG5SudBAXn9qSSBRwSESRu0MkbO7bPaX+ s9ChKZCGoAmjbO7StJuhaSdXWfucQx+ppTyQDjU68qAWxeSi7ZHfwEozjRedFs+V 85wDgp61B7TkahxbXaEXIwclMFOsusi4gjwfqdAII08rC1o+WaswiV2CUhrvwGbt XSXwJIbgz9B5iasheNYSL/TZLnaPXq4nT3/u0BZqYLJkrB0INmI= =r53x -----END PGP SIGNATURE-----
jolheiser <john.olheiser@gmail.com>
6 months ago
2 changed files, 61 additions(+), 2 deletions(-)
M nixfig.go -> nixfig.go
diff --git a/nixfig.go b/nixfig.go
index b91d34929c055cd58708980b26f806baee668465..d89e98b2bd19bb7fc9ac5033d11abe738f6a97e4 100644
--- a/nixfig.go
+++ b/nixfig.go
@@ -7,12 +7,14 @@ 	"errors"
 	"fmt"
 	"os"
 	"os/exec"
+	"strings"
 )
 
 var (
-	// Nix is the command to call for nix
+	Nix            string   // Nix is the command to call for nix
-	Nix            string
+	Fmt            []string // Fmt is the command (and args) to call to format the nix output
 	ErrNixNotFound = errors.New("nix was not found or set. You can set it either with `nixfig.Nix` or the `NIXFIG_NIX` environment variable")
+	ErrFmtNotFound = errors.New("nix formatter was not found or set. You can set it either with `nixfig.Fmt` or the `NIXFIG_FMT` environment variable")
 )
 
 func init() {
@@ -20,6 +22,17 @@ 	nixPath, _ := exec.LookPath("nix")
 	Nix = nixPath
 	if envPath, ok := os.LookupEnv("NIXFIG_NIX"); ok {
 		Nix = envPath
+	}
+
+	for _, formatter := range []string{"alejandra", "nixfmt-rfc-style", "nixfmt"} {
+		fmtPath, _ := exec.LookPath(formatter)
+		if fmtPath != "" {
+			Fmt = []string{fmtPath, "--quiet"}
+			break
+		}
+	}
+	if envPath, ok := os.LookupEnv("NIXFIG_FMT"); ok {
+		Fmt = strings.Split(envPath, " ")
 	}
 }
 
@@ -70,3 +83,29 @@ 	}
 
 	return stdout.Bytes(), nil
 }
+
+// MarshalFormat marshals a struct into a nix expression and formats it with Fmt
+func MarshalFormat(v any) ([]byte, error) {
+	if Fmt == nil {
+		return nil, ErrFmtNotFound
+	}
+
+	data, err := Marshal(v)
+	if err != nil {
+		return nil, err
+	}
+
+	var stdout, stderr bytes.Buffer
+	cmd := exec.Command(Fmt[0], Fmt[1:]...)
+	cmd.Stdin = bytes.NewBuffer(data)
+	cmd.Stdout = &stdout
+	cmd.Stderr = &stderr
+	if err := cmd.Run(); err != nil {
+		return nil, fmt.Errorf("could not run %v: %w", Fmt, err)
+	}
+	if stderr.Len() > 0 {
+		return nil, errors.New(stderr.String())
+	}
+
+	return stdout.Bytes(), nil
+}
M nixfig_test.go -> nixfig_test.go
diff --git a/nixfig_test.go b/nixfig_test.go
index 1d70aee17e7838ca41ce74183489c9ae304f7983..bfde939e03a2c1c9c20a271dafaaef339856a573 100644
--- a/nixfig_test.go
+++ b/nixfig_test.go
@@ -50,6 +50,16 @@
 	Nix = oldNix
 }
 
+func TestFmtNotFound(t *testing.T) {
+	oldFmt := Fmt
+	Fmt = nil
+
+	_, err := MarshalFormat(nil)
+	assert.IsError(t, err, ErrFmtNotFound)
+
+	Fmt = oldFmt
+}
+
 func TestUnmarshal(t *testing.T) {
 	data, err := os.ReadFile("testdata/config.nix")
 	assert.NoError(t, err)
@@ -69,3 +79,13 @@ 	err = Unmarshal(data, &cfg)
 	assert.NoError(t, err)
 	assert.Equal(t, testCfg, cfg)
 }
+
+func TestMarshalFormat(t *testing.T) {
+	data, err := MarshalFormat(testCfg)
+	assert.NoError(t, err)
+
+	var cfg Config
+	err = Unmarshal(data, &cfg)
+	assert.NoError(t, err)
+	assert.Equal(t, testCfg, cfg)
+}