diff --git a/flake.nix b/flake.nix index 27c8d4d60..67a0426d3 100644 --- a/flake.nix +++ b/flake.nix @@ -172,8 +172,13 @@ headscale-docker = pkgs.dockerTools.buildLayeredImage { name = "headscale"; tag = headscaleVersion; - contents = [ pkgs.headscale ]; + # CA certificates for outbound HTTPS, such as the DERP map. + contents = [ + pkgs.headscale + pkgs.cacert + ]; config.Entrypoint = [ (pkgs.headscale + "/bin/headscale") ]; + config.Env = [ "SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt" ]; }; # Go flake checks from the flake-checks library. CI gates on diff --git a/hscontrol/config/load_test.go b/hscontrol/config/load_test.go index a5d346591..863cbd8e6 100644 --- a/hscontrol/config/load_test.go +++ b/hscontrol/config/load_test.go @@ -3308,7 +3308,9 @@ func unsetHeadscaleEnv(t *testing.T) { // dns.nameservers.global set; nixos-module-minimal.yaml is the flake module's // configFile with only those three set, before the module moved to the // generated options, and nixos-*-options.yaml the same two configFiles after -// it; nixos-module-cli.env is what that module exports for the local CLI; +// it; nixos-module-minimal-defaults.yaml is that module's configFile again once +// it set only its state paths, socket and disable_check_updates; +// nixos-module-cli.env is what that module exports for the local CLI; // hsic-* mirror integration/hsic's // MinimumConfigYAML and DefaultConfigEnv. -strict is the same source // as updated for the strict loader, -pruned without the keys removed diff --git a/hscontrol/config/testdata/compat/nixos-module-minimal-defaults.golden.json b/hscontrol/config/testdata/compat/nixos-module-minimal-defaults.golden.json new file mode 100644 index 000000000..6d3988abd --- /dev/null +++ b/hscontrol/config/testdata/compat/nixos-module-minimal-defaults.golden.json @@ -0,0 +1,191 @@ +{ + "config": { + "ServerURL": "https://headscale.example.com", + "Addr": "0.0.0.0:8080", + "MetricsAddr": "", + "TrustedProxies": null, + "Node": { + "Expiry": 0, + "Ephemeral": { + "InactivityTimeout": 1800000000000 + }, + "Routes": { + "HA": { + "ProbeInterval": 10000000000, + "ProbeTimeout": 5000000000 + } + } + }, + "PreAuthKeys": { + "RevokedRetention": 604800000000000 + }, + "PrefixV4": "100.64.0.0/10", + "PrefixV6": "fd7a:115c:a1e0::/48", + "IPAllocation": "sequential", + "NoisePrivateKeyPath": "/var/lib/headscale/noise_private.key", + "BaseDomain": "tailnet.example.com", + "Log": { + "Format": "text", + "Level": "info" + }, + "DisableUpdateCheck": true, + "Database": { + "Type": "sqlite3", + "Debug": false, + "Gorm": { + "Debug": false, + "SlowThreshold": 0, + "SkipErrRecordNotFound": false, + "ParameterizedQueries": false + }, + "Sqlite": { + "Path": "/var/lib/headscale/db.sqlite", + "WriteAheadLog": true, + "WALAutoCheckPoint": 1000 + }, + "Postgres": { + "Host": "", + "Port": 0, + "Name": "", + "User": "", + "Ssl": "false", + "MaxOpenConnections": 10, + "MaxIdleConnections": 10, + "ConnMaxIdleTimeSecs": 3600 + } + }, + "DERP": { + "ServerEnabled": false, + "AutomaticallyAddEmbeddedDerpRegion": true, + "ServerRegionID": 0, + "ServerRegionCode": "", + "ServerRegionName": "", + "ServerPrivateKeyPath": "/var/lib/headscale/derp_server_private.key", + "ServerVerifyClients": true, + "STUNAddr": "", + "URLs": [ + { + "Scheme": "https", + "Opaque": "", + "User": null, + "Host": "controlplane.tailscale.com", + "Path": "/derpmap/default", + "Fragment": "", + "RawQuery": "", + "RawPath": "", + "RawFragment": "", + "ForceQuery": false, + "OmitHost": false + } + ], + "Paths": [], + "DERPMap": null, + "AutoUpdate": true, + "UpdateFrequency": 10800000000000, + "IPv4": "", + "IPv6": "" + }, + "TLS": { + "CertPath": "", + "KeyPath": "", + "ACME": { + "Hostname": "", + "ChallengeType": "HTTP-01", + "HTTPListen": ":http", + "URL": "", + "Email": "", + "CacheDir": "/var/lib/headscale/cache" + } + }, + "DNSConfig": { + "MagicDNS": true, + "BaseDomain": "tailnet.example.com", + "OverrideLocalDNS": true, + "Nameservers": { + "Global": [ + "1.1.1.1", + "8.8.8.8" + ], + "Split": {} + }, + "SearchDomains": [], + "ExtraRecords": [], + "ExtraRecordsPath": "", + "Certificates": { + "Enabled": false, + "Zone": "tailnet.example.com" + } + }, + "TailcfgDNSConfig": { + "Resolvers": [ + { + "Addr": "1.1.1.1" + }, + { + "Addr": "8.8.8.8" + } + ], + "Domains": [ + "tailnet.example.com" + ], + "Proxied": true + }, + "DNSChallenge": { + "PropagationTimeout": 120000000000, + "Resolvers": [ + "1.1.1.1:53", + "8.8.8.8:53" + ], + "ProviderName": "" + }, + "UnixSocket": "/run/headscale/headscale.sock", + "UnixSocketPermission": 504, + "OIDC": { + "OnlyStartIfOIDCIsAvailable": true, + "Issuer": "", + "ClientID": "", + "Scope": [ + "openid", + "profile", + "email" + ], + "ExtraParams": {}, + "AllowedDomains": [], + "AllowedUsers": [], + "AllowedGroups": [], + "EmailVerifiedRequired": true, + "UseExpiryFromToken": false, + "PKCE": { + "Enabled": false, + "Method": "S256" + } + }, + "LogTail": { + "Enabled": false + }, + "Taildrop": { + "Enabled": true + }, + "AutoUpdate": { + "Enabled": false + }, + "CLI": { + "Address": "", + "Timeout": 5000000000, + "Insecure": false + }, + "Policy": { + "Path": "", + "Mode": "file" + }, + "Tuning": { + "BatchChangeDelay": 800000000, + "NodeMapSessionBufferedChanSize": 30, + "BatcherWorkers": 0, + "RegisterCacheExpiration": 900000000000, + "RegisterCacheMaxEntries": 1024, + "NodeStoreBatchSize": 100, + "NodeStoreBatchTimeout": 500000000 + } + } +} diff --git a/hscontrol/config/testdata/compat/nixos-module-minimal-defaults.yaml b/hscontrol/config/testdata/compat/nixos-module-minimal-defaults.yaml new file mode 100644 index 000000000..95b096863 --- /dev/null +++ b/hscontrol/config/testdata/compat/nixos-module-minimal-defaults.yaml @@ -0,0 +1,132 @@ +%YAML 1.1 +--- +auto_update: + enabled: false +database: + debug: false + gorm: + parameterized_queries: false + skip_err_record_not_found: false + slow_threshold: 0 + postgres: + conn_max_idle_time_secs: 3600 + host: '' + max_idle_conns: 10 + max_open_conns: 10 + name: '' + pass: '' + port: 0 + ssl: 'false' + user: '' + sqlite: + path: /var/lib/headscale/db.sqlite + wal_autocheckpoint: 1000 + write_ahead_log: true + type: sqlite +derp: + auto_update_enabled: true + paths: [] + server: + automatically_add_embedded_derp_region: true + enabled: false + ipv4: '' + ipv6: '' + private_key_path: /var/lib/headscale/derp_server_private.key + region_code: '' + region_id: 0 + region_name: '' + stun_listen_addr: '' + verify_clients: true + update_frequency: 3h + urls: + - https://controlplane.tailscale.com/derpmap/default +disable_check_updates: true +dns: + base_domain: tailnet.example.com + certificates: + enabled: false + zone: '' + challenge: + propagation_timeout: 2m + provider: {} + resolvers: + - 1.1.1.1:53 + - 8.8.8.8:53 + extra_records: [] + extra_records_path: '' + magic_dns: true + nameservers: + global: + - 1.1.1.1 + - 8.8.8.8 + split: {} + override_local_dns: true + search_domains: [] +listen_addr: 0.0.0.0:8080 +log: + format: text + level: info +logtail: + enabled: false +metrics_listen_addr: '' +node: + ephemeral: + inactivity_timeout: 30m + expiry: 0s + routes: + ha: + probe_interval: 10s + probe_timeout: 5s +noise: + private_key_path: /var/lib/headscale/noise_private.key +oidc: + allowed_domains: [] + allowed_groups: [] + allowed_users: [] + client_id: '' + client_secret: '' + email_verified_required: true + extra_params: {} + issuer: '' + only_start_if_oidc_is_available: true + pkce: + enabled: false + method: S256 + scope: + - openid + - profile + - email + use_expiry_from_token: false +policy: + mode: file + path: '' +preauth_keys: + revoked_retention: 168h +prefixes: + allocation: sequential + v4: 100.64.0.0/10 + v6: fd7a:115c:a1e0::/48 +server_url: https://headscale.example.com +taildrop: + enabled: true +tls: + acme: + cache_dir: /var/lib/headscale/cache + challenge_type: HTTP-01 + email: '' + hostname: '' + http_listen: :http + url: '' + cert_path: '' + key_path: '' +trusted_proxies: [] +tuning: + batch_change_delay: 800ms + batcher_workers: 0 + node_mapsession_buffered_chan_size: 30 + node_store_batch_size: 100 + node_store_batch_timeout: 500ms + register_cache_expiration: 15m + register_cache_max_entries: 1024 +unix_socket: /run/headscale/headscale.sock +unix_socket_permission: '0770' diff --git a/nix/module.nix b/nix/module.nix index 422984775..dd964e74e 100644 --- a/nix/module.nix +++ b/nix/module.nix @@ -141,7 +141,7 @@ let || (listenDigits != null && lib.toIntBase10 (lib.head listenDigits) < 1024); # Below lib.mkDefault, so a user's own mkDefault replaces these values - # instead of conflicting with a scalar or merging into a list. + # instead of conflicting with them. distroDefault = lib.mkOverride 1200; # Go reads cli, nested or dotted, as the client-only cli.*; written to the @@ -256,24 +256,13 @@ in ]; services.headscale.settings = lib.mapAttrsRecursive (_: distroDefault) { - # Where this module keeps state and the socket. Nix pins the package, so - # an update notice cannot be acted on. + # Where NixOS keeps headscale's state and socket. Nix pins the package, + # so an update notice cannot be acted on. noise.private_key_path = "${dataDir}/noise_private.key"; derp.server.private_key_path = "${dataDir}/derp_server_private.key"; - database.type = "sqlite"; database.sqlite.path = "${dataDir}/db.sqlite"; unix_socket = "${runDir}/headscale.sock"; disable_check_updates = true; - - # Earlier defaults of this module that headscale's own do not match, - # kept so an upgrade does not change a running configuration. - listen_addr = "127.0.0.1:8080"; - prefixes.v4 = "100.64.0.0/10"; - prefixes.v6 = "fd7a:115c:a1e0::/48"; - derp.urls = [ "https://controlplane.tailscale.com/derpmap/default" ]; - derp.auto_update_enabled = true; - derp.update_frequency = "24h"; - node.ephemeral.inactivity_timeout = "30m"; }; environment = { diff --git a/nix/tests/module.nix b/nix/tests/module.nix index de0e2d78b..08a2c99b6 100644 --- a/nix/tests/module.nix +++ b/nix/tests/module.nix @@ -6,8 +6,6 @@ let required = { server_url = "https://headscale.example.com"; - dns.base_domain = "tailnet.example.com"; - dns.nameservers.global = [ "192.0.2.53" ]; }; system = m: @@ -22,6 +20,19 @@ let }; }; settings = s: system { services.headscale.settings = s; }; + # Only server_url: headscale's defaults cover the rest. + minimal = system { }; + # Between the module's 1200 and the options' defaults: a probe shows only + # where the module stops setting a distro fact, even one equal to Go's. + facts = settings ( + lib.mapAttrsRecursive (_: lib.mkOverride 1300) { + noise.private_key_path = "/probe"; + derp.server.private_key_path = "/probe"; + database.sqlite.path = "/probe"; + unix_socket = "/probe"; + disable_check_updates = false; + } + ); policy = pkgs.writeText "policy.json" "{}"; derpMap = pkgs.writeText "derp.yaml" "regions: {}"; @@ -34,6 +45,8 @@ let derp.paths = [ derpMap ]; unix_socket = "/run/headscale/moved.sock"; dns = { + # Certificates need a domain you control. + base_domain = "tailnet.example.com"; certificates.enabled = true; challenge.provider = { name = "rfc2136"; @@ -128,9 +141,8 @@ let ); # A user's mkDefault and a plain assignment both replace the module's values. overrides = settings { - prefixes.v4 = lib.mkDefault "100.96.0.0/11"; - derp.urls = lib.mkDefault [ "https://derp.example.com/map.json" ]; - derp.update_frequency = "1h"; + noise.private_key_path = lib.mkDefault "/opt/headscale/noise.key"; + unix_socket = "/run/headscale/other.sock"; }; # Both spellings Go reads as cli.*. clis = settings { @@ -217,23 +229,18 @@ assert lib.assertMsg ( sessionVariables.HEADSCALE_DISABLE_CHECK_UPDATES or null == "true" ) "ok: HEADSCALE_DISABLE_CHECK_UPDATES is not true"; assert lib.assertMsg ( - overridden.prefixes.v4 == "100.96.0.0/11" -) "overrides: a user's mkDefault does not replace prefixes.v4"; + overridden.noise.private_key_path == "/opt/headscale/noise.key" +) "overrides: a user's mkDefault does not replace noise.private_key_path"; assert lib.assertMsg ( - overridden.derp.urls == [ - "https://derp.example.com/map.json" - ] -) "overrides: a user's mkDefault does not replace derp.urls"; -assert lib.assertMsg ( - overridden.derp.update_frequency == "1h" -) "overrides: a plain assignment does not replace derp.update_frequency"; + overridden.unix_socket == "/run/headscale/other.sock" +) "overrides: a plain assignment does not replace unix_socket"; assert lib.assertMsg (lib.any ( a: !a.assertion && lib.hasPrefix "services.headscale.settings: remove cli, cli.api_key:" a.message ) clis.config.assertions) "clis: no failed assertion names every spelling of settings.cli"; assert lib.assertMsg ( !(ok.config.environment.etc ? "headscale/config.yaml") ) "ok: /etc/headscale/config.yaml is written"; -assert lib.assertMsg (!binds ok) "ok: 127.0.0.1:8080 grants CAP_NET_BIND_SERVICE"; +assert lib.assertMsg (!binds ok) "ok: 0.0.0.0:8080 grants CAP_NET_BIND_SERVICE"; assert lib.assertMsg (binds http) "http: :http does not grant CAP_NET_BIND_SERVICE"; assert lib.assertMsg (binds bad) "bad: [::]:443 does not grant CAP_NET_BIND_SERVICE"; assert lib.assertMsg ( @@ -270,6 +277,7 @@ pkgs.runCommand "headscale-nixos-module" { # Built as an input: a config with a file: secret passes. okBuild = configCheckOf ok; + minimalBuild = configCheckOf minimal; badBuild = pkgs.testers.testBuildFailure (configCheckOf bad); badEnvBuild = pkgs.testers.testBuildFailure (configCheckOf badEnv); nativeBuildInputs = [ @@ -298,10 +306,14 @@ pkgs.runCommand "headscale-nixos-module" ok=${configFile ok} want $ok '.policy.path == "${policy}" and .derp.paths == ["${derpMap}"]' want $ok 'has("cli") | not' - want $ok '.listen_addr == "127.0.0.1:8080" and .disable_check_updates == true' + want $ok '.listen_addr == "0.0.0.0:8080" and .disable_check_updates == true' want $ok '.database.type == "sqlite"' want $ok '.derp.urls[0] == "https://controlplane.tailscale.com/derpmap/default"' want $ok '.dns.challenge.provider.key == "file:''${CREDENTIALS_DIRECTORY}/tsig"' + want ${configFile facts} '.noise.private_key_path == "/var/lib/headscale/noise_private.key" + and .derp.server.private_key_path == "/var/lib/headscale/derp_server_private.key" + and .database.sqlite.path == "/var/lib/headscale/db.sqlite" + and .unix_socket == "/run/headscale/headscale.sock" and .disable_check_updates == true' headscale check config -f $ok bad=${configFile bad}