From 3f71ad2a99f79bd6a392f17e293caa4b5c3f2e02 Mon Sep 17 00:00:00 2001 From: Ziad Arafat Date: Sun, 15 Nov 2026 12:07:19 -0700 Subject: [PATCH] Recover shared-home Nix config links and check the actual flakes gate --- README.md | 8 +++++++ bin/dot | 33 ++++++++++++++++++++++++--- scripts/doctor-nix.sh | 25 +++++++++++++++------ tests/doctor-nix.sh | 19 +++++++++++++++- tests/nix-bootstrap.sh | 51 ++++++++++++++++++++++++++++++++++++++++++ tests/webtop.sh | 5 +++-- 6 files changed, 128 insertions(+), 13 deletions(-) create mode 100644 tests/nix-bootstrap.sh diff --git a/README.md b/README.md index a05955e..0add50e 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,14 @@ flake files. This catches interrupted installers and feature overrides before gets a different repair hint from a single-user store problem. Doctor also checks the profile's Git credential tools and desktop SourceGit/OAuth helper. +In a Distrobox with a shared home, a Home Manager `nix.conf` symlink can point +into the host's store while that store is absent inside the container. Doctor +reports this explicitly. `dot nix` and `dot switch` preserve a broken config +link as `nix.conf.broken-link.*` and create a writable bootstrap config enabling +`nix-command` and flakes. Existing readable settings are preserved when adding +features; healthy managed configurations remain linked. You can recover with +`git -C ~/.dotfiles pull` followed by `dot nix` and `dot switch`. + Apps that write their own settings (Claude Code, pi, Vesktop, Konsole profiles, VSCodium, Zed, `~/.ssh/config`) are linked **out of the Nix store** into this checkout. Changes made in the app show up in `git diff`, so commit them. ## What's in it diff --git a/bin/dot b/bin/dot index e697021..e016a35 100755 --- a/bin/dot +++ b/bin/dot @@ -81,9 +81,35 @@ cmd_profile() { } # ---------------------------------------------------------------- nix +nix_user_config() { printf '%s/nix/nix.conf\n' "${XDG_CONFIG_HOME:-$HOME/.config}"; } + +ensure_nix_features() { + local file dir tmp backup + file="$(nix_user_config)"; dir="$(dirname "$file")" + mkdir -p "$dir" + # Home Manager links this file into /nix/store. Shared distrobox homes may + # see the link but not its target; never append through a managed store link. + if [ -L "$file" ] && [ ! -e "$file" ]; then + backup="$(mktemp "$dir/nix.conf.broken-link.XXXXXX")" + cp -P -- "$file" "$backup" + warn "Nix config link is broken (shared container store?) — preserving it at $backup and creating a local bootstrap config" + elif [ -e "$file" ] && [ ! -f "$file" ]; then + die "Nix config is not a regular file: $file" + fi + # Append an additive setting, preserving unrelated settings and comments. + if [ -f "$file" ] && grep -Eq '^[[:space:]]*(extra-)?experimental-features[[:space:]]*=[[:space:]]*nix-command[[:space:]]+flakes[[:space:]]*(#.*)?$' "$file"; then + return 0 + fi + tmp="$(mktemp "$dir/.nix.conf.dot.XXXXXX")" + if [ -f "$file" ]; then + if ! cat -- "$file" >"$tmp"; then rm -f "$tmp"; die "Cannot read existing Nix config: $file"; fi + fi + printf '\nextra-experimental-features = nix-command flakes\n' >>"$tmp" + mv -f -- "$tmp" "$file" +} + cmd_nix() { - mkdir -p "$HOME/.config/nix" - grep -q flakes "$HOME/.config/nix/nix.conf" 2>/dev/null || echo "experimental-features = nix-command flakes" >>"$HOME/.config/nix/nix.conf" + ensure_nix_features if source_nix; then ok "nix $(nix --version | awk '{print $NF}') present"; return; fi local os; os="$(os_id)" if [ "$os" = steamos ] || ! has_systemd || in_container; then @@ -107,6 +133,7 @@ cmd_nix() { cmd_switch() { [ -n "${1:-}" ] && cmd_profile "$1" source_nix || die "nix missing: run dot nix" + ensure_nix_features local attr; attr="$(flake_attr)" step "home-manager switch → $attr" export DOTFILES_DIR @@ -627,7 +654,7 @@ cmd_update() { git -C "$DOTFILES_DIR" pull --rebase --autostash || warn "git pull failed; continuing with local tree" if [ "${1:-}" = --inputs ]; then step "nix flake update" - source_nix && nix flake update --flake "$DOTFILES_DIR" + source_nix && ensure_nix_features && nix flake update --flake "$DOTFILES_DIR" fi cmd_switch is_desktop && ! in_container && have flatpak && { step "flatpak update"; flatpak update --user -y --noninteractive || true; } diff --git a/scripts/doctor-nix.sh b/scripts/doctor-nix.sh index e8e0781..7ecb3e3 100644 --- a/scripts/doctor-nix.sh +++ b/scripts/doctor-nix.sh @@ -5,8 +5,14 @@ doctor_nix_home() { printf '%s\n' "$HOME"; } doctor_timeout() { if have timeout; then timeout 10 "$@"; else "$@"; fi; } doctor_nix() { - local root home daemon=0 cli=0 flakes="" version="" + local root home daemon=0 cli=0 flakes="" version="" conf probe root="$(doctor_nix_root)"; home="$(doctor_nix_home)" + conf="${XDG_CONFIG_HOME:-$home/.config}/nix/nix.conf" + if [ -L "$conf" ] && [ ! -e "$conf" ]; then + warn "Nix config link is broken — a shared container home may point to another /nix/store; run dot nix to preserve the link and create a local config" + elif [ -L "$conf" ]; then + ok "Nix config link target accessible" + fi if ! source_nix; then if [ -d "$root" ] || [ -L "$home/.nix-profile" ]; then warn "Nix installation incomplete or missing from PATH — inspect $root/receipt.json and the profile scripts; finish/repair the existing installer before dot switch" @@ -50,15 +56,20 @@ doctor_nix() { else warn "Nix nix-command feature disabled or CLI configuration invalid — enable experimental-features = nix-command flakes in ~/.config/nix/nix.conf; check NIX_CONFIG overrides" fi - if have nix-instantiate; then - flakes="$(doctor_timeout nix-instantiate --eval --expr 'builtins ? getFlake' 2>/dev/null)" || flakes="" - elif [ "$cli" = 1 ]; then - flakes="$(doctor_timeout nix eval --expr 'builtins ? getFlake' 2>/dev/null)" || flakes="" + # Builtin presence does NOT prove flakes are enabled. Exercise the CLI's + # feature gate with a deliberately absent local path. Offline mode prevents + # network access; the absent path prevents evaluation, store or lock writes. + # Force nix-command only, so flakes are tested independently of that feature. + probe="$home/.config/dotfiles/.doctor-absent-flake" + if [ ! -e "$probe" ] && [ ! -L "$probe" ]; then + flakes="$(LC_ALL=C doctor_timeout nix --extra-experimental-features nix-command flake metadata --offline --no-write-lock-file --json "path:$probe" 2>&1)" || true fi - if [ "$flakes" = true ]; then + if [[ "$flakes" == *"experimental Nix feature 'flakes' is disabled"* ]]; then + warn "Nix flakes feature disabled — run dot nix to enable nix-command + flakes; if overridden, fix NIX_CONFIG/system config and retry dot doctor" + elif [[ "$flakes" == *"$probe"* && ( "$flakes" == *"No such file or directory"* || "$flakes" == *"does not exist"* ) ]]; then ok "Nix flakes feature enabled" else - warn "Nix flakes feature disabled or unavailable — run dot nix to enable nix-command + flakes; if overridden, fix NIX_CONFIG/system config and retry dot doctor" + warn "Nix flakes check inconclusive — inspect Nix configuration and retry dot doctor" fi if [ -f "$DOTFILES_DIR/flake.nix" ] && [ -s "$DOTFILES_DIR/flake.lock" ]; then if have jq && ! jq empty "$DOTFILES_DIR/flake.lock" >/dev/null 2>&1; then diff --git a/tests/doctor-nix.sh b/tests/doctor-nix.sh index 6f16666..56f01fc 100644 --- a/tests/doctor-nix.sh +++ b/tests/doctor-nix.sh @@ -8,6 +8,7 @@ mkdir -p "$fixture/nix/store" "$fixture/home" "$fixture/repo" printf '{}\n' >"$fixture/repo/flake.lock" touch "$fixture/repo/flake.nix" DOTFILES_DIR="$fixture/repo" +export XDG_CONFIG_HOME="$fixture/home/.config" doctor_nix_root() { echo "$fixture/nix"; } doctor_nix_home() { echo "$fixture/home"; } doctor_timeout() { "$@"; } @@ -22,10 +23,18 @@ nix() { --version) [ "${broken:-0}" = 0 ] && echo 'nix (Nix) 2.35.3';; '--extra-experimental-features nix-command store ping --json') [ "${store_broken:-0}" = 0 ];; 'eval --expr 1') [ "${cli_disabled:-0}" = 0 ];; + '--extra-experimental-features nix-command flake metadata --offline --no-write-lock-file --json '* ) + if [ "${flakes_disabled:-0}" = 1 ]; then + echo "error: experimental Nix feature 'flakes' is disabled" >&2 + else + printf 'error: opening file "%s": No such file or directory\n' "${*: -1}" >&2 + fi + return 1;; *) return 1;; esac } -nix-instantiate() { if [ "${flakes_disabled:-0}" = 1 ]; then echo false; else echo true; fi; } +# Modern Nix exposes the builtin even with flakes disabled; do not use it. +nix-instantiate() { echo true; } result="$(doctor_nix)" ! grep -q WARN <<<"$result" grep -q 'Nix store/database connection' <<<"$result" @@ -42,6 +51,14 @@ cli_disabled=1 flakes_disabled=1 result="$(doctor_nix)" grep -q 'nix-command feature disabled' <<<"$result" grep -q 'flakes feature disabled' <<<"$result" unset cli_disabled flakes_disabled +flakes_disabled=1 result="$(doctor_nix)" +grep -q 'flakes feature disabled' <<<"$result" +! grep -q 'flakes feature enabled' <<<"$result" +unset flakes_disabled +mkdir -p "$XDG_CONFIG_HOME/nix" +ln -s "$fixture/absent-config" "$XDG_CONFIG_HOME/nix/nix.conf" +result="$(doctor_nix)" +grep -q 'Nix config link is broken' <<<"$result" ln -s "$fixture/absent-generation" "$fixture/home/.nix-profile" result="$(doctor_nix)" grep -q 'user profile link is broken' <<<"$result" diff --git a/tests/nix-bootstrap.sh b/tests/nix-bootstrap.sh new file mode 100644 index 0000000..5cfd079 --- /dev/null +++ b/tests/nix-bootstrap.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash +set -euo pipefail +cd "$(dirname "$0")/.." +export DOTFILES_DIR="$PWD" +source <(sed '$d' bin/dot) +fixture="$(mktemp -d)" +trap 'rm -rf "$fixture"' EXIT +export XDG_CONFIG_HOME="$fixture/config" +mkdir -p "$XDG_CONFIG_HOME/nix" +conf="$XDG_CONFIG_HOME/nix/nix.conf" +source_nix() { return 0; } +nix() { echo 'nix (Nix) fixture'; } + +# Reproduce a host Home Manager symlink whose store target is absent in a box. +ln -s "$fixture/missing-host-store/nix.conf" "$conf" +cmd_nix +[ ! -L "$conf" ] +grep -qx 'extra-experimental-features = nix-command flakes' "$conf" +backups=("$conf".broken-link.*) +[ "${#backups[@]}" = 1 ] && [ -L "${backups[0]}" ] +[ "$(readlink "${backups[0]}")" = "$fixture/missing-host-store/nix.conf" ] +cmd_nix +[ "$(grep -c '^extra-experimental-features' "$conf")" = 1 ] + +# Preserve extra settings and do not write into a valid immutable target. +printf '# flakes mentioned in a comment is not enabling it\nexperimental-features = ca-derivations\nsubstituters = https://cache.nixos.org\n' >"$fixture/store-config" +chmod 444 "$fixture/store-config" +mv "$conf" "$fixture/recovered-config" +ln -s "$fixture/store-config" "$conf" +cmd_nix +[ ! -L "$conf" ] +! grep -q '^extra-experimental-features' "$fixture/store-config" +grep -qx 'experimental-features = ca-derivations' "$conf" +grep -qx 'substituters = https://cache.nixos.org' "$conf" +grep -qx 'extra-experimental-features = nix-command flakes' "$conf" + +# A healthy managed file remains linked through repeated bootstraps. +printf 'experimental-features = nix-command flakes\n' >"$fixture/healthy-config" +mv "$conf" "$fixture/preserved-config" +ln -s "$fixture/healthy-config" "$conf" +cmd_nix +[ -L "$conf" ] + +# Switching must also bootstrap features before invoking Home Manager. +mv "$conf" "$fixture/healthy-link" +ln -s "$fixture/missing-again" "$conf" +flake_attr() { echo headless; } +have() { [ "$1" = home-manager ]; } +home-manager() { grep -qx 'extra-experimental-features = nix-command flakes' "$conf"; } +cmd_switch +echo 'Nix bootstrap recovery checks passed' diff --git a/tests/webtop.sh b/tests/webtop.sh index 82454a1..3d29675 100644 --- a/tests/webtop.sh +++ b/tests/webtop.sh @@ -3,6 +3,7 @@ set -euo pipefail cd "$(dirname "$0")/.." export DOTFILES_DIR="$PWD" export HOME="$(mktemp -d)" +export XDG_CONFIG_HOME="$HOME/.config" trap 'rm -rf "$HOME"' EXIT unset USER source <(sed '$d' bin/dot) @@ -10,9 +11,9 @@ source <(sed '$d' bin/dot) source_nix() { return 0; } nix() { echo 'nix (Nix) test'; } cmd_nix -grep -qx 'experimental-features = nix-command flakes' "$HOME/.config/nix/nix.conf" +grep -qx 'extra-experimental-features = nix-command flakes' "$HOME/.config/nix/nix.conf" cmd_nix -[ "$(wc -l < "$HOME/.config/nix/nix.conf")" = 1 ] +[ "$(grep -c '^extra-experimental-features' "$HOME/.config/nix/nix.conf")" = 1 ] in_container() { return 0; } os_id() { echo fedora; } SUDO() { printf '%s\n' "$*" >>"$HOME/system-calls"; }