From 1e6acabaebb2d3eb13cde9d8742aadb9480abcd7 Mon Sep 17 00:00:00 2001 From: Vladimir Panteleev Date: Sun, 30 Jun 2024 15:40:46 +0000 Subject: [PATCH] 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 --- nixos/modules/config/nix-channel.nix | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/nixos/modules/config/nix-channel.nix b/nixos/modules/config/nix-channel.nix index 6498ce6c469c..772831ab6d17 100644 --- a/nixos/modules/config/nix-channel.nix +++ b/nixos/modules/config/nix-channel.nix @@ -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 + ''); }; }