Persistent NixOS Rebuild Error: Unrecognized Archive Format Despite Correct NIX_PATH Settings

Hello everyone,

I’m reaching out to seek assistance with a persistent issue I’ve been experiencing on my NixOS system. Despite numerous attempts and following various troubleshooting steps, I’m unable to resolve an error that occurs when I attempt to rebuild my system using nixos-rebuild switch. The error message indicates an “Unrecognized archive format,” and it seems related to the NIX_PATH environment variable being set automatically, regardless of the steps I’ve taken to correct it.

System Information:

  • NixOS Version: Initially installed with nixos-24.05, attempting to switch to nixos-unstable.
  • Channels Configured:
    • nixos https://nixos.org/channels/nixos-unstable
    • nixpkgs https://nixos.org/channels/nixpkgs-unstable
  • Error Encountered:
    • error: failed to open archive: Unrecognized archive format
    • Occurs during nixos-rebuild switch and nix-instantiate commands.

Detailed Description of the Issue:

Every time I run sudo nixos-rebuild switch --show-trace --verbose, I receive the following error:

error: failed to open archive: Unrecognized archive format

This error persists even after attempting multiple solutions. It appears that the NIX_PATH environment variable is being set automatically to incorrect values, specifically mapping nixpkgs to https://nixos.org/channels/nixos-24.05, despite my efforts to change it.

Steps Taken to Resolve the Issue:

1. Checking and Setting NIX_PATH:

  • Ran echo $NIX_PATH and found it was incorrectly set to:
/root/.nix-defexpr/channels:nixpkgs=https://nixos.org/channels/nixos-24.05:nixpkgs-unstable=https://nixos.org/channels/nixpkgs-unstable
  • Attempted to unset and correctly set NIX_PATH:
unset NIX_PATH
export NIX_PATH="nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/etc/nixos/configuration.nix"
  • Added the correct NIX_PATH to /root/.bashrc:
# Unset incorrect NIX_PATH
unset NIX_PATH

# Set NIX_PATH correctly
export NIX_PATH="nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/etc/nixos/configuration.nix"
  • Despite these changes, NIX_PATH reverts to the incorrect value after rebooting or starting a new shell.

2. Modifying configuration.nix:

  • Added nix.nixPath to configuration.nix:
{
  # ... existing configuration ...

  nix.nixPath = [
    "nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos"
    "nixos-config=/etc/nixos/configuration.nix"
  ];

  # ... rest of the configuration ...
}
  • Removed any previous NIX_PATH settings from configuration.nix and other imported files.
  • Rebuilt the system using sudo nixos-rebuild switch --show-trace --verbose.
  • Result: The error persisted, and /etc/pam/environment and /etc/set-environment continued to contain the incorrect NIX_PATH mapping to nixos-24.05.

3. Removing and Re-adding Channels:

  • Removed existing channels:
sudo nix-channel --remove nixos
sudo nix-channel --remove nixpkgs
  • Cleared channel profiles and links:
sudo rm -rf /nix/var/nix/profiles/per-user/root/channels*
sudo rm -rf /root/.nix-defexpr
  • Re-added channels:
sudo nix-channel --add https://nixos.org/channels/nixos-unstable nixos
sudo nix-channel --add https://nixos.org/channels/nixpkgs-unstable nixpkgs
sudo nix-channel --update
  • Verified channels were set correctly using sudo nix-channel --list.

4. Verifying and Repairing the Nix Store:

  • Checked disk space and filesystem integrity; no issues found.
  • Ran sudo nix-store --verify --check-contents --repair to verify and repair the Nix store; no errors reported.

5. Attempting Rebuild with Correct NIX_PATH:

  • Tried rebuilding with NIX_PATH set in the command:
sudo NIX_PATH="nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/etc/nixos/configuration.nix" nixos-rebuild switch --show-trace --verbose
  • Result: The error persisted.

6. Inspecting Environment Files:

  • Found that /etc/pam/environment and /etc/set-environment contained incorrect NIX_PATH settings:
NIX_PATH   DEFAULT="nixpkgs=https://nixos.org/channels/nixos-24.05:nixpkgs-unstable=https://nixos.org/channels/nixpkgs-unstable"
  • Unable to determine what is causing these files to be generated with the incorrect NIX_PATH.

7. Checking for Hardcoded References:

  • Searched the entire /etc/nixos directory for hardcoded references to nixos-24.05 using:
grep -R "nixos-24.05" /etc/nixos
  • Result: No such references were found.

8. Ensuring No Other Configurations Override NIX_PATH:

  • Verified that no other NixOS options or modules were overriding nix.nixPath.
  • Checked /etc/nix/nix.conf, /etc/profile, and other global configuration files; no conflicting settings found.

9. Attempted to Reinstall Nix:

  • Considered reinstalling Nix but wanted to seek further assistance before proceeding due to potential data loss.

Current State:

  • The NIX_PATH environment variable is being automatically set to include nixpkgs=https://nixos.org/channels/nixos-24.05, despite all efforts to correct it.
  • The error persists, preventing me from rebuilding my NixOS system.
  • I’m unable to determine what is causing the NIX_PATH to be set incorrectly in /etc/pam/environment and /etc/set-environment.

Questions and Assistance Requested:

  1. What could be causing NIX_PATH to be set automatically to the incorrect value, and how can I prevent or override this?
  2. Are there any additional steps I can take to identify and resolve the source of the incorrect NIX_PATH settings?
  3. Could this be a bug in NixOS, or is there a misconfiguration that I’ve overlooked?
  4. Is there a way to force NixOS to regenerate /etc/pam/environment and /etc/set-environment with the correct NIX_PATH?

Additional Information:

  • Affected User: The issue seems to affect the root user environment, which is critical since nixos-rebuild runs as root.
  • No Manual Modifications: I have not modified any system files manually outside of the standard configuration files.
  • Reproducibility: The problem persists across reboots and after clearing caches and profiles.
  • Logs and Outputs:
    • echo $NIX_PATH Output:
/root/.nix-defexpr/channels:nixpkgs=https://nixos.org/channels/nixos-24.05:nixpkgs-unstable=https://nixos.org/channels/nixpkgs-unstable
  • Error Message During Rebuild:
error: failed to open archive: Unrecognized archive format
  • Contents of /etc/pam/environment:
NIX_PATH   DEFAULT="nixpkgs=https://nixos.org/channels/nixos-24.05:nixpkgs-unstable=https://nixos.org/channels/nixpkgs-unstable"
  • Contents of /etc/set-environment:
export NIX_PATH="nixpkgs=https://nixos.org/channels/nixos-24.05:nixpkgs-unstable=https://nixos.org/channels/nixpkgs-unstable"

Conclusion:

I’m at a loss as to why NIX_PATH is being set incorrectly, and I’m unable to rebuild my system because of this persistent error. I’ve tried setting nix.nixPath in my configuration.nix, unsetting NIX_PATH in various ways, and even removing and re-adding channels and profiles, but nothing has resolved the issue.

Any help or guidance on resolving this issue would be greatly appreciated.

Thank you in advance for your time and assistance.

Could you please share your configuration and the contents of /etc/nix/nix.conf?

When you’re talking about the “nix path,” there are actually two things: the $NIX_PATH environment variable, and the nix-path setting in nix.conf.

$NIX_PATH the env var is supposed to override nix-path the setting.

The nix.nixPath option sets $NIX_PATH. You can set nix-path in configuration.nix, but it doesn’t show up on search.nixos.org.

Try removing all instances of $NIX_PATH and nix.nixPath from your config, and instead using this:

  nix.settings.nix-path = [
    "nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos"
    "nixos-config=/etc/nixos/configuration.nix"
  ];