nix: add generated NixOS options

Updates #1926
This commit is contained in:
Kristoffer Dalby committed 2026-11-11 12:19:28 +01:00
1 parent cd77d50bf6
commit 63fdd50505
4 files changed
+1129

No files matched your search

+9
View File
@@ -64,3 +64,12 @@ jobs:
- uses: Mic92/hestia@dfed9ced335d28978ba74e513939a10db1f71025 # v3.1.0
- name: formatting
run: nix build -L .#checks.x86_64-linux.formatting
nixos-options:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: NixOS/nix-installer-action@62c1943b776c509394b550f3f983adc14e9212d6 # main
- uses: Mic92/hestia@dfed9ced335d28978ba74e513939a10db1f71025 # v3.1.0
- name: nixos-options
run: nix build -L .#checks.x86_64-linux.nixos-options
+96
View File
@@ -259,6 +259,101 @@
}
);
};
# nix/options.generated.nix: what the binary prints, every default
# and example of its option's type, the examples loaded unchanged by
# headscale, and descriptions that render.
nixos-options =
let
inherit (pkgs) lib;
options = import ./nix/options.generated.nix { inherit lib; };
each = f: lib.mapAttrsRecursiveCond (o: !lib.isOption o) (_: f) options;
eval = config: lib.evalModules { modules = [ { inherit options config; } ]; };
defaults = eval (each (o: lib.mkIf (!(o ? default)) o.example));
examples = eval (each (o: o.example));
doc = pkgs.nixosOptionsDoc { inherit (examples) options; };
# Development tool, not shipped: loads a file as headscale does.
files = pkgs.headscale.overrideAttrs { subPackages = [ "tools/configgen/files" ]; };
# Values of the wrong shape: each must fail the option it is set on.
rejected = {
listen_addr = [
"8080"
"::1:8080"
"0.0.0.0:abc"
];
"node.expiry" = [
"30"
""
"3 hours"
];
unix_socket_permission = [
"0778"
"07777"
504
];
"prefixes.v4" = [
"100.64.0.1"
"100.64.0.0/33"
];
"prefixes.v6" = [ "fd7a::/129" ];
"oidc.issuer" = [
"headscale.example.com"
"ftp://x"
"https://"
];
"oidc.client_secret" = [ "hunter2" ];
"database.postgres.pass" = [ "hunter2" ];
"dns.extra_records" = map (r: [ ({ name = "grafana.example.com"; } // r) ]) [
{ value = "grafana"; }
{ value = "256.1.1.1"; }
{
type = "CNAME";
value = "100.64.0.3";
}
{ }
];
};
# Only the option itself is forced, so nothing else can fail it.
fails =
name: value:
let
path = lib.splitString "." name;
config = (eval (lib.setAttrByPath path value)).config;
in
lib.hasAttrByPath path options
&& !(builtins.tryEval (builtins.deepSeq (lib.getAttrFromPath path config) null)).success;
accepted = lib.concatLists (
lib.mapAttrsToList (
name: values: map (v: "${name} = ${builtins.toJSON v}") (lib.filter (v: !fails name v) values)
) rejected
);
in
assert lib.assertMsg (
accepted == [ ]
) "nixos-options: types accept ${lib.concatStringsSep "; " accepted}";
pkgs.runCommand "headscale-nixos-options"
{
nativeBuildInputs = [
pkgs.headscale
pkgs.yq-go
pkgs.jq
];
# Unused; evaluating it checks every default against its type.
defaults = builtins.toJSON defaults.config;
examples = builtins.toJSON examples.config;
passAsFile = [ "examples" ];
}
''
headscale generate nixos-options | diff -u ${./nix/options.generated.nix} -
cp "$examplesPath" examples.json
${files}/bin/files -load examples.json > loaded.yaml
jq -S . examples.json > want.json
yq -o json . loaded.yaml | jq -S . > got.json
diff -u want.json got.json
# A secret's NixOS form renders as Nix source takes it.
grep -F '"file:\''${CREDENTIALS_DIRECTORY}/oidc_client_secret"' ${doc.optionsCommonMark}
touch $out
'';
in
{
# `nix develop`
@@ -313,6 +408,7 @@
goChecks
// {
headscale = pkgs.testers.runNixOSTest (import ./nix/tests/headscale.nix self);
inherit nixos-options;
}
);
}
File diff suppressed because it is too large. Load diff
+1
View File
@@ -21,6 +21,7 @@ var generatedPaths = map[string]bool{
"gen/client/v1/client.gen.go": true,
"gen/client/v2/client.gen.go": true,
".github/workflows/test-integration.yaml": true,
"nix/options.generated.nix": true,
}
// oapiPin finds the oapi-codegen version the Makefile pins, so the clients are