nixpkgs/nixos/modules/services/misc
Maximilian Bosch 48459567ae nixos/postgresql: drop ensurePermissions, fix ensureUsers for postgresql15
Closes #216989

First of all, a bit of context: in PostgreSQL, newly created users don't
have the CREATE privilege on the public schema of a database even with
`ALL PRIVILEGES` granted via `ensurePermissions` which is how most of
the DB users are currently set up "declaratively"[1]. This means e.g. a
freshly deployed Nextcloud service will break early because Nextcloud
itself cannot CREATE any tables in the public schema anymore.

The other issue here is that `ensurePermissions` is a mere hack. It's
effectively a mixture of SQL code (e.g. `DATABASE foo` is relying on how
a value is substituted in a query. You'd have to parse a subset of SQL
to actually know which object are permissions granted to for a user).

After analyzing the existing modules I realized that in every case with
a single exception[2] the UNIX system user is equal to the db user is
equal to the db name and I don't see a compelling reason why people
would change that in 99% of the cases. In fact, some modules would even
break if you'd change that because the declarations of the system user &
the db user are mixed up[3].

So I decided to go with something new which restricts the ways to use
`ensure*` options rather than expanding those[4]. Effectively this means
that

* The DB user _must_ be equal to the DB name.
* Permissions are granted via `ensureDBOwnerhip` for an attribute-set in
  `ensureUsers`. That way, the user is actually the owner and can
  perform `CREATE`.
* For such a postgres user, a database must be declared in
  `ensureDatabases`.

For anything else, a custom state management should be implemented. This
can either be `initialScript`, doing it manual, outside of the module or
by implementing proper state management for postgresql[5], but the
current state of `ensure*` isn't even declarative, but a convergent tool
which is what Nix actually claims to _not_ do.

Regarding existing setups: there are effectively two options:

* Leave everything as-is (assuming that system user == db user == db
  name): then the DB user will automatically become the DB owner and
  everything else stays the same.

* Drop the `createDatabase = true;` declarations: nothing will change
  because a removal of `ensure*` statements is ignored, so it doesn't
  matter at all whether this option is kept after the first deploy (and
  later on you'd usually restore from backups anyways).

  The DB user isn't the owner of the DB then, but for an existing setup
  this is irrelevant because CREATE on the public schema isn't revoked
  from existing users (only not granted for new users).

[1] not really declarative though because removals of these statements
    are simply ignored for instance: https://github.com/NixOS/nixpkgs/issues/206467
[2] `services.invidious`: I removed the `ensure*` part temporarily
    because it IMHO falls into the category "manage the state on your
    own" (see the commit message). See also
    https://github.com/NixOS/nixpkgs/pull/265857
[3] e.g. roundcube had `"DATABASE ${cfg.database.username}" = "ALL PRIVILEGES";`
[4] As opposed to other changes that are considered a potential fix, but
    also add more things like collation for DBs or passwords that are
    _never_ touched again when changing those.
[5] As suggested in e.g. https://github.com/NixOS/nixpkgs/issues/206467
2023-11-13 17:16:25 +01:00
..
sourcehut nixos/postgresql: drop ensurePermissions, fix ensureUsers for postgresql15 2023-11-13 17:16:25 +01:00
taskserver nixos/manual: render module chapters with nixos-render-docs 2023-01-27 20:07:34 +01:00
airsonic.nix treewide: use types.port in nixos modules 2022-11-10 09:30:00 +01:00
amazon-ssm-agent.nix amazon-ssm-agent: rename from ssm-agent 2023-10-22 22:04:09 +02:00
ananicy.nix nixos/ananicy: take listOf attrs instead of string 2023-07-12 20:13:03 +03:00
ankisyncd.nix replace ankisyncd with ankisyncd-rs 2023-07-04 18:14:18 +09:00
apache-kafka.nix treewide: use types.port in nixos modules 2022-11-10 09:30:00 +01:00
atuin.nix nixos/postgresql: drop ensurePermissions, fix ensureUsers for postgresql15 2023-11-13 17:16:25 +01:00
autofs.nix treewide: automatically md-convert option descriptions 2022-07-30 15:16:34 +02:00
autorandr.nix autorandr: 1.12.1 -> 1.13, nixos/services/autorandr: add ignoreLeadOption 2023-01-15 16:57:13 +01:00
autosuspend.nix nixos/autosuspend: init at version 4.3.0 2023-01-22 17:25:01 +01:00
bazarr.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
bcg.nix nixos/bcg: init module 2023-07-19 09:47:04 +02:00
beanstalkd.nix treewide: switch to port type for nixos modules 2022-12-01 22:30:00 +01:00
bees.nix nixos/*: automatically convert option docs to MD 2022-08-03 22:46:41 +02:00
bepasty.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
calibre-server.nix services/calibre-server: Add new http & auth options (#216497) 2023-06-28 14:06:47 +02:00
canto-daemon.nix treewide: automatically md-convert option descriptions 2022-07-30 15:16:34 +02:00
cfdyndns.nix nixos/cfdyndns: dynamic user and loadcredentials 2023-09-14 07:53:36 +00:00
cgminer.nix Update nixos/modules/services/misc/cgminer.nix 2023-07-02 19:03:11 +02:00
clipcat.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
clipmenu.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
confd.nix treewide: remove execute bit for non-executable *.nix files 2023-10-20 19:55:39 +02:00
cpuminer-cryptonight.nix treewide: automatically md-convert option descriptions 2022-07-30 15:16:34 +02:00
devmon.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
dictd.nix treewide: automatically md-convert option descriptions 2022-07-30 15:16:34 +02:00
disnix.nix treewide: use optionalAttrs instead of 'else {}' 2023-06-25 11:01:34 -03:00
docker-registry.nix treewide: use optionalAttrs instead of 'else {}' 2023-06-25 11:01:34 -03:00
domoticz.nix treewide: switch to port type for nixos modules 2022-12-01 22:30:00 +01:00
duckling.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
dwm-status.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
dysnomia.nix dysnomia: 0.10.1 -> 0.10.2 2023-09-27 18:35:30 +02:00
errbot.nix treewide: automatically md-convert option descriptions 2022-07-30 15:16:34 +02:00
etcd.nix nixos/etcd: allow to choose the package 2023-05-28 08:04:43 +10:00
etebase-server.nix nixos/etebase-server: Leverage $PATH 2023-01-07 12:20:03 +01:00
etesync-dav.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
evdevremapkeys.nix nixos/evdevremapkeys: init 2023-07-19 23:06:31 -04:00
felix.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
forgejo.nix nixos/postgresql: drop ensurePermissions, fix ensureUsers for postgresql15 2023-11-13 17:16:25 +01:00
freeswitch.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
fstrim.nix nixos/fstrim: fix overriding the timer interval 2023-04-30 17:36:49 +02:00
gammu-smsd.nix treewide: use more lib.optionalString 2023-04-07 13:38:33 +02:00
geoipupdate.nix treewide: convert fake octal ints to strings 2022-10-28 17:23:44 +02:00
gitea.nix nixos/postgresql: drop ensurePermissions, fix ensureUsers for postgresql15 2023-11-13 17:16:25 +01:00
gitlab.md nixos/gitlab: convert manual chapter to MD 2023-01-10 10:31:56 +01:00
gitlab.nix nixos/gitlab: Add a warning message 2023-09-05 13:46:44 +02:00
gitolite.nix nixos/gitolite: add 'description' module option 2022-10-10 23:14:46 +02:00
gitweb.nix nixos/*: literalDocBook -> literalMD 2022-08-27 19:18:29 +02:00
gogs.nix nixos/gogs: fix deprecations for 0.13.0 2023-07-30 15:54:16 +02:00
gollum.nix nixos/gollum: remove non-existent maintainer 2023-10-06 23:52:42 +03:00
gpsd.nix nixos/gpsd: add extraArgs option 2023-10-02 15:10:12 +02:00
greenclip.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
headphones.nix treewide: automatically md-convert option descriptions 2022-07-30 15:16:34 +02:00
heisenbridge.nix treewide: update path to getent 2023-07-12 02:32:23 +07:00
homepage-dashboard.nix nixos/homepage-dashboard: init 2023-07-13 09:38:26 +01:00
ihaskell.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
input-remapper.nix nixos/*: remove trailing period in mkEnableOptions 2023-02-08 15:23:34 +01:00
irkerd.nix treewide: automatically md-convert option descriptions 2022-07-30 15:16:34 +02:00
jackett.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
jellyfin.nix Jellyfin service starts after network-online.target 2023-09-14 18:31:22 +02:00
jellyseerr.nix nixos/jellyseerr: init 2023-03-10 16:18:00 +01:00
klipper.nix nixos: fix bad mkEnableOption descriptions 2023-10-20 16:22:40 +01:00
languagetool.nix nixos/languagetool: fix startup configuration option 2022-10-29 18:05:53 +02:00
leaps.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
libreddit.nix libreddit: Add package option 2022-12-04 14:00:17 +01:00
lidarr.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
lifecycled.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
logkeys.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
mame.nix treewide: automatically md-convert option descriptions 2022-07-30 15:16:34 +02:00
mbpfan.nix nixos/mbpfan: adjust defaults 2023-09-20 16:10:26 +03:00
mediatomb.nix treewide: fix redirected and broken URLs 2023-11-11 10:49:01 +01:00
metabase.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
moonraker.nix moonraker: add useGpiod 2023-10-02 06:09:58 +04:00
mqtt2influxdb.nix nixos/mqtt2influxdb: init module 2023-07-19 09:47:04 +02:00
n8n.nix nixos/n8n: correct configuration, webhookUrl type 2023-07-05 23:15:52 +01:00
nitter.nix Merge pull request #208924 from erdnaxe/nitter-network-online 2023-07-12 14:19:30 +02:00
nix-gc.nix nixos/*: automatically convert option docs 2022-08-06 20:39:12 +02:00
nix-optimise.nix nixos/nix-optimise: persist timer 2023-05-20 02:32:19 +02:00
nix-ssh-serve.nix treewide: automatically md-convert option descriptions 2022-07-30 15:16:34 +02:00
novacomd.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
ntfy-sh.nix nixos/ntfy.sh: require base-url setting 2023-07-03 08:58:05 +08:00
nzbget.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
nzbhydra2.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
octoprint.nix Octoprint vcgencmd patch (#213201) 2023-01-29 16:11:48 +08:00
ombi.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
osrm.nix treewide: switch to port type for nixos modules 2022-12-01 22:30:00 +01:00
owncast.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
packagekit.nix nixos: fix bad mkEnableOption descriptions 2023-10-20 16:22:40 +01:00
paperless.nix nixos/paperless: set PAPERLESS_SECRET_KEY 2023-10-27 21:11:01 +02:00
parsoid.nix treewide: use types.port in nixos modules 2022-11-10 09:30:00 +01:00
persistent-evdev.nix nixos/*: convert straggler options to MD 2022-08-31 17:27:38 +02:00
pinnwand.nix nixos/pinnwand: convert to freeform type, drop reaper unit 2022-11-27 13:50:44 +01:00
plex.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
plikd.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
podgrab.nix nixos: fix typos 2022-12-17 19:31:14 -05:00
polaris.nix modules: add mkPackageOptionMD 2023-01-05 02:33:13 +01:00
portunus.nix nixos/portunus: use openldap compiled with libxcrypt-legacy 2023-05-21 18:54:56 +02:00
prowlarr.nix nixos/prowlarr: make use of 'lib.getExe' 2023-07-14 13:43:26 +00:00
pufferpanel.nix nixos/pufferpanel: buildFHSUserEnv -> buildFHSEnv 2023-05-29 07:08:19 +03:00
pykms.nix nixos/pykms: rename systemd deprecated MemoryLimit to MemoryMax. 2023-02-07 10:04:24 +08:00
radarr.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
readarr.nix readarr: init at 0.1.4.1596 2023-03-12 20:54:23 +01:00
redmine.nix nixos/postgresql: drop ensurePermissions, fix ensureUsers for postgresql15 2023-11-13 17:16:25 +01:00
ripple-data-api.nix treewide: switch to port type for nixos modules 2022-12-08 00:00:00 +01:00
rippled.nix treewide: use types.port in nixos modules 2022-11-10 09:30:00 +01:00
rkvm.nix nixos/rkvm: init 2023-10-18 06:21:40 +03:00
rmfakecloud.nix treewide: convert fake octal ints to strings 2022-10-28 17:23:44 +02:00
rshim.nix nixos: fix bad mkEnableOption descriptions 2023-10-20 16:22:40 +01:00
safeeyes.nix Merge pull request #182329 from linsui/safeeyes 2022-09-14 12:30:09 +02:00
sdrplay.nix nixos/*: convert options with admonitions to MD 2022-08-31 16:36:16 +02:00
serviio.nix nixos: fix typos 2022-12-17 19:31:14 -05:00
sickbeard.nix treewide: automatically md-convert option descriptions 2022-07-30 15:16:34 +02:00
signald.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
siproxd.nix nixos: fix typos 2023-05-19 22:31:04 -04:00
snapper.nix nixos/snapper: support more options 2023-04-09 02:28:16 -04:00
soft-serve.nix nixos/soft-serve: fix typos in option descriptions 2023-11-12 22:34:54 +01:00
sonarr.nix nixos/sonarr: add package option 2022-10-27 08:49:23 -04:00
spice-autorandr.nix nixos/spice-autorandr: init 2023-10-22 13:45:56 +00:00
spice-vdagentd.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
spice-webdavd.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
sssd.nix Merge pull request #206273 from wucke13/master 2023-07-18 13:12:06 +02:00
subsonic.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
sundtek.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
svnserve.nix treewide: automatically md-convert option descriptions 2022-07-30 15:16:34 +02:00
synergy.nix synergy: fix services.synergy.server.tls.cert (#196867) 2022-10-20 12:34:45 +08:00
sysprof.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
tandoor-recipes.nix nixos/tandoor-recipes: disable debug toolbar 2023-05-06 14:22:17 +01:00
tautulli.nix treewide: use types.port in nixos modules 2022-11-10 09:30:00 +01:00
tiddlywiki.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
tp-auto-kbbl.nix nixos: fix bad mkEnableOption descriptions 2023-10-20 16:22:40 +01:00
tzupdate.nix treewide: automatically md-convert option descriptions 2022-07-30 15:16:34 +02:00
uhub.nix nixos/*: automatically convert option descriptions 2022-08-31 16:32:53 +02:00
weechat.md nixos/weechat: convert manual chapter to MD 2023-01-10 10:31:57 +01:00
weechat.nix nixos/manual: render module chapters with nixos-render-docs 2023-01-27 20:07:34 +01:00
xmr-stak.nix modules/xmr-stak: drop broken cudaSupport option 2023-09-22 17:40:04 +01:00
xmrig.nix xmrig: add meta.mainProgram 2023-10-28 11:21:57 -07:00
zoneminder.nix nixos: fix bad mkEnableOption descriptions 2023-10-20 16:22:40 +01:00
zookeeper.nix treewide: use mkEnableOption in nixos modules 2022-11-10 09:30:00 +01:00