NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
17.92k stars 13.95k forks source link

`sudo nixos-rebuild edit` doesn't use default editor #232558

Open awall opened 1 year ago

awall commented 1 year ago

Describe the bug When running nixos-rebuild edit as root, the default editor is ignored. It always uses nano, no matter how anything is configured.

👟 Reproduction steps

  1. Add programs.neovim.enable = true; and programs.neovim.defaultEditor = true; to /etc/nixos/configuration.nix.
  2. Run sudo nixos-rebuild switch.
  3. Reboot the machine.
  4. In a prompt, type sudo nixos-rebuild edit.
  5. BUG: the file is opened in nano, instead of neovim.

👀 Expected behavior

sudo nixos-rebuild edit should open /etc/nixos/configuration.nix in neovim.

😓 Actual Behavior

sudo nixos-rebuild edit opens configuration.nix in nano, regardless of my configuration.

Priorities

Add :+1: to issues you find important.

Pacman99 commented 1 year ago

This isn't an issue with nix, but rather nixpkgs where the relevant nixos code is stored (the distinctions can be confusing). You should file the issue in https://github.com/NixOS/nixpkgs

SuperSandro2000 commented 1 year ago

@fricklerhandwerk can you please move this?

This is working as expected, see https://github.com/NixOS/nixpkgs/blob/nixos-unstable/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh#L418 The EDITOR variable is not set, you probably want to run sudo -E or chmod/chown your config so that it can be edited by your normal user.

awall commented 1 year ago

@SuperSandro2000 Thanks for the insight. I set EDITOR to nvim using environment.variables.EDITOR = "nvim";, but this doesn't appear to change the root user's variable. I wrote a script with echo "$EDITOR", and running with sudo prints "", while running with my user prints "nvim".

Am I just misunderstanding how environment.variables works? Is it not meant to touch the root's environment?

SuperSandro2000 commented 1 year ago

For me there is also a difference if I run an interactive shell (EDITOR is set to nvim) or not (editor is set to vim). Not sure why though.

mildlycoder commented 1 month ago

https://nixos.wiki/wiki/NixOS_configuration_editors

I guess this helps you edit /etc/nixos/configuration.nix in editor of your choice. worked for me.

SuperSandro2000 commented 1 month ago

Please use links to the new wiki https://wiki.nixos.org/wiki/NixOS_configuration_editors

neverbot commented 1 month ago

https://nixos.wiki/wiki/NixOS_configuration_editors

I guess this helps you edit /etc/nixos/configuration.nix in editor of your choice. worked for me.

Just to point it out for other future readers: this is not a solution, the issue is related to how to change the default editor when you use sudo nixos-rebuild edit, not how to use an alternative one.