Skip to content

Commit

Permalink
nix-channel: do not set empty nix-path when disabling channels
Browse files Browse the repository at this point in the history
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
- Mic92/nixpkgs-review#343
- NixOS/nix#10998

Co-authored-by: oxalica <[email protected]>
(cherry picked from commit 1e6acab)
  • Loading branch information
CyberShadow authored and github-actions[bot] committed Jul 28, 2024
1 parent e2494c5 commit 98bccac
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions nixos/modules/config/nix-channel.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ let
mkDefault
mkIf
mkOption
stringAfter
types
;

Expand Down Expand Up @@ -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
'');
};
}

0 comments on commit 98bccac

Please sign in to comment.