172 Commits
Author SHA1 Message Date
nixpkgs-ci[bot]andGitHub a800466adb Merge staging-next into staging 2026-06-13 18:31:02 +00:00
Wolfgang Walther e5699f2308 postgresqlPackages.plv8: drop
`plv8` depends on the outdated NodeJS 20. Upstream waits for v8-cmake to
make a move (https://github.com/plv8/plv8/issues/610), but that project
has been inactive for a bit more than a year.

Even when allowing the insecure nodejs20, the package fails to build.
Insecure and broken - we might as well drop it.
2026-06-13 11:53:42 +02:00
Wolfgang Walther 9c5e58d079 postgresql: 17 -> 18
Now that 26.05 is branched off, we should bump PostgreSQL's default
version to v18.
2026-05-25 16:10:08 +02:00
h7x4 b36f8e0b79 various: add {file} tags for nixos option descriptions 2026-01-18 21:49:31 +09:00
NAHO a2ed7e8d88 nixos: remove optional builtins prefixes from prelude functions
Remove optional builtins prefixes from prelude functions by running:

    builtins=(
      abort
      baseNameOf
      break
      derivation
      derivationStrict
      dirOf
      false
      fetchGit
      fetchMercurial
      fetchTarball
      fetchTree
      fromTOML
      import
      isNull
      map
      null
      placeholder
      removeAttrs
      scopedImport
      throw
      toString
      true
    )

    fd \
      --exclude doc/manual/release-notes \
      --type file \
      . \
      nixos \
      --exec-batch sed --in-place --regexp-extended "
        s/\<builtins\.($(
          printf '%s\n' "${builtins[@]}" |
            paste --delimiter '|' --serial -
        ))\>/\1/g
      "

    nix fmt
2026-01-15 16:07:55 +01:00
MikilioandWolfgang Walther fcdfa7b5d0 nixos/postgresql: remove explicit submodule options
These can be set via the freemform type anyway.
2025-12-26 11:55:54 +01:00
MikilioandWolfgang Walther 3c589f7550 nixos/postgresql: add freeformType to ensureClauses option
This commit adds a freeform type to accomodate for new options to use
with ALTER ROLE.
It also will perform extra checking and conversion for options with
parameters.
Converts option names into upper case with space instead of underscore.
2025-12-26 11:55:53 +01:00
Bouke van der Bijl b67248cd59 nixos/postgresql: don't create postgresql-setup if no setup is needed
Also add postgresql replication test

Fixes #469859
2025-12-22 11:01:11 +01:00
Leona MaroniandWolfgang Walther a3f56d583f postgresql_13: drop as it's EOL 2025-11-18 12:32:17 +01:00
Maximilian Bosch 6ae194e419 nixos/postgresql: set Restart=always for postgresql.service
...including a slightly more careful config around restarts, i.e.

* We have intervals of 5 seconds between restarts instead of 100ms.

* If we exceed 5 start attempts in 5*120s (with 120s being the timeout),
  start job gets rate-limited and thus aborted. Do note that there are
  at most 5 start attempts allowed in ~625s by default. If the startup
  fails very quickly, either wait until the rate-limit is over or reset
  the counter using `systemctl reset-failed postgresql.service`.

* The interval of 625s (plus 5s of buffer) are automatically derived
  from RestartSec & TimeoutSec. Changing either will also affect
  StartLimitIntervalSec unless overridden with `mkForce`.
2025-07-26 19:11:54 +02:00
Maximilian Bosch 03d0fed6f8 nixos/postgresql: implement auto-restart & rework dependencies of postgresql.target
At my employer's NixOS-based platform, PostgreSQL is configured with
`Restart=always` which got never upstreamed, unfortunately.

This however revealed an interesting problem when using bi-directional
BindsTo: when killing `postgresql.service`, sometimes both the service &
target starts back up and sometimes they don't. According to an upstream
bugreport[1] this is a known problem because you have two conflicting
operations scheduled in a single transaction, namely

* When (auto-)restarting, a restart job for all units bound to the
  restarting unit are immediately scheduled[2].

* Due to the `BindsTo` relationship, a stop-job for `postgresql.target`
  is scheduled immediately by the manager loop[3]. This is caused by the
  `UNIT_ATOM_CANNOT_BE_ACTIVE_WITHOUT` "atom" which is ONLY set for a
  BindsTo relationship[4].

  When this is processed first, the restart is inhibited:

      Jul 12 13:25:51 nixos systemd[1]: postgresql.service: Main process exited, code=killed, status=9/KILL
      Jul 12 13:25:51 nixos systemd[1]: postgresql.service: Changed running -> stop-sigterm
      Jul 12 13:25:51 nixos systemd[1]: postgresql.target: Trying to enqueue job postgresql.target/stop/replace
      Jul 12 13:25:51 nixos systemd[1]: postgresql.service: Installed new job postgresql.service/stop as 80053
      Jul 12 13:25:51 nixos systemd[1]: postgresql.target: Installed new job postgresql.target/stop as 80052
      Jul 12 13:25:51 nixos systemd[1]: postgresql.target: Enqueued job postgresql.target/stop as 80052
      [...]
      Jul 12 13:25:51 nixos systemd[1]: postgresql.service: Service restart not allowed.

It's subtle and non-obvious from the man-page, but the way how units are
stopped is different when using `PartOf=` or `Requires=` which don't have the
`UNIT_ATOM_CANNOT_BE_ACTIVE_WITHOUT` property, but instead schedules the
stop/start of the target AFTER the stop-job of postgresql.service which
is turned into a start-job because of Restart=always:

    Jul 12 13:33:00 nixos systemd[1]: postgresql.service: Main process exited, code=killed, status=9/KILL
    [...]
    Jul 12 13:33:00 nixos systemd[1]: postgresql.service: Failed with result 'signal'.
    Jul 12 13:33:00 nixos systemd[1]: postgresql.service: Service will restart (restart setting)
    [...]
    Jul 12 13:33:00 nixos systemd[1]: postgresql.target: Installed new job postgresql.target/restart as 80996
    Jul 12 13:33:00 nixos systemd[1]: postgresql.service: Installed new job postgresql.service/restart as 80907
    [...]
    Jul 12 13:33:00 nixos systemd[1]: postgresql.service: Scheduled restart job, restart counter is at 1.
    [...]
    Jul 12 13:33:00 nixos systemd[1]: Stopped target postgresql.target.
    Jul 12 13:33:00 nixos systemd[1]: postgresql.target: Converting job postgresql.target/restart -> postgresql.target/start
    Jul 12 13:33:00 nixos systemd[1]: Stopping postgresql.target...
    [...]
    Jul 12 13:33:00 nixos systemd[1]: Stopped postgresql.service.
    Jul 12 13:33:00 nixos systemd[1]: postgresql.service: Converting job postgresql.service/restart -> postgresql.service/start
    [...]
    Jul 12 13:33:00 nixos systemd[1]: postgresql.service: Changed dead -> running
    Jul 12 13:33:00 nixos systemd[1]: postgresql.service: Job 80907 postgresql.service/start finished, result=done
    Jul 12 13:33:00 nixos systemd[1]: Started postgresql.service.
    Jul 12 13:33:00 nixos systemd[1]: postgresql.target: Changed dead -> active
    [...]
    Jul 12 13:33:00 nixos systemd[1]: Reached target postgresql.target.

Do note that the stop job (including the restart) of postgresql.service
is fully processed here before dealing with PartOf/ConsistsOf
relationships.

I tested this against the following cases:

    | Unit               | Action       | Propagates to      |
    | ------------------ | ------------ | ------------------ |
    | postgresql.target  | restart      | postgresql.service |
    | postgresql.target  | start        | postgresql.service |
    | postgresql.target  | stop         | psotgresql.service |
    | postgresql.service | start        | postgresql.target  |
    | postgresql.service | restart      | postgresql.target  |
    | postgresql.service | stop         | postgresql.target  |
    | postgresql.service | auto-restart | postgresql.target  |
    | postgresql.service | failure      | postgresql.target  |

[1] e.g. systemd issue 8374
[2] https://github.com/systemd/systemd/blob/v256-stable/src/core/service.c#L2535-L2542
[3] https://github.com/systemd/systemd/blob/v256-stable/src/core/manager.c#L1611-L1626
[4] https://github.com/systemd/systemd/blob/v256-stable/src/core/unit-dependency-atom.c#L30-L35
2025-07-26 19:09:48 +02:00
Wolfgang Walther 5a0711127c treewide: run nixfmt 1.0.0 2025-07-24 13:55:40 +02:00
Kamil Monicz 82248a6f7a nixos/postgresql: warn about unstable status 2025-07-05 15:36:14 +02:00
Kamil Monicz abc5cff372 nixos/postgresql: deduplicate postgresql and use cfg.finalPackage 2025-07-05 11:52:42 +02:00
Wolfgang Walther 9656e1aa9d nixos/postgresql: make postgresql.target wait until recovery is done
The new postgresql.target will now wait until recovery is done and
read/write connections are possible.

This allows ensure* scripts and downstream migrations to work properly
after recovery from backup.

Resolves #346886
2025-06-24 15:26:48 +02:00
Wolfgang WaltherandMarcel 41c5662cbe nixos/postgresql: move postStart into separate unit
This avoids restarting the postgresql server, when only ensureDatabases
or ensureUsers have been changed. It will also allow to properly wait
for recovery to finish later.

To wait for "postgresql is ready" in other services, we now provide a
postgresql.target.

Resolves #400018

Co-authored-by: Marcel <me@m4rc3l.de>
2025-06-24 15:26:47 +02:00
Wolfgang Walther c119848700 nixos/postgresql: align maintainers with postgresql package 2025-06-24 15:24:21 +02:00
Maximilian Bosch 950a089393 nixos/postgresql: make postgresql_17 the new default
That's overdue actually, given that it was released last fall.
2025-06-17 12:33:21 +02:00
Maximilian Bosch e6cc23ba3f nixos/postgresql: fix docs on default package
Closes #416595
2025-06-14 23:25:43 +02:00
Wolfgang Walther 3d29b7d3a2 nixos/postgresql: improve local peer authentication with default map
This allows to easily map allowed database roles to system users.
2025-05-11 10:24:21 +02:00
Wolfgang Walther 7d0363742c nixos/postgresql: remove recoveryConfig option
Since v12, PostgreSQL doesn't support recovery.conf anymore and fails to
start up when this option is set:

  FATAL:  using recovery command file "recovery.conf" is not supported

This is documented at:

https://www.postgresql.org/docs/current/recovery-config.html
2025-05-08 12:16:25 +02:00
Maximilian BoschandGitHub 25310642c6 Merge: nixos/postgresql: allow customisations of SystemCallFilter (#386345) 2025-03-11 17:11:28 +01:00
Maximilian Bosch 8bf1e138bc nixos/postgresql: fix merging of shared_preload_libraries option
Closes #386804

The issue with coercing to `types.str` is that it's not mergeable, so
any declarations will result in an eval error like this:

    error: The option `interactive.nodes.tmp.services.postgresql.settings.shared_preload_libraries' has conflicting definition values:
    - In `/home/ma27/Projects/nixpkgs-hack/tmp.nix@node-tmp': "foo"
    - In `/home/ma27/Projects/nixpkgs-hack/tmp.nix@node-tmp': "bar2"
    Use `lib.mkForce value` or `lib.mkDefault value` to change the priority on any of these definitions.

Using a mergeable type (`types.comma`, i.e. a string, where all declarations
get joined with a comma as delimiter) fixes the problem.
2025-03-10 18:07:44 +01:00
Maximilian Bosch 6e87867ee3 nixos/postgresql: allow customisations of SystemCallFilter
Closes #385603

The problem described is that `wal-g` requires syscalls from `@resources`.
However, we don't have support for it in the module now and I don't
think it's reasonable to only support hardening adjustments for things
support by this module. Also, list is a bad datatype here since it
doesn't allow the level of customizations we need.

This is only for the syscall filterset since it's the option that's hard
to customize otherwise. For downstream configs, it's recommended to
adjust the hardening as needed in other cases.

Hence I decided to implement `services.postgresql.systemCallFilter` with
the following semantics:

* `systemCallFilter."~@resources" = true` adds `~@resources` to the
  filterset.

* Setting this to `false` (e.g. in a downstream configuration using
  `wal-g`) removes the entry `~@resources` from the filterset. In this
  case it's sufficient since `@system-service` implies `@resources` and
  the `~@resources` declaration after that discards that.

  I decided to not implement logic about negations in here, but to keep
  it rather simple by only allowing to set/unset entries.

As described in `systemd.exec(5)`, the ordering matters: e.g.
`@system-service` implies `@resources`, but `~@resources` _after_ that
reverts that. By default, the ordering of the keys is as follows:

* syscall groups (starting with `@`) come at first.
* negations of syscall groups (starting with `~@`) come after that.
* anything else at the end.

If further ordering is needed, it can be done like this:

```
{
  services.postgresql.systemCallFilter."~@resources" = {
    enable = true; # whether or not it's part of the final SystemCallFilter
    priority = 23; # ordering priority in the filterset.
  };
}
```

The lower the priority, the higher up the entry will be in the final
filterset.
2025-03-02 11:20:18 +01:00
Julien Langlois 37372eb227 nixos/postgresql/citus: fix syscall filter and add test 2025-02-09 18:07:27 -05:00
Martin Weinelt 4d0f802848 nixos/postgresql: fix condition for readwritepaths
In the case that the user wants to provide a custom data directory, we
need to grant `ReadWritePaths` for that directory. Previously this would
not happen when `/var/lib/postgresql` was used, because the condition
was not in fact checking for the default data directory, creating a gap
in then if-else scenario.

Fixes: #371680
2025-01-08 16:08:46 +01:00
Tom HubrechtandMaximilian Bosch 0bdb7a72b1 nixos/postgresql: Add finalPackage attribute
This allows refering to the actual installed package, which is necessary
when using plugins and wanting to use postgres in scripts
2024-12-12 16:54:06 +01:00
Silvan Mosberger 4f0dadbf38 treewide: format all inactive Nix files
After final improvements to the official formatter implementation,
this commit now performs the first treewide reformat of Nix files using it.
This is part of the implementation of RFC 166.

Only "inactive" files are reformatted, meaning only files that
aren't being touched by any PR with activity in the past 2 months.
This is to avoid conflicts for PRs that might soon be merged.
Later we can do a full treewide reformat to get the rest,
which should not cause as many conflicts.

A CI check has already been running for some time to ensure that new and
already-formatted files are formatted, so the files being reformatted here
should also stay formatted.

This commit was automatically created and can be verified using

    nix-build https://github.com/infinisil/treewide-nixpkgs-reformat-script/archive/a08b3a4d199c6124ac5b36a889d9099b4383463f.tar.gz \
      --argstr baseRev b32a094368
    result/bin/apply-formatting $NIXPKGS_PATH
2024-12-10 20:26:33 +01:00
Ian KerinsandGitHub 307090125f nixos/postgresql: fix warning typo 2024-11-16 20:59:04 -05:00
Maximilian BoschandGitHub 97a911e8fb Merge: nixos/postgresql: extension based hardening relaxation (#355010) 2024-11-16 22:29:36 +01:00
Martin WeineltandMaximilian Bosch d370af0785 nixos/postgresql: create infrastructure for relaxing systemd hardening
By matching on the package names of the plugins passed into the package
we can relax the systemd unit hardening as needed.
2024-11-16 17:29:34 +01:00
Martin WeineltandMaximilian Bosch 223a6c6ed0 nixos/postgresql: rename extraPlugins to extensions
This is the upstream lingo, and it makes everything slightly less
confusing.
2024-11-16 14:46:16 +01:00
Maximilian Bosch 0b3eef7441 postgresql_12: remove
This will be EOL at the end of November, so there's little reason to
keep it in 24.11[1]. As discussed, we'd like to keep it for as long as
possible to make sure there's a state in nixpkgs that has the latest
minor of postgresql_12 available with the most recent CVEs fixed for
people who cannot upgrade[2].

This aspect has been made explicit in the manual now for the next .11
release.

During the discussions it has been brought up that if people just do
`services.postgresql.enable = true;` and let the code decide the
postgresql version based on `system.stateVersion`, there's a chance that
such EOL dates will be missed. To make this harder, a warning will now
be raised when using the stateVersion-condition and the oldest still
available major is selected.

Additionally regrouped the postgresql things in the release notes to
make sure these are all shown consecutively. Otherwise it's a little
hard to keep track of all the changes made to postgresql in 24.11.

[1] https://endoflife.date/postgresql
[2] https://github.com/NixOS/nixpkgs/pull/353158#issuecomment-2453056692
2024-11-15 09:17:06 +01:00
Maximilian BoschandMartin Weinelt 0f1e2a1cd8 nixos/postgresql: MemoryDenyWriteExecute must be off when doing JIT
The test breaks like this otherwise:

    machine # WARNING:  error during JITing: Permission denied
    machine # [   14.012280] postgres[913]: [913] WARNING:  error during JITing: Permission denied
    machine # ERROR:  failed to look up symbol "evalexpr_0_1": Failed to materialize symbols: { (main, { evalexpr_0_1, evalexpr_0_0 }) }
2024-10-14 23:57:39 +02:00
Maximilian BoschandMartin Weinelt f800d8e42b nixos/postgresql: enable private /tmp & private mounts; fix wal-receiver test
The issue was that the old test-case used `/tmp` to share data. Using
`JoinsNamespaceOf=` wasn't a real workaround since the private `/tmp` is
recreated when a service gets stopped/started which is the case here, so
the wals were still lost.

To keep the test building with `PrivateTmp=yes`, create a dedicated
directory in `/var/cache` with tmpfiles and allow the hardened
`postgresql.service` to access it via `ReadWritePaths`.
2024-10-01 10:31:20 +02:00
Martin Weinelt 2ebffcc4c7 nixos/postgresql: set up sandboxing
Reduces the general exposure of the postgresql.service through systemd
hardening options.
2024-10-01 10:31:20 +02:00
Maximilian Bosch 11d1f8776c nixos/postgresql: escape initdbArgs
It doesn't look correct that this isn't the case already.
2024-09-29 21:03:52 +02:00
Maximilian Bosch bf2d040432 postgresql: 15 -> 16 for 24.11+
Upgrade default postgresql for stateVersion >=24.11.
This also rebuilds all packages linking against `libpq.so` to use
postgresql 16.

After re-reading https://www.postgresql.org/docs/16/release-16.html
I don't see any major risks about doing that.
2024-07-24 10:43:02 +02:00
github-actions[bot]andGitHub 67f8ba4a30 Merge master into staging-next 2024-05-01 18:01:22 +00:00
Tom Hubrecht 0ccd40450f nixos/postgresql: Remove toplevel with lib; 2024-05-01 12:25:24 +02:00
Maximilian BoschandGitHub cba6af761a Merge pull request #293996 from wolfgangwalther/postgresql-cleanup
postgresql: more cleanup
2024-04-19 13:53:20 +00:00
stuebinmandJonathan Ringer 6afb255d97 nixos: remove all uses of lib.mdDoc
these changes were generated with nixq 0.0.2, by running

  nixq ">> lib.mdDoc[remove] Argument[keep]" --batchmode nixos/**.nix
  nixq ">> mdDoc[remove] Argument[keep]" --batchmode nixos/**.nix
  nixq ">> Inherit >> mdDoc[remove]" --batchmode nixos/**.nix

two mentions of the mdDoc function remain in nixos/, both of which
are inside of comments.

Since lib.mdDoc is already defined as just id, this commit is a no-op as
far as Nix (and the built manual) is concerned.
2024-04-13 10:07:35 -07:00
Wolfgang Walther 1682b4cc39 nixos/postgresql: fix enableJIT = false
The enableJIT = true case was fixed in #221851 or
e2fb651752 respectively.

However this did not take the case into consideration, when doing this:

    services.postgresql = {
      enable = true;
      enableJIT = false;
      package = pkgs.postgresql_15_jit;
    };

If enableJIT is treated as the source of truth, then this should indeed
cause JIT to be disabled, which this commit does.
2024-04-04 08:41:19 +02:00
Maximilian Bosch 5142b7afa8 nixos/postgresql: turn settings into a submodule
The main idea behind that was to be able to do more sophisticated
merging for stuff that goes into `postgresql.conf`:
`shared_preload_libraries` is a comma-separated list in a `types.str`
and thus not mergeable. With this change, the option accepts both a
comma-separated string xor a list of strings.

This can be implemented rather quick using `coercedTo` +
freeform modules. The interface still behaves equally, but it allows to
merge declarations for this option together.

One side-effect was that I had to change the `attrsOf (oneOf ...)` part into
a submodule to allow declaring options for certain things. While at it,
I decided to move `log_line_prefix` and `port` into this structure as
well.
2024-03-30 14:23:05 +01:00
Wolfgang Walther 14b3ea2789 postgresql: refactor to pass jitSupport/llvm via scope instead of passthru
This makes it less error-prone to use the llvm package in extensions, because
it will always match the package used by the postgresql derivation itself.

Previously, you could've accidentally used llvm instead of postgresql.llvm
with a different result.
2024-03-15 21:11:09 +01:00
Maximilian Bosch d363f52625 nixos/postgresql: drop ensurePermissions option
...effectively what was planned already in #266270, but it was too late
because the branches were restricted and didn't allow any breaking
changes anymore.

It also suffers from the same issue that we already had when discussing
this the last time[1] when `ensureDBOwnership` was ultimately introduced
as band-aid fix: newly created users don't get CREATE permission on
the `public` schema anymore (since psql 15), even with `ALL PRIVILEGES`.

If one's use-case is more sophisticated than having a single owner, it's
questionable anyways if this module is the correct tool since
permissions aren't dropped on a change to this option or a removal which
is pretty surprising in the context of NixOS.

[1] https://github.com/NixOS/nixpkgs/pull/266270
2024-02-12 21:10:33 +01:00
Thomas GerbetandGitHub a9da4c2260 Merge pull request #257504 from SuperSandro2000/postgres-doc
nixos/postgresql: point doc link to current like all others
2023-12-15 00:43:13 +01:00
Sandro Jäckel 4fe5824fc7 nixos/postgresql: take extraPlugins packageset from package option
This allows to reuse the extraPlugins option in other context's for
example an upgrade script.
2023-12-08 14:58:18 +01:00
h7x4 0a37316d6c treewide: use mkPackageOption
This commit replaces a lot of usages of `mkOption` with the package
type, to be `mkPackageOption`, in order to reduce the amount of code.
2023-11-27 01:28:36 +01:00
Sandro Jäckel 515ce669bc nixos/postgresql: point doc link to current like all others 2023-11-20 14:44:23 +01:00