How to get nvim to edit files without sudo?

I am attempting to use Neovim (nvim) to edit files without requiring sudo privileges, regardless of the file’s permission settings. I’ve tried it with security.sudo, but it does not seem to work. Any idea why?

security.sudo = {
    enable = true;
    extraRules = [{
    commands = [
       command = "${pkgs.neovim}/bin/nvim";
       options = [ "SETENV" "NOPASSWD" ];
    groups = [ "wheel" ];

What happens instead of your desired behavior? If another editor opens, you might have to make sure that $EDITOR variable exists for the root user.

If the behaviour is that nvim opens, but without all you configuration, that is due to fact again that your nvim config is bound to your user (and you are opening files as root instead). I saw people mess before with sudo -e but I would not recommend. Have you used the sudoedit command before (it basically is a wrapper around sudo -e)?

If your editor for the root user is set correctly, this should work.

I have configured Neovim with Home Manager, but I’m encountering an unexpected behavior with environment variables. Despite setting SUDO_EDITOR, SYSTEMD_EDITOR, EDITOR, and VISUAL to "nvim" in my root configuration, the default editor still opens as nano when running commands like sudoedit.

environment.variables = {
        SUDO_EDITOR = "nvim";
        SYSTEMD_EDITOR = "nvim";
        EDITOR = "nvim";
        VISUAL = "nvim";

Additionally, echoing $EDITOR returns variable not found. For this matter, not even cli file managers such as nnn work. They will give out the error permission denied for some reason.

I would write

security.sudo.configFile = ''
user_name ALL=(ALL) NOPASSWD:/usr/bin/env nvim

into my configuration.nix

1 Like

That does not seem to work either. I still get:
Warning: Changing a readonly file.

Are you running Nixos or another system? That would give additional context.

Aside from on which system you are trying to achieve this, if $EDITOR is unset for root, that is an indicator of where your problem lies.

It might be that you have configured nvim to only be available within you user scope, and that means it won’t be present outside that scope. That is something you haven’t told us yet.

I am running Nixos.

➜ nix-shell -p nix-info --run "nix-info -m"
 - system: `"x86_64-linux"`
 - host os: `Linux 6.6.0-zen1, NixOS, 23.11 (Tapir), 23.11.20231104.85f1ba3`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.17.1`
 - channels(root): `"nixos-23.05"`
 - nixpkgs: `/nix/var/nix/profiles/per-user/root/channels/nixos`

I have neovim set up as a home manager user. I just attempted to set it up in the root configuration, but that does not seem to work either:

environment.variables = { 
        EDITOR = "nvim"; 
        VISUAL = "nvim";
    programs.neovim = {
		enable = true;
		defaultEditor = true;
		viAlias = true;
		vimAlias = true;
		configure = {
                customRC = ''
                    set clipboard=unnamedplus
                    set number relativenumber
                    set shiftwidth=4
                    set tabstop=4
                    set expandtab
                    filetype plugin indent on
                    set autoindent
                    set ttyfast

I did log out and in again, before anyone asks.

1 Like