2025-12-13 13:29:48 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
|
|
# Prevent double-sourcing
|
|
|
|
|
if [[ -n "${PKGMGR_NIX_CONF_FILE_SH:-}" ]]; then
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
PKGMGR_NIX_CONF_FILE_SH=1
|
|
|
|
|
|
|
|
|
|
nixconf_file_path() {
|
|
|
|
|
echo "/etc/nix/nix.conf"
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-13 21:25:02 +01:00
|
|
|
# Ensure a given nix.conf key contains required tokens (merged, no duplicates)
|
|
|
|
|
nixconf_ensure_features_key() {
|
|
|
|
|
local nix_conf="$1"
|
|
|
|
|
local key="$2"
|
|
|
|
|
shift 2
|
|
|
|
|
local required=("$@")
|
2025-12-13 13:29:48 +01:00
|
|
|
|
|
|
|
|
mkdir -p /etc/nix
|
|
|
|
|
|
2025-12-13 21:25:02 +01:00
|
|
|
# Create file if missing (with just the required tokens)
|
2025-12-13 13:29:48 +01:00
|
|
|
if [[ ! -f "${nix_conf}" ]]; then
|
2025-12-13 21:25:02 +01:00
|
|
|
local want="${key} = ${required[*]}"
|
2025-12-13 13:29:48 +01:00
|
|
|
echo "[nix-conf] Creating ${nix_conf} with: ${want}"
|
|
|
|
|
printf "%s\n" "${want}" >"${nix_conf}"
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
|
2025-12-13 21:25:02 +01:00
|
|
|
# Key exists -> merge tokens
|
|
|
|
|
if grep -qE "^\s*${key}\s*=" "${nix_conf}"; then
|
|
|
|
|
local ok=1
|
|
|
|
|
local t
|
|
|
|
|
for t in "${required[@]}"; do
|
|
|
|
|
if ! grep -qE "^\s*${key}\s*=.*\b${t}\b" "${nix_conf}"; then
|
|
|
|
|
ok=0
|
|
|
|
|
break
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
if [[ "$ok" -eq 1 ]]; then
|
|
|
|
|
echo "[nix-conf] ${key} already correct"
|
2025-12-13 13:29:48 +01:00
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
|
2025-12-13 21:25:02 +01:00
|
|
|
echo "[nix-conf] Extending ${key} in ${nix_conf}"
|
2025-12-13 13:29:48 +01:00
|
|
|
|
|
|
|
|
local current
|
2025-12-13 21:25:02 +01:00
|
|
|
current="$(grep -E "^\s*${key}\s*=" "${nix_conf}" | head -n1 | cut -d= -f2-)"
|
2025-12-13 13:29:48 +01:00
|
|
|
current="$(echo "${current}" | xargs)" # trim
|
|
|
|
|
|
2025-12-13 21:25:02 +01:00
|
|
|
local merged=""
|
2025-12-13 13:29:48 +01:00
|
|
|
local token
|
2025-12-13 21:25:02 +01:00
|
|
|
|
|
|
|
|
# Start with existing tokens
|
2025-12-13 13:29:48 +01:00
|
|
|
for token in ${current}; do
|
|
|
|
|
if [[ " ${merged} " != *" ${token} "* ]]; then
|
|
|
|
|
merged="${merged} ${token}"
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
2025-12-13 21:25:02 +01:00
|
|
|
# Add required tokens
|
|
|
|
|
for token in "${required[@]}"; do
|
|
|
|
|
if [[ " ${merged} " != *" ${token} "* ]]; then
|
|
|
|
|
merged="${merged} ${token}"
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
merged="$(echo "${merged}" | xargs)" # trim
|
|
|
|
|
|
|
|
|
|
sed -i "s|^\s*${key}\s*=.*|${key} = ${merged}|" "${nix_conf}"
|
2025-12-13 13:29:48 +01:00
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
|
2025-12-13 21:25:02 +01:00
|
|
|
# Key missing -> append
|
|
|
|
|
local want="${key} = ${required[*]}"
|
2025-12-13 13:29:48 +01:00
|
|
|
echo "[nix-conf] Appending to ${nix_conf}: ${want}"
|
|
|
|
|
printf "\n%s\n" "${want}" >>"${nix_conf}"
|
|
|
|
|
}
|
2025-12-13 21:25:02 +01:00
|
|
|
|
|
|
|
|
nixconf_ensure_experimental_features() {
|
|
|
|
|
local nix_conf
|
|
|
|
|
nix_conf="$(nixconf_file_path)"
|
|
|
|
|
|
|
|
|
|
# Ensure both keys to avoid prompts and cover older/alternate expectations
|
|
|
|
|
nixconf_ensure_features_key "${nix_conf}" "experimental-features" "nix-command" "flakes"
|
|
|
|
|
nixconf_ensure_features_key "${nix_conf}" "extra-experimental-features" "nix-command" "flakes"
|
|
|
|
|
}
|