cli: generate NixOS options from the config schema

Updates #1926
This commit is contained in:
Kristoffer Dalby committed 2026-11-11 12:19:28 +01:00
1 parent 439ea1b676
commit cd77d50bf6
9 files changed
+899 -7

No files matched your search

+2
View File
@@ -35,6 +35,8 @@ func TestSkipsInitConfig(t *testing.T) {
{"generate config with config flag", []string{"-c", "x.yaml", "generate", "config"}, true},
{"gen config", []string{"gen", "config"}, true},
{"help generate config", []string{"help", "generate", "config"}, true},
{"generate nixos-options", []string{"generate", "nixos-options"}, true},
{"generate nixos-options release", []string{"generate", "nixos-options", "--release"}, true},
{"generate private-key", []string{"generate", "private-key"}, false},
{"policy get", []string{"policy", "get"}, false},
{"help nodes", []string{"help", "nodes"}, false},
+55
View File
@@ -2,12 +2,14 @@ package cli
import (
"context"
"errors"
"fmt"
"net/http"
clientv1 "github.com/juanfont/headscale/gen/client/v1"
"github.com/juanfont/headscale/hscontrol/config"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/spf13/cobra"
"tailscale.com/types/key"
)
@@ -19,8 +21,13 @@ func init() {
generateConfigCmd.Flags().Bool("no-docs", false, "Leave out the documentation comments")
generateConfigCmd.Flags().Bool("running", false, "Print the running server's configuration, secrets redacted, instead of the defaults")
generateCmd.AddCommand(generateConfigCmd)
generateNixOSOptionsCmd.Flags().Bool("release", false, "Stamp the version and commit of this binary, which must be a clean build of a known commit")
generateCmd.AddCommand(generateNixOSOptionsCmd)
}
var errNotRelease = errors.New("--release needs a binary built from a clean, known commit")
var generateCmd = &cobra.Command{
Use: "generate",
Short: "Generate commands",
@@ -108,3 +115,51 @@ func printRunningConfig(cmd *cobra.Command, docs bool) error {
return err
})
}
var generateNixOSOptionsCmd = &cobra.Command{
Use: "nixos-options",
Short: "Print the NixOS options for services.headscale.settings",
Long: `Print the NixOS options for services.headscale.settings: one option per
configuration key, with its type, default, documentation and an example. The
output is a function of lib only, not a module:
options = import ./options.generated.nix { inherit lib; };
With --release, its header names this binary's version and commit instead
of the development tree.`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
release, err := cmd.Flags().GetBool("release")
if err != nil {
return err
}
var stamp *types.VersionInfo
if release {
stamp, err = releaseStamp(types.GetVersionInfo())
if err != nil {
return err
}
}
out, err := config.GenerateNixOptions(stamp)
if err != nil {
return fmt.Errorf("generating NixOS options: %w", err)
}
_, err = cmd.OutOrStdout().Write(out)
return err
},
}
// releaseStamp is v, if it can stamp a release: built from a known commit
// without local changes.
func releaseStamp(v *types.VersionInfo) (*types.VersionInfo, error) {
if v.Dirty || v.Commit == "unknown" {
return nil, fmt.Errorf("%w: commit %s, dirty %t", errNotRelease, v.Commit, v.Dirty)
}
return v, nil
}
+61
View File
@@ -363,3 +363,64 @@ func TestGenerateConfigCommand(t *testing.T) {
})
}
}
// TestGenerateNixOSOptionsCommand: generate nixos-options prints the
// options and only them, whatever config is around.
func TestGenerateNixOSOptionsCommand(t *testing.T) {
broken := writeFile(t, t.TempDir(), "broken.yaml", "bogus_key: 1\n")
want, err := config.GenerateNixOptions(nil)
require.NoError(t, err)
tests := []struct {
name string
args []string
wantStdout string
wantStderr string // "" when stderr must be empty
}{
{name: "development", args: []string{"generate", "nixos-options"}, wantStdout: string(want)},
// A test binary has no VCS stamp: commit unknown.
{name: "release from a dev build", args: []string{"generate", "nixos-options", "--release"}, wantStderr: "commit unknown"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
stdout, stderr, err := runHeadscale(t, []string{"HEADSCALE_BOGUS=1"}, append([]string{"-c", broken}, tt.args...)...)
if tt.wantStderr == "" {
require.NoError(t, err, stderr)
assert.Empty(t, stderr)
} else {
require.Error(t, err)
assert.Contains(t, stderr, tt.wantStderr)
}
assert.Equal(t, tt.wantStdout, stdout)
})
}
}
func TestReleaseStamp(t *testing.T) {
tests := []struct {
name string
info types.VersionInfo
wantErr bool
}{
{name: "release", info: types.VersionInfo{Version: "v1.2.3", Commit: "abc"}},
{name: "dirty", info: types.VersionInfo{Version: "v1.2.3", Commit: "abc", Dirty: true}, wantErr: true},
{name: "unknown commit", info: types.VersionInfo{Version: "dev", Commit: "unknown"}, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := releaseStamp(&tt.info)
if tt.wantErr {
require.ErrorIs(t, err, errNotRelease)
return
}
require.NoError(t, err)
assert.Equal(t, &tt.info, got)
})
}
}
+7 -7
View File
@@ -22,8 +22,8 @@ var (
// loaded is what initConfig loaded, or generate config --running, which
// skips initConfig and loads it itself; nil for the other commands that
// skip initConfig: check, generate config, version, mockoidc and
// completion.
// skip initConfig: check, generate config, generate nixos-options,
// version, mockoidc and completion.
loaded *config.Loaded
)
@@ -50,10 +50,10 @@ func init() {
})
}
// skipsInitConfig reports whether args select the check command group or
// generate config, which must work without a loadable config. Find runs
// lazily because init order across files is not guaranteed to have
// registered every command.
// skipsInitConfig reports whether args select the check command group,
// generate config or generate nixos-options, which must work without a
// loadable config. Find runs lazily because init order across files is not
// guaranteed to have registered every command.
func skipsInitConfig(args []string) bool {
cmd, rest, err := rootCmd.Find(args)
// `help check …` must work wherever `check …` does.
@@ -61,7 +61,7 @@ func skipsInitConfig(args []string) bool {
cmd, _, err = rootCmd.Find(rest)
}
return err == nil && (cmd == checkCmd || cmd.HasParent() && cmd.Parent() == checkCmd || cmd == generateConfigCmd)
return err == nil && (cmd == checkCmd || cmd.HasParent() && cmd.Parent() == checkCmd || cmd == generateConfigCmd || cmd == generateNixOSOptionsCmd)
}
// loadConfig loads path, else the file -c names, else $HEADSCALE_CONFIG,
+352
View File
@@ -0,0 +1,352 @@
package config
import (
"errors"
"fmt"
"maps"
"reflect"
"regexp"
"slices"
"strings"
"github.com/juanfont/headscale/hscontrol/types"
"tailscale.com/tailcfg"
)
const nixHeader = "# Generated from hscontrol/types/config.go by `headscale generate nixos-options`; do not edit.\n"
// nixTypes names the option types nixpkgs lacks, so an IP or a listen
// address of the wrong shape fails evaluation at the option.
const nixTypes = `let
o4 = "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])";
ip4 = "${o4}(\\.${o4}){3}";
# Two colons at least, so host:port is not taken for one.
ip6 = "[0-9A-Fa-f.]*:[0-9A-Fa-f.]*:[0-9A-Fa-f:.]*";
shape = description: pattern: lib.types.strMatching pattern // { inherit description; };
t = {
ip = shape "IP address" "(${ip4}|${ip6}(%[^%]+)?)?";
prefix = shape "IP prefix in CIDR notation" "(${ip4}/([0-9]|[12][0-9]|3[0-2])|${ip6}/([0-9]|[1-9][0-9]|1[01][0-9]|12[0-8]))?";
listenAddr = shape "host:port address" "((\\[[^]]*]|[^]:[]*):([0-9]+|https?))?";
httpURL = shape "http:// or https:// URL" "(https?://[^/?#]+([/?#].*)?)?";
duration = shape "duration such as 1h30m" "[-+]?(0|(([0-9]+(\\.[0-9]*)?|\\.[0-9]+)(ns|us|µs|μs|ms|s|m|h|d|w|y))+)";
fileMode = shape "quoted octal file mode" "(0[oO])?0*[0-7]{1,3}";
secretFile = shape "file: reference to a secret" "(file:.+)?";
readFile = lib.types.either lib.types.path (lib.types.enum [ "" ]);
stateFile = lib.types.either lib.types.externalPath (lib.types.enum [ "" ]);
};
in
`
var (
errNoNixType = errors.New("no NixOS type")
recordsType = reflect.TypeFor[[]tailcfg.DNSRecord]()
secretType = reflect.TypeFor[types.Secret]()
nixIdent = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_'-]*$`)
nixKeywords = []string{"assert", "else", "if", "in", "inherit", "let", "or", "rec", "then", "with"}
nixQuoted = strings.NewReplacer(`\`, `\\`, `"`, `\"`, "${", `\${`, "\n", `\n`, "\r", `\r`, "\t", `\t`)
// A ' before an escape starting with '' would join it, so it is
// escaped itself.
nixIndented = strings.NewReplacer("'${", `''\'''${`, "''", "'''", "${", "''${")
)
// GenerateNixOptions is the NixOS options of services.headscale.settings:
// a function of lib only, one option per key, nested as the file nests
// keys. release stamps a release's version and commit; nil marks the
// development tree.
func GenerateNixOptions(release *types.VersionInfo) ([]byte, error) {
keys, err := schema()
if err != nil {
return nil, err
}
var b strings.Builder
b.WriteString(nixHeader)
if release == nil {
b.WriteString("# headscale development tree, not a release\n")
} else {
fmt.Fprintf(&b, "# headscale %s, commit %s\n", release.Version, release.Commit)
}
b.WriteString("# Options for services.headscale.settings, not a module: import ./options.generated.nix { inherit lib; }\n{ lib, ... }:\n" + nixTypes + "{\n")
var open []string // the sections around the key, outermost first
for _, k := range keys {
if clientOnly(k.Path) {
continue
}
for len(open) > 0 && !strings.HasPrefix(k.Path, open[len(open)-1]+".") {
open = open[:len(open)-1]
fmt.Fprintf(&b, "%s};\n", strings.Repeat(" ", len(open)+1))
}
indent := 2 * (len(open) + 1)
name := nixAttr(k.Path[strings.LastIndex(k.Path, ".")+1:])
if k.Section {
fmt.Fprintf(&b, "%s%s = {\n", strings.Repeat(" ", indent), name)
open = append(open, k.Path)
continue
}
opt, err := nixOption(k, indent)
if err != nil {
return nil, fmt.Errorf("%s: %w", k.Path, err)
}
fmt.Fprintf(&b, "%s%s = %s;\n", strings.Repeat(" ", indent), name, opt)
}
for i := len(open); i > 0; i-- {
fmt.Fprintf(&b, "%s};\n", strings.Repeat(" ", i))
}
b.WriteString("}\n")
return []byte(b.String()), nil
}
// nixOption is the lib.mkOption call for leaf k, on a line indented by
// indent.
func nixOption(k keyInfo, indent int) (string, error) {
in := indent + 2
var typ string
switch {
case len(k.Type.Enum) > 0:
// Every enum decodes "" as its default.
typ = "lib.types.enum " + nixLiteral(append(toAny(k.Type.Enum), ""), in)
case k.Type.Nix != "":
typ = k.Type.Nix
case k.Field.Type == recordsType:
// The record fields' examples are the first example record's.
var first map[string]any
if records, _ := k.Example.([]any); len(records) > 0 {
first, _ = records[0].(map[string]any)
}
if first == nil {
return "", fmt.Errorf("%w: the example holds no record", errNoNixType)
}
var fields strings.Builder
pad := strings.Repeat(" ", in+6)
for _, f := range dnsRecordFields {
attrs := [][2]string{{"type", f.Nix}}
if f.Enum != nil {
attrs[0][1] = "lib.types.enum " + nixLiteral(toAny(f.Enum), in+8)
}
if f.Default != nil {
attrs = append(attrs, [2]string{"default", nixLiteral(f.Default, in+8)})
}
attrs = append(attrs,
[2]string{"description", nixLiteral(f.Doc+"\n", in+8)},
[2]string{"example", nixLiteral(first[f.Name], in+8)},
)
fmt.Fprintf(&fields, "%s%s = %s;\n", pad, f.Name, mkOption(in+6, attrs))
}
typ = fmt.Sprintf("lib.types.listOf (\n%[1]s lib.types.submodule {\n%[1]s options = {\n%[2]s%[1]s };\n%[1]s }\n%[1]s)",
strings.Repeat(" ", in), fields.String())
default:
return "", fmt.Errorf("%w for %s", errNoNixType, k.Field.Type)
}
attrs := [][2]string{{"type", typ}}
if !k.Required || k.RequiredWhen != "" {
attrs = append(attrs, [2]string{"default", nixLiteral(k.Default, in)})
}
if k.DefaultText != "" {
attrs = append(attrs, [2]string{"defaultText", "lib.literalMD " + nixLiteral(k.DefaultText, in)})
}
attrs = append(attrs,
[2]string{"description", nixLiteral(nixDescription(k)+"\n", in)},
[2]string{"example", nixLiteral(k.Example, in)},
)
return mkOption(indent, attrs), nil
}
// mkOption is a lib.mkOption call on a line indented by indent, of
// attribute names and Nix values.
func mkOption(indent int, attrs [][2]string) string {
var b strings.Builder
b.WriteString("lib.mkOption {\n")
for _, a := range attrs {
fmt.Fprintf(&b, "%s %s = %s;\n", strings.Repeat(" ", indent), a[0], a[1])
}
return b.String() + strings.Repeat(" ", indent) + "}"
}
// nixDescription is k's documentation in Markdown: its doc, its enum
// values, whether it is required and, for a secret, what the NixOS type
// accepts and how NixOS passes it as a file.
func nixDescription(k keyInfo) string {
parts := []string{k.Doc}
if len(k.Type.Enum) > 0 {
var values strings.Builder
values.WriteString("One of:\n")
for _, v := range k.Type.Enum {
fmt.Fprintf(&values, "\n- `%s`", v)
if doc := types.ConfigValueDoc(k.Field.Type, v); doc != "" {
values.WriteString(": " + doc)
}
}
parts = append(parts, values.String())
}
switch {
case k.Required && k.RequiredWhen == "":
parts = append(parts, "Required.")
case k.Required:
parts = append(parts, "Required when "+k.RequiredWhen+".")
}
switch {
case k.Field.Type == secretType:
parts = append(parts, nixCredential("the value must be `\"\"` or `file:<path>`", credentialName(k.Path), "the secret", "this option"))
case k.Path == providerKey:
parts = append(parts, nixCredential("keep a secret setting out of the Nix store", credentialName(k.Path+".<setting>"), "it", "the setting")+" `name` is not a secret.")
}
return strings.Join(parts, "\n\n")
}
// nixCredential is rule on NixOS, then how to keep what out of the Nix
// store: as the systemd credential name, which target reads with file:.
func nixCredential(rule, name, what, target string) string {
return "On NixOS, " + rule + ": load " + what + " as a systemd credential with " +
"`systemd.services.headscale.serviceConfig.LoadCredential = [ \"" + name + ":/run/secrets/" + name + "\" ];` " +
"and set " + target + " to `\"file:\\${CREDENTIALS_DIRECTORY}/" + name + "\"`."
}
// credentialName is the systemd credential NixOS loads the secret at path
// as.
func credentialName(path string) string {
return strings.ReplaceAll(path, ".", "_")
}
// nixLiteral is v, as plainFields prints values, as a Nix expression whose
// first line continues one indented by indent. A non-empty list or
// attribute set has one element per line, an attribute name is quoted
// unless it is an identifier, and a string ending in a newline is an
// indented string, as nixfmt leaves them.
func nixLiteral(v any, indent int) string {
pad := strings.Repeat(" ", indent+2)
switch x := v.(type) {
case nil:
return "null"
case string:
if !indentable(x) {
return `"` + nixQuoted.Replace(x) + `"`
}
var b strings.Builder
b.WriteString("''\n")
for l := range strings.Lines(x) {
if l != "\n" {
b.WriteString(pad)
}
b.WriteString(nixIndented.Replace(l))
}
return b.String() + strings.Repeat(" ", indent) + "''"
case []any:
if len(x) == 0 {
return "[ ]"
}
var b strings.Builder
b.WriteString("[\n")
for _, e := range x {
b.WriteString(pad + nixLiteral(e, indent+2) + "\n")
}
return b.String() + strings.Repeat(" ", indent) + "]"
case map[string]any:
if len(x) == 0 {
return "{ }"
}
var b strings.Builder
b.WriteString("{\n")
for _, k := range slices.Sorted(maps.Keys(x)) {
fmt.Fprintf(&b, "%s%s = %s;\n", pad, nixAttr(k), nixLiteral(x[k], indent+2))
}
return b.String() + strings.Repeat(" ", indent) + "}"
}
// shortcut: other values (bool, int, float64) print as Go does, which
// Nix reads alike; parenthesised, a negative number stays one list
// element. A float in exponent form (1e+21) is no Nix literal; format
// floats with a decimal point once an example holds one.
s := fmt.Sprint(v)
if strings.HasPrefix(s, "-") {
return "(" + s + ")"
}
return s
}
// indentable reports whether s reads back as written from an indented
// string: it ends in a newline, which the closing line keeps, and a line
// starts at the indentation Nix strips. Lines with trailing whitespace or
// a carriage return stay quoted.
func indentable(s string) bool {
lines := strings.Split(strings.TrimSuffix(s, "\n"), "\n")
return strings.HasSuffix(s, "\n") && !strings.Contains(s, "\r") &&
!slices.ContainsFunc(lines, func(l string) bool { return strings.TrimRight(l, " \t") != l }) &&
slices.ContainsFunc(lines, func(l string) bool { return l != "" && l[0] != ' ' })
}
// nixAttr is name as an attribute name, which takes no indented string.
func nixAttr(name string) string {
if nixIdent.MatchString(name) && !slices.Contains(nixKeywords, name) {
return name
}
return `"` + nixQuoted.Replace(name) + `"`
}
func toAny(s []string) []any {
out := make([]any, len(s))
for i, v := range s {
out[i] = v
}
return out
}
+290
View File
@@ -0,0 +1,290 @@
package config
import (
"regexp"
"slices"
"strings"
"testing"
gocmp "github.com/google/go-cmp/cmp"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var nixOptionLine = regexp.MustCompile(`^( *)(\S+) = (lib\.mkOption )?\{$`)
// nixOptions are the dotted paths of the lib.mkOption calls in src, in
// order, and each one's attributes, dedented to two spaces and between
// newlines. A "name = {" line opens a section.
func nixOptions(src string) ([]string, map[string]string) {
var (
paths, stack []string
indents []int
opts = map[string]string{}
)
lines := strings.Split(src, "\n")
for i, l := range lines {
m := nixOptionLine.FindStringSubmatch(l)
if m == nil {
continue
}
for len(indents) > 0 && indents[len(indents)-1] >= len(m[1]) {
indents, stack = indents[:len(indents)-1], stack[:len(stack)-1]
}
indents, stack = append(indents, len(m[1])), append(stack, m[2])
if m[3] == "" {
continue
}
path := strings.Join(stack, ".")
end := i + slices.Index(lines[i:], m[1]+"};")
var body []string
for _, b := range lines[i+1 : end] {
body = append(body, strings.TrimPrefix(b, m[1]))
}
paths = append(paths, path)
opts[path] = "\n" + strings.Join(body, "\n") + "\n"
}
return paths, opts
}
// nixCode is src without its comments and string literals.
func nixCode(src string) string {
var code strings.Builder
for i := 0; i < len(src); i++ {
switch {
case src[i] == '#':
for i < len(src) && src[i] != '\n' {
i++
}
case src[i] == '"':
for i++; i < len(src) && src[i] != '"'; i++ {
if src[i] == '\\' {
i++
}
}
case strings.HasPrefix(src[i:], "''"):
for i += 2; i < len(src); i++ {
rest := src[i:]
if strings.HasPrefix(rest, "'''") || strings.HasPrefix(rest, "''$") {
i += 2
} else if strings.HasPrefix(rest, "''\\") {
i += 3
} else if strings.HasPrefix(rest, "''") {
i++
break
}
}
default:
code.WriteByte(src[i])
}
}
return code.String()
}
func TestGenerateNixOptionsHeader(t *testing.T) {
tests := []struct {
name string
release *types.VersionInfo
want string
}{
{
name: "development",
want: "# Generated from hscontrol/types/config.go by `headscale generate nixos-options`; do not edit.\n" +
"# headscale development tree, not a release\n" +
"# Options for services.headscale.settings, not a module: import ./options.generated.nix { inherit lib; }\n" +
"{ lib, ... }:\n" + nixTypes + "{\n",
},
{
name: "release",
release: &types.VersionInfo{Version: "v1.2.3", Commit: "abc"},
want: "# Generated from hscontrol/types/config.go by `headscale generate nixos-options`; do not edit.\n" +
"# headscale v1.2.3, commit abc\n" +
"# Options for services.headscale.settings, not a module: import ./options.generated.nix { inherit lib; }\n" +
"{ lib, ... }:\n" + nixTypes + "{\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out, err := GenerateNixOptions(tt.release)
require.NoError(t, err)
got := string(out[:min(len(tt.want), len(out))])
if diff := gocmp.Diff(tt.want, got); diff != "" {
t.Errorf("header (-want +got):\n%s", diff)
}
})
}
}
func TestGenerateNixOptions(t *testing.T) {
out, err := GenerateNixOptions(nil)
require.NoError(t, err)
again, err := GenerateNixOptions(nil)
require.NoError(t, err)
require.Equal(t, string(out), string(again), "output is not deterministic")
src := string(out)
paths, opts := nixOptions(src)
d := Default()
// cli.* is read by the CLI from its own configuration, not the server's.
fields := slices.DeleteFunc(types.ConfigFields(&d), func(f types.ConfigField) bool { return strings.HasPrefix(f.Path, "cli.") })
t.Run("one option per key, nested, in order", func(t *testing.T) {
want := make([]string, 0, len(fields))
for _, f := range fields {
want = append(want, f.Path)
}
got := slices.DeleteFunc(slices.Clone(paths), func(p string) bool { return strings.HasPrefix(p, "dns.extra_records.") })
if diff := gocmp.Diff(want, got); diff != "" {
t.Errorf("option paths (-keys +options):\n%s", diff)
}
assert.Len(t, paths, len(fields)+len(dnsRecordFields))
assert.Equal(t, len(fields)+len(dnsRecordFields), strings.Count(src, "lib.mkOption"))
assert.NotRegexp(t, `(?m)^ cli = \{$`, src)
})
t.Run("every option has an example", func(t *testing.T) {
for _, p := range paths {
assert.Contains(t, opts[p], "\n example = ", p)
}
})
t.Run("a function of lib only", func(t *testing.T) {
// Free variables fail evaluation; these would not. The update
// operator is no path.
assert.NotRegexp(t, `import|builtins|[/<>]`, strings.ReplaceAll(nixCode(src), " // ", " "))
})
t.Run("types once", func(t *testing.T) {
assert.Equal(t, 1, strings.Count(src, nixTypes))
})
t.Run("layout", func(t *testing.T) {
assert.True(t, strings.HasSuffix(src, "}\n") && !strings.HasSuffix(src, "\n\n"), "ends in one newline")
assert.NotRegexp(t, `(?m)[ \t]$`, src, "trailing whitespace")
})
tests := []struct {
path string
want []string
hide []string
}{
{path: "server_url", want: []string{"\n example = \"https://", "Required."}, hide: []string{"\n default = "}},
{path: "dns.base_domain", want: []string{"\n default = \"\";\n", "Required when `dns.magic_dns`"}},
{
path: "database.type",
want: []string{
"\n type = lib.types.enum [\n \"sqlite\"\n", "\n \"postgres\"\n \"\"\n ];\n",
"\n - `postgres`: ",
},
},
{path: "tuning.batcher_workers", want: []string{"\n default = 0;\n defaultText = lib.literalMD \"three quarters of the CPU cores, at least one\";\n"}},
{
path: "oidc.client_secret",
want: []string{
"On NixOS, the value must be `\"\"` or `file:<path>`: load the secret as a systemd credential with ",
"`systemd.services.headscale.serviceConfig.LoadCredential = [ \"oidc_client_secret:/run/secrets/oidc_client_secret\" ];`",
// Rendered: "file:\${CREDENTIALS_DIRECTORY}/oidc_client_secret", as Nix source takes it.
"`\"file:\\''${CREDENTIALS_DIRECTORY}/oidc_client_secret\"`",
},
},
{
path: "dns.challenge.provider",
want: []string{
"`systemd.services.headscale.serviceConfig.LoadCredential = [ \"dns_challenge_provider_<setting>:/run/secrets/dns_challenge_provider_<setting>\" ];`",
"`\"file:\\''${CREDENTIALS_DIRECTORY}/dns_challenge_provider_<setting>\"`", "`name` is not a secret.",
},
},
{path: "dns.nameservers.split", want: []string{"\n type = lib.types.attrsOf (lib.types.listOf lib.types.str);\n", "\n \"corp.example.com\" = [\n"}},
{
path: "dns.extra_records",
want: []string{
"\n type = lib.types.listOf (\n lib.types.submodule {\n options = {\n name = lib.mkOption {\n",
"\n default = [ ];\n",
},
},
{path: "dns.extra_records.options.name", want: []string{"\n type = lib.types.str;\n"}, hide: []string{"\n default = "}},
{
path: "dns.extra_records.options.type",
want: []string{"\n type = lib.types.enum [\n \"\"\n \"A\"\n \"AAAA\"\n ];\n", "\n default = \"\";\n", "\n example = \"A\";\n"},
},
{path: "dns.extra_records.options.value", want: []string{"\n type = t.ip;\n"}, hide: []string{"\n default = "}},
{path: "log.level", want: []string{"\n example = \"debug\";\n"}},
}
for _, tt := range tests {
t.Run(tt.path, func(t *testing.T) {
opt, ok := opts[tt.path]
require.True(t, ok, "no option %s", tt.path)
for _, s := range tt.want {
assert.Contains(t, opt, s)
}
for _, s := range tt.hide {
assert.NotContains(t, opt, s)
}
})
}
}
func TestNixLiteral(t *testing.T) {
tests := []struct {
name string
v any
indent int
want string
}{
{name: "interpolation", v: "file:${CREDENTIALS_DIRECTORY}/x", want: `"file:\${CREDENTIALS_DIRECTORY}/x"`},
{name: "quote and backslash", v: `a"b\c`, want: `"a\"b\\c"`},
{name: "newline without a final one", v: "a\nb", want: `"a\nb"`},
{name: "every line indented", v: " a\n", want: `" a\n"`},
{
name: "indented string", v: "it''s ${x}\n\n'${y}\nend'\n", indent: 2,
want: "''\n it'''s ''${x}\n\n ''\\'''${y}\n end'\n ''",
},
{name: "true", v: true, want: "true"},
{name: "int", v: 8, want: "8"},
{name: "negative int", v: -1, want: "(-1)"},
{name: "null", v: nil, want: "null"},
{name: "empty list", v: []any{}, want: "[ ]"},
{name: "empty map", v: map[string]any{}, want: "{ }"},
{name: "one element", v: []any{"a"}, want: "[\n \"a\"\n]"},
{
name: "attr names", v: map[string]any{"corp.example.com": 1, "api_token": "t", "if": true},
want: "{\n api_token = \"t\";\n \"corp.example.com\" = 1;\n \"if\" = true;\n}",
},
// An indented string is no attribute name.
{name: "attr name with a newline", v: map[string]any{"line\n": 1, "or": 2}, want: "{\n \"line\\n\" = 1;\n \"or\" = 2;\n}"},
{
name: "nested", v: map[string]any{"a": map[string]any{"b": []any{1, "c"}}}, indent: 4,
want: "{\n a = {\n b = [\n 1\n \"c\"\n ];\n };\n }",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if diff := gocmp.Diff(tt.want, nixLiteral(tt.v, tt.indent)); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
}
+1
View File
@@ -1,6 +1,7 @@
package types
//go:generate go run ../../tools/configgen table . config_docs_generated.go
//go:generate go run ../../tools/configgen/files ../..
import (
"cmp"
+67
View File
@@ -0,0 +1,67 @@
// Command files writes the files generated from the configuration schema
// of hscontrol/config: `files <root>` writes nix/options.generated.nix
// below the repository root. It is not part of configgen, which stays
// standard library only so its table regenerates when the committed one
// does not compile.
//
// `files -load <file>` prints the file as config.Load reads it, for the
// NixOS options check to compare with the values it was written from.
package main
import (
"io"
"log"
"os"
"path/filepath"
"github.com/juanfont/headscale/hscontrol/config"
)
func main() {
var err error
switch {
case len(os.Args) == 3 && os.Args[1] == "-load":
err = load(os.Stdout, os.Args[2])
case len(os.Args) == 2:
err = write(os.Args[1])
default:
log.Fatal("usage: files <repository root> | files -load <config file>")
}
if err != nil {
log.Fatal(err)
}
}
// load writes path, as config.Load reads it, to w as YAML without the
// client-only keys, which no NixOS option sets.
func load(w io.Writer, path string) error {
l, err := config.Load(path, true, nil)
for _, warning := range l.Warnings {
log.Print(warning)
}
if err != nil {
return err
}
out, err := config.GenerateExample(l.Config, config.ExampleOptions{OmitClientOnly: true})
if err != nil {
return err
}
_, err = w.Write(out)
return err
}
// write writes every generated file below root.
func write(root string) error {
nix, err := config.GenerateNixOptions(nil)
if err != nil {
return err
}
return os.WriteFile(filepath.Join(root, "nix", "options.generated.nix"), nix, 0o600)
}
+64
View File
@@ -0,0 +1,64 @@
package main
import (
"bytes"
"io"
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/juanfont/headscale/hscontrol/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yaml.in/yaml/v3"
)
func TestWrite(t *testing.T) {
root := t.TempDir()
require.NoError(t, os.Mkdir(filepath.Join(root, "nix"), 0o755))
require.NoError(t, write(root))
want, err := config.GenerateNixOptions(nil)
require.NoError(t, err)
tests := []struct {
path string
want []byte
}{
{path: "nix/options.generated.nix", want: want},
}
for _, tt := range tests {
t.Run(tt.path, func(t *testing.T) {
got, err := os.ReadFile(filepath.Join(root, tt.path))
require.NoError(t, err)
if diff := cmp.Diff(string(tt.want), string(got)); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
}
// TestLoad: load prints a file as config.Load reads it, in canonical form
// and without the cli section; it fails on what Load rejects.
func TestLoad(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.json")
require.NoError(t, os.WriteFile(path, []byte(`{"server_url": "https://example.com", "node": {"ephemeral": {"inactivity_timeout": "60m"}}, "cli": {"timeout": "9s"}}`), 0o600))
var buf bytes.Buffer
require.NoError(t, load(&buf, path))
var got map[string]any
require.NoError(t, yaml.Unmarshal(buf.Bytes(), &got))
assert.Equal(t, "https://example.com", got["server_url"])
assert.Equal(t, "1h", got["node"].(map[string]any)["ephemeral"].(map[string]any)["inactivity_timeout"])
assert.NotContains(t, got, "cli")
broken := filepath.Join(dir, "broken.json")
require.NoError(t, os.WriteFile(broken, []byte(`{"bogus_key": 1}`), 0o600))
require.Error(t, load(io.Discard, broken))
}