nix-channel: do not set empty nix-path when disabling channels

An empty nix-path in nix.conf will disable NIX_PATH environment variable
entirely, which is not necessarily implied by users who want to disable
nix channels. NIX_PATH also has some usages in tools like nixos-rebuild
or just as user aliases.

That change is surprising and debatable, and also caused breakages in
nixpkgs-review and user configs.

See:
- https://github.com/NixOS/nixpkgs/pull/242098/files#r1269891427
- https://github.com/Mic92/nixpkgs-review/issues/343
- https://github.com/NixOS/nix/pull/10998

Co-authored-by: oxalica <oxalicc@pm.me>
This commit is contained in:
Vladimir Panteleev 2024-06-30 15:40:46 +00:00
parent 9e1d223e59
commit 1e6acabaeb
No known key found for this signature in database
GPG Key ID: 5004F0FAD051576D

View File

@ -12,6 +12,7 @@ let
mkDefault
mkIf
mkOption
stringAfter
types
;
@ -94,10 +95,29 @@ in
NIX_PATH = cfg.nixPath;
};
nix.settings.nix-path = mkIf (! cfg.channel.enable) (mkDefault "");
systemd.tmpfiles.rules = lib.mkIf cfg.channel.enable [
''f /root/.nix-channels - - - - ${config.system.defaultChannel} nixos\n''
];
system.activationScripts.no-nix-channel = mkIf (!cfg.channel.enable)
(stringAfter [ "etc" "users" ] ''
if [ -e "/root/.nix-defexpr/channels" ]; then
echo "WARNING: /root/.nix-defexpr/channels exists, but channels have been disabled." 1>&2
echo "Due to https://github.com/NixOS/nix/issues/9574, Nix may still use these channels when NIX_PATH is unset." 1>&2
echo "Delete the above directory to prevent this." 1>&2
fi
if [ -e "/nix/var/nix/profiles/per-user/root/channels" ]; then
echo "WARNING: /nix/var/nix/profiles/per-user/root/channels exists, but channels have been disabled." 1>&2
echo "Due to https://github.com/NixOS/nix/issues/9574, Nix may still use these channels when NIX_PATH is unset." 1>&2
echo "Delete the above directory to prevent this." 1>&2
fi
getent passwd | while IFS=: read -r _ _ _ _ _ home _ ; do
if [ -n "$home" -a -e "$home/.nix-defexpr/channels" ]; then
echo "WARNING: $home/.nix-defexpr/channels exists, but channels have been disabled." 1>&2
echo "Due to https://github.com/NixOS/nix/issues/9574, Nix may still use these channels when NIX_PATH is unset." 1>&2
echo "Delete the above directory to prevent this." 1>&2
fi
done
'');
};
}