Skip to content

nixos: Disable nix-channel#242098

Merged
roberth merged 5 commits intoNixOS:masterfrom
hercules-ci:nixos-no-nix-channel
Jul 18, 2023
Merged

nixos: Disable nix-channel#242098
roberth merged 5 commits intoNixOS:masterfrom
hercules-ci:nixos-no-nix-channel

Conversation

@roberth
Copy link
Member

@roberth roberth commented Jul 7, 2023

Motivation

This fixes a warning that's been bothering me.

warning: Nix search path entry '/nix/var/nix/profiles/per-user/root/channels' does not exist, ignoring
Description of changes
  • Allow users to remove nix-channel and its accompanying files. Opt-in.

    configuration.nix:

     nix.channel.enable = false;
    
  • Extends the installer test case to make sure users can switch from a traditional config to a flake.

Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 23.11 Release Notes (or backporting 23.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

@github-actions github-actions bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` labels Jul 7, 2023
@roberth roberth mentioned this pull request Jul 7, 2023
12 tasks
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. labels Jul 7, 2023
@roberth
Copy link
Member Author

roberth commented Jul 7, 2023

@ofborg test installer.simple
@ofborg test installer.switchToFlake

Copy link
Member

@SuperSandro2000 SuperSandro2000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea, then I no longer need to hack around removing channels

Comment on lines +44 to +51
default =
if cfg.channel.enable
then [
"nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos"
"nixos-config=/etc/nixos/configuration.nix"
"/nix/var/nix/profiles/per-user/root/channels"
]
else [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
default =
if cfg.channel.enable
then [
"nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos"
"nixos-config=/etc/nixos/configuration.nix"
"/nix/var/nix/profiles/per-user/root/channels"
]
else [];
default = lib.optionals cfg.channel.enable [
"nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos"
"nixos-config=/etc/nixos/configuration.nix"
"/nix/var/nix/profiles/per-user/root/channels"
];

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not exactly equal to the documented default. Let's keep them in sync, and see other comment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference?

Comment on lines +53 to +59
if nix.channel.enable
then [
"nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos"
"nixos-config=/etc/nixos/configuration.nix"
"/nix/var/nix/profiles/per-user/root/channels"
]
else [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if nix.channel.enable
then [
"nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos"
"nixos-config=/etc/nixos/configuration.nix"
"/nix/var/nix/profiles/per-user/root/channels"
]
else [];
lib.optionals cfg.channel.enable [
"nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos"
"nixos-config=/etc/nixos/configuration.nix"
"/nix/var/nix/profiles/per-user/root/channels"
];

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is harder to understand for someone who doesn't contribute to nixpkgs or other expressions regularly. optionals is idiosyncratic to Nixpkgs lib, whereas everyone understand if then else.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we had multiple treewide replacement for this already, so I don't think we should add new candidates for this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This documentation should not require someone to know the optionals function.

Copy link
Member

@Ericson2314 Ericson2314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!

@roberth roberth merged commit 8ad59ed into NixOS:master Jul 18, 2023
@github-actions
Copy link
Contributor

Successfully created backport PR for release-23.05:


system.activationScripts.nix-channel = stringAfter [ "etc" "users" ]
''
nix.settings.nix-path = mkIf (! cfg.channel.enable) (mkDefault "");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to set this to empty string here because that clears the default nix-path setting of nix which breaks many programs like nix-tree or comma which rely on it to find some nixpkgs which in my case points to a flake.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. The point of this is one really doesn't want to use a populated NIX_PATH. If other programs can't cope with that, they should be fixed.

One can always set nix.settings.nix-path elsewhere than this module instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default nix-path is left empty which means it default to NIX_PATH env. When we explicitly set it to empty, that is no longer the case and things completely break for many tools which makes this option rather useless.

 ➜ nix show-config | rg nix-path
nix-path = nixpkgs=/nix/store/x6ly3m0dylzsbrgz0sx937xxcyswr2yp-source nixos=/nix/store/x6ly3m0dylzsbrgz0sx937xxcyswr2yp-source nixos-config=/etc/nixos/configuration.nix

 ➜ (unset NIX_PATH; nix show-config | rg nix-path)
nix-path =

 ➜ (unset NIX_PATH; nix --option nix-path "" show-config | rg nix-path)
nix-path =

 ➜ nix --option nix-path "" show-config | rg nix-path
nix-path =

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems like the behaviour that someone ripping out a formerly-mandatory system component to avoid non-flakes things would want, given that properly flakes-aware tools like nixos-rebuild(8) won't look at the Nix path when used correctly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm, yeah, that sounds about right. Maybe I misunderstood the feature a bit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. The point of this is one really doesn't want to use a populated NIX_PATH

Kind of debatable, IMO. Feels like you're assuming that disabling channels automatically implies that:

  • The person is using flakes
  • The person wants to use a PURE flake workflow everywhere.

The reality is that:

  • nixpkgs is kind of special
  • You can use --impure with flakes
  • Some people may want a more "it just works" sort of thing with certain types of commands/ and /or the ability to build their system against a flake and then have all other commands use the same version of nixpkgs that their system was built against, or maybe still allow the NIX_PATH variable to take effect.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems like the behaviour that someone ripping out a formerly-mandatory system component to avoid non-flakes things would want, given that properly flakes-aware tools like nixos-rebuild(8) won't look at the Nix path when used correctly.

This is a weird position that doesn't make sense.

You can only use NIX_PATH at all, if you pass the --impure flag to these commands. If you dont, you'll get:

error: cannot look up '<nixpkgs>' in pure evaluation mode (use '--impure' to override)

If you're explicitly opting IN by passing the --impure flag, this behavior makes absolutely no sense.

Really, the problem is not setting nix-path to the empty string, the problem is NixOS/nix#8890 and NixOS/nix#8890

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also encountered the problem where I don't use channels but put aliases in NIX_PATH for commands. This setting disables NIX_PATH envvar entirely, which is not what I want. I'm kinda thinking we should revert this line.

@colonelpanic8
Copy link
Contributor

@roberth I find some of the details of this a little confusing:

In particular, this:

    environment.sessionVariables = {
      NIX_PATH = cfg.nixPath;
    };

    nix.settings.nix-path = mkIf (! cfg.channel.enable) (mkDefault "");

is very weird, because it makes it so that there are now two variables for setting the nix path:

nix.settings.nix-path and nix.nixPath

and nix.settings.nix-path is not defaulted to the value of nix.nixPath

it also seems to me that nix.settings.nix-path

should be defined in nix.nix, not in nix-channel.nix, but yeah I'm finding all of this pretty confusing.

This is especially an issue in light of NixOS/nix#8890 which is the same as NixOS/nix#8890

@roberth
Copy link
Member Author

roberth commented Sep 4, 2023

weird, because it makes it so that there are now two variables for setting the nix path:

nix.settings.nix-path and nix.nixPath

I have only preserved nix.nixPath, which was already somewhat redundant. It sets the environment instead of /etc/nix/nix.conf.
iirc I did have to affect both in order to achieve a Nix that doesn't have rudimentary channel behavior.

Main thing is that I wasn't aware of Nix's strange treatment of nix-path vs NIX_PATH.

My original intent with this PR had two layers

  • Make it possible to not have any channels, without Nix complaining about it. I didn't have any channels on a fresh install and it was bothering me.
  • Help find possible spurious usages of channels and/or the default NIX_PATH, for both flakes and - perhaps - pure mode without flakes.

It was not my intent to break NIX_PATH entirely. If you have, say, a nix shell which sets it to what the repo has pinned, that should just work.

@colonelpanic8
Copy link
Contributor

I have only preserved nix.nixPath, which was already somewhat redundant. It sets the environment instead of /etc/nix/nix.conf

Yeah I guess its just kind of confusing. If someone sets the value of that variable to something but disables channels, it actually just has no effect. I wasn't aware of the distinction. Would it really be so bad to default the nix-path setting to whatever nixPath is in the event where channels are disabled?

Alternatively, maybe we should make it an error to set nixPath at all when channels are disabled.

Or maybe its just fine to have different settings for the environment variable and the nix.conf setting, as long as my nix PR is merged.

@roberth
Copy link
Member Author

roberth commented Sep 4, 2023

If someone sets the value of that variable to something but disables channels, it actually just has no effect.

Yeah. It shouldn't be like that. I almost started explaining that it does have an effect, but that's the entire point of the bug, which I consider it to be.

Though I think it is worth noting that in the case where the bug is fixed in nix, we'll probably still want the NixOS behavior that we currently have.

The difference between the nix.nixPath and nix.settings.nix-path will matter when you unset NIX_PATH in a shell, and I'm sure there's some scripts and shellHooks out there that do it, and possibly even by mistake.

To be honest, applying a fix to both projects seems a bit daunting as we'll have three scenarios to consider, while even a single scenario is already quite confusing. So I'd prefer to focus on making Nix treat its option more sensibly, and come back to this NixOS change later if needed.

@oxalica
Copy link
Contributor

oxalica commented Dec 9, 2023

I created #273170 to revert the line setting an empty nix-path.

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/24-05-add-flake-to-nix-path/46310/11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants