diff --git a/modules/misc/nix.nix b/modules/misc/nix.nix index cc569c68d..a16097b1b 100644 --- a/modules/misc/nix.nix +++ b/modules/misc/nix.nix @@ -103,6 +103,7 @@ let ${mkKeyValuePairs cfg.settings} ${cfg.extraOptions} ''; + passthru.doCheck = cfg.checkConfig; checkPhase = if pkgs.stdenv.hostPlatform != pkgs.stdenv.buildPlatform then '' @@ -120,7 +121,8 @@ let NIX_CONF_DIR=$PWD \ ${cfg.package}/bin/nix ${showCommand} ${optionalString (isNixAtLeast "2.3pre") "--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 ''; }; @@ -294,7 +296,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 000000000..623e98a1b --- /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 000000000..397328a01 --- /dev/null +++ b/tests/modules/misc/nix/allow-unknown-settings.nix @@ -0,0 +1,33 @@ +{ + 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 4c8822bf2..9392b7bf9 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 000000000..8c93d8ea5 --- /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 000000000..97c197db2 --- /dev/null +++ b/tests/modules/misc/nix/skip-check-settings.nix @@ -0,0 +1,33 @@ +{ + 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} + ''; +}