tests: migrate config.sh

Change-Id: Ia37a84e0036dd52af112a359817c709a15aa8c90
This commit is contained in:
rootile
2026-07-15 10:53:10 +00:00
committed by Rutile
parent f51148a75b
commit 25f53b755c
6 changed files with 115 additions and 68 deletions
-65
View File
@@ -1,65 +0,0 @@
source common.sh
# Isolate the home for this test.
# Other tests (e.g. flake registry tests) could be writing to $HOME in parallel.
export HOME=$TEST_ROOT/userhome
# Test that using XDG_CONFIG_HOME works
# Assert the config folder didn't exist initially.
[ ! -e "$HOME/.config" ]
# Without XDG_CONFIG_HOME, creates $HOME/.config
unset XDG_CONFIG_HOME
# Run against the nix registry to create the config dir
# (Tip: this relies on removing non-existent entries being a no-op!)
nix registry remove userhome-without-xdg
# Verifies it created it
[ -e "$HOME/.config" ]
# Remove the directory it created
rm -rf "$HOME/.config"
# Run the same test, but with XDG_CONFIG_HOME
export XDG_CONFIG_HOME=$TEST_ROOT/confighome
# Assert the XDG_CONFIG_HOME/nix path does not exist yet.
[ ! -e "$TEST_ROOT/confighome/nix" ]
nix registry remove userhome-with-xdg
# Verifies the confighome path has been created
[ -e "$TEST_ROOT/confighome/nix" ]
# Assert the .config folder hasn't been created.
[ ! -e "$HOME/.config" ]
# Test that files are loaded from XDG by default
export XDG_CONFIG_HOME=$TEST_ROOT/confighome
export XDG_CONFIG_DIRS=$TEST_ROOT/dir1:$TEST_ROOT/dir2
files=$(nix-build --verbose --version | grep "User config" | cut -d ':' -f2- | xargs)
[[ $files == "$TEST_ROOT/confighome/nix/nix.conf:$TEST_ROOT/dir1/nix/nix.conf:$TEST_ROOT/dir2/nix/nix.conf" ]]
# Test that setting NIX_USER_CONF_FILES overrides all the default user config files
export NIX_USER_CONF_FILES=$TEST_ROOT/file1.conf:$TEST_ROOT/file2.conf
files=$(nix-build --verbose --version | grep "User config" | cut -d ':' -f2- | xargs)
[[ $files == "$TEST_ROOT/file1.conf:$TEST_ROOT/file2.conf" ]]
# Test that it's possible to load the config from a custom location
here=$(readlink -f "$(dirname "${BASH_SOURCE[0]}")")
export NIX_USER_CONF_FILES=$here/config/nix-with-substituters.conf
var=$(nix config show | grep '^substituters =' | cut -d '=' -f 2 | xargs)
[[ $var == https://example.com ]]
# Test that it's possible to load config from the environment
prev=$(nix config show | grep '^cores' | cut -d '=' -f 2 | xargs)
export NIX_CONFIG="cores = 4242"$'\n'"experimental-features = nix-command flakes"
exp_cores=$(nix config show | grep '^cores' | cut -d '=' -f 2 | xargs)
exp_features=$(nix config show | grep '^experimental-features' | cut -d '=' -f 2 | xargs)
[[ $prev != $exp_cores ]]
[[ $exp_cores == "4242" ]]
[[ $exp_features == "flakes nix-command" ]]
# Test that it's possible to retrieve a single setting's value
val=$(nix config show | grep '^warn-dirty' | cut -d '=' -f 2 | xargs)
val2=$(nix config show warn-dirty)
[[ $val == $val2 ]]
# Regression test for `[extra-]trusted-users` (daemonAuthorizationSettings) being ignored.
# https://git.lix.systems/lix-project/lix/issues/1183
val="$(NIX_CONFIG=$'trusted-users = as-configured' nix config show trusted-users)"
[[ "$val" == "as-configured" ]]
val="$(NIX_CONFIG=$'trusted-users = as-configured\nextra-trusted-users = extra' nix config show trusted-users)"
[[ "$val" == "as-configured extra" ]]
-1
View File
@@ -75,7 +75,6 @@ functional_tests_scripts = [
'nar-access.sh',
'repl.sh',
'logging.sh',
'config.sh',
'filter-source.sh',
'linux-sandbox.sh',
'supplementary-groups.sh',
@@ -0,0 +1,74 @@
from testlib.fixtures.file_helper import File
from testlib.fixtures.file_helper import with_files
from testlib.fixtures.nix import Nix
from pathlib import Path
import pytest
pytestmark = pytest.mark.no_daemon
def test_home_dot_config_no_xdg(nix: Nix, files: Path):
# Test that using XDG_CONFIG_HOME works
# Assert the config folder didn't exist initially.
config_path = files / ".config"
assert not config_path.exists()
# Without XDG_CONFIG_HOME, creates $HOME/.config
# Run against the nix registry to create the config dir
# (Tip: this relies on removing non-existent entries being a no-op!)
nix.nix(["registry", "remove", "userhome-without-xdg"], flake=True).run().ok()
assert config_path.exists()
def test_config_xdg(nix: Nix, files: Path):
config_path = files / ".config"
xdg_config_path = files / ".xdg-config"
nix.env["XDG_CONFIG_HOME"] = str(xdg_config_path)
assert not config_path.exists()
assert not xdg_config_path.exists()
nix.nix(["registry", "remove", "userhome-without-xdg"], flake=True).run().ok()
assert not config_path.exists()
assert xdg_config_path.exists()
assert (xdg_config_path / "nix").exists()
def test_load_files_xdg(nix: Nix, files: Path):
# Test that files are loaded from XDG by default
xdg_config_path = files / ".xdg-config"
nix.env["XDG_CONFIG_HOME"] = str(xdg_config_path)
nix.env["XDG_CONFIG_DIRS"] = f"{files}/dir1:{files}/dir2"
res = nix.nix_build(["-v", "--version"]).run().ok()
clean = res.stdout_plain.replace(str(files.parent), "/PWD")
assert (
"User configuration files: /PWD/test-home/.xdg-config/nix/nix.conf:/PWD/test-home/dir1/nix/nix.conf:/PWD/test-home/dir2/nix/nix.conf"
in clean
)
def test_user_conf_overrides(nix: Nix, files: Path):
conf_files = f"{files}/file1.conf:{files}/file2.conf"
nix.env["NIX_USER_CONF_FILES"] = conf_files
res = nix.nix_build(["-v", "--version"]).run().ok()
clean = res.stdout_plain.replace(str(files.parent), "/PWD")
assert "User configuration files: /PWD/test-home/file1.conf:/PWD/test-home/file2.conf" in clean
@with_files(
{
"my-config": {
"my-nix.conf": File(
"experimental-features = nix-command\nsubstituters = https://example.com"
)
}
}
)
def test_conf_load_custom_location(nix: Nix, files: Path):
nix.env["NIX_USER_CONF_FILES"] = f"{files}/my-config/my-nix.conf"
nix.settings.disabled = True
res = nix.nix(["config", "show"]).run().ok()
assert "substituters = https://example.com" in res.stdout_plain
@@ -0,0 +1,31 @@
from testlib.fixtures.nix import Nix
import pytest
pytestmark = pytest.mark.no_daemon
def test_config_env_var(nix: Nix):
nix.settings.disabled = True
nix.env["NIX_CONFIG"] = "cores = 4242\nexperimental-features = nix-command flakes"
res = nix.nix(["config", "show"]).run().ok()
assert "cores = 4242" in res.stdout_plain
assert "experimental-features = flakes nix-command" in res.stdout_plain
def test_config_show_single_value(nix: Nix):
res = nix.nix(["config", "show", "warn-dirty"], flake=True).run().ok()
assert res.stdout_plain == "true"
def test_trusted_user_ignored(nix: Nix):
"""
regression test for https://git.lix.systems/lix-project/lix/issues/1183
"""
nix.settings.disabled = True
nix.env["NIX_CONFIG"] = "experimental-features = nix-command\ntrusted-users = as-configured"
res = nix.nix(["config", "show", "trusted-users"]).run().ok()
assert res.stdout_plain == "as-configured"
nix.env["NIX_CONFIG"] += "\nextra-trusted-users = extra"
res = nix.nix(["config", "show", "trusted-users"]).run().ok()
assert res.stdout_plain == "as-configured extra"
+10 -2
View File
@@ -87,14 +87,19 @@ class NixSettings:
"extra-experimental-features": [],
"extra-deprecated-features": [],
}
self.disabled = False
"""
This is *not* a nix specific setting, but disables the application of the configured settings.
Use this, if you need to test config file discovery or similar.
"""
def __getattr__(self, attr: str) -> _NixSettingValue:
if attr.startswith("__"):
if attr.startswith("__") or attr == "disabled":
return super().__getattr__(attr)
return self._settings[attr.replace("_", "-")]
def __setattr__(self, attr: str, value: _NixSettingValue):
if attr == "_settings":
if attr in ("_settings", "disabled"):
super().__setattr__(attr, value)
else:
self._settings[attr.replace("_", "-")] = value
@@ -136,6 +141,7 @@ class NixSettings:
"""
new_settings = NixSettings()
new_settings._settings = copy.deepcopy(self._settings)
new_settings.disabled = self.disabled
new_settings.update(args, **kwargs)
return new_settings
@@ -155,6 +161,8 @@ class NixSettings:
return config
def to_env_overlay(self, env: ManagedEnv) -> None:
if self.disabled:
return
cfg = self.to_config(env)
(env.dirs.nix_conf_dir / "nix.conf").write_text(cfg)
env.set_env("NIX_CONFIG", cfg)