diff --git a/modules/misc/nix.nix b/modules/misc/nix.nix index 652d3eb89179..7bcdc53dc253 100644 --- a/modules/misc/nix.nix +++ b/modules/misc/nix.nix @@ -74,6 +74,7 @@ let ${mkKeyValuePairs cfg.settings} ${cfg.extraOptions} ''; + passthru.doCheck = cfg.checkConfig; checkPhase = if pkgs.stdenv.hostPlatform != pkgs.stdenv.buildPlatform then '' echo "Ignoring validation for cross-compilation" @@ -92,9 +93,13 @@ let "--no-net --option experimental-features nix-command" } \ |& sed -e 's/^warning:/error:/' \ - | (! grep '${ - if cfg.checkConfig then "^error:" else "^error: unknown setting" - }') + | ${ + if cfg.allowUnknownSettings then + "grep -v '^error: unknown setting'" + else + "cat" + } \ + | (! grep '^error:') set -o pipefail ''; }; @@ -241,7 +246,16 @@ in { default = true; description = '' If enabled (the default), checks for data type mismatches and that Nix - can parse the generated nix.conf. + can parse the generated {file}`nix.conf`. + ''; + }; + + allowUnknownSettings = mkOption { + type = types.bool; + default = false; + description = '' + If enabled, unknown setting errors will be allowed when checking the + generated {file}`nix.conf`. This has no effect if {option}`checkConfig` is false. ''; }; diff --git a/tests/modules/misc/nix/allow-unknown-settings-expected.conf b/tests/modules/misc/nix/allow-unknown-settings-expected.conf new file mode 100644 index 000000000000..623e98a1b7b3 --- /dev/null +++ b/tests/modules/misc/nix/allow-unknown-settings-expected.conf @@ -0,0 +1,6 @@ +# WARNING: this file is generated from the nix.settings option in +# your Home Manager configuration at $XDG_CONFIG_HOME/nix/nix.conf. +# Do not edit it! +and-another-one = hello +some-arbitrary-setting = true + diff --git a/tests/modules/misc/nix/allow-unknown-settings.nix b/tests/modules/misc/nix/allow-unknown-settings.nix new file mode 100644 index 000000000000..68525fdd1981 --- /dev/null +++ b/tests/modules/misc/nix/allow-unknown-settings.nix @@ -0,0 +1,27 @@ +{ config, lib, pkgs, ... }: { + nix = { + package = config.lib.test.mkStubPackage { + version = lib.getVersion pkgs.nixVersions.stable; + buildScript = '' + target=$out/bin/nix + mkdir -p "$(dirname "$target")" + + echo -n "true" > "$target" + + chmod +x "$target" + ''; + }; + + settings = { + some-arbitrary-setting = true; + and-another-one = "hello"; + }; + checkConfig = false; + }; + + nmt.script = '' + assertFileContent \ + home-files/.config/nix/nix.conf \ + ${./allow-unknown-settings-expected.conf} + ''; +} diff --git a/tests/modules/misc/nix/default.nix b/tests/modules/misc/nix/default.nix index 4c8822bf2a03..9392b7bf9425 100644 --- a/tests/modules/misc/nix/default.nix +++ b/tests/modules/misc/nix/default.nix @@ -5,4 +5,6 @@ nix-keep-old-nix-path = ./keep-old-nix-path.nix; nix-example-channels = ./example-channels.nix; nix-example-channels-xdg = ./example-channels-xdg.nix; + nix-skip-check-settings = ./skip-check-settings.nix; + nix-allow-unknown-settings = ./allow-unknown-settings.nix; } diff --git a/tests/modules/misc/nix/skip-check-settings-expected.conf b/tests/modules/misc/nix/skip-check-settings-expected.conf new file mode 100644 index 000000000000..8c93d8ea5ce9 --- /dev/null +++ b/tests/modules/misc/nix/skip-check-settings-expected.conf @@ -0,0 +1,7 @@ +# WARNING: this file is generated from the nix.settings option in +# your Home Manager configuration at $XDG_CONFIG_HOME/nix/nix.conf. +# Do not edit it! + +some! nonsense { + which should fail validation + diff --git a/tests/modules/misc/nix/skip-check-settings.nix b/tests/modules/misc/nix/skip-check-settings.nix new file mode 100644 index 000000000000..9484e0da7e37 --- /dev/null +++ b/tests/modules/misc/nix/skip-check-settings.nix @@ -0,0 +1,27 @@ +{ config, lib, pkgs, ... }: { + nix = { + package = config.lib.test.mkStubPackage { + version = lib.getVersion pkgs.nixVersions.stable; + buildScript = '' + target=$out/bin/nix + mkdir -p "$(dirname "$target")" + + echo -n "true" > "$target" + + chmod +x "$target" + ''; + }; + + extraOptions = '' + some! nonsense { + which should fail validation + ''; + checkConfig = false; + }; + + nmt.script = '' + assertFileContent \ + home-files/.config/nix/nix.conf \ + ${./skip-check-settings-expected.conf} + ''; +}