Use vscode editor configuration.nix cant save

code /etc/nixos/configuration.nix

use root user try save
tips:

not save “configuration.nix”: Unable to find pkexec or kdesudo.

how to deal with? need to config with some things?

Use sudoedit or sudo -e to edit the /etc/nixos/configuration.nix or edit in a user owned location and build from there using an appropriate nix path entry for nixos-config.

1 Like

use sudo -e or sudo vim can edit, but use code for edit cant write the file.

You can use vscode with sudo -e by setting EDITOR="code --wait".

Remember to close it that changes are actually applied to the file.

2 Likes

thk you, its working.

this should work:

vscode
new window
open folder: /etc/nixos
open file: configuration.nix
modify
save → failed to save → retry as sudo

on kde plasma, “policykit1 kde agent” will ask password

limitations:
need password on every “save file”
cannot create new files
git integration is not working

more comfortable is this solution:

for example

mkdir ~/etc
sudo mv /etc/nixos ~/etc/
sudo chown -R $(id -un):users ~/etc/nixos
sudo ln -s ~/etc/nixos /etc/

#code ~/etc/nixos/
codium ~/etc/nixos/

see also
Nixos-rebuild: Specifying a different configuration location
Github strategies for configuration.nix?

Note that the issue was:

This means that vscode was running under a system with no DE capable of asking for the password to save the file.

Overall, just editing config in a user-owned directory is probably best. Note you can do this without symlinks by telling nixos-rebuild where your config is:

nixos-rebuild switch -I nixos-config=$HOME/etc/nixos/configuration.nix

See also the wiki entry on the topic: Nixos-rebuild - NixOS Wiki

1 Like