How to configure psql wrapper in NixOS module

Hi, I’m having some trouble making my psql behave nicely in NixOS. Since NixOS’s psql is wrapped, it doesn’t read my $EDITOR and I’m having to use nano to edit my SQL commands :frowning::frowning::frowning:. I’d like to configure the wrapper, or set a psqlrc.

Having read PostgreSQL - NixOS Wiki, I understand that I can use services.postgresql.settings to configure the PostgreSQL server settings, but I couldn’t find mention on how to configure the psql client. I didn’t figure out configuration options from reading the source either.

What do you think? Thanks.

If there is no way to configure it, maybe add a shellscriptBin or add an alias to your shell, which simply does EDITOR=... wrapped-psql?

Since it’s a wrapped psql, it simply ignores the environment. I would be happy to just use the EDITOR from my environment, but it gets ignored.

What makes you say that psql on NixOS is wrapped? It doesn’t seem to be to me: psql is a binary rather than a script on my system. In my testing, it also seemed to respect the EDITOR variable.

I checked that the environment is ignored like this:

TEST=test sudo -u postgres psql <db-name>

# \getenv a TEST                                                                                                                                                                                  
# \echo :a                                                                                                                                                                                        

Conversely, if I use getenv to see the PATH, it gives me my Nix profiles apparently.

# \getenv a PATH                                                                                                                                                                                  
# \echo :a                                                                                                                                                                                        

Indeed it seems that wrapping is not the issue. file /nix/store/cf5rrb1l9hmhys7346ghi13qd97p34xf-postgresql-15.3/bin/psql tells us the binary is an ELF.

The issue here is probably sudo, not psql. sudo generally only keeps around environment variables it’s configured to. And indeed, if I compare the output of env | sort and sudo env | sort, the latter is missing my EDITOR among many other things.

1 Like